Untitled
plain_text
a month ago
2.5 kB
2
Indexable
Never
import { action, makeObservable, observable, runInAction } from "mobx"; import { IOnBoarding } from "@hrm/shared/types"; import { GetOnBoarding } from "../api-client/cms-data/onboarding"; export class OnBoardingStoreImpl { onBoardingList: IOnBoarding[] = []; isLoadingOnBoardingList = true; isErrorOnBoardingList = false; constructor() { makeObservable(this, { isLoadingOnBoardingList: observable, isErrorOnBoardingList: observable, addOnBoarding: action, pullOnBoarding: action, setLoadingOnBoardingList: action, getById: action, }); } getById(id: string) { return this.onBoardingList.find((item) => item.id === id); } setLoadingOnBoardingList(bool: boolean) { this.isLoadingOnBoardingList = bool; } pullOnBoarding(authToken: string) { this.setLoadingOnBoardingList(true); GetOnBoarding({ headers: { Authorization: `Bearer ${authToken}`, }, }) .then((onBoardingResponse) => { if (!onBoardingResponse.data || onBoardingResponse.errors) { this.isErrorOnBoardingList = true; } else { this.isErrorOnBoardingList = false; try { const onBoardingListData: IOnBoarding[] = onBoardingResponse?.data?.map( (onBoarding: any) => { return { id: onBoarding?.id, ...onBoarding.attributes, }; }, ); runInAction(() => { this.onBoardingList = onBoardingListData; }); } catch (e) { this.isErrorOnBoardingList = true; } } }) .catch((e) => { this.isErrorOnBoardingList = true; console.error(e); }) .finally(() => { this.setLoadingOnBoardingList(false); }); } addOnBoarding(data: any) { this.setLoadingOnBoardingList(true); runInAction(() => { this.onBoardingList.push({ id: data?.id, ...data.attributes }); }); this.setLoadingOnBoardingList(false); } editOnBoarding(data: any) { this.setLoadingOnBoardingList(true); const index = this.onBoardingList.findIndex((elem) => elem.id === data.id); if (index === -1) { this.setLoadingOnBoardingList(false); return; } this.onBoardingList.splice(index, 1, { ...{ id: data?.id, ...data?.attributes, }, }); this.setLoadingOnBoardingList(false); } } export const OnBoardingStore = new OnBoardingStoreImpl();