All files / app/src/entities/task/ui/task-form-modal task-form-modal.tsx

77.77% Statements 91/117
75% Branches 6/8
50% Functions 5/10
77.77% Lines 91/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 1651x                                                         1x 3x   3x 3x   3x 3x   3x 3x   3x 3x 3x 3x   3x   3x                   3x 3x   3x 3x 3x 3x 3x 3x       3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                     1x 1x   3x   3x  
import { FC, useState } from 'react'
import { Controller } from 'react-hook-form'
import { Button, Datepicker, Input, Select, Textarea } from '@tmk/ui-kit'
 
import {
  getAppendTaskBody,
  getTaskModalText,
  MAX_TASK_DESCRIPTION_LENGTH,
  MAX_TASK_TITLE_LENGTH,
  Task,
  TaskFormWarehouseBlock,
  TaskFormMode,
  TaskModalConfig,
  TaskStatusEnum,
  taskValidationSchema,
  useTaskActions,
  useTaskById,
} from '@/entities/task'
import { OperationsFormLayout, useCloseOperationModal } from '@/entities/accounting-object'
 
import { Form, useTranslate } from '@/shared/lib'
import { normalizeSelectOptionsFromEnum } from '@/shared/helpers'
 
export interface TaskFormModalProps {
  taskModalConfig: TaskModalConfig
  openCancelModal?: (cb: () => void) => void
  onCloseModal: () => void
}
 
export const TaskFormModal: FC<TaskFormModalProps> = ({ taskModalConfig, onCloseModal, openCancelModal }) => {
  const { t } = useTranslate(['accounting-object'])
 
  const [isFirstEditRender, setIsFirstEditRender] = useState(false || !!taskModalConfig.taskId)
  const { canCloseWithoutConfirm } = useCloseOperationModal()
 
  const { taskId, formMode, isViewerManager } = taskModalConfig
  const { title, subtitle } = getTaskModalText(formMode)
 
  const isCreate = formMode === TaskFormMode.CREATE
  const isEdit = formMode === TaskFormMode.EDIT && isViewerManager
 
  const { data: task, isLoading: isLoadingTask } = useTaskById(taskId as string, {
    enabled: !!taskId,
    onSuccess: data => setIsFirstEditRender(!!data),
  })
 
  const { isCreatingTask, isEditingTask, createAsyncTask, editAsyncTask } = useTaskActions()
 
  const handleSubmit = async (data: Task) => {
    if (isCreate) {
      await createAsyncTask(data)
      onCloseModal()
    } else if (task) {
      await editAsyncTask({ taskId: task.id, task: data })
      onCloseModal()
    }
  }
 
  return (
    <Form<Task>
      // @ts-expect-error
      validationSchema={taskValidationSchema(t, isEdit)}
      formParams={{
        values: task,
        ...(isCreate && { defaultValues: getAppendTaskBody() }),
      }}
      onSubmit={data => {
        handleSubmit(data)
      }}
    >
      {({ control }) => (
        <OperationsFormLayout
          title={t(`${title}`)}
          subtitle={t(`${subtitle}`)}
          isLoading={isLoadingTask}
          customButtonActions={
            <div className='flex items-center justify-center gap-x-2.5 px-[50px] py-4'>
              <Button
                variant='outlined'
                color='secondary'
                className='w-[120px]'
                onClick={() => (canCloseWithoutConfirm ? onCloseModal() : openCancelModal?.(onCloseModal))}
              >
                <h2>{t('common:Cancel')}</h2>
              </Button>
              <Button type='submit' className='w-[120px]' loading={isCreatingTask || isEditingTask}>
                <h2>{t('common:Save')}</h2>
              </Button>
            </div>
          }
        >
          <div className='flex flex-col gap-y-2.5 w-full'>
            <Controller
              name='title'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <Input
                  {...field}
                  label={t('title') + ' *'}
                  name='title'
                  error={error}
                  maxLength={MAX_TASK_TITLE_LENGTH}
                  isLoading={isLoadingTask}
                />
              )}
            />
            <Controller
              name='description'
              render={({ field, fieldState: { error } }) => (
                <Textarea
                  {...field}
                  label={t('description') + ' *'}
                  error={error}
                  className='text-main-regular'
                  textareaClassName='text-main-regular min-h-[105px]'
                  maxLength={MAX_TASK_DESCRIPTION_LENGTH}
                />
              )}
            />
            <TaskFormWarehouseBlock
              task={task}
              isFirstEditRender={isFirstEditRender}
              setIsFirstEditRender={setIsFirstEditRender}
            />
            <Controller
              name='dueDate'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <Datepicker
                  inputProps={{
                    label: t('completedAt') + ' *',
                    error,
                    isLoading: isLoadingTask,
                  }}
                  className='col-span-1'
                  {...field}
                />
              )}
            />
            {isEdit && (
              <Controller
                name='status'
                control={control}
                render={({ field, fieldState: { error } }) => (
                  <Select
                    label={t('status') + ' *'}
                    options={normalizeSelectOptionsFromEnum(TaskStatusEnum, t)}
                    className='col-span-1'
                    t={t}
                    isLoading={isLoadingTask}
                    inputProps={{
                      error,
                    }}
                    {...field}
                  />
                )}
              />
            )}
          </div>
        </OperationsFormLayout>
      )}
    </Form>
  )
}