-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cacae6
commit 16c6cce
Showing
15 changed files
with
687 additions
and
15 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
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
44 changes: 44 additions & 0 deletions
44
...lt)/account/(tabs)/wishlists/_components/update-wishlist-form/_actions/update-wishlist.ts
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,44 @@ | ||
'use server'; | ||
|
||
import { z } from 'zod'; | ||
|
||
import { updateWishlist as updateWishlistMutation } from '~/client/mutations/update-wishlist'; | ||
|
||
const UpdateWishlistSchema = z.object({ | ||
entityId: z.string(), | ||
name: z.string(), | ||
}); | ||
|
||
export const updateWishlist = async (formData: FormData) => { | ||
const { entityId, name } = UpdateWishlistSchema.parse({ | ||
entityId: formData.get('entityId'), | ||
name: formData.get('name'), | ||
}); | ||
|
||
const input = { | ||
data: { | ||
name, | ||
}, | ||
entityId: +entityId, | ||
}; | ||
|
||
try { | ||
const updatedWishlist = await updateWishlistMutation({ input }); | ||
|
||
if (updatedWishlist) { | ||
return { | ||
status: 'success' as const, | ||
data: updatedWishlist, | ||
}; | ||
} | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
return { | ||
status: 'error' as const, | ||
message: error.message, | ||
}; | ||
} | ||
} | ||
|
||
return { status: 'error' as const, message: 'Unknown error.' }; | ||
}; |
119 changes: 119 additions & 0 deletions
119
...pp/[locale]/(default)/account/(tabs)/wishlists/_components/update-wishlist-form/index.tsx
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,119 @@ | ||
import * as DialogPrimitive from '@radix-ui/react-alert-dialog'; | ||
import { useTranslations } from 'next-intl'; | ||
import { ChangeEvent, useState } from 'react'; | ||
import { useFormStatus } from 'react-dom'; | ||
|
||
import { createWishlist as createWishlistMutation } from '~/client/mutations/create-wishlist'; | ||
import { Button } from '~/components/ui/button'; | ||
import { | ||
Field, | ||
FieldControl, | ||
FieldLabel, | ||
FieldMessage, | ||
Form, | ||
FormSubmit, | ||
Input, | ||
} from '~/components/ui/form'; | ||
|
||
import { updateWishlist } from './_actions/update-wishlist'; | ||
|
||
type Wishlist = NonNullable<Awaited<ReturnType<typeof createWishlistMutation>>>; | ||
|
||
interface UpdateWishlistFormProps { | ||
entityId: number; | ||
name: string; | ||
onWishlistUpdated: (updatedWishlist: Wishlist) => void; | ||
} | ||
|
||
const SubmitButton = () => { | ||
const { pending } = useFormStatus(); | ||
const t = useTranslations('Account.Wishlist'); | ||
|
||
return ( | ||
<Button | ||
className="relative w-full items-center px-8 py-2 lg:w-fit" | ||
loading={pending} | ||
loadingText={t('onSubmitText')} | ||
variant="primary" | ||
> | ||
{t('update')} | ||
</Button> | ||
); | ||
}; | ||
|
||
export const UpdateWishlistForm = ({ | ||
entityId, | ||
name, | ||
onWishlistUpdated, | ||
}: UpdateWishlistFormProps) => { | ||
const [isInputValid, setInputValidation] = useState(true); | ||
|
||
const t = useTranslations('Account.Wishlist'); | ||
|
||
const handleInputValidation = (e: ChangeEvent<HTMLInputElement>) => { | ||
const validationStatus = e.target.validity.valueMissing; | ||
|
||
setInputValidation(!validationStatus); | ||
}; | ||
|
||
const onSubmit = async (formData: FormData) => { | ||
const submit = await updateWishlist(formData); | ||
|
||
if (submit.status === 'success') { | ||
onWishlistUpdated(submit.data); | ||
} | ||
}; | ||
|
||
return ( | ||
<Form action={onSubmit} className="w-full" onSubmit={(e) => e.stopPropagation()}> | ||
<Field className="relative space-y-2 pb-7" name="name"> | ||
<FieldLabel htmlFor="wishlist-name">{t('inputLabel')}</FieldLabel> | ||
<FieldControl asChild> | ||
<Input | ||
// eslint-disable-next-line jsx-a11y/no-autofocus | ||
autoFocus | ||
error={!isInputValid} | ||
id="wishlist-name" | ||
onChange={handleInputValidation} | ||
onInvalid={handleInputValidation} | ||
required | ||
type="text" | ||
/> | ||
</FieldControl> | ||
<FieldMessage | ||
className="absolute inset-x-0 bottom-0 inline-flex w-full text-xs font-normal text-error" | ||
match="valueMissing" | ||
> | ||
{t('emptyName')} | ||
</FieldMessage> | ||
<FieldMessage | ||
className="absolute inset-x-0 bottom-0 inline-flex w-full text-xs font-normal text-error" | ||
match={(value) => { | ||
const isValid = value !== name; | ||
|
||
setInputValidation(isValid); | ||
|
||
return !isValid; | ||
}} | ||
> | ||
{t('differentName')} | ||
</FieldMessage> | ||
</Field> | ||
<Field name="entityId"> | ||
<FieldControl asChild> | ||
<Input id="entityId" type="hidden" value={entityId} /> | ||
</FieldControl> | ||
</Field> | ||
<div className="mt-3 flex flex-col lg:flex-row"> | ||
<FormSubmit asChild> | ||
<SubmitButton /> | ||
</FormSubmit> | ||
<DialogPrimitive.Cancel asChild> | ||
<Button className="mt-2 w-full border-0 lg:ms-2 lg:mt-0 lg:w-fit" variant="secondary"> | ||
{t('cancel')} | ||
</Button> | ||
</DialogPrimitive.Cancel> | ||
</div> | ||
</Form> | ||
); | ||
}; |
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
Oops, something went wrong.