import { Action, action, Thunk, thunk } from 'easy-peasy'; import { updateAccountEmail } from '@/api/account'; export interface UserData { uuid: string; username: string; email: string; language: string; rootAdmin: boolean; useTotp: boolean; avatarURL: string; roleName: string; createdAt: Date; updatedAt: Date; } export interface UserStore { data?: UserData; setUserData: Action; updateUserData: Action>; updateUserEmail: Thunk>; } const user: UserStore = { data: undefined, setUserData: action((state, payload) => { state.data = payload; }), updateUserData: action((state, payload) => { // Limitation of Typescript, can't do much about that currently unfortunately. // @ts-ignore state.data = { ...state.data, ...payload }; }), updateUserEmail: thunk(async (actions, payload) => { await updateAccountEmail(payload.email, payload.password); actions.updateUserData({ email: payload.email }); }), }; export default user;