Skip to main content
Frontend tutorial

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:

src/store/UI.ts
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​

src/components/Header.tsx
<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:

src/components/Content.tsx
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.

05 / ShippingBoard Lite

localhost:5173
The header has new Dashboard and Loadings buttons. Dashboard is active and the page only says Ramps.

Dashboard is the default view. For now it only says Ramps.