|
| 1 | +// +page.server.ts |
| 2 | + |
| 3 | +import type { Actions, PageServerLoad } from './$types.js'; |
| 4 | + |
| 5 | +import { superValidate, message } from '$lib/index.js'; |
| 6 | +import { zod } from '$lib/adapters/zod.js'; |
| 7 | +import { fail } from '@sveltejs/kit'; |
| 8 | +import { |
| 9 | + inviteUserToGroupSchema, |
| 10 | + modifyGroupAccessSchema, |
| 11 | + fixedModifyGroupAccessSchema |
| 12 | +} from './schema.js'; |
| 13 | + |
| 14 | +const group = { |
| 15 | + users: [ |
| 16 | + { |
| 17 | + username: 'user1' |
| 18 | + } |
| 19 | + ] |
| 20 | +}; |
| 21 | + |
| 22 | +export const load: PageServerLoad = async () => { |
| 23 | + return { group }; |
| 24 | +}; |
| 25 | + |
| 26 | +export const actions: Actions = { |
| 27 | + invite: async ({ request }) => { |
| 28 | + const form = await superValidate(request, zod(inviteUserToGroupSchema)); |
| 29 | + |
| 30 | + if (!form.valid) return fail(400, { form }); |
| 31 | + |
| 32 | + group.users.push({ username: form.data.username }); |
| 33 | + |
| 34 | + return message(form, 'Form posted successfully!'); |
| 35 | + }, |
| 36 | + modify: async ({ request }) => { |
| 37 | + const form = await superValidate(request, zod(modifyGroupAccessSchema)); |
| 38 | + |
| 39 | + if (!form.valid) return fail(400, { form }); |
| 40 | + |
| 41 | + const removedUsernames = form.data.users.filter((u) => u.remove).map((u) => u.username); |
| 42 | + |
| 43 | + group.users = group.users.filter((u) => removedUsernames.includes(u.username)); |
| 44 | + |
| 45 | + return message(form, 'Form posted successfully!'); |
| 46 | + }, |
| 47 | + ['fixed-modify']: async ({ request }) => { |
| 48 | + const form = await superValidate(request, zod(fixedModifyGroupAccessSchema)); |
| 49 | + |
| 50 | + if (!form.valid) return fail(400, { form }); |
| 51 | + |
| 52 | + const removedUsernames = form.data.users.filter((u) => u.removed).map((u) => u.username); |
| 53 | + |
| 54 | + group.users = group.users.filter((u) => removedUsernames.includes(u.username)); |
| 55 | + |
| 56 | + return message(form, 'Form posted successfully!'); |
| 57 | + } |
| 58 | +}; |
0 commit comments