Skip to content

Commit 9a12212

Browse files
committed
refactor(ResourceSchema): reorganize imports and streamline state updates
1 parent f3cadbe commit 9a12212

File tree

1 file changed

+36
-20
lines changed
  • app/[locale]/dashboard/[entityType]/[entitySlug]/dataset/[id]/edit/resources/components

1 file changed

+36
-20
lines changed

app/[locale]/dashboard/[entityType]/[entitySlug]/dataset/[id]/edit/resources/components/ResourceSchema.tsx

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
DataTable,
3-
Select,
4-
TextField
5-
} from 'opub-ui';
61
import React from 'react';
7-
2+
import { DataTable, Select, TextField } from 'opub-ui';
83

94
const DescriptionCell = ({
105
value,
@@ -17,7 +12,7 @@ const DescriptionCell = ({
1712
}) => {
1813
const [description, setDescription] = React.useState(value || '');
1914

20-
const handleChange = (e:any) => {
15+
const handleChange = (e: any) => {
2116
setDescription(e?.target?.value);
2217
handleFieldChange('description', e?.target?.value, rowIndex);
2318
};
@@ -34,8 +29,12 @@ const DescriptionCell = ({
3429
);
3530
};
3631

37-
export const ResourceSchema = ({ setSchema, data }: any) => {
38-
32+
export const ResourceSchema = ({
33+
setSchema,
34+
data,
35+
mutate,
36+
resourceId,
37+
}: any) => {
3938
const [updatedData, setUpdatedData] = React.useState<any>(data);
4039

4140
React.useEffect(() => {
@@ -49,17 +48,32 @@ export const ResourceSchema = ({ setSchema, data }: any) => {
4948
newValue: string,
5049
rowIndex: any
5150
) => {
52-
setUpdatedData((prev: any) => {
53-
const newData = [...prev];
54-
newData[rowIndex] = {
55-
...newData[rowIndex],
56-
[field]: newValue,
57-
};
58-
return newData;
59-
});
51+
const newData = [...updatedData];
52+
newData[rowIndex] = {
53+
...newData[rowIndex],
54+
[field]: newValue,
55+
};
56+
57+
setUpdatedData(newData);
58+
setSchema(newData);
59+
handleSave(newData);
60+
};
61+
62+
const handleSave = (newdata: any) => {
63+
const isSchemaChanged = JSON.stringify(newdata) !== JSON.stringify(data);
64+
if (isSchemaChanged) {
65+
mutate({
66+
schemaUpdateInput: {
67+
resource: resourceId,
68+
updates: newdata?.map((item: any) => {
69+
const { fieldName, ...rest } = item;
70+
return rest;
71+
}),
72+
},
73+
});
74+
}
6075
};
6176

62-
setSchema(updatedData);
6377
const options = [
6478
{
6579
label: 'Integer',
@@ -127,11 +141,13 @@ export const ResourceSchema = ({ setSchema, data }: any) => {
127141
{data && data.length > 0 ? (
128142
<DataTable
129143
columns={generateColumnData()}
130-
rows={generateTableData(data)}
144+
rows={generateTableData(updatedData)}
131145
hideFooter={false}
132146
hideSelection
133147
/>
134-
):<div className="mt-8 flex justify-center">Click on Reset Fields</div>}
148+
) : (
149+
<div className="mt-8 flex justify-center">Click on Reset Fields</div>
150+
)}
135151
</div>
136152
</>
137153
);

0 commit comments

Comments
 (0)