Skip to content

Commit 011af9f

Browse files
committed
add editable fields with mutations
1 parent 19d74f5 commit 011af9f

File tree

2 files changed

+159
-14
lines changed

2 files changed

+159
-14
lines changed

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditResource.tsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
import { graphql } from '@/gql';
22
import {
33
CreateFileResourceInput,
4+
SchemaUpdateInput,
45
UpdateFileResourceInput,
56
} from '@/gql/generated/graphql';
6-
import { useMutation, useQuery } from '@tanstack/react-query';
7-
import { parseAsString, useQueryState } from 'next-usequerystate';
8-
import { useParams, useRouter } from 'next/navigation';
7+
import { IconTrash } from '@tabler/icons-react';
8+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
9+
import {
10+
parseAsBoolean,
11+
parseAsString,
12+
useQueryState,
13+
} from 'next-usequerystate';
914
import {
1015
Button,
16+
ButtonGroup,
17+
Checkbox,
1118
Combobox,
19+
DataTable,
1220
Dialog,
1321
Divider,
1422
DropZone,
1523
Icon,
24+
IconButton,
1625
Select,
1726
Text,
1827
TextField,
19-
toast
28+
toast,
2029
} from 'opub-ui';
21-
import React from 'react';
2230

23-
import { Icons } from '@/components/icons';
2431
import { GraphQL } from '@/lib/api';
32+
import { Icons } from '@/components/icons';
2533
import { createResourceFilesDoc } from './ResourceDropzone';
26-
import { ResourceSchema } from './ResourceSchema';
34+
import { ResourceSchema, updateSchema } from './ResourceSchema';
35+
import { useParams } from 'next/navigation';
36+
import React from 'react';
2737

2838
interface TListItem {
2939
label: string;
@@ -50,6 +60,7 @@ export const EditResource = ({ reload, data }: any) => {
5060
`);
5161

5262
const [resourceId, setResourceId] = useQueryState<any>('id', parseAsString);
63+
const [schema, setSchema] = React.useState([]);
5364

5465
const { mutate, isLoading } = useMutation(
5566
(data: { fileResourceInput: UpdateFileResourceInput }) =>
@@ -92,6 +103,18 @@ export const EditResource = ({ reload, data }: any) => {
92103
}
93104
`);
94105

106+
const { mutate: modify } = useMutation(
107+
(data: { input: SchemaUpdateInput }) => GraphQL(updateSchema, data),
108+
{
109+
onSuccess: (data) => {
110+
console.log(data);
111+
},
112+
onError: (err: any) => {
113+
console.log('Error ::: ', err);
114+
},
115+
}
116+
);
117+
95118
const { mutate: transform } = useMutation(
96119
(data: { fileResourceInput: CreateFileResourceInput }) =>
97120
GraphQL(createResourceFilesDoc, data),
@@ -112,8 +135,6 @@ export const EditResource = ({ reload, data }: any) => {
112135
}
113136
);
114137

115-
const router = useRouter();
116-
117138
const ResourceList: TListItem[] =
118139
data.map((item: any) => ({
119140
label: item.name,
@@ -215,6 +236,14 @@ export const EditResource = ({ reload, data }: any) => {
215236
file: resourceFile,
216237
},
217238
});
239+
if (schema.length > 0) {
240+
modify({
241+
input: {
242+
resource: resourceId,
243+
updates: schema,
244+
},
245+
});
246+
}
218247
};
219248

220249
return (
@@ -384,6 +413,7 @@ export const EditResource = ({ reload, data }: any) => {
384413
</div>*/}
385414
{resourceId && payload && Object.keys(payload).length > 0 ? (
386415
<ResourceSchema
416+
setSchema={setSchema}
387417
resourceId={resourceId}
388418
isPending={isPending}
389419
refetch={refetch}

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/ResourceSchema.tsx

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,98 @@
11
import React from 'react';
22
import { useParams } from 'next/navigation';
33
import { graphql } from '@/gql';
4+
import { SchemaUpdateInput } from '@/gql/generated/graphql';
45
import { useMutation, useQuery } from '@tanstack/react-query';
5-
import { Button, DataTable, Icon, IconButton, Spinner, Text } from 'opub-ui';
6+
import {
7+
Button,
8+
DataTable,
9+
Icon,
10+
Select,
11+
Spinner,
12+
Text,
13+
TextField,
14+
} from 'opub-ui';
615

716
import { GraphQL } from '@/lib/api';
817
import { Icons } from '@/components/icons';
918

19+
export const updateSchema: any = graphql(`
20+
mutation updateSchema($input: SchemaUpdateInput!) {
21+
updateSchema(input: $input) {
22+
__typename
23+
... on TypeResource {
24+
id
25+
}
26+
}
27+
}
28+
`);
29+
30+
const DescriptionCell = ({
31+
value,
32+
rowIndex,
33+
handleFieldChange,
34+
}: {
35+
value: string;
36+
rowIndex: any;
37+
handleFieldChange: any;
38+
}) => {
39+
const [description, setDescription] = React.useState(value || '');
40+
41+
const handleChange = (text: string) => {
42+
setDescription(text);
43+
handleFieldChange('description', text, rowIndex);
44+
};
45+
46+
return (
47+
<TextField
48+
label="Description"
49+
labelHidden
50+
name="description"
51+
type="text"
52+
value={description}
53+
onChange={(e: any) => handleChange(e)}
54+
/>
55+
);
56+
};
57+
1058
export const ResourceSchema = ({
59+
setSchema,
1160
resourceId,
1261
isPending,
1362
data,
1463
refetch,
1564
}: any) => {
16-
65+
66+
const transformedData = data.map((item: any) => ({
67+
schemaId: parseInt(item.id, 10),
68+
format: item.format,
69+
description: item.description,
70+
}));
71+
const [updatedData, setUpdatedData] = React.useState<any>(transformedData);
72+
73+
React.useEffect(() => {
74+
if (data && data.length > 0) {
75+
setUpdatedData(transformedData);
76+
}
77+
}, [data]);
78+
79+
const handleFieldChange = (
80+
field: string,
81+
newValue: string,
82+
rowIndex: any
83+
) => {
84+
setUpdatedData((prev: any) => {
85+
const newData = [...prev];
86+
newData[rowIndex] = {
87+
...newData[rowIndex],
88+
[field]: newValue,
89+
};
90+
return newData;
91+
});
92+
};
93+
94+
setSchema(updatedData);
95+
1796
const resetSchema: any = graphql(`
1897
mutation resetFileResourceSchema($resourceId: UUID!) {
1998
resetFileResourceSchema(resourceId: $resourceId) {
@@ -42,6 +121,17 @@ export const ResourceSchema = ({
42121
}
43122
);
44123

124+
const options = [
125+
{
126+
label: 'Integer',
127+
value: 'INTEGER',
128+
},
129+
{
130+
label: 'String',
131+
value: 'STRING',
132+
},
133+
];
134+
45135
const generateColumnData = () => {
46136
return [
47137
{
@@ -51,16 +141,41 @@ export const ResourceSchema = ({
51141
{
52142
accessorKey: 'description',
53143
header: 'DESCRIPTION',
144+
cell: (info: any) => {
145+
const rowIndex = info.row.index;
146+
const description = updatedData[rowIndex]?.description || '';
147+
return (
148+
<DescriptionCell
149+
value={description}
150+
rowIndex={rowIndex}
151+
handleFieldChange={handleFieldChange}
152+
/>
153+
);
154+
},
54155
},
55156
{
56157
accessorKey: 'format',
57158
header: 'FORMAT',
159+
cell: (info: any) => {
160+
const rowIndex = info.row.index;
161+
const format = updatedData[rowIndex]?.format || '';
162+
return (
163+
<Select
164+
label="Resource List"
165+
labelHidden
166+
options={options}
167+
value={format}
168+
onChange={(e) => handleFieldChange('format', e, rowIndex)}
169+
name="Select format"
170+
/>
171+
);
172+
},
58173
},
59174
];
60175
};
61176

62-
const generateTableData = (data: any[]) => {
63-
return data.map((item: any) => ({
177+
const generateTableData = (updatedData: any[]) => {
178+
return updatedData.map((item: any) => ({
64179
fieldName: item.fieldName,
65180
description: item.description,
66181
format: item.format,
@@ -108,7 +223,7 @@ export const ResourceSchema = ({
108223
hideSelection
109224
/>
110225
) : (
111-
<div className="flex justify-center mt-8">Click on Reset Fields</div>
226+
<div className="mt-8 flex justify-center">Click on Reset Fields</div>
112227
)}
113228
</div>
114229
</>

0 commit comments

Comments
 (0)