Skip to main content
Frontend tutorial

7.4. Do it in ShippingBoard

Now the same in the real app. Clone shippingboard-lite, branch create-fetch. This branch is older than the one from chapter 5 and doesn't start in StackBlitz, so work on your computer. It keeps its old lockfile, so install and run it with the pnpm it was made with:

git clone --branch create-fetch \
https://github.com/adam-zielonka/shippingboard-lite.git
cd shippingboard-lite
pnpm dlx pnpm@7 install --frozen-lockfile
pnpm dlx pnpm@7 dev

Base URL and types​

In src/api/api.ts, delete the CUSTOMERS and RAMPS JSON imports, and add this after the other imports:

src/api/api.ts
const BASE_URL = "https://raw.githubusercontent.com/adam-zielonka/shippingboard-lite/bdd5494f80b7/src/api/";

type CustomerResponse = {
kunnr: string
name1: string
ort01: string
land1: string
}

type RampResponse = {
ramp_id: string
description: string
}

The customer fields have SAP names: kunnr is the customer number, name1 the name, ort01 the city and land1 the country. The frontend maps them to its own Customer.

Fetch using await and then​

Customers use await, ramps use then:

src/api/api.ts
export class API {
async getCustomers(): Promise<Customer[]> {
const response = await fetch(BASE_URL + "CUSTOMERS.json");

if (!response.ok) {
throw new Error(`Customers: HTTP ${response.status}`);
}

const json = await response.json() as CustomerResponse[];

return json.map(c => new Customer(c.kunnr, c.name1, c.ort01, c.land1));
}

getRamps(): Promise<Ramp[]> {
return fetch(BASE_URL + "RAMPS.json")
.then(response => {
if (!response.ok) {
throw new Error(`Ramps: HTTP ${response.status}`);
}

return response.json() as Promise<RampResponse[]>;
})
.then(json => json.map(r => new Ramp(r.ramp_id, r.description)));
}

// getLoadings stays as it is

The editor now underlines Store.init: it gets promises where it expects arrays.

Fixing store​

In src/store/Store.ts, add the two fields and replace init:

src/store/Store.ts
import { makeAutoObservable, runInAction } from "mobx";
// ...
loadings: Loading[] = [];
status: "loading" | "error" | "done" = "loading";
error = "";
// ...
init = async () => {
try {
const customers = await api.getCustomers();
const ramps = await api.getRamps();

runInAction(() => {
this.customers = customers;
this.ramps = ramps;
this.loadings = api.getLoadings();
store.ui.init();
this.status = "done";
});
} catch (error) {
runInAction(() => {
this.status = "error";
this.error = String(error);
});
}
};

getLoadings finds customers and ramps by their ids, so it runs only after both arrays are in the store.

Loading and errors​

Content already decides what the board shows. It gets two more cases:

src/components/Content.tsx
import { NonIdealState, Spinner } from "@blueprintjs/core";
// ...
export const Content = observer(() => {
const { ui, ramps, status, error } = store;

if (status === "loading") {
return <NonIdealState icon={<Spinner />} title="Loading the board…" />;
}

if (status === "error") {
return <NonIdealState icon="error" title="Could not load the board"
description={error} />;
}

if (ui.isLoadingsOpen) {

Misspell one of the file names: instead of an empty board, the user now sees why.

07 / Fetch

localhost:5173
ShippingBoard Lite with its header (Dashboard, Loadings, Add loading) and below it the message: Could not load the board. Error: Ramps: HTTP 404

A misspelled file name, as the user sees it.