feat: Route list page
This commit is contained in:
@ -14,23 +14,32 @@ export type Carrier = {
|
||||
right_color: string;
|
||||
};
|
||||
|
||||
type Carriers = Carrier[];
|
||||
type Carriers = {
|
||||
data: Carrier[];
|
||||
loaded: boolean;
|
||||
};
|
||||
|
||||
type CashedCarrier = Record<number, Carrier>;
|
||||
|
||||
class CarrierStore {
|
||||
carriers: Carriers = [];
|
||||
carriers: Carriers = {
|
||||
data: [],
|
||||
loaded: false,
|
||||
};
|
||||
carrier: CashedCarrier = {};
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
getCarriers = async () => {
|
||||
if (this.carriers.length > 0) return;
|
||||
if (this.carriers.loaded) return;
|
||||
|
||||
const response = await authInstance.get("/carrier");
|
||||
|
||||
runInAction(() => {
|
||||
this.carriers = response.data;
|
||||
this.carriers.data = response.data;
|
||||
this.carriers.loaded = true;
|
||||
});
|
||||
};
|
||||
|
||||
@ -38,13 +47,15 @@ class CarrierStore {
|
||||
await authInstance.delete(`/carrier/${id}`);
|
||||
|
||||
runInAction(() => {
|
||||
this.carriers = this.carriers.filter((carrier) => carrier.id !== id);
|
||||
this.carriers.data = this.carriers.data.filter(
|
||||
(carrier) => carrier.id !== id
|
||||
);
|
||||
delete this.carrier[id];
|
||||
});
|
||||
};
|
||||
|
||||
getCarrier = async (id: number) => {
|
||||
if (this.carrier[id]) return this.carrier[id];
|
||||
if (this.carrier[id]) return;
|
||||
const response = await authInstance.get(`/carrier/${id}`);
|
||||
|
||||
runInAction(() => {
|
||||
@ -90,7 +101,7 @@ class CarrierStore {
|
||||
logo: logoId,
|
||||
});
|
||||
runInAction(() => {
|
||||
this.carriers.push(response.data);
|
||||
this.carriers.data.push(response.data);
|
||||
});
|
||||
};
|
||||
|
||||
@ -137,7 +148,7 @@ class CarrierStore {
|
||||
);
|
||||
|
||||
runInAction(() => {
|
||||
this.carriers = this.carriers.map((carrier) =>
|
||||
this.carriers.data = this.carriers.data.map((carrier) =>
|
||||
carrier.id === id ? { ...carrier, ...response.data } : carrier
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user