refactor(stores): standardize state machine structure

This commit is contained in:
2026-07-15 11:19:08 +08:00
parent ed3e800245
commit 20d07023e4
68 changed files with 3492 additions and 3193 deletions
@@ -0,0 +1,84 @@
import type { DoneActorEvent, ErrorActorEvent } from "xstate";
import type { LoginStatus } from "@/data/dto/auth";
import { toAuthErrorMessage } from "../helper/error";
import {
baseAuthMachineSetup,
createAuthActorActionSetup,
} from "./setup";
const clearAuthErrorAction = baseAuthMachineSetup.assign({
errorMessage: null,
});
export const credentialsMachineSetup = baseAuthMachineSetup.extend({
actions: {
clearAuthError: clearAuthErrorAction,
},
});
const authSuccessActionSetup =
createAuthActorActionSetup<DoneActorEvent<LoginStatus>>();
const authErrorActionSetup = createAuthActorActionSetup<ErrorActorEvent>();
const applyAuthSuccessAction = authSuccessActionSetup.assign(({ event }) => ({
loginStatus: event.output,
errorMessage: null,
hasInitialized: true,
}));
const applyAuthErrorAction = authErrorActionSetup.assign(({ event }) => ({
errorMessage: toAuthErrorMessage(event.error),
hasInitialized: true,
}));
export const loadingEmailLoginState =
credentialsMachineSetup.createStateConfig({
entry: "clearAuthError",
invoke: {
src: "emailLogin",
input: ({ event }) =>
event.type === "AuthEmailLoginSubmitted"
? { email: event.email, password: event.password }
: { email: "", password: "" },
onDone: {
target: "idle",
actions: applyAuthSuccessAction,
},
onError: {
target: "idle",
actions: applyAuthErrorAction,
},
},
});
export const loadingEmailRegisterState =
credentialsMachineSetup.createStateConfig({
entry: "clearAuthError",
invoke: {
src: "emailRegisterThenLogin",
input: ({ event }) =>
event.type === "AuthEmailRegisterSubmitted"
? {
email: event.email,
password: event.password,
username: event.username,
confirmPassword: event.confirmPassword,
}
: {
email: "",
password: "",
username: "",
confirmPassword: "",
},
onDone: {
target: "idle",
actions: applyAuthSuccessAction,
},
onError: {
target: "idle",
actions: applyAuthErrorAction,
},
},
});