aboutsummaryrefslogtreecommitdiff
path: root/modern/src/attributes/EditAttributesView.js
blob: e8ab83e18696d0ec30af74ef1a1f43bd8d1d24af (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
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
import React, { useState } from 'react';

import {
  Button, Checkbox, FilledInput, FormControl, FormControlLabel, Grid, IconButton, InputAdornment, InputLabel, makeStyles,
} from '@material-ui/core';
import CloseIcon from '@material-ui/icons/Close';
import AddIcon from '@material-ui/icons/Add';
import AddAttributeDialog from './AddAttributeDialog';
import { useTranslation } from '../LocalizationProvider';

const useStyles = makeStyles((theme) => ({
  addButton: {
    marginTop: theme.spacing(2),
    marginBottom: theme.spacing(1),
  },
  removeButton: {
    marginRight: theme.spacing(1.5),
  },
}));

const EditAttributesView = ({ attributes, setAttributes, definitions }) => {
  const classes = useStyles();
  const t = useTranslation();

  const [addDialogShown, setAddDialogShown] = useState(false);

  const updateAttribute = (key, value) => {
    const updatedAttributes = { ...attributes };
    updatedAttributes[key] = value;
    setAttributes(updatedAttributes);
  };

  const deleteAttribute = (key) => {
    const updatedAttributes = { ...attributes };
    delete updatedAttributes[key];
    setAttributes(updatedAttributes);
  };

  const getAttributeName = (key) => {
    const definition = definitions[key];
    return definition ? definition.name : key;
  };

  const getAttributeType = (value) => {
    if (typeof value === 'number') {
      return 'number';
    } if (typeof value === 'boolean') {
      return 'boolean';
    }
    return 'string';
  };

  const convertToList = (attributes) => {
    const booleanList = [];
    const otherList = [];
    Object.keys(attributes || []).forEach((key) => {
      const value = attributes[key];
      const type = getAttributeType(value);
      if (type === 'boolean') {
        booleanList.push({ key, value, type });
      } else {
        otherList.push({ key, value, type });
      }
    });
    return otherList.concat(booleanList);
  };

  const handleAddResult = (definition) => {
    setAddDialogShown(false);
    if (definition) {
      switch (definition.type) {
        case 'number':
          updateAttribute(definition.key, 0);
          break;
        case 'boolean':
          updateAttribute(definition.key, false);
          break;
        default:
          updateAttribute(definition.key, '');
          break;
      }
    }
  };

  return (
    <>
      {convertToList(attributes).map(({ key, value, type }) => {
        if (type === 'boolean') {
          return (
            <Grid container direction="row" justify="space-between">
              <FormControlLabel
                control={(
                  <Checkbox
                    checked={value}
                    onChange={(e) => updateAttribute(key, e.target.checked)}
                  />
                )}
                label={getAttributeName(key)}
              />
              <IconButton className={classes.removeButton} onClick={() => deleteAttribute(key)}>
                <CloseIcon />
              </IconButton>
            </Grid>
          );
        }
        return (
          <FormControl variant="filled" margin="normal" key={key}>
            <InputLabel>{getAttributeName(key)}</InputLabel>
            <FilledInput
              type={type === 'number' ? 'number' : 'text'}
              value={value || ''}
              onChange={(e) => updateAttribute(key, e.target.value)}
              endAdornment={(
                <InputAdornment position="end">
                  <IconButton onClick={() => deleteAttribute(key)}>
                    <CloseIcon />
                  </IconButton>
                </InputAdornment>
                )}
            />
          </FormControl>
        );
      })}
      <Button
        size="large"
        variant="outlined"
        color="primary"
        onClick={() => setAddDialogShown(true)}
        startIcon={<AddIcon />}
        className={classes.addButton}
      >
        {t('sharedAdd')}
      </Button>
      <AddAttributeDialog
        open={addDialogShown}
        onResult={handleAddResult}
        definitions={definitions}
      />
    </>
  );
};

export default EditAttributesView;