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 >(); const albumErrorSetup = createPrivateZoomActorActionSetup(); 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", }, });