aboutsummaryrefslogtreecommitdiff
path: root/modern/src/settings/ComputedAttributePage.js
blob: 99bbfadbf02ad2487a1be9ab6993dc4cea0f608e (plain)
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
import React, { useState } from 'react';
import {
  Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControl, InputLabel, MenuItem, Select, TextField,
} from '@material-ui/core';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import EditItemView from '../EditItemView';
import { useTranslation } from '../LocalizationProvider';
import usePositionAttributes from '../attributes/usePositionAttributes';

const useStyles = makeStyles(() => ({
  details: {
    flexDirection: 'column',
  },
}));

const ComputedAttributePage = () => {
  const classes = useStyles();
  const t = useTranslation();

  const positionAttributes = usePositionAttributes(t);

  const [item, setItem] = useState();
  const [key, setKey] = useState();

  const options = Object.entries(positionAttributes).map(([key, value]) => ({
    key,
    name: value.name,
    type: value.type,
  }));

  const handleChange = (event) => {
    const newValue = event.target.value;
    setKey(newValue);
    const positionAttribute = positionAttributes[newValue];
    if (positionAttribute && positionAttribute.type) {
      setItem({ ...item, attribute: newValue, type: positionAttribute.type });
    } else {
      setItem({ ...item, attribute: newValue });
    }
  };

  return (
    <EditItemView endpoint="attributes/computed" item={item} setItem={setItem}>
      {item
        && (
        <Accordion defaultExpanded>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography variant="subtitle1">
              {t('sharedRequired')}
            </Typography>
          </AccordionSummary>
          <AccordionDetails className={classes.details}>
            <TextField
              margin="normal"
              value={item.description || ''}
              onChange={(event) => setItem({ ...item, description: event.target.value })}
              label={t('sharedDescription')}
              variant="filled"
            />
            <FormControl variant="filled" margin="normal" fullWidth>
              <InputLabel>{t('sharedAttribute')}</InputLabel>
              <Select
                value={item.attribute || ''}
                onChange={handleChange}
              >
                {options.map((option) => (
                  <MenuItem key={option.key} value={option.key}>{option.name}</MenuItem>
                ))}
              </Select>
            </FormControl>
            <TextField
              margin="normal"
              value={item.expression || ''}
              onChange={(event) => setItem({ ...item, expression: event.target.value })}
              label={t('sharedExpression')}
              multiline
              rows={4}
              variant="filled"
            />
            <FormControl
              variant="filled"
              margin="normal"
              fullWidth
              disabled={key in positionAttributes}
            >
              <InputLabel>{t('sharedType')}</InputLabel>
              <Select
                value={item.type || ''}
                onChange={(event) => setItem({ ...item, type: event.target.value })}
              >
                <MenuItem value="string">{t('sharedTypeString')}</MenuItem>
                <MenuItem value="number">{t('sharedTypeNumber')}</MenuItem>
                <MenuItem value="boolean">{t('sharedTypeBoolean')}</MenuItem>
              </Select>
            </FormControl>
          </AccordionDetails>
        </Accordion>
        )}
    </EditItemView>
  );
};

export default ComputedAttributePage;