42 lines
979 B
TypeScript
42 lines
979 B
TypeScript
import type { Admin } from './types.ts';
|
|
import { actions } from 'astro:actions';
|
|
import { admins } from './state.ts';
|
|
import { actionErrorPopup } from '@util/action.ts';
|
|
|
|
export async function fetchAdmins() {
|
|
const { data, error } = await actions.admin.admins();
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
admins.set(data.admins);
|
|
}
|
|
|
|
export async function addAdmin(admin: Admin & { password: string }) {
|
|
const { data, error } = await actions.admin.addAdmin(admin);
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
admins.update((old) => {
|
|
old.push(Object.assign(admin, { id: data.id }));
|
|
return old;
|
|
});
|
|
}
|
|
|
|
export async function editAdmin(admin: Admin & { password: string }) {
|
|
const { error } = await actions.admin.editAdmin(admin);
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
admins.update((old) => {
|
|
const index = old.findIndex((a) => a.id == admin.id);
|
|
old[index] = admin;
|
|
return old;
|
|
});
|
|
}
|