Skip to content

Commit 337ec23

Browse files
aashimgargbhavabhuthi
authored andcommitted
fix schema listing
1 parent 5ef4f80 commit 337ec23

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
@@ -71,6 +71,25 @@ export const EditResource = ({ reload, data }: any) => {
7171
}
7272
);
7373

74+
const fetchSchema: any = graphql(`
75+
query datasetSchema($datasetId: UUID!) {
76+
datasetResources(datasetId: $datasetId) {
77+
schema {
78+
id
79+
fieldName
80+
format
81+
description
82+
}
83+
id
84+
}
85+
}
86+
`);
87+
88+
const { data: payload, refetch, isLoading: isPending,} = useQuery<any>(
89+
[`fetch_schema_${params.id}`],
90+
() => GraphQL(fetchSchema, { datasetId: params.id })
91+
);
92+
7493
const { mutate: transform } = useMutation(
7594
(data: { fileResourceInput: CreateFileResourceInput }) =>
7695
GraphQL(createResourceFilesDoc, data),
@@ -361,9 +380,17 @@ export const EditResource = ({ reload, data }: any) => {
361380
<Text>See Preview</Text>
362381
</div>
363382
</div>*/}
364-
{resourceId &&
365-
data?.find((item: any) => item.id === resourceId).schema.length > 0 ? (
366-
<ResourceSchema resourceId={resourceId} data={data} />
383+
{resourceId && payload && Object.keys(payload).length > 0 ? (
384+
<ResourceSchema
385+
resourceId={resourceId}
386+
isPending={isPending}
387+
refetch={refetch}
388+
data={
389+
payload?.datasetResources?.filter(
390+
(item: any) => item.id === resourceId
391+
)[0]?.schema
392+
}
393+
/>
367394
) : null}
368395
</div>
369396
);

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)