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>
|
||||
Reference in New Issue
Block a user