feat: Add edit pages with cache
This commit is contained in:
@@ -6,17 +6,20 @@ export type User = {
|
||||
email: string;
|
||||
is_admin: boolean;
|
||||
name: string;
|
||||
password?: string;
|
||||
};
|
||||
|
||||
class UserStore {
|
||||
users: User[] = [];
|
||||
user: User | null = null;
|
||||
user: Record<string, User> = {};
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
getUsers = async () => {
|
||||
if (this.users.length > 0) return;
|
||||
|
||||
const response = await authInstance.get("/user");
|
||||
|
||||
runInAction(() => {
|
||||
@@ -25,18 +28,77 @@ class UserStore {
|
||||
};
|
||||
|
||||
getUser = async (id: number) => {
|
||||
if (this.user[id]) return;
|
||||
const response = await authInstance.get(`/user/${id}`);
|
||||
|
||||
runInAction(() => {
|
||||
this.user = response.data as User;
|
||||
this.user[id] = response.data as User;
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
deleteUser = async (id: number) => {
|
||||
await authInstance.delete(`/users/${id}`);
|
||||
await authInstance.delete(`/user/${id}`);
|
||||
|
||||
runInAction(() => {
|
||||
this.users = this.users.filter((user) => user.id !== id);
|
||||
delete this.user[id];
|
||||
});
|
||||
};
|
||||
|
||||
createUserData: Partial<User> = {
|
||||
name: "",
|
||||
email: "",
|
||||
password: "",
|
||||
is_admin: false,
|
||||
};
|
||||
|
||||
setCreateUserData = (
|
||||
name: string,
|
||||
email: string,
|
||||
password: string,
|
||||
is_admin: boolean
|
||||
) => {
|
||||
this.createUserData = { name, email, password, is_admin };
|
||||
};
|
||||
|
||||
createUser = async () => {
|
||||
const id = this.users[this.users.length - 1].id + 1;
|
||||
const response = await authInstance.post("/user", this.createUserData);
|
||||
|
||||
runInAction(() => {
|
||||
this.users.push({
|
||||
id,
|
||||
...response.data,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
editUserData: Partial<User> = {
|
||||
name: "",
|
||||
email: "",
|
||||
password: "",
|
||||
is_admin: false,
|
||||
};
|
||||
|
||||
setEditUserData = (
|
||||
name: string,
|
||||
email: string,
|
||||
password: string,
|
||||
is_admin: boolean
|
||||
) => {
|
||||
this.editUserData = { name, email, password, is_admin };
|
||||
};
|
||||
|
||||
editUser = async (id: number) => {
|
||||
const response = await authInstance.patch(`/user/${id}`, this.editUserData);
|
||||
|
||||
runInAction(() => {
|
||||
this.users = this.users.map((user) =>
|
||||
user.id === id ? { ...user, ...response.data } : user
|
||||
);
|
||||
this.user[id] = { ...this.user[id], ...response.data };
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user