Skip to content
Snippets Groups Projects
Verified Commit c681e289 authored by Miniontoby's avatar Miniontoby :writing_hand_tone1:
Browse files

Updated playlist create and updated user create

Playlist create now with team select
user create now works
parent e2116991
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,6 @@ export const lucia = new Lucia(adapter, {
interface DatabaseUserAttributes {
name: string;
email: string;
teams: any;
roleId: Roles;
}
......
......@@ -16,6 +16,30 @@ export async function getAll() {
})
}
/**
* Get all teams where userId is in
*
* @param userId
* @returns { object[]|null } team
*/
export async function getAllOfUser(userId: number) {
return await db.team.findMany({
where: {
users: {
some: {
id: userId,
}
},
},
select: {
id: true,
name: true,
description: true,
users: true,
},
})
}
/**
* Find team by ID
*
......
......@@ -26,7 +26,7 @@ export const actions = {
if (user !== undefined) {
// send mail with defined transport object
const info = await sendMail(
String(origUser.email),
String(user.email),
'Account Created', // Subject line
`Hello!\n\nYou are receiving this email because an account has been created for you on PraiseLink.\n\nName: ${name ?? ''}\nEmail: ${email}\nPassword: ${password}\n\nLogin over at ${url}/login\n\nRegards,\nPraiseLink`, // plain text body
`Hello!<br><br>You are receiving this email because an account has been created for you on PraiseLink.<br><br><b>Name:</b> ${name ?? ''}<br><b>Email:</b> ${email}<br><b>Password:</p> ${password}<br><br>Login over at <a href="${url}/login">${url}/login</a><br><br>Regards,<br>PraiseLink`, // html body
......
......@@ -79,6 +79,7 @@
label={true}
labelText={$_('fields.user.email.label')}
required={true}
autocomplete="email"
bind:value={email}
/>
<TextField
......@@ -88,6 +89,7 @@
label={true}
labelText={$_('fields.user.password.label')}
required={true}
autocomplete="new-password"
bind:value={password}
/>
<SelectField
......@@ -105,6 +107,7 @@
label={true}
labelText={$_('fields.user.username.label')}
required={false}
autocomplete="username"
bind:value={name}
/>
<button class="btn-success" disabled={working}>{$_('actions.create')}</button>
......
import { fail } from '@sveltejs/kit';
import * as Playlist from '$lib/server/models/Playlist';
import * as Team from '$lib/server/models/Team';
import { parseAndCheckFormData } from '$lib/utils';
export const load = async ({ parent }) => {
const data = await parent();
const teams = await Team.getAllOfUser(data.authUser.id);
return {
...data,
teams,
}
};
/** @type {import('./$types').Actions} */
export const actions = {
create: async ({ request }) => {
......@@ -9,15 +19,14 @@ export const actions = {
const formData = parseAndCheckFormData(data, [
{ name: 'name', required: true, type: 'string' },
{ name: 'date', required: true, type: 'date' },
{ name: 'teamId', required: true, type: 'number' },
]);
if (!formData.success) return formData.data;
const { name, date } = formData.data;
const { name, date, teamId } = formData.data;
if (await Playlist.findByName(name))
return fail(400, { unique: true });
const teamId = 1;
const playlist = await Playlist.create(teamId, name, date);
return { success: true, playlist };
}
......
<script>
import TextField from '$lib/components/inputs/TextField.svelte';
import SelectField from '$lib/components/inputs/SelectField.svelte';
import Error from '$lib/components/alerts/Error.svelte';
import Notice from '$lib/components/alerts/Notice.svelte';
import Success from '$lib/components/alerts/Success.svelte';
......@@ -8,14 +9,31 @@
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { slide } from 'svelte/transition';
import { writable } from 'svelte/store';
import { _ } from 'svelte-i18n';
import { getContext } from "svelte";
import { getContext, onMount } from 'svelte';
/** @type {import('./$types').ActionData} */
$: form = $page.form;
$: authUser = $page.data.authUser;
$: teams = $page.data.teams;
/** @type {string} */ let name;
/** @type {string} */ let date;
/** @type {number} */ let teamId;
/**
* @type {import('svelte/store').Writeable<{ label: string; value: string; disabled: boolean; }[]>}
*/
let teamOptions = writable(null);
onMount(() => {
const tmpTeamOptions = [];
for (const team of teams) {
if (team.users.find(usr => usr.id === authUser.id))
tmpTeamOptions.push({ label: team.name, value: team.id, disabled: false });
}
$teamOptions = tmpTeamOptions;
});
let working = false;
......@@ -55,6 +73,7 @@
</div>
{/if}
{#if $teamOptions}
<form class="space-y-4 md:space-y-6" action="?/create" method="POST" use:enhance={() => { if (!working) { working = true; return async ({ result }) => { working = false; await applyAction(result); if (result.type === 'redirect') { goto(result.location); } }; } }}>
<TextField
id="name"
......@@ -64,6 +83,7 @@
labelText={$_('fields.playlist.name.label')}
required={true}
bind:value={name}
disabled={working}
/>
<TextField
id="date"
......@@ -73,8 +93,21 @@
labelText={$_('fields.playlist.date.label')}
required={true}
bind:value={date}
disabled={working}
/>
<SelectField
id="teamId"
label={true}
labelText={$_('fields.playlist.team.label')}
required={true}
bind:value={teamId}
options={$teamOptions}
disabled={working}
/>
<button class="btn-success" disabled={working}>{$_('actions.create')}</button>
</form>
{:else}
<Notice message={$_('messages.working')} />
{/if}
</div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment