0

我想在应用程序 Blazor 程序集中实现状态管理,所以我添加了 Fluxor 作为库。

我创建了一个包含共享属性的抽象根状态:

public abstract class RootState
{
    public RootState(bool isLoading, string currentErrorMessage)
        => (IsLoading, CurrentErrorMessage) = (isLoading, currentErrorMessage);

    public bool IsLoading { get; }
    public string CurrentErrorMessage { get; }

    public bool HasCurrentErrors => !string.IsNullOrWhiteSpace(CurrentErrorMessage);

}

然后,我创建了继承自 Rootstate 的登录状态:

 public class LoginState : RootState
{
    public bool IsAuthenticated { get; set; }
    public LoginResponseDto UserData { get; set; }
    public LoginState(bool isLoading, string currentErrorMessage, bool isAuthenticated, LoginResponseDto userData)
        :base(isLoading, currentErrorMessage)
    {
        IsAuthenticated = isAuthenticated;
        UserData = userData;
    }
}

之后,我创建了要素类:

public class LoginFeature : Feature<LoginState>
{
    public override string GetName() => nameof(LoginState);

    protected override LoginState GetInitialState()
        => new LoginState(false, null, false, null);
}

然后是动作:

public class LoginAction
{
    public LoginDto LoginDto { get; }
    public LoginAction(LoginDto LoginDto)
    {
        this.LoginDto = LoginDto;
    }
}

public class LoginAccessFailure : FailureAction
{
    public LoginAccessFailure(string errorMessage)
        :base(errorMessage)
    {

    }
}

public abstract class FailureAction
{
    public string ErrorMessage { get; }

    protected FailureAction(string errorMessage) => ErrorMessage = errorMessage;
}

之后,我创建了 Reducer 和 Effect:

public class LoginReducer
{
    [ReducerMethod]
    public static LoginState ReduceLoginAction(LoginState state, LoginAction action)
        => new LoginState(true, null, false, state.UserData);

    [ReducerMethod]
    public static LoginState ReduceLoginSuccessAction(LoginState state, LoginActionSuccess action)
    {
        Console.WriteLine("State from Reducer", action);
        var result = new LoginState(false, null, action.IsAuthenticated, action.UserData);
        Console.WriteLine("Result from Reducer", result);

        return result;
    }

    [ReducerMethod]
    public static LoginState ReduceLoginFailureAction(LoginState state, LoginAccessFailure action)
        => new LoginState(false, action.ErrorMessage, false, null);
}

影响:

 public class LoginEffect : Effect<LoginAction>
{
    private readonly IAccountService _accountService;

    public LoginEffect(IAccountService accountService)
    {
        _accountService = accountService;
    }

    public  override  async Task HandleAsync(LoginAction action, IDispatcher dispatcher)
    {
        try
        {
            var loginResponse = await _accountService.Login(action.LoginDto);

            await Task.Delay(TimeSpan.FromMilliseconds(1000));
            dispatcher.Dispatch(new LoginActionSuccess(loginResponse, true));
        }
        catch (Exception e)
        {
            dispatcher.Dispatch(new LoginAccessFailure(e.Message));
        }
    }
}

当我想用这条指令调用调度程序时,dispatcher.Dispatch(new LoginAction(new LoginDto { Email = "test@email.com", Password = "test" }));我在 DevTools 中得到了结果,但没有数据,IState<LoginState>所以我无法访问状态。请问我的代码有什么错误吗?

4

0 回答 0