aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/Modals/Inputs/CreateThingModal.jsx
blob: 59263ff4d744cc004fe595cd9346243431dca9d9 (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
import {
  Box,
  Button,
  FormLabel,
  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 || 'numeric')
  const [state, setState] = useState(currentThing?.state || '')
  useEffect(() => {
    if (type === 'boolean') {
      if (state !== 'true' && state !== 'false') {
        setState('false')
      }
    } else if (type === 'number') {
      if (isNaN(state)) {
        setState(0)
      }
    }
  }, [type])
  const handleSave = () => {
    onSave({ name, type, id: currentThing?.id, state: state || null })
    onClose()
  }

  return (
    <Modal open={isOpen} onClose={onClose}>
      <ModalDialog>
        {/* <ModalClose /> */}
        <Typography variant='h4'>P;lease add info</Typography>
        <FormLabel>Name</FormLabel>

        <Textarea
          placeholder='Thing name'
          value={name}
          onChange={e => setName(e.target.value)}
          sx={{ minWidth: 300 }}
        />
        <FormLabel>Type</FormLabel>
        <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>

        {type === 'text' && (
          <>
            <FormLabel>Value</FormLabel>
            <Input
              placeholder='Thing value'
              value={state || ''}
              onChange={e => setState(e.target.value)}
              sx={{ minWidth: 300 }}
            />
          </>
        )}
        {type === 'number' && (
          <>
            <FormLabel>Value</FormLabel>
            <Input
              placeholder='Thing value'
              type='number'
              value={state || ''}
              onChange={e => {
                setState(e.target.value)
              }}
              sx={{ minWidth: 300 }}
            />
          </>
        )}
        {type === 'boolean' && (
          <>
            <FormLabel>Value</FormLabel>
            <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>
          </>
        )}

        <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