refactor admin crud popups
All checks were successful
deploy / build-and-deploy (push) Successful in 23s
All checks were successful
deploy / build-and-deploy (push) Successful in 23s
This commit is contained in:
48
src/app/admin/usersBlocked/usersBlocked.ts
Normal file
48
src/app/admin/usersBlocked/usersBlocked.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user