Skip to content

Commit 6379876

Browse files
committed
fix schema listing
1 parent 5213c14 commit 6379876

2 files changed

Lines changed: 75 additions & 51 deletions

File tree

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ export const EditResource = ({ reload, data }: any) => {
8686
}
8787
);
8888

89+
const fetchSchema: any = graphql(`
90+
query datasetSchema($datasetId: UUID!) {
91+
datasetResources(datasetId: $datasetId) {
92+
schema {
93+
id
94+
fieldName
95+
format
96+
description
97+
}
98+
id
99+
}
100+
}
101+
`);
102+
103+
const { data: payload, refetch, isLoading: isPending,} = useQuery<any>(
104+
[`fetch_schema_${params.id}`],
105+
() => GraphQL(fetchSchema, { datasetId: params.id })
106+
);
107+
89108
const { mutate: transform } = useMutation(
90109
(data: { fileResourceInput: CreateFileResourceInput }) =>
91110
GraphQL(createResourceFilesDoc, data),
@@ -373,9 +392,17 @@ export const EditResource = ({ reload, data }: any) => {
373392
<Text>See Preview</Text>
374393
</div>
375394
</div>*/}
376-
{resourceId &&
377-
data?.find((item: any) => item.id === resourceId).schema.length > 0 ? (
378-
<ResourceSchema resourceId={resourceId} data={data} />
395+
{resourceId && payload && Object.keys(payload).length > 0 ? (
396+
<ResourceSchema
397+
resourceId={resourceId}
398+
isPending={isPending}
399+
refetch={refetch}
400+
data={
401+
payload?.datasetResources?.filter(
402+
(item: any) => item.id === resourceId
403+
)[0]?.schema
404+
}
405+
/>
379406
) : null}
380407
</div>
381408
);

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

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,19 @@
11
import React from 'react';
2+
import { useParams } from 'next/navigation';
23
import { graphql } from '@/gql';
3-
import { useMutation } from '@tanstack/react-query';
4-
import { Button, DataTable, Icon, IconButton, Spinner, Text } from 'opub-ui';
4+
import { useMutation, useQuery } from '@tanstack/react-query';
5+
import { Button, Table, Icon, IconButton, Spinner, Text } from 'opub-ui';
6+
57
import { GraphQL } from '@/lib/api';
68
import { Icons } from '@/components/icons';
79

8-
export const ResourceSchema = ({ resourceId, data }: any) => {
9-
const table = {
10-
columns: [
11-
{
12-
accessorKey: 'fieldName',
13-
header: 'FIELD NAME',
14-
},
15-
{
16-
accessorKey: 'description',
17-
header: 'DESCRIPTION',
18-
},
19-
{
20-
accessorKey: 'format',
21-
header: 'FORMAT',
22-
},
23-
{
24-
header: 'DELETE',
25-
cell: ({ row }: any) => (
26-
<IconButton
27-
size="medium"
28-
icon={Icons.delete}
29-
color="interactive"
30-
onClick={(e) => console.log(row.original)}
31-
>
32-
Delete
33-
</IconButton>
34-
),
35-
},
36-
],
37-
rows: data
38-
.find((item: any) => item.id === resourceId)
39-
.schema.map((item: any) => ({
40-
fieldName: item.fieldName,
41-
description: item.description,
42-
format: item.format,
43-
})),
44-
};
45-
10+
export const ResourceSchema = ({
11+
resourceId,
12+
isPending,
13+
data,
14+
refetch,
15+
}: any) => {
16+
4617
const resetSchema: any = graphql(`
4718
mutation resetFileResourceSchema($resourceId: UUID!) {
4819
resetFileResourceSchema(resourceId: $resourceId) {
@@ -62,15 +33,40 @@ export const ResourceSchema = ({ resourceId, data }: any) => {
6233
const { mutate, isLoading } = useMutation(
6334
(data: { resourceId: string }) => GraphQL(resetSchema, data),
6435
{
65-
onSuccess: (data) => {
66-
console.log(data, '-data');
36+
onSuccess: () => {
37+
refetch();
6738
},
6839
onError: (err: any) => {
6940
console.log('Error ::: ', err);
7041
},
7142
}
7243
);
7344

45+
const generateColumnData = () => {
46+
return [
47+
{
48+
accessorKey: 'fieldName',
49+
header: 'FIELD NAME',
50+
},
51+
{
52+
accessorKey: 'description',
53+
header: 'DESCRIPTION',
54+
},
55+
{
56+
accessorKey: 'format',
57+
header: 'FORMAT',
58+
},
59+
];
60+
};
61+
62+
const generateTableData = (data: any[]) => {
63+
return data.map((item: any) => ({
64+
fieldName: item.fieldName,
65+
description: item.description,
66+
format: item.format,
67+
}));
68+
};
69+
7470
const setFields = () => {
7571
mutate({
7672
resourceId: resourceId,
@@ -100,17 +96,18 @@ export const ResourceSchema = ({ resourceId, data }: any) => {
10096
be changed in Access Models.
10197
</Text>
10298
<div className="mt-3">
103-
{isLoading ? (
99+
{isPending || isLoading ? (
104100
<div className=" mt-8 flex justify-center">
105101
<Spinner size={30} />
106102
</div>
107-
) : (
108-
<DataTable
109-
columns={table.columns}
110-
rows={table.rows}
103+
) : data && data.length > 0 ? (
104+
<Table
105+
columns={generateColumnData()}
106+
rows={generateTableData(data)}
111107
hideFooter={true}
112-
defaultRowCount={10}
113108
/>
109+
) : (
110+
<div className="flex justify-center mt-8">Click on Reset Fields</div>
114111
)}
115112
</div>
116113
</>

0 commit comments

Comments
 (0)