Skip to content

Commit

Permalink
feat(forms): Add client-side limit checks for skills and links
Browse files Browse the repository at this point in the history
  • Loading branch information
tawaks committed Oct 20, 2024
1 parent 721e98d commit f4db616
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/lib/components/MyProfile/LinkForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import { linksSchema, type LinksSchema } from '$lib/schemas/links';
import { type SuperValidated, type Infer, superForm } from 'sveltekit-superforms';
import { zodClient } from 'sveltekit-superforms/adapters';
import type { Link } from '@prisma/client';
export let data: SuperValidated<Infer<LinksSchema>>;
export let linksLength: number;
export let links: Link[] = [];
let isLimitReached = false;
$: isLimitReached = links.length >= 15;
const form = superForm(data, {
validators: zodClient(linksSchema)
});
const { form: formData, enhance } = form;
const { form: formData, enhance, message } = form;
$: $formData.order = linksLength;
</script>
Expand Down Expand Up @@ -49,5 +53,11 @@
</Form.Control>
</Form.Field>

<Form.Button class="mt-5 flex align-bottom">Add</Form.Button>
<Form.Button disabled = {isLimitReached} class="mt-5 flex align-bottom">Add</Form.Button>
</form>

{#if isLimitReached}
<p class="text-red-500 mt-2 text-center">You have reached the maximum limit of 15 links.</p>
{:else if $message}
<p class="text-red-500 mt-2 text-center">{$message}</p>
{/if}
15 changes: 12 additions & 3 deletions src/lib/components/MyProfile/SkillsForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import { Select } from 'bits-ui';
import { type SuperValidated, type Infer, superForm } from 'sveltekit-superforms';
import { zodClient } from 'sveltekit-superforms/adapters';
import type { Skill } from '@prisma/client';
export let data: SuperValidated<Infer<SkillsSchema>>;
export let skillsLength: number;
export let skills: Skill[] = [];
let isLimitReached = false;
$: isLimitReached = skills.length >= 15;
const form = superForm(data, {
validators: zodClient(skillsSchema),
resetForm: false,
Expand All @@ -20,7 +23,7 @@
}
});
const { form: formData, enhance } = form;
const { form: formData, enhance ,message} = form;
$: $formData.order = skillsLength;
$: selectedLevel = $formData.level
Expand Down Expand Up @@ -81,5 +84,11 @@
</Form.Control>
</Form.Field>

<Form.Button>Add</Form.Button>
<Form.Button disabled = {isLimitReached}>Add</Form.Button>
</form>

{#if isLimitReached}
<p class="text-red-500 mt-2 text-center">You have reached the maximum limit of 15 links.</p>
{:else if $message}
<p class="text-red-500 mt-2 text-center">{$message}</p>
{/if}

0 comments on commit f4db616

Please sign in to comment.