Files
cozsweet-frontend-nextjs/src/stores/private-zoom/machine/album-flow.ts
T

66 lines
1.7 KiB
TypeScript

import type { DoneActorEvent, ErrorActorEvent } from "xstate";
import type { PrivateAlbumsResponse } from "@/data/schemas/private-zoom";
import { applyAlbumsResponse } from "../helper/albums";
import { toPrivateZoomErrorMessage } from "../helper/unlock";
import {
basePrivateZoomMachineSetup,
createPrivateZoomActorActionSetup,
} from "./setup";
const clearAlbumLoadStateAction = basePrivateZoomMachineSetup.assign({
errorMessage: null,
unlockPaywallRequest: null,
});
export const albumMachineSetup = basePrivateZoomMachineSetup.extend({
actions: {
clearAlbumLoadState: clearAlbumLoadStateAction,
},
});
const albumsDoneSetup =
createPrivateZoomActorActionSetup<
DoneActorEvent<PrivateAlbumsResponse>
>();
const albumErrorSetup =
createPrivateZoomActorActionSetup<ErrorActorEvent>();
const applyAlbumsResponseAction = albumsDoneSetup.assign(({ event }) =>
applyAlbumsResponse(event.output),
);
const applyAlbumLoadErrorAction = albumErrorSetup.assign(({ event }) => ({
errorMessage: toPrivateZoomErrorMessage(event.error),
}));
export const idleState = albumMachineSetup.createStateConfig({
on: {
PrivateZoomInit: "loading",
},
});
export const loadingState = albumMachineSetup.createStateConfig({
entry: "clearAlbumLoadState",
invoke: {
src: "loadAlbums",
input: ({ context }) => ({ characterId: context.characterId }),
onDone: {
target: "ready",
actions: applyAlbumsResponseAction,
},
onError: {
target: "failed",
actions: applyAlbumLoadErrorAction,
},
},
});
export const failedState = albumMachineSetup.createStateConfig({
on: {
PrivateZoomInit: "loading",
PrivateZoomRefresh: "loading",
},
});