-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(brands): add EditBrandForm component and implement brand creatio…
…n and editing functionality Fixes #247
- Loading branch information
1 parent
34db907
commit 0d7e3f8
Showing
10 changed files
with
389 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<template> | ||
<form | ||
:class="$style.form" | ||
:disabled="submitting" | ||
@submit.prevent="onSubmit" | ||
> | ||
<div :class="$style.inputs"> | ||
<PerdInput | ||
required | ||
autofocus | ||
autocomplete="off" | ||
label="Name" | ||
placeholder="The West Pace" | ||
v-model.trim="name" | ||
/> | ||
|
||
<PerdInput | ||
label="Website URL" | ||
placeholder="https://perd.pages.dev" | ||
type="url" | ||
v-model.trim="websiteUrl" | ||
/> | ||
</div> | ||
|
||
<div :class="$style.buttons"> | ||
<PerdButton | ||
secondary | ||
type="button" | ||
:disabled="submitting" | ||
@click="onCancel" | ||
> | ||
Cancel | ||
</PerdButton> | ||
|
||
<PerdButton | ||
type="submit" | ||
:loading="submitting" | ||
> | ||
{{ saveButtonText }} | ||
</PerdButton> | ||
</div> | ||
</form> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import PerdButton from '~/components/PerdButton.vue' | ||
import PerdInput from '~/components/PerdInput.vue' | ||
interface Props { | ||
readonly submitting: boolean; | ||
readonly saveButtonText: string; | ||
} | ||
type Emits = (event: 'submit') => void; | ||
defineProps<Props>() | ||
const emit = defineEmits<Emits>() | ||
const router = useRouter() | ||
const name = defineModel<string>('name', { | ||
required: true | ||
}) | ||
const websiteUrl = defineModel<string>('websiteUrl', { | ||
required: true | ||
}) | ||
function onSubmit() { | ||
emit('submit') | ||
} | ||
function onCancel() { | ||
router.back() | ||
} | ||
</script> | ||
|
||
<style module> | ||
.form { | ||
display: grid; | ||
row-gap: var(--spacing-32); | ||
} | ||
.inputs { | ||
display: grid; | ||
gap: var(--spacing-16); | ||
} | ||
.buttons { | ||
display: grid; | ||
grid-auto-flow: column; | ||
grid-auto-columns: 1fr; | ||
column-gap: var(--spacing-16); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<template> | ||
<PerdInput | ||
clearable | ||
v-model.trim="model" | ||
label="Search" | ||
placeholder="Name, description, etc." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
<template> | ||
<PageContent page-title="Add new brand"> | ||
<EditBrandForm | ||
v-model:name="name" | ||
v-model:website-url="websiteUrl" | ||
:submitting="isSubmitting" | ||
save-button-text="Add brand" | ||
@submit="onSubmit" | ||
/> | ||
</PageContent> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import PageContent from '~/components/layout/PageContent.vue' | ||
import EditBrandForm from '~/components/brands/EditBrandForm.vue' | ||
definePageMeta({ | ||
layout: 'page', | ||
middleware: ['admin'] | ||
}) | ||
const name = ref('') | ||
const websiteUrl = ref('') | ||
const isSubmitting = ref(false) | ||
const { addToast } = useToaster() | ||
const { showErrorToast } = useApiErrorToast() | ||
function resetForm() { | ||
name.value = '' | ||
websiteUrl.value = '' | ||
} | ||
async function onSubmit() { | ||
if (isSubmitting.value) { | ||
return | ||
} | ||
try { | ||
isSubmitting.value = true | ||
const websiteUrlValue = websiteUrl.value === '' ? undefined : websiteUrl.value | ||
await $fetch('/api/brands', { | ||
method: 'post', | ||
body: { | ||
name: name.value, | ||
websiteUrl: websiteUrlValue | ||
} | ||
}) | ||
addToast({ | ||
title: 'Brand added 🎉', | ||
message: 'The brand has been successfully added' | ||
}) | ||
resetForm() | ||
} catch (error) { | ||
showErrorToast(error, 'Failed to add brand 🥲') | ||
} finally { | ||
isSubmitting.value = false | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
|
||
<template> | ||
<PageContent page-title="Edit brand"> | ||
<EmptyState | ||
v-if="error" | ||
:icon="errorIcon" | ||
> | ||
{{ errorText }} | ||
</EmptyState> | ||
|
||
<EditBrandForm | ||
v-else | ||
v-model:name="name" | ||
v-model:website-url="websiteUrl" | ||
:submitting="isSubmitting" | ||
save-button-text="Save changes" | ||
@submit="onSubmit" | ||
/> | ||
</PageContent> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import PageContent from '~/components/layout/PageContent.vue' | ||
import EmptyState from '~/components/EmptyState.vue' | ||
import EditBrandForm from '~/components/brands/EditBrandForm.vue' | ||
definePageMeta({ | ||
layout: 'page', | ||
middleware: ['admin'] | ||
}) | ||
const route = useRoute() | ||
const router = useRouter() | ||
const brandId = route.params.brandId?.toString() ?? '' | ||
const name = ref('') | ||
const websiteUrl = ref('') | ||
const isSubmitting = ref(false) | ||
const { addToast } = useToaster() | ||
const { showErrorToast } = useApiErrorToast() | ||
const { data, error } = await useFetch(`/api/brands/${brandId}`) | ||
if (data.value) { | ||
name.value = data.value.name | ||
websiteUrl.value = data.value.websiteUrl ?? '' | ||
} | ||
const errorIcon = computed(() => { | ||
if (error.value?.statusCode === 404) { | ||
return 'streamline-emojis:man-shrugging-1' | ||
} | ||
return 'streamline-emojis:face-screaming-in-fear' | ||
}) | ||
const errorText = computed(() => { | ||
if (error.value?.statusCode === 404) { | ||
return `Brand with ID ${brandId} not found` | ||
} | ||
return 'Something went wrong' | ||
}) | ||
async function onSubmit() { | ||
if (isSubmitting.value) { | ||
return | ||
} | ||
try { | ||
isSubmitting.value = true | ||
await $fetch(`/api/brands/${brandId}`, { | ||
method: 'patch', | ||
body: { | ||
name: name.value, | ||
websiteUrl: websiteUrl.value || undefined | ||
} | ||
}) | ||
addToast({ | ||
title: 'Brand updated 🎉', | ||
message: 'The brand has been successfully updated' | ||
}) | ||
router.push(`/brands/details/${brandId}`) | ||
} catch (error) { | ||
showErrorToast(error, 'Failed to update brand 🥲') | ||
} finally { | ||
isSubmitting.value = false | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { eq } from 'drizzle-orm' | ||
import * as v from 'valibot' | ||
import { limits } from '~~/constants' | ||
|
||
const paramsSchema = v.object({ | ||
brandId: stringToIntegerValidator | ||
}) | ||
|
||
const bodySchema = v.object({ | ||
name: v.pipe( | ||
v.string(), | ||
v.nonEmpty(), | ||
v.maxLength(limits.maxBrandNameLength) | ||
), | ||
|
||
websiteUrl: v.optional( | ||
v.pipe( | ||
v.string(), | ||
v.url(), | ||
) | ||
) | ||
}) | ||
|
||
function validateParams(params: unknown) { | ||
return v.parse(paramsSchema, params) | ||
} | ||
|
||
function validateBody(body: unknown) { | ||
return v.parse(bodySchema, body) | ||
} | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { db } = event.context | ||
const { brandId } = await getValidatedRouterParams(event, validateParams) | ||
const { name, websiteUrl } = await readValidatedBody(event, validateBody) | ||
|
||
await validateAdmin(event) | ||
|
||
const [updated] = await db | ||
.update(tables.brands) | ||
.set({ | ||
name, | ||
websiteUrl | ||
}) | ||
.where( | ||
eq(tables.brands.id, brandId) | ||
) | ||
.returning({ | ||
id: tables.brands.id, | ||
name: tables.brands.name, | ||
websiteUrl: tables.brands.websiteUrl | ||
}) | ||
|
||
if (updated === undefined) { | ||
throw createError({ | ||
statusCode: 404, | ||
message: `Brand with ID ${brandId} not found` | ||
}) | ||
} | ||
|
||
return updated | ||
}) |
Oops, something went wrong.