49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { type ActionReturnType, actions } from 'astro:actions';
|
|
import { writable } from 'svelte/store';
|
|
import { actionErrorPopup } from '@util/action.ts';
|
|
|
|
// types
|
|
export type BlockedUsers = Exclude<ActionReturnType<typeof actions.user.blocked>['data'], undefined>['blocked'];
|
|
export type BlockedUser = BlockedUsers[0];
|
|
|
|
// state
|
|
export const blockedUsers = writable<BlockedUsers>([]);
|
|
|
|
// actions
|
|
export async function fetchBlockedUsers() {
|
|
const { data, error } = await actions.user.blocked();
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
blockedUsers.set(data.blocked);
|
|
}
|
|
|
|
export async function addBlockedUser(blockedUser: BlockedUser) {
|
|
const { data, error } = await actions.user.addBlocked(blockedUser);
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
blockedUsers.update((old) => {
|
|
old.push(Object.assign(blockedUser, { id: data.id }));
|
|
return old;
|
|
});
|
|
}
|
|
|
|
export async function editBlockedUser(blockedUser: BlockedUser) {
|
|
const { error } = await actions.user.editBlocked(blockedUser);
|
|
if (error) {
|
|
actionErrorPopup(error);
|
|
return;
|
|
}
|
|
|
|
blockedUsers.update((old) => {
|
|
const index = old.findIndex((a) => a.id == blockedUser.id);
|
|
old[index] = blockedUser;
|
|
return old;
|
|
});
|
|
}
|