5.2. Store info about view
First we need a place for our new view. The store also has to know which view is open, with an action for changing it. Because the view is only relevant on the frontend, there is a store for exactly this purpose: UI.ts.
Add a view field to the UI class, with getters and actions for it. The View type is already there:
type View = "dashboard" | "loadings"
// ...
export class UI {
view: View = "dashboard";
selectedLoading?: Loading;
// ...
closeLoadingDialog = () => {
this.selectedLoading = undefined;
};
get isDashboardOpen(): boolean {
return this.view === "dashboard";
}
get isLoadingsOpen(): boolean {
return this.view === "loadings";
}
openDashboard = () => {
this.view = "dashboard";
};
openLoadings = () => {
this.view = "loadings";
};
}
The first spell is already in the constructor: makeAutoObservable makes view observable, the getters computed and the arrow functions actions. Arrow functions also keep this, so we can pass them straight to onClick.
Buttons for changing the view
<Navbar.Group align={Alignment.LEFT}>
<Navbar.Heading>ShippingBoard Lite</Navbar.Heading>
<Navbar.Divider />
<ButtonGroup>
<Button outlined icon="dashboard" text="Dashboard"
intent="primary" onClick={ui.openDashboard}
active={ui.isDashboardOpen} />
<Button outlined icon="th" text="Loadings"
intent="primary" onClick={ui.openLoadings}
active={ui.isLoadingsOpen} />
</ButtonGroup>
</Navbar.Group>
The second spell is there too: Header is already an observer, so the active button follows ui.view.
Switching between views
Content still always returns the table. Return it only for Loadings:
export const Content = observer(() => {
const { ui } = store;
if (ui.isLoadingsOpen) {
return <LoadingsTable/>;
}
return <div className="Content">
Ramps
</div>;
});
Click both buttons: Loadings shows the table, and Dashboard shows "Ramps" for now.
