57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { actions } from 'astro:actions';
|
|
import { users } from './state.ts';
|
|
import type { User } from './types.ts';
|
|
import { actionErrorPopup } from '@util/action.ts';
|
|
|
|
export async function fetchUsers(options?: { username?: string | null }) {
|
|
const { data, error } = await actions.user.users({ username: options?.username });
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
users.set(data.users);
|
|
}
|
|
|
|
export async function addUser(user: User) {
|
|
const { data, error } = await actions.user.addUser({
|
|
username: user.username,
|
|
firstname: user.firstname,
|
|
lastname: user.lastname,
|
|
birthday: user.birthday,
|
|
telephone: user.telephone,
|
|
uuid: user.uuid
|
|
});
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
users.update((old) => {
|
|
old.push(Object.assign(user, { id: data.id }));
|
|
return old;
|
|
});
|
|
}
|
|
|
|
export async function editUser(user: User) {
|
|
const { error } = await actions.user.editUser({
|
|
id: user.id,
|
|
username: user.username,
|
|
firstname: user.firstname,
|
|
lastname: user.lastname,
|
|
birthday: user.birthday,
|
|
telephone: user.telephone,
|
|
uuid: user.uuid
|
|
});
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
users.update((old) => {
|
|
const index = old.findIndex((a) => a.id == user.id);
|
|
old[index] = user;
|
|
return old;
|
|
});
|
|
}
|