Skip to content

Commit

Permalink
Merge pull request s1lvax#50 from tawaks/feature/limit-techstack-link…
Browse files Browse the repository at this point in the history
…-entries

[Feature] Limit TechStack and Link entries to 15 per user (Fixes s1lvax#25)
  • Loading branch information
s1lvax authored Oct 20, 2024
2 parents c8b6af2 + 8d0ed1e commit 69053d9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 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 skills.</p>
{:else if $message}
<p class="text-red-500 mt-2 text-center">{$message}</p>
{/if}
8 changes: 8 additions & 0 deletions src/routes/profile/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export const actions: Actions = {

if (user) {
try {
const linkCount = await prisma.link.count({where: { userId: user.githubId }});
if (linkCount >= 15) {
return fail(400, {form, message: 'You have reached the maximum limit of 15 links.'});
}
await prisma.link.create({
data: {
title,
Expand Down Expand Up @@ -144,6 +148,10 @@ export const actions: Actions = {

if (user) {
try {
const skillCount = await prisma.skill.count({where :{userId:user.githubId}});
if (skillCount >= 15) {
return fail(400, {form,message:'You have reached the maximum limit of 15 skills'})
}
await prisma.skill.create({
data: {
title,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/profile/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<Card.Description>
The links visible on your profile. You can drag links around to modify the order
</Card.Description>
<LinkForm data={data.form} linksLength={data.links.length} />
<LinkForm data={data.form} linksLength={data.links.length} links = {data.links} />
</div>
</Card.Header>
<Card.Content>
Expand All @@ -82,7 +82,7 @@
<Card.Header>
<Card.Title>Tech Stack</Card.Title>
<Card.Description>You can drag skills around to modify the order</Card.Description>
<SkillsForm data={data.skillsForm} skillsLength={data.skills.length} />
<SkillsForm data={data.skillsForm} skillsLength={data.skills.length} skills = {data.skills} />
</Card.Header>
<Card.Content class="grid gap-8">
<UserSkills skills={data.skills} />
Expand Down

0 comments on commit 69053d9

Please sign in to comment.