Skip to content

Commit

Permalink
add default categories on register
Browse files Browse the repository at this point in the history
  • Loading branch information
fivaz committed Jan 13, 2024
1 parent dc78a6e commit 5692bd8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
11 changes: 3 additions & 8 deletions src/lib/task/seed.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { fun, routine, sleep, work } from '$lib/category/seed';
import type { TTask } from '$lib/task/utils';
import { addDays } from 'date-fns';
import { addDays, set } from 'date-fns';

function getTodayAtTime(time: string): Date {
// Parse the hour and minute from the input time
const [hour, minute] = time.split(':').map(Number);
const [hours, minutes] = time.split(':').map(Number);

// Get today's date at the given hour and minute
// Format the date as an ISO 8601 string
const date = new Date();

date.setHours(hour, minute, 0, 0);

return date;
return set(date, { hours, minutes });
}

let id = 0;
Expand Down
6 changes: 2 additions & 4 deletions src/routes/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fail, redirect } from '@sveltejs/kit';
import { homeRoute } from '$lib/consts';
import { handleError } from '$lib/server/form-utils';
import { auth } from '$lib/server/lucia';
import { LuciaError } from 'lucia';

Expand Down Expand Up @@ -39,10 +40,7 @@ export const actions = {
});
}

console.log(error);
return fail(422, {
error: error instanceof Error ? error.message : 'unknown error',
});
return handleError(error);
}

throw redirect(302, homeRoute);
Expand Down
38 changes: 34 additions & 4 deletions src/routes/register/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import { fail, redirect } from '@sveltejs/kit';
import { fun, sleep, work } from '$lib/category/seed';
import { homeRoute } from '$lib/consts';
import prisma from '$lib/prisma';
import { handleError } from '$lib/server/form-utils';
import { auth } from '$lib/server/lucia';

import { LuciaError } from 'lucia';
import type { PageServerLoad, Actions } from './$types';

async function createDefaultCategories(userId: string) {
return prisma.category.createMany({
data: [
{
userId,
name: work.name,
isDefault: work.isDefault,
color: work.color,
group: work.group,
},
{
userId,
name: fun.name,
isDefault: fun.isDefault,
color: fun.color,
group: fun.group,
},
{
userId,
name: sleep.name,
isDefault: sleep.isDefault,
color: sleep.color,
group: sleep.group,
},
],
});
}

export const load: PageServerLoad = async ({ locals }) => {
const session = await locals.auth.validate();
if (session) {
Expand Down Expand Up @@ -39,17 +70,16 @@ export const actions = {
});

locals.auth.setSession(session); // set session cookie

await createDefaultCategories(user.userId);
} catch (error) {
if (error instanceof LuciaError && error.message === 'AUTH_DUPLICATE_KEY_ID') {
return fail(400, {
message: 'Username already taken',
});
}

console.log(error);
return fail(422, {
error: error instanceof Error ? error.message : 'unknown error',
});
return handleError(error);
}

throw redirect(302, homeRoute);
Expand Down

0 comments on commit 5692bd8

Please sign in to comment.