Skip to content

Commit c6b12b2

Browse files
committed
add create space support
1 parent beaff45 commit c6b12b2

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { SpaceUserRole, type Space } from '@prisma/client';
2+
import { error, fail, redirect, type Actions } from '@sveltejs/kit';
3+
import { isPrismaClientKnownRequestError } from '@zenstackhq/runtime';
4+
5+
export const actions = {
6+
default: async ({ request, locals }) => {
7+
if (!locals.user) {
8+
throw error(401, 'Unauthorized');
9+
}
10+
11+
const data = await request.formData();
12+
const name = data.get('name');
13+
const slug = data.get('slug');
14+
15+
if (typeof name !== 'string' || typeof slug !== 'string') {
16+
return fail(400, { name, slug, missing: true });
17+
}
18+
19+
let space: Space;
20+
try {
21+
space = await locals.db.space.create({
22+
data: {
23+
name,
24+
slug,
25+
members: {
26+
create: {
27+
user: { connect: { id: locals.user.id } },
28+
role: SpaceUserRole.ADMIN,
29+
},
30+
},
31+
},
32+
});
33+
} catch (err) {
34+
if (isPrismaClientKnownRequestError(err) && err.code === 'P2002') {
35+
return error(400, 'Space slug already in use');
36+
} else {
37+
throw err;
38+
}
39+
}
40+
41+
throw redirect(303, `/space/${space.slug}`);
42+
},
43+
} satisfies Actions;
+52-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
1-
<h1>Create Space</h1>
1+
<script lang="ts">
2+
import { enhance } from '$app/forms';
3+
import { goto } from '$app/navigation';
4+
5+
let name = '';
6+
let slug = '';
7+
</script>
8+
9+
<div class="flex items-center justify-center h-full">
10+
<form method="post" use:enhance>
11+
<h1 class="text-3xl mb-8">Create a space</h1>
12+
<div class="flex-col space-y-4">
13+
<div>
14+
<label for="name" class="text-lg"> Space name </label>
15+
<input
16+
name="name"
17+
type="text"
18+
required
19+
placeholder="Name of your space"
20+
class="input input-bordered w-full max-w-xs mt-2"
21+
bind:value={name}
22+
autoFocus
23+
/>
24+
</div>
25+
<div>
26+
<label for="slug" class="text-lg"> Space slug </label>
27+
<input
28+
name="slug"
29+
type="text"
30+
required
31+
placeholder="Slug of your space"
32+
class="input input-bordered w-full max-w-xs mt-2"
33+
bind:value={slug}
34+
/>
35+
</div>
36+
</div>
37+
38+
<div class="flex space-x-4 mt-6">
39+
<input
40+
type="submit"
41+
disabled={name.length < 4 ||
42+
name.length > 20 ||
43+
!slug.match(/^[0-9a-zA-Z]{4,16}$/)}
44+
value="Create"
45+
class="btn btn-primary px-8"
46+
/>
47+
<button class="btn btn-outline" on:click={() => goto('/')}>
48+
Cancel
49+
</button>
50+
</div>
51+
</form>
52+
</div>

src/routes/(auth)/signin/+page.server.ts

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const actions = {
1818
const user = await prisma.user.findFirst({
1919
where: { email },
2020
});
21-
console.log(user, password);
2221
if (!user || !compareSync(password, user.password)) {
2322
return fail(401, { email, password, invalid: true });
2423
}

0 commit comments

Comments
 (0)