|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { updateUserSubscribeNote } from '@/services/user/subscribe'; |
| 4 | +import { Button } from '@workspace/ui/components/button'; |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogContent, |
| 8 | + DialogDescription, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | + DialogTrigger, |
| 13 | +} from '@workspace/ui/components/dialog'; |
| 14 | +import { Textarea } from '@workspace/ui/components/textarea'; |
| 15 | +import { Icon } from '@workspace/ui/custom-components/icon'; |
| 16 | +import { LoaderCircle } from 'lucide-react'; |
| 17 | +import { useTranslations } from 'next-intl'; |
| 18 | +import { useCallback, useState, useTransition } from 'react'; |
| 19 | +import { toast } from 'sonner'; |
| 20 | + |
| 21 | +interface EditNoteProps { |
| 22 | + id: number; |
| 23 | + currentNote?: string; |
| 24 | + onSuccess?: () => void; |
| 25 | +} |
| 26 | + |
| 27 | +export default function EditNote({ id, currentNote = '', onSuccess }: Readonly<EditNoteProps>) { |
| 28 | + const t = useTranslations('dashboard'); |
| 29 | + const [open, setOpen] = useState<boolean>(false); |
| 30 | + const [note, setNote] = useState<string>(currentNote); |
| 31 | + const [loading, startTransition] = useTransition(); |
| 32 | + |
| 33 | + const handleSubmit = useCallback(async () => { |
| 34 | + startTransition(async () => { |
| 35 | + try { |
| 36 | + await updateUserSubscribeNote({ |
| 37 | + user_subscribe_id: id, |
| 38 | + note: note, |
| 39 | + }); |
| 40 | + toast.success(t('noteUpdateSuccess')); |
| 41 | + setOpen(false); |
| 42 | + if (onSuccess) { |
| 43 | + onSuccess(); |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + toast.error(t('noteUpdateFailed')); |
| 47 | + } |
| 48 | + }); |
| 49 | + }, [id, note, onSuccess, t]); |
| 50 | + |
| 51 | + return ( |
| 52 | + <Dialog open={open} onOpenChange={setOpen}> |
| 53 | + <DialogTrigger asChild> |
| 54 | + <Button size='sm' variant='outline'> |
| 55 | + <Icon icon='uil:edit' className='mr-1 size-4' /> |
| 56 | + {t('editNote')} |
| 57 | + </Button> |
| 58 | + </DialogTrigger> |
| 59 | + <DialogContent className='sm:max-w-[425px]'> |
| 60 | + <DialogHeader> |
| 61 | + <DialogTitle>{t('editNote')}</DialogTitle> |
| 62 | + <DialogDescription>{t('noteDescription')}</DialogDescription> |
| 63 | + </DialogHeader> |
| 64 | + <div className='grid gap-4 py-4'> |
| 65 | + <Textarea |
| 66 | + placeholder={t('notePlaceholder')} |
| 67 | + value={note} |
| 68 | + onChange={(e) => setNote(e.target.value)} |
| 69 | + maxLength={500} |
| 70 | + rows={4} |
| 71 | + className='resize-none' |
| 72 | + /> |
| 73 | + <p className='text-muted-foreground text-xs'> |
| 74 | + {note.length}/500 {t('characters')} |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + <DialogFooter> |
| 78 | + <Button variant='outline' onClick={() => setOpen(false)} disabled={loading}> |
| 79 | + {t('cancel')} |
| 80 | + </Button> |
| 81 | + <Button onClick={handleSubmit} disabled={loading}> |
| 82 | + {loading && <LoaderCircle className='mr-2 size-4 animate-spin' />} |
| 83 | + {t('save')} |
| 84 | + </Button> |
| 85 | + </DialogFooter> |
| 86 | + </DialogContent> |
| 87 | + </Dialog> |
| 88 | + ); |
| 89 | +} |
0 commit comments