CRUD - Affirmations
This commit is contained in:
@@ -1,7 +1,146 @@
|
||||
<script lang="ts">
|
||||
let { method: String = 'POST' } = $props();
|
||||
import TipTap from '$lib/components/tiptap/TipTap.svelte';
|
||||
|
||||
type Category = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type Frequency = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type Affirmation = {
|
||||
id?: string;
|
||||
name?: string;
|
||||
content?: string;
|
||||
price?: number;
|
||||
categoryId?: string;
|
||||
solfeggioFrequencyId?: string;
|
||||
};
|
||||
|
||||
type FormData = {
|
||||
id?: string;
|
||||
name?: string;
|
||||
content?: string;
|
||||
price?: number;
|
||||
categoryId?: string;
|
||||
categoryName?: string;
|
||||
solfeggioFrequencyId?: string;
|
||||
};
|
||||
|
||||
type Props = {
|
||||
affirmation?: Affirmation;
|
||||
categories: Category[];
|
||||
frequencies: Frequency[];
|
||||
form?: FormData;
|
||||
action: string;
|
||||
submitLabel?: string;
|
||||
};
|
||||
|
||||
let {
|
||||
affirmation = {},
|
||||
categories,
|
||||
frequencies,
|
||||
form,
|
||||
action,
|
||||
submitLabel = 'Save Affirmation'
|
||||
}: Props = $props();
|
||||
|
||||
const currentCategory = $derived(
|
||||
categories.find((c : Category) => c.id === (form?.categoryId ?? affirmation.categoryId))
|
||||
);
|
||||
|
||||
let categoryInput = $derived(form?.categoryName ?? currentCategory?.name ?? '');
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<form></form>
|
||||
</div>
|
||||
<form method="POST" {action} class="space-y-5">
|
||||
{#if affirmation.id || form?.id}
|
||||
<input type="hidden" name="id" value={form?.id ?? affirmation.id} />
|
||||
{/if}
|
||||
|
||||
<div>
|
||||
<label for="name" class="mb-1 block text-sm font-medium text-gray-700">Name</label>
|
||||
<input
|
||||
type="text"
|
||||
id="name"
|
||||
name="name"
|
||||
value={form?.name ?? affirmation.name ?? ''}
|
||||
required
|
||||
class="w-full rounded-lg border border-gray-300 px-3 py-2 outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="mb-1 block text-sm font-medium text-gray-700" for="content">Content</label>
|
||||
<TipTap initialContent={form?.content ?? affirmation.content ?? ''} name="content" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="solfeggioFrequencyId" class="mb-1 block text-sm font-medium text-gray-700">
|
||||
Solfeggio Frequency
|
||||
</label>
|
||||
<select
|
||||
id="solfeggioFrequencyId"
|
||||
name="solfeggioFrequencyId"
|
||||
required
|
||||
class="w-full rounded-lg border border-gray-300 bg-white px-3 py-2 outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="" disabled selected={!affirmation.solfeggioFrequencyId && !form?.solfeggioFrequencyId}>
|
||||
Select a frequency
|
||||
</option>
|
||||
{#each frequencies as frequency}
|
||||
<option
|
||||
value={frequency.id}
|
||||
selected={frequency.id === (form?.solfeggioFrequencyId ?? affirmation.solfeggioFrequencyId)}
|
||||
>
|
||||
{frequency.name}
|
||||
</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="categoryName" class="mb-1 block text-sm font-medium text-gray-700">Category</label>
|
||||
<input
|
||||
type="text"
|
||||
id="categoryName"
|
||||
name="categoryName"
|
||||
bind:value={categoryInput}
|
||||
list="categories"
|
||||
required
|
||||
class="w-full rounded-lg border border-gray-300 px-3 py-2 outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
<datalist id="categories">
|
||||
{#each categories as category}
|
||||
<option value={category.name}>{category.name}</option>
|
||||
{/each}
|
||||
</datalist>
|
||||
<p class="mt-1 text-sm text-gray-500">Select existing or type to create new</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="price" class="mb-1 block text-sm font-medium text-gray-700">Price</label>
|
||||
<div class="relative">
|
||||
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500">$</span>
|
||||
<input
|
||||
type="number"
|
||||
id="price"
|
||||
name="price"
|
||||
value={form?.price ?? affirmation.price ?? ''}
|
||||
min="0"
|
||||
step="0.01"
|
||||
required
|
||||
class="w-full rounded-lg border border-gray-300 py-2 pr-3 pl-7 outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
class="rounded-lg bg-blue-600 px-4 py-2.5 font-medium text-white transition-colors hover:bg-blue-700 focus:ring-4 focus:ring-blue-200"
|
||||
>
|
||||
{submitLabel}
|
||||
</button>
|
||||
</form>
|
||||
@@ -0,0 +1,61 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { Editor } from '@tiptap/core';
|
||||
import StarterKit from '@tiptap/starter-kit';
|
||||
|
||||
let { initialContent = '', name = 'content' } = $props<{ initialContent: string; name?: string }>();
|
||||
|
||||
let element: HTMLElement;
|
||||
let editor: Editor;
|
||||
let content = $state('');
|
||||
|
||||
onMount(() => {
|
||||
content = initialContent;
|
||||
|
||||
editor = new Editor({
|
||||
element,
|
||||
extensions: [StarterKit],
|
||||
content: initialContent,
|
||||
onUpdate: ({ editor }) => {
|
||||
content = editor.getHTML();
|
||||
}
|
||||
});
|
||||
|
||||
return () => editor.destroy();
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="border border-gray-300 rounded-lg overflow-hidden">
|
||||
<div class="flex gap-1 p-2 bg-gray-100 border-b border-gray-300">
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => editor.chain().focus().toggleBold().run()}
|
||||
class="px-3 py-1 rounded font-bold hover:bg-gray-200 transition-colors"
|
||||
>B</button>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => editor.chain().focus().toggleItalic().run()}
|
||||
class="px-3 py-1 rounded italic hover:bg-gray-200 transition-colors"
|
||||
>I</button>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => editor.chain().focus().toggleStrike().run()}
|
||||
class="px-3 py-1 rounded line-through hover:bg-gray-200 transition-colors"
|
||||
>S</button>
|
||||
<div class="w-px bg-gray-300 mx-1"></div>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => editor.chain().focus().toggleBulletList().run()}
|
||||
class="px-3 py-1 rounded hover:bg-gray-200 transition-colors"
|
||||
>• List</button>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => editor.chain().focus().toggleOrderedList().run()}
|
||||
class="px-3 py-1 rounded hover:bg-gray-200 transition-colors"
|
||||
>1. List</button>
|
||||
</div>
|
||||
|
||||
<div bind:this={element} class="p-3 min-h-40 prose prose-sm max-w-none focus:outline-none"></div>
|
||||
</div>
|
||||
|
||||
<input type="hidden" {name} value={content} />
|
||||
@@ -0,0 +1,235 @@
|
||||
<script lang="ts">
|
||||
type Affirmation = {
|
||||
id: string;
|
||||
name: string;
|
||||
content: string;
|
||||
price: number;
|
||||
solfeggioFrequency?: {
|
||||
name: string;
|
||||
value: number;
|
||||
};
|
||||
category?: {
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
|
||||
type SortKey = 'name' | 'category' | 'frequency' | 'price';
|
||||
type SortDirection = 'asc' | 'desc';
|
||||
|
||||
type Props = {
|
||||
affirmations: Affirmation[];
|
||||
};
|
||||
|
||||
let { affirmations }: Props = $props();
|
||||
|
||||
let search = $state('');
|
||||
let categoryFilter = $state('');
|
||||
let frequencyFilter = $state('');
|
||||
let sortKey = $state<SortKey>('name');
|
||||
let sortDirection = $state<SortDirection>('asc');
|
||||
|
||||
const categories = $derived([...new Set(affirmations.map((a) => a.category?.name).filter(Boolean))].sort());
|
||||
const frequencies = $derived([...new Set(affirmations.map((a) => a.solfeggioFrequency?.name).filter(Boolean))].sort());
|
||||
|
||||
const filteredAndSorted = $derived(() => {
|
||||
let result = affirmations;
|
||||
|
||||
// Search filter
|
||||
if (search) {
|
||||
const searchLower = search.toLowerCase();
|
||||
result = result.filter(
|
||||
(a) =>
|
||||
a.name.toLowerCase().includes(searchLower) ||
|
||||
a.content.toLowerCase().includes(searchLower) ||
|
||||
a.category?.name.toLowerCase().includes(searchLower) ||
|
||||
a.solfeggioFrequency?.name.toLowerCase().includes(searchLower)
|
||||
);
|
||||
}
|
||||
|
||||
// Category filter
|
||||
if (categoryFilter) {
|
||||
result = result.filter((a) => a.category?.name === categoryFilter);
|
||||
}
|
||||
|
||||
// Frequency filter
|
||||
if (frequencyFilter) {
|
||||
result = result.filter((a) => a.solfeggioFrequency?.name === frequencyFilter);
|
||||
}
|
||||
|
||||
// Sort
|
||||
result = [...result].sort((a, b) => {
|
||||
let aVal: string | number;
|
||||
let bVal: string | number;
|
||||
|
||||
switch (sortKey) {
|
||||
case 'name':
|
||||
aVal = a.name.toLowerCase();
|
||||
bVal = b.name.toLowerCase();
|
||||
break;
|
||||
case 'category':
|
||||
aVal = a.category?.name?.toLowerCase() ?? '';
|
||||
bVal = b.category?.name?.toLowerCase() ?? '';
|
||||
break;
|
||||
case 'frequency':
|
||||
aVal = a.solfeggioFrequency?.value ?? 0;
|
||||
bVal = b.solfeggioFrequency?.value ?? 0;
|
||||
break;
|
||||
case 'price':
|
||||
aVal = a.price;
|
||||
bVal = b.price;
|
||||
break;
|
||||
}
|
||||
|
||||
if (aVal < bVal) return sortDirection === 'asc' ? -1 : 1;
|
||||
if (aVal > bVal) return sortDirection === 'asc' ? 1 : -1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
function toggleSort(key: SortKey) {
|
||||
if (sortKey === key) {
|
||||
sortDirection = sortDirection === 'asc' ? 'desc' : 'asc';
|
||||
} else {
|
||||
sortKey = key;
|
||||
sortDirection = 'asc';
|
||||
}
|
||||
}
|
||||
|
||||
function getSortIndicator(key: SortKey) {
|
||||
if (sortKey !== key) return '↕';
|
||||
return sortDirection === 'asc' ? '↑' : '↓';
|
||||
}
|
||||
|
||||
function clearFilters() {
|
||||
search = '';
|
||||
categoryFilter = '';
|
||||
frequencyFilter = '';
|
||||
}
|
||||
|
||||
const hasActiveFilters = $derived(search || categoryFilter || frequencyFilter);
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-4">
|
||||
<div class="flex flex-wrap items-center gap-3">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search affirmations..."
|
||||
bind:value={search}
|
||||
class="rounded-lg border border-gray-300 px-3 py-2 text-sm outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
|
||||
<select
|
||||
bind:value={categoryFilter}
|
||||
class="rounded-lg border border-gray-300 bg-white px-3 py-2 text-sm outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="">All Categories</option>
|
||||
{#each categories as category}
|
||||
<option value={category}>{category}</option>
|
||||
{/each}
|
||||
</select>
|
||||
|
||||
<select
|
||||
bind:value={frequencyFilter}
|
||||
class="rounded-lg border border-gray-300 bg-white px-3 py-2 text-sm outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="">All Frequencies</option>
|
||||
{#each frequencies as frequency}
|
||||
<option value={frequency}>{frequency}</option>
|
||||
{/each}
|
||||
</select>
|
||||
|
||||
{#if hasActiveFilters}
|
||||
<button
|
||||
type="button"
|
||||
onclick={clearFilters}
|
||||
class="rounded-lg px-3 py-2 text-sm text-gray-500 hover:bg-gray-100 hover:text-gray-700"
|
||||
>
|
||||
Clear filters
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
<span class="ml-auto text-sm text-gray-500">
|
||||
{filteredAndSorted().length} of {affirmations.length}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full border-collapse text-left text-sm">
|
||||
<thead>
|
||||
<tr class="border-b border-gray-200 bg-gray-50">
|
||||
<th class="px-4 py-3 font-medium text-gray-700">
|
||||
<button
|
||||
type="button"
|
||||
class="flex items-center gap-1 hover:text-gray-900"
|
||||
onclick={() => toggleSort('name')}
|
||||
>
|
||||
Name
|
||||
<span class="text-xs text-gray-400">{getSortIndicator('name')}</span>
|
||||
</button>
|
||||
</th>
|
||||
<th class="px-4 py-3 font-medium text-gray-700">
|
||||
<button
|
||||
type="button"
|
||||
class="flex items-center gap-1 hover:text-gray-900"
|
||||
onclick={() => toggleSort('category')}
|
||||
>
|
||||
Category
|
||||
<span class="text-xs text-gray-400">{getSortIndicator('category')}</span>
|
||||
</button>
|
||||
</th>
|
||||
<th class="px-4 py-3 font-medium text-gray-700">
|
||||
<button
|
||||
type="button"
|
||||
class="flex items-center gap-1 hover:text-gray-900"
|
||||
onclick={() => toggleSort('frequency')}
|
||||
>
|
||||
Frequency
|
||||
<span class="text-xs text-gray-400">{getSortIndicator('frequency')}</span>
|
||||
</button>
|
||||
</th>
|
||||
<th class="px-4 py-3 text-right font-medium text-gray-700">
|
||||
<button
|
||||
type="button"
|
||||
class="ml-auto flex items-center gap-1 hover:text-gray-900"
|
||||
onclick={() => toggleSort('price')}
|
||||
>
|
||||
Price
|
||||
<span class="text-xs text-gray-400">{getSortIndicator('price')}</span>
|
||||
</button>
|
||||
</th>
|
||||
<th class="px-4 py-3 font-medium text-gray-700"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each filteredAndSorted() as affirmation}
|
||||
<tr class="border-b border-gray-100 hover:bg-gray-50">
|
||||
<td class="px-4 py-3 font-medium text-gray-900">{affirmation.name}</td>
|
||||
<td class="px-4 py-3 text-gray-600">{affirmation.category?.name ?? '—'}</td>
|
||||
<td class="px-4 py-3 text-gray-600">{affirmation.solfeggioFrequency?.name ?? '—'}</td>
|
||||
<td class="px-4 py-3 text-right text-gray-600">${affirmation.price.toFixed(2)}</td>
|
||||
<td class="px-4 py-3 text-right">
|
||||
<a
|
||||
href="/affirmations/{affirmation.id}"
|
||||
class="text-blue-600 hover:text-blue-800 hover:underline"
|
||||
>
|
||||
Edit
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
{:else}
|
||||
<tr>
|
||||
<td colspan="5" class="px-4 py-8 text-center text-gray-500">
|
||||
{#if hasActiveFilters}
|
||||
No affirmations match your filters
|
||||
{:else}
|
||||
No affirmations yet
|
||||
{/if}
|
||||
</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -2,13 +2,30 @@
|
||||
import Window from '$lib/components/containers/Window.svelte';
|
||||
|
||||
const { children } = $props();
|
||||
|
||||
let adminExpanded = $state(true);
|
||||
</script>
|
||||
|
||||
<div class="flex min-h-0 flex-1">
|
||||
<div class="mt-12 mr-12 flex flex-col">
|
||||
<div class="mt-12 mr-12 flex flex-col gap-1">
|
||||
<a href="/dashboard" class="text-blue-600">Dashboard</a>
|
||||
|
||||
<div class="flex flex-col">
|
||||
<button
|
||||
class="flex items-center gap-1 text-left text-gray-700 hover:text-gray-900"
|
||||
onclick={() => adminExpanded = !adminExpanded}
|
||||
>
|
||||
<span class="w-4 text-xs">{adminExpanded ? '▼' : '▶'}</span>
|
||||
Admin
|
||||
</button>
|
||||
{#if adminExpanded}
|
||||
<div class="ml-5 flex flex-col">
|
||||
<a href="/affirmations" class="text-blue-600">Affirmations</a>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<Window>
|
||||
{@render children()}
|
||||
</Window>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,7 +1,76 @@
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { prisma } from "$lib/db";
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { prisma } from '$lib/db';
|
||||
import { fail, type Actions, type RequestEvent } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const affirmations = await prisma.affirmation.findMany();
|
||||
return { affirmations }
|
||||
}
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: locals.user.id },
|
||||
select: { id: true, name: true, role: true }
|
||||
});
|
||||
const affirmations = await prisma.affirmation.findMany({
|
||||
include: {
|
||||
solfeggioFrequency: true,
|
||||
category: true
|
||||
}
|
||||
});
|
||||
const affirmationCategories = await prisma.affirmationCategory.findMany();
|
||||
const solfeggioFrequencies = await prisma.solfeggioFrequency.findMany();
|
||||
return { user, affirmationCategories, affirmations, solfeggioFrequencies };
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
create: async ({ request }: RequestEvent) => {
|
||||
const form = await request.formData();
|
||||
const frequencyId = Number(form.get('solfeggioFrequencyId')?.toString()) || 0;
|
||||
const price = Number(form.get('price')) || 0.0;
|
||||
const categoryName = form.get('categoryName') as string;
|
||||
|
||||
if (frequencyId === 0) {
|
||||
return fail(400, {
|
||||
error: 'Solfeggio Frequency not selected!',
|
||||
...Object.fromEntries(form)
|
||||
});
|
||||
}
|
||||
|
||||
if (!categoryName) {
|
||||
return fail(400, {
|
||||
error: 'Category is required!',
|
||||
...Object.fromEntries(form)
|
||||
});
|
||||
}
|
||||
|
||||
let category = await prisma.affirmationCategory.findFirst({ where: { name: categoryName } });
|
||||
|
||||
if (!category) {
|
||||
category = await prisma.affirmationCategory.create({
|
||||
data: {
|
||||
name: categoryName
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const newAffirmation = await prisma.affirmation.create({
|
||||
data: {
|
||||
name: form.get('name') as string,
|
||||
content: form.get('content') as string,
|
||||
solfeggioFrequencyId: frequencyId,
|
||||
categoryId: category.id,
|
||||
price: price
|
||||
},
|
||||
include: {
|
||||
category: true,
|
||||
solfeggioFrequency: true
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, newAffirmation };
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return fail(500, {
|
||||
error: 'An unknown error has occurred. Please try again later.',
|
||||
...Object.fromEntries(form)
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,10 +1,75 @@
|
||||
<script lang="ts">
|
||||
let { data } = $props();
|
||||
import AffirmationForm from '$lib/components/forms/affirmation/AffirmationForm.svelte';
|
||||
import Center from '$lib/components/containers/Center.svelte';
|
||||
import Window from '$lib/components/containers/Window.svelte';
|
||||
import type { ActionData, PageData } from './$types';
|
||||
import AffirmationList from '$lib/components/ui/AffirmationList/AffirmationList.svelte';
|
||||
|
||||
let { data, form } = $props<{ data: PageData; form: ActionData }>();
|
||||
let showModal = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
if (form?.success) {
|
||||
showModal = false;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{#each data.affirmations as affirmation}
|
||||
<span>{affirmation.name}</span>
|
||||
<span>{affirmation.price}</span>
|
||||
{/each}
|
||||
<div class="flex h-full flex-col gap-6 p-6">
|
||||
<div class="flex items-center justify-between">
|
||||
<h1 class="text-2xl font-semibold text-gray-900">Affirmations</h1>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-lg bg-green-600 px-4 py-2 font-medium text-white transition-colors hover:bg-green-700"
|
||||
onclick={() => (showModal = true)}
|
||||
>
|
||||
Create +
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if data.affirmations.length > 0}
|
||||
<Window>
|
||||
<AffirmationList affirmations={data.affirmations} />
|
||||
</Window>
|
||||
{:else}
|
||||
<Center>
|
||||
<div class="text-center">
|
||||
<p class="text-lg text-gray-500">No affirmations yet</p>
|
||||
<p class="mt-1 text-sm text-gray-400">Click "Create +" to add your first one</p>
|
||||
</div>
|
||||
</Center>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if showModal}
|
||||
<div class="fixed inset-0 z-50 overflow-y-auto bg-black/50">
|
||||
<div class="flex min-h-full items-center justify-center p-4">
|
||||
<div class="w-full max-w-2xl rounded-lg bg-white p-8 shadow-xl">
|
||||
<div class="mb-6 flex items-center justify-between">
|
||||
<h2 class="text-xl font-semibold text-gray-900">Create New Affirmation</h2>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded p-1 text-gray-400 transition-colors hover:bg-gray-100 hover:text-gray-600"
|
||||
onclick={() => (showModal = false)}
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if form?.error}
|
||||
<div class="mb-4 rounded-lg border border-red-200 bg-red-50 p-3 text-red-700">
|
||||
{form.error}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<AffirmationForm
|
||||
categories={data.affirmationCategories}
|
||||
frequencies={data.solfeggioFrequencies}
|
||||
{form}
|
||||
action="?/create"
|
||||
submitLabel="Create Affirmation"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -0,0 +1,75 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { prisma } from '$lib/db';
|
||||
import { error, fail, type Actions, type RequestEvent, type ServerLoadEvent } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ params }: ServerLoadEvent) => {
|
||||
const affirmation = await prisma.affirmation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: { solfeggioFrequency: true, category: true }
|
||||
});
|
||||
|
||||
const solfeggioFrequencies = await prisma.solfeggioFrequency.findMany();
|
||||
const affirmationCategories = await prisma.affirmationCategory.findMany();
|
||||
|
||||
if (!affirmation) {
|
||||
throw error(404, 'not found');
|
||||
}
|
||||
|
||||
console.log(affirmation);
|
||||
|
||||
return { affirmation, solfeggioFrequencies, affirmationCategories };
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
update: async ({ request }: RequestEvent) => {
|
||||
const form = await request.formData();
|
||||
const frequencyId = Number(form.get('solfeggioFrequencyId')?.toString()) || 0;
|
||||
const price = Number(form.get('price')) || 0.0;
|
||||
const categoryName = form.get('categoryName') as string;
|
||||
|
||||
let category = await prisma.affirmationCategory.findFirst({ where: { name: categoryName } });
|
||||
|
||||
if (!category) {
|
||||
category = await prisma.affirmationCategory.create({
|
||||
data: {
|
||||
name: categoryName
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (frequencyId === 0) {
|
||||
return fail(400, {
|
||||
error: 'Solfeggio Frequency not selected!',
|
||||
...Object.fromEntries(form)
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const updatedAffirmation = await prisma.affirmation.update({
|
||||
where: {
|
||||
id: form.get('id') as string
|
||||
},
|
||||
data: {
|
||||
name: form.get('name') as string,
|
||||
content: form.get('content') as string,
|
||||
solfeggioFrequencyId: frequencyId,
|
||||
categoryId: category.id,
|
||||
price: price
|
||||
},
|
||||
include: {
|
||||
category: true,
|
||||
solfeggioFrequency: true
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, updatedAffirmation };
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return fail(500, {
|
||||
error:
|
||||
'An unknown error has occurred. Please try again later. Your changes have not been saved.',
|
||||
...Object.fromEntries(form)
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
<script lang="ts">
|
||||
import AffirmationForm from '$lib/components/forms/affirmation/AffirmationForm.svelte';
|
||||
import type { ActionData, PageData } from './$types';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
let { data, form } = $props<{ data: PageData; form: ActionData }>();
|
||||
let seconds = $state(0);
|
||||
let remainder = $derived(5 - seconds);
|
||||
|
||||
onMount(() => {
|
||||
const interval = setInterval(() => {
|
||||
if (form?.success) {
|
||||
seconds += 1;
|
||||
if (seconds === 6) {
|
||||
window.location.href = '/affirmations';
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="mx-auto mb-auto mt-24 max-w-2xl px-4">
|
||||
<a href="/affirmations" class="text-blue-600">Back to Affirmations</a>
|
||||
|
||||
{#if form?.error}
|
||||
<div class="mb-4 rounded-lg border border-red-200 bg-red-50 p-3 text-red-700">
|
||||
{form.error}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if form?.success}
|
||||
<div class="mb-4 rounded-lg border border-green-200 bg-green-50 p-3 text-green-700">
|
||||
Affirmation updated successfully! You will be redirected in {remainder} seconds.
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="mt-6">
|
||||
<AffirmationForm
|
||||
affirmation={data.affirmation}
|
||||
categories={data.affirmationCategories}
|
||||
frequencies={data.solfeggioFrequencies}
|
||||
{form}
|
||||
action="?/update"
|
||||
submitLabel="Save Affirmation"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user