2019-06-25 04:43:46 +00:00
|
|
|
import { Action, action, Thunk, thunk } from 'easy-peasy';
|
2022-02-27 17:25:31 +00:00
|
|
|
import { updateAccountEmail } from '@/api/account';
|
2019-06-25 04:43:46 +00:00
|
|
|
|
|
|
|
export interface UserData {
|
|
|
|
uuid: string;
|
|
|
|
username: string;
|
|
|
|
email: string;
|
|
|
|
language: string;
|
|
|
|
rootAdmin: boolean;
|
|
|
|
useTotp: boolean;
|
2021-01-07 17:21:09 +00:00
|
|
|
avatarURL: string;
|
|
|
|
roleName: string;
|
2019-06-25 04:43:46 +00:00
|
|
|
createdAt: Date;
|
|
|
|
updatedAt: Date;
|
|
|
|
}
|
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
export interface UserStore {
|
2019-06-25 04:43:46 +00:00
|
|
|
data?: UserData;
|
2019-07-10 04:25:57 +00:00
|
|
|
setUserData: Action<UserStore, UserData>;
|
|
|
|
updateUserData: Action<UserStore, Partial<UserData>>;
|
2020-07-05 01:30:50 +00:00
|
|
|
updateUserEmail: Thunk<UserStore, { email: string; password: string }, any, UserStore, Promise<void>>;
|
2019-06-25 04:43:46 +00:00
|
|
|
}
|
2019-06-23 00:07:28 +00:00
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
const user: UserStore = {
|
2019-06-23 00:07:28 +00:00
|
|
|
data: undefined,
|
2021-01-07 17:21:09 +00:00
|
|
|
|
2019-06-23 00:07:28 +00:00
|
|
|
setUserData: action((state, payload) => {
|
|
|
|
state.data = payload;
|
|
|
|
}),
|
2019-06-25 04:43:46 +00:00
|
|
|
|
|
|
|
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 });
|
|
|
|
}),
|
2019-06-23 00:07:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default user;
|