DEV Community

Discussion on: Advanced Blazor State Management Using Fluxor, part 7 - Client-to-Client comms with SignalR

Collapse
jcoble profile image
jcoble

I answered my own question I'm pretty sure. I need to SubscribeToAction and then load the rest of the data once that has been called. Below is some code that in my actual app where I'm waiting on UserSetUserResponseAction to be called then loading the rest of the component data. I need the CurrentUser to be able to finish the component data, so if it is not Initialized yet, then load the user data by dispatching the UserLoadUserResponseAction, else just use the UserStore.Value.CurrentUser.

I'd like to be able to just use UserStore.Value.CurrentUser instead of using another property that I'm setting, from the action.UserResponse, but the UserSetUserResponseAction callback is called before the reducer is which sets it, so UserStore.Value.CurrentUser is not yet set, when the rest of the code is run.

    protected override void OnInitialized()
    {
        IsLoading = true;
        isFirstRender = true;
        SubscribeToAction<UserSetUserResponseAction>(async action => await LoadComponentData(action.UserResponse));
        if (UserState.Value.Initialized == false)
        {
            loadUserDate();
            Dispatcher.Dispatch(new UserSetInitalizedAction());
        }
        else
        {
            LoadComponentData(UserState.Value.CurrentUser);
        }
        base.OnInitialized();
    }

    private void loadUserDate()
    {
        Dispatcher.Dispatch(new UserLoadUserResponseAction());
    }

    private async Task LoadComponentData(UserResponse userResponse)
    {
        CurrentUser = userResponse;
        await loadTrainingCourses();
        HubConnection = HubConnection.TryInitialize(_navigationManager);
        if (HubConnection.State == HubConnectionState.Disconnected)
        {
            await HubConnection.StartAsync();
        }

        IsLoading = false;
        StateHasChanged();
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
mr_eking profile image
Eric King Author

In the WeatherStore, you'll notice that the LoadForecasts EffectMethod dispatches the WeatherLoadForecastsSuccessAction to indicate that the forecasts have been loaded. If I want to have some action happen after the forecasts have been loaded, I can SubscribeToAction<WeatherLoadForecastsSuccessAction> and put whatever I need to do in there.

It looks like that's what you're doing with the SubscribeToAction<UserSetUserResponseAction> so I think we're on the same page there.