aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/Modals/Inputs/CreateThingModal.jsx
blob: 96b79542a8c8d4c295291d2a81c96d6f0139cc6d (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
  Box,
  Button,
  FormControl,
  FormHelperText,
  Input,
  Modal,
  ModalDialog,
  Option,
  Select,
  Textarea,
  Typography,
} from '@mui/joy'
import { useEffect, useState } from 'react'

function CreateThingModal({ isOpen, onClose, onSave, currentThing }) {
  const [name, setName] = useState(currentThing?.name || '')
  const [type, setType] = useState(currentThing?.type || 'number')
  const [state, setState] = useState(currentThing?.state || '')
  const [errors, setErrors] = useState({})
  useEffect(() => {
    if (type === 'boolean') {
      if (state !== 'true' && state !== 'false') {
        setState('false')
      }
    } else if (type === 'number') {
      if (isNaN(state)) {
        setState(0)
      }
    }
  }, [type])

  const isValid = () => {
    const newErrors = {}
    if (!name || name.trim() === '') {
      newErrors.name = 'Name is required'
    }

    if (type === 'number' && isNaN(state)) {
      newErrors.state = 'State must be a number'
    }
    if (type === 'boolean' && !['true', 'false'].includes(state)) {
      newErrors.state = 'State must be true or false'
    }
    if ((type === 'text' && !state) || state.trim() === '') {
      newErrors.state = 'State is required'
    }

    setErrors(newErrors)
    return Object.keys(newErrors).length === 0
  }

  const handleSave = () => {
    if (!isValid()) {
      return
    }
    onSave({ name, type, id: currentThing?.id, state: state || null })
    onClose()
  }

  return (
    <Modal open={isOpen} onClose={onClose}>
      <ModalDialog>
        {/* <ModalClose /> */}
        <Typography level='h4'>
          {currentThing?.id ? 'Edit' : 'Create'} Thing
        </Typography>
        <FormControl>
          <Typography>Name</Typography>
          <Textarea
            placeholder='Thing name'
            value={name}
            onChange={e => setName(e.target.value)}
            sx={{ minWidth: 300 }}
          />
          <FormHelperText color='danger'>{errors.name}</FormHelperText>
        </FormControl>
        <FormControl>
          <Typography>Type</Typography>
          <Select value={type} sx={{ minWidth: 300 }}>
            {['text', 'number', 'boolean'].map(type => (
              <Option value={type} key={type} onClick={() => setType(type)}>
                {type.charAt(0).toUpperCase() + type.slice(1)}
              </Option>
            ))}
          </Select>

          <FormHelperText color='danger'>{errors.type}</FormHelperText>
        </FormControl>
        {type === 'text' && (
          <FormControl>
            <Typography>Value</Typography>
            <Input
              placeholder='Thing value'
              value={state || ''}
              onChange={e => setState(e.target.value)}
              sx={{ minWidth: 300 }}
            />
            <FormHelperText color='danger'>{errors.state}</FormHelperText>
          </FormControl>
        )}
        {type === 'number' && (
          <FormControl>
            <Typography>Value</Typography>
            <Input
              placeholder='Thing value'
              type='number'
              value={state || ''}
              onChange={e => {
                setState(e.target.value)
              }}
              sx={{ minWidth: 300 }}
            />
          </FormControl>
        )}
        {type === 'boolean' && (
          <FormControl>
            <Typography>Value</Typography>
            <Select sx={{ minWidth: 300 }} value={state}>
              {['true', 'false'].map(value => (
                <Option
                  value={value}
                  key={value}
                  onClick={() => setState(value)}
                >
                  {value.charAt(0).toUpperCase() + value.slice(1)}
                </Option>
              ))}
            </Select>
          </FormControl>
        )}

        <Box display={'flex'} justifyContent={'space-around'} mt={1}>
          <Button onClick={handleSave} fullWidth sx={{ mr: 1 }}>
            {currentThing?.id ? 'Update' : 'Create'}
          </Button>
          <Button onClick={onClose} variant='outlined'>
            {currentThing?.id ? 'Cancel' : 'Close'}
          </Button>
        </Box>
      </ModalDialog>
    </Modal>
  )
}
export default CreateThingModal