-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Edit.jsx
executable file
·164 lines (151 loc) · 4.68 KB
/
Edit.jsx
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
/* eslint-disable react-hooks/exhaustive-deps */
// Evolutility-UI-React :: /views/one/Edit.js
// View to add or update one record at a time.
// https://github.com/evoluteur/evolutility-ui-react
// (c) 2023 Olivier Giulieri
// #region ---------------- Imports ----------------
import React, { useState, useCallback } from "react";
import PropTypes from "prop-types";
import modelPropType from "components/views/modelPropTypes";
import { useParams } from "react-router-dom";
import { toast } from "react-toastify";
import { i18n_actions, i18n_validation } from "i18n/i18n";
import { fieldId2Field, fieldTypes as ft } from "utils/dico";
import { validate, validateField } from "utils/validation";
import Button from "components/widgets/Button/Button";
import Field from "components/Field/Field";
import Panel from "components/widgets/Panel/Panel";
import Collection from "../shared/Collection/Collection";
import Timestamps from "../shared/Timestamps/Timestamps";
// #endregion
import "../shared/Form.scss";
const Edit = ({ entity, model, data, onFieldChange, onSave, onCancel }) => {
const [invalids, setInvalids] = useState(null);
const { id = 0 } = useParams();
const isNew = id === 0 || id === "0";
const clickSave = useCallback(() => {
const v = validate(model, data);
if (v.isValid) {
setInvalids(null);
onSave(data);
} else {
setInvalids(v.invalids);
toast.error(i18n_validation.incomplete + " " + v.messages.join(" "));
}
}, [entity, data, onSave]);
const fieldChange = useCallback(
(evt, obj) => {
const fid = evt.target ? evt.target.id : obj.name;
const f = model.fieldsH[fid];
let v;
if (f.type === ft.bool) {
v = evt.target.checked;
} else if (f.type === ft.lov) {
if (f.object) {
v = { id: evt.value, name: evt.label };
} else {
v = { id: evt.target.value };
}
} else {
v = evt.target.value;
}
if (invalids && invalids[fid]) {
const fValidation = validateField(f, v);
if (!fValidation) {
let newInvalids = { ...invalids };
delete newInvalids[fid];
if (Object.keys(newInvalids).length === 0) {
newInvalids = null;
}
setInvalids(newInvalids);
}
}
onFieldChange(fid, v);
},
[entity, invalids, onFieldChange]
);
const linkCancel = `../${entity}/${isNew ? "list" : "browse/" + id}`;
const fnField = (f) => {
if (f) {
const invalidMsg = invalids ? invalids[f.id] : null;
return (
<Field
key={f.id}
fieldDef={f}
value={data?.[f.id]}
data={data}
onChange={fieldChange}
entity={entity}
message={invalidMsg}
invalid={!!invalidMsg}
/>
);
}
return null;
};
const panelActionButtons = (
<div className="form-buttons noprint">
<Button type="default" label={i18n_actions.cancel} url={linkCancel} />
<Button
type="primary"
onClick={clickSave}
icon="save"
label={i18n_actions.save}
/>
{invalids && (
<span className="evo-fld-invalid">{i18n_validation.incomplete}</span>
)}
</div>
);
return (
<div className="evo-one-edit" role="form">
<div className="evol-pnls">
{model.groups.map((g, idx) => {
const groupFields = fieldId2Field(g.fields, model.fieldsH);
return (
<Panel
key={g.id || "g" + idx}
title={g.label || g.title}
header={g.header}
footer={g.footer}
width={g.width}
>
<div className="evol-fset">{groupFields?.map(fnField)}</div>
</Panel>
);
})}
{!isNew &&
model.collections &&
model.collections?.map((c) => {
const cData = data[c.id];
return c.hideIfEmpty && (!cData || cData.length === 0) ? null : (
<Panel
key={c.id || c.object}
title={c.title}
collapsible
header={c.header}
footer={c.footer}
>
<Collection collecModel={c} collecData={cData} />
</Panel>
);
})}
{panelActionButtons}
{!isNew && (
<Timestamps created={data.created_at} updated={data.updated_at} />
)}
</div>
</div>
);
};
export default Edit;
Edit.propTypes = {
entity: PropTypes.string.isRequired,
model: modelPropType.isRequired,
data: PropTypes.shape({
id: PropTypes.number,
}),
onFieldChange: PropTypes.func,
onSave: PropTypes.func,
// onCancel: PropTypes.func,
};