File size: 4,339 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
import { Bars2Icon } from '@heroicons/react/24/outline';
import { t } from 'i18next';
import React, { useEffect, useState } from 'react';

import PrimaryButton from '../../../components/button/PrimaryButton';
import SimpleInput from '../../../components/input/SimpleInput';
import DraggableList from '../../../components/table/DraggableTable';

type Section = {
  _id: string;
  name: string;
  field: string;
  icon: string;
};

type SectionListProps = {
  data: Section[];
  setSections: (data: Section[]) => void;
  isDisabled: boolean;
  onUpdateList: (data: Section[]) => void;
};

/**
 * Este componente renderea el contenido de EditCard
 */
const SectionList: React.FC<SectionListProps> = ({
  data,
  setSections,
  isDisabled,
  onUpdateList,
}) => {
  /**
   * Estado parcial de las filas
   * Al mover o modificar el contenido de una, se actualiza acá
   * Se debe hacer PUT al backend si se desea guardar los cambios
   *
   * También se debe agregar un atributo id único a cada fila si es que no se posee.
   */
  const [rows, setRows] = useState<
    { _id: string; name: string; field: string; icon: string }[]
  >([]);

  const handleInputChange = (
    id: string,
    field: keyof Section,
    value: string | boolean,
  ) => {
    setRows(prevRows =>
      prevRows.map(row => (row._id === id ? { ...row, [field]: value } : row)),
    );
  };

  const handleRemoveRow = (id: string) => {
    setRows(prevRows => prevRows.filter(row => row._id !== id));
    setSections(rows.filter((row: Section) => row._id !== id));
  };
  /**
   * Renderiza la fila del DnD
   *
   * Se debe manejar el disable de los Input en esta función.
   */

  const renderRow = (row: {
    id: string;
    name: string;
    field: string;
    icon: string;
  }) => (
    <div
      className={
        isDisabled
          ? 'grid grid-cols-1 md:grid-cols-4 place-items-center'
          : 'grid grid-cols-1 md:grid-cols-6 place-items-center'
      }
    >
      {!isDisabled ? (
        <div>
          <Bars2Icon className="size-4" />
        </div>
      ) : null}
      <div className="pr-2">
        <SimpleInput
          disabled={isDisabled}
          id={`name-${row.id}`}
          label={t('name')}
          name="name"
          onChange={value => handleInputChange(row.id, 'name', value)}
          placeholder={t('name')}
          type="text"
          value={row.name}
        />
      </div>
      <div className="pr-2">
        <SimpleInput
          disabled={isDisabled}
          id={`field-${row.id}`}
          label={t('field')}
          name="field"
          onChange={value => handleInputChange(row.id, 'field', value)}
          placeholder={t('field')}
          type="text"
          value={row.field}
        />
      </div>
      <div className="pr-2">
        <SimpleInput
          disabled={isDisabled}
          id={`icon-${row.id}`}
          label={t('icon')}
          name="icon"
          onChange={value => handleInputChange(row.id, 'icon', value)}
          placeholder={t('icon')}
          type="text"
          value={row.icon}
        />
      </div>
      {row.icon ? (
        row.icon.startsWith('fa-') ? (
          <i className={`fa ${row.icon}`} />
        ) : (
          <i className="material-icons">{row.icon}</i>
        )
      ) : null}
      {!isDisabled ? (
        <div>
          <PrimaryButton color="red" onClick={() => handleRemoveRow(row.id)}>
            X
          </PrimaryButton>
        </div>
      ) : null}
    </div>
  );

  /**
   * Actualiza las rows al modificar data
   * (agregar una section en el componente padre)
   */
  useEffect(() => {
    if (rows.map(row => row._id).length !== data.length) {
      setRows(
        data.map((section, index) => ({ ...section, _id: index.toString() })),
      );
    }
  }, [data, rows]);

  /**
   * Le "avisa" al componente padre
   * que cambió la lista.
   */
  useEffect(() => {
    onUpdateList(rows);
  }, [onUpdateList, rows]);

  /**
   * Se debe hacer map de las rows para agregarle ID.
   * Puede ser cualquier string ÚNICO dentro del objeto.
   */
  return (
    <div>
      <DraggableList
        isDisabled={isDisabled}
        items={rows.map(row => ({ ...row, id: row._id }))}
        onOrderChange={setRows}
        renderItem={renderRow}
      />
    </div>
  );
};

export default SectionList;