File size: 7,515 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* eslint-disable import/extensions */
import { t } from 'i18next';

import DefaultRadioGroup from '@/components/button/DefaultRadioGroup';
import SimpleInput from '@/components/input/SimpleInput';
import RichText from '@/components/text/RichText';
import {
  CheckboxButtonCustom,
  DayPickerCustom,
  MultiSelectDropdownCustom,
  SelectDropdownCustom,
} from '@/routes/data/CustomData/custom-fields/customComponents';

type TransformedField = {
  description: string;
  display: string;
  displaySub: string;
  fieldType: string;
  label: string;
  offset: number;
  text: {
    locale: string;
    value: string;
  }[];
  options: {
    locale: string;
    value: string;
  }[];
  required: boolean;
  size: number;
  _id: string;
};

type ListItem = {
  id: number;
  value: string;
  label?: string;
  locale?: string;
};

type AuditCustomFieldsProps = {
  fields: TransformedField[];
  currentLanguage: ListItem;
  // es necesaria una refactorización de los componentes
  // se hará un issue al respecto
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  setCurrentCustomFields: (fields: any) => void;
  onSave: () => void;
};

const AuditCustomFields: React.FC<AuditCustomFieldsProps> = ({
  fields,
  currentLanguage,
  setCurrentCustomFields,
  onSave,
}) => {
  const handlerInputChangeText = (id: string, value: string) => {
    setCurrentCustomFields((prevFields: TransformedField[]) => {
      return prevFields.map((field: TransformedField) =>
        field._id === id
          ? {
              ...field,
              text: field.text.map(item =>
                item.locale === currentLanguage.value
                  ? { locale: item.locale, value }
                  : item,
              ),
            }
          : field,
      );
    });
  };

  const handlerTextString = (
    text: { locale: string; value: string | string[] }[],
  ) => {
    return Array.isArray(text[0].value) ? text[0].value[0] : text[0].value;
  };

  const handlerTextArray = (
    text: {
      locale: string;
      value: string | string[];
    }[],
  ) => {
    return text
      .filter(item => item.locale === currentLanguage.value)
      .flatMap(item => (Array.isArray(item.value) ? item.value : [item.value]));
  };

  return (
    <div>
      <div className="flex justify-end mt-4">
        <button
          className="bg-green-600 text-white rounded-md px-4 py-2"
          onClick={onSave}
          type="button"
        >
          {t('btn.save')}
        </button>
      </div>
      {fields.map(customField => {
        const { _id, fieldType, size, offset, options, label, text } =
          customField;
        const validSize = Math.min(12, Math.max(0, size));
        const sizeStyle =
          validSize > 0
            ? Math.round((validSize / 12) * 100)
            : Math.round((1 / 12) * 100);

        return (
          <div
            className="bg-[#101827] shadow-md rounded-lg p-4 my-4"
            key={_id}
            style={{ width: '100%' }}
          >
            <span className="block font-medium leading-6 text-white-600">
              {label}
            </span>
            <span className="block font-medium leading-6 text-white-600">
              {t('offset')}: {offset}
            </span>

            <div className="mt-4" style={{ width: `calc(${sizeStyle}%)` }}>
              {(() => {
                switch (fieldType) {
                  case 'checkbox':
                    return (
                      <CheckboxButtonCustom
                        currentLanguage={currentLanguage.value}
                        id={_id}
                        options={options.map(option => option.value)}
                        setCurrentCustomFields={setCurrentCustomFields}
                        text={handlerTextArray(text)}
                      />
                    );

                  case 'space':
                    return (
                      <div className="bg-white w-full rounded-lg mt-1 h-28" />
                    );

                  case 'text':
                    return (
                      <RichText
                        label=""
                        onChange={(value: string) =>
                          handlerInputChangeText(_id, value)
                        }
                        placeholder=""
                        value={handlerTextString(text)}
                      />
                    );

                  case 'input':
                    return (
                      <SimpleInput
                        id={_id}
                        name={_id}
                        onChange={(value: string) =>
                          handlerInputChangeText(_id, value)
                        }
                        placeholder=""
                        type="text"
                        value={handlerTextString(text)}
                      />
                    );

                  case 'radio':
                    return (
                      <DefaultRadioGroup
                        name={_id}
                        onChange={(value: string) =>
                          handlerInputChangeText(_id, value)
                        }
                        options={options.map((option, index) => ({
                          id: index.toString(),
                          label: option.value,
                          value: option.value,
                        }))}
                        value={handlerTextString(text)}
                      />
                    );

                  case 'select':
                    return (
                      <SelectDropdownCustom
                        currentLanguage={currentLanguage.value}
                        id={_id}
                        items={options.map((option, index) => ({
                          id: index,
                          value: option.value,
                          label: option.value,
                        }))}
                        placeholder="Select"
                        setCurrentCustomFields={setCurrentCustomFields}
                        text={handlerTextString(text)}
                        title=""
                      />
                    );

                  case 'select-multiple':
                    return (
                      <MultiSelectDropdownCustom
                        currentLanguage={currentLanguage.value}
                        id={_id}
                        items={options.map((option, index) => ({
                          id: index,
                          value: option.value,
                          label: option.value,
                        }))}
                        placeholder="Select"
                        setCurrentCustomFields={setCurrentCustomFields}
                        text={handlerTextArray(text)}
                        title=""
                      />
                    );

                  case 'date':
                    return (
                      <DayPickerCustom
                        currentLanguage={currentLanguage.value}
                        id={_id}
                        label=""
                        setCurrentCustomFields={setCurrentCustomFields}
                        text={handlerTextString(text)}
                      />
                    );

                  default:
                    return null;
                }
              })()}
            </div>
          </div>
        );
      })}
    </div>
  );
};

export default AuditCustomFields;