C
C#15mo ago
Camster

❔ Blazor State Container Prompt, Standardized for all Pages

I have a simple state container in blazor. The goal is to provide the AppEntity value:
public class StateContainer {
public AppEntity App { get; set; }
public event Action OnStateChange;
public void SetApp(AppEntity app) {
App = app;
NotifyStateChanged();
}

private void NotifyStateChanged() => OnStateChange?.Invoke();
}
public class StateContainer {
public AppEntity App { get; set; }
public event Action OnStateChange;
public void SetApp(AppEntity app) {
App = app;
NotifyStateChanged();
}

private void NotifyStateChanged() => OnStateChange?.Invoke();
}
The container is injected in the services collection.
builder.Services.AddSingleton<StateContainer>();
builder.Services.AddSingleton<StateContainer>();
I have a component which provides the user a selection dropdown to set the AppEntity in the State Container. Subsequent pages inject the State Container and use it if needed. My question is: if for some reason a page that requires the State Container has a null value for AppEntity (e.g., user navigated to a specific page by url, bypassing the selection page), how can I have a generic popup prompt that prompts the user for the selection? Ideally, I'd want to minimize the code in any page that needs it as much as possible. The only way I can think to do this is to build the prompt in a base component and inherit from it, but perhaps there might be a simpler or more elegant way.
2 Replies
Camster
CamsterOP15mo ago
I always ask the weird questions 😅 . I think I figured this one out though. Just need to create a component that checks if state container is empty and prompts selection box if it is via popup/dialog. Then I just include that component in each page that needs it. Sometimes my mind wanders in a completely direction, and I begin to think it's more complicated. ah well 😄
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server