evaluations-edit-dialog.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { useForm, useFieldArray } from 'react-hook-form'
  2. import { z } from 'zod'
  3. import { zodResolver } from '@hookform/resolvers/zod'
  4. import { AlertTriangle } from 'lucide-react'
  5. import { toast } from 'sonner'
  6. import {
  7. Dialog,
  8. DialogContent,
  9. DialogHeader,
  10. DialogTitle,
  11. } from '@/components/ui/dialog'
  12. import {
  13. Form,
  14. FormControl,
  15. FormField,
  16. FormItem,
  17. FormLabel,
  18. FormMessage,
  19. } from '@/components/ui/form'
  20. import { Table, TableBody, TableCell, TableRow } from '@/components/ui/table'
  21. import { Input } from '@/components/ui/input'
  22. import { Textarea } from '@/components/ui/textarea'
  23. import { Button } from '@/components/ui/button'
  24. import VideoPlayer from '@/components/video-player'
  25. import { useEvaluations } from './evaluations-provider'
  26. import { useEvaluation } from '@/hooks/evaluations/useEvaluation'
  27. import { useUpdateEvaluationDetails } from '@/hooks/evaluations/useUpdateEvaluationDetails'
  28. import { type Evaluation } from '@/types/evaluations.types'
  29. const schema = z.object({
  30. pros_comment: z.string(),
  31. cons_comment: z.string(),
  32. suggestion: z.string(),
  33. summary: z.string(),
  34. score: z.number({ error: 'نمره ارزیابی الزامی است' }),
  35. items: z.array(
  36. z.object({
  37. item_id: z.number(),
  38. name: z.string(),
  39. score: z.number({ error: 'نمره آیتم الزامی است' }),
  40. })
  41. ),
  42. })
  43. type FormValues = z.infer<typeof schema>
  44. function getDefaultValues(evaluation: Evaluation): FormValues {
  45. return {
  46. pros_comment: evaluation.pros_comment ?? '',
  47. cons_comment: evaluation.cons_comment ?? '',
  48. suggestion: evaluation.suggestion ?? '',
  49. summary: evaluation.summary ?? '',
  50. score: evaluation.score ? Number(evaluation.score) : 0,
  51. items: evaluation.items.map((item) => ({
  52. item_id: item.item.id,
  53. name: item.item.name,
  54. score: item.score,
  55. })),
  56. }
  57. }
  58. export function EvaluationsEditDialog() {
  59. const { open, setOpen, currentRow } = useEvaluations()
  60. const opened = open === 'edit'
  61. const { data: evaluation, isLoading } = useEvaluation(
  62. opened ? (currentRow?.id as number) : ''
  63. )
  64. const mediaSrc = evaluation?.media?.[0]?.url
  65. const handleClose = () => setOpen(null)
  66. return (
  67. <Dialog open={opened} modal onOpenChange={handleClose}>
  68. <DialogContent className='max-h-[90vh] max-w-5xl overflow-y-auto'>
  69. <DialogHeader>
  70. <DialogTitle>ویرایش ارزیابی</DialogTitle>
  71. </DialogHeader>
  72. {isLoading || !evaluation ? (
  73. <div className='py-10 text-center'>در حال دریافت اطلاعات...</div>
  74. ) : (
  75. <div className='flex flex-col gap-4'>
  76. {mediaSrc && <VideoPlayer src={mediaSrc} />}
  77. {evaluation.items.length === 0 ? (
  78. <div className='flex items-center justify-center gap-3 rounded-xl border bg-muted px-2 py-4 text-center text-sm font-bold text-primary'>
  79. <AlertTriangle />
  80. <span>ارزیابی برای این کاربر یافت نشد.</span>
  81. </div>
  82. ) : (
  83. <EvaluationEditForm
  84. key={evaluation.id}
  85. evaluation={evaluation}
  86. onClose={handleClose}
  87. />
  88. )}
  89. </div>
  90. )}
  91. </DialogContent>
  92. </Dialog>
  93. )
  94. }
  95. type EvaluationEditFormProps = {
  96. evaluation: Evaluation
  97. onClose: () => void
  98. }
  99. function EvaluationEditForm({ evaluation, onClose }: EvaluationEditFormProps) {
  100. const { mutate, isPending } = useUpdateEvaluationDetails()
  101. const form = useForm<FormValues>({
  102. resolver: zodResolver(schema),
  103. defaultValues: getDefaultValues(evaluation),
  104. })
  105. const { fields } = useFieldArray({
  106. control: form.control,
  107. name: 'items',
  108. })
  109. const onSubmit = (values: FormValues) => {
  110. mutate(
  111. {
  112. id: evaluation.id,
  113. payload: {
  114. pros_comment: values.pros_comment,
  115. cons_comment: values.cons_comment,
  116. suggestion: values.suggestion,
  117. summary: values.summary,
  118. score: values.score,
  119. items: values.items.map((item) => ({
  120. item_id: item.item_id,
  121. score: item.score,
  122. })),
  123. },
  124. },
  125. {
  126. onSuccess: () => {
  127. toast.success('ویرایش ارزیابی با موفقیت انجام شد.')
  128. onClose()
  129. },
  130. onError: () => {
  131. toast.error('خطا در ویرایش ارزیابی!')
  132. },
  133. }
  134. )
  135. }
  136. return (
  137. <Form {...form}>
  138. <form
  139. onSubmit={form.handleSubmit(onSubmit)}
  140. className='flex flex-col gap-4'
  141. >
  142. <h2>نتیجه ارزیابی</h2>
  143. <div className='overflow-hidden rounded-2xl border border-slate-200'>
  144. <Table>
  145. <TableBody>
  146. {fields.map((fieldItem, index) => (
  147. <TableRow
  148. key={fieldItem.id}
  149. className='border-b border-slate-100 last:border-0 hover:bg-transparent'
  150. >
  151. <TableCell className='p-3 text-right text-sm leading-5'>
  152. {fieldItem.name}
  153. </TableCell>
  154. <TableCell className='p-3'>
  155. <FormField
  156. control={form.control}
  157. name={`items.${index}.score`}
  158. render={({ field }) => (
  159. <FormItem>
  160. <div className='flex items-center justify-end gap-1'>
  161. <span className='text-foreground'>100 /</span>
  162. <FormControl>
  163. <Input
  164. type='number'
  165. className='h-8 w-20 text-center text-base'
  166. value={field.value ?? ''}
  167. onChange={(e) => {
  168. const value = e.target.value
  169. field.onChange(
  170. value === '' ? 0 : Number(value)
  171. )
  172. }}
  173. />
  174. </FormControl>
  175. </div>
  176. <FormMessage />
  177. </FormItem>
  178. )}
  179. />
  180. </TableCell>
  181. </TableRow>
  182. ))}
  183. <TableRow className='border-b border-slate-100 last:border-0 hover:bg-transparent'>
  184. <TableCell className='p-3 text-right text-sm leading-5 font-semibold'>
  185. مجموع عملکرد
  186. </TableCell>
  187. <TableCell className='p-3'>
  188. <FormField
  189. control={form.control}
  190. name='score'
  191. render={({ field }) => (
  192. <FormItem>
  193. <div className='flex items-center justify-end gap-1'>
  194. <span className='text-base font-semibold'>100 /</span>
  195. <FormControl>
  196. <Input
  197. type='number'
  198. className='h-8 w-20 text-center text-base font-semibold'
  199. value={field.value ?? ''}
  200. onChange={(e) => {
  201. const value = e.target.value
  202. field.onChange(
  203. value === '' ? 0 : Number(value)
  204. )
  205. }}
  206. />
  207. </FormControl>
  208. </div>
  209. <FormMessage />
  210. </FormItem>
  211. )}
  212. />
  213. </TableCell>
  214. </TableRow>
  215. </TableBody>
  216. </Table>
  217. </div>
  218. <FormField
  219. control={form.control}
  220. name='pros_comment'
  221. render={({ field }) => (
  222. <FormItem className='flex flex-col gap-2'>
  223. <FormLabel className='leading-6'>نقاط قوت:</FormLabel>
  224. <FormControl>
  225. <Textarea {...field} rows={4} />
  226. </FormControl>
  227. <FormMessage />
  228. </FormItem>
  229. )}
  230. />
  231. <FormField
  232. control={form.control}
  233. name='cons_comment'
  234. render={({ field }) => (
  235. <FormItem className='flex flex-col gap-2'>
  236. <FormLabel className='leading-6'>نقاط ضعف:</FormLabel>
  237. <FormControl>
  238. <Textarea {...field} rows={4} />
  239. </FormControl>
  240. <FormMessage />
  241. </FormItem>
  242. )}
  243. />
  244. <FormField
  245. control={form.control}
  246. name='suggestion'
  247. render={({ field }) => (
  248. <FormItem className='flex flex-col gap-2'>
  249. <FormLabel className='leading-6'>تمرین پیشنهادی:</FormLabel>
  250. <FormControl>
  251. <Textarea {...field} rows={4} />
  252. </FormControl>
  253. <FormMessage />
  254. </FormItem>
  255. )}
  256. />
  257. <FormField
  258. control={form.control}
  259. name='summary'
  260. render={({ field }) => (
  261. <FormItem className='flex flex-col gap-2'>
  262. <FormLabel className='leading-6'>نتیجه:</FormLabel>
  263. <FormControl>
  264. <Textarea {...field} rows={4} />
  265. </FormControl>
  266. <FormMessage />
  267. </FormItem>
  268. )}
  269. />
  270. <div className='flex justify-end gap-3'>
  271. <Button type='button' variant='outline' onClick={onClose}>
  272. انصراف
  273. </Button>
  274. <Button type='submit' disabled={isPending}>
  275. {isPending ? 'در حال ارسال...' : 'ذخیره تغییرات'}
  276. </Button>
  277. </div>
  278. </form>
  279. </Form>
  280. )
  281. }