aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/Modals/EditHistoryModal.jsx
blob: 1baab49bf22f4a76e7b66e739c257808940e8b89 (plain) (blame)
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
import {
  Box,
  Button,
  FormLabel,
  Input,
  Modal,
  ModalDialog,
  Typography,
} from '@mui/joy'
import moment from 'moment'
import { useEffect, useState } from 'react'
import ConfirmationModal from './Inputs/ConfirmationModal'

function EditHistoryModal({ config, historyRecord }) {
  useEffect(() => {
    setCompletedDate(
      moment(historyRecord.completedAt).format('YYYY-MM-DDTHH:mm'),
    )
    setDueDate(moment(historyRecord.dueDate).format('YYYY-MM-DDTHH:mm'))
    setNotes(historyRecord.notes)
  }, [historyRecord])

  const [completedDate, setCompletedDate] = useState(
    moment(historyRecord.completedDate).format('YYYY-MM-DDTHH:mm'),
  )
  const [dueDate, setDueDate] = useState(
    moment(historyRecord.dueDate).format('YYYY-MM-DDTHH:mm'),
  )
  const [notes, setNotes] = useState(historyRecord.notes)
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false)
  return (
    <Modal open={config?.isOpen} onClose={config?.onClose}>
      <ModalDialog>
        <Typography level='h4' mb={1}>
          Edit History
        </Typography>
        <FormLabel>Due Date</FormLabel>
        <Input
          type='datetime-local'
          value={dueDate}
          onChange={e => {
            setDueDate(e.target.value)
          }}
        />
        <FormLabel>Completed Date</FormLabel>
        <Input
          type='datetime-local'
          value={completedDate}
          onChange={e => {
            setCompletedDate(e.target.value)
          }}
        />
        <FormLabel>Note</FormLabel>
        <Input
          fullWidth
          multiline
          label='Additional Notes'
          placeholder='Additional Notes'
          value={notes}
          onChange={e => {
            if (e.target.value.trim() === '') {
              setNotes(null)
              return
            }
            setNotes(e.target.value)
          }}
          size='md'
          sx={{
            mb: 1,
          }}
        />

        {/* 3 button save , cancel and delete */}
        <Box display={'flex'} justifyContent={'space-around'} mt={1}>
          <Button
            onClick={() =>
              config.onSave({
                id: historyRecord.id,
                completedAt: moment(completedDate).toISOString(),
                dueDate: moment(dueDate).toISOString(),
                notes,
              })
            }
            fullWidth
            sx={{ mr: 1 }}
          >
            Save
          </Button>
          <Button onClick={config.onClose} variant='outlined'>
            Cancel
          </Button>
          <Button
            onClick={() => {
              setIsDeleteModalOpen(true)
            }}
            variant='outlined'
            color='danger'
          >
            Delete
          </Button>
        </Box>
        <ConfirmationModal
          config={{
            isOpen: isDeleteModalOpen,
            onClose: isConfirm => {
              if (isConfirm) {
                config.onDelete(historyRecord.id)
              }
              setIsDeleteModalOpen(false)
            },
            title: 'Delete History',
            message: 'Are you sure you want to delete this history?',
            confirmText: 'Delete',
            cancelText: 'Cancel',
          }}
        />
      </ModalDialog>
    </Modal>
  )
}
export default EditHistoryModal