Skip to content

Commit 5ef4f80

Browse files
aashimgargbhavabhuthi
authored andcommitted
add schema fetch query
1 parent 72fdaa1 commit 5ef4f80

3 files changed

Lines changed: 132 additions & 111 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export const getReourceDoc = graphql(`
3737
dataset {
3838
pk
3939
}
40+
schema {
41+
id
42+
fieldName
43+
format
44+
description
45+
}
4046
type
4147
name
4248
description
@@ -190,6 +196,7 @@ const NoList = ({
190196
return (
191197
<>
192198
<DropZone
199+
accept=".json, .csv, application/json, text/csv"
193200
name="file_details"
194201
label="Upload"
195202
allowMultiple={true}

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

Lines changed: 7 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { GraphQL } from '@/lib/api';
2525
import { Icons } from '@/components/icons';
2626
import { createResourceFilesDoc } from './DistributionList';
27+
import { ResourceSchema } from './ResourceSchema';
2728

2829
interface TListItem {
2930
label: string;
@@ -34,6 +35,8 @@ interface TListItem {
3435
}
3536

3637
export const EditResource = ({ reload, data }: any) => {
38+
const params = useParams();
39+
3740
const updateResourceDoc: any = graphql(`
3841
mutation updateFileResource($fileResourceInput: UpdateFileResourceInput!) {
3942
updateFileResource(fileResourceInput: $fileResourceInput) {
@@ -47,22 +50,6 @@ export const EditResource = ({ reload, data }: any) => {
4750
}
4851
`);
4952

50-
const resetSchema: any = graphql(`
51-
mutation resetFileResourceSchema($resourceId: UUID!) {
52-
resetFileResourceSchema(resourceId: $resourceId) {
53-
... on TypeResource {
54-
id
55-
schema {
56-
format
57-
description
58-
id
59-
fieldName
60-
}
61-
}
62-
}
63-
}
64-
`);
65-
6653
const [resourceId, setResourceId] = useQueryState<any>('id', parseAsString);
6754

6855
const { mutate, isLoading } = useMutation(
@@ -104,61 +91,6 @@ export const EditResource = ({ reload, data }: any) => {
10491
}
10592
);
10693

107-
const table = {
108-
columns: [
109-
{
110-
accessorKey: 'field_key',
111-
header: 'FIELD KEY',
112-
},
113-
{
114-
accessorKey: 'display_name',
115-
header: 'DISPLAY NAME',
116-
},
117-
{
118-
accessorKey: 'description',
119-
header: 'DESCRIPTION',
120-
},
121-
{
122-
accessorKey: 'format',
123-
header: 'FORMAT',
124-
},
125-
{
126-
header: 'DELETE',
127-
cell: ({ row }: any) => (
128-
<IconButton
129-
size="medium"
130-
icon={Icons.delete}
131-
color="interactive"
132-
onClick={(e) => console.log(row.original)}
133-
>
134-
Delete
135-
</IconButton>
136-
),
137-
},
138-
],
139-
rows: [
140-
{
141-
field_key: 'date',
142-
display_name: 'Date',
143-
description: 'Date on which measurements are taken',
144-
format: 'Date',
145-
},
146-
{
147-
field_key: 'date',
148-
display_name: 'Date',
149-
description: 'Date on which measurements are taken',
150-
format: 'Date',
151-
},
152-
{
153-
field_key: 'date',
154-
display_name: 'Date',
155-
description: 'Date on which measurements are taken',
156-
format: 'Date',
157-
},
158-
],
159-
};
160-
161-
const params = useParams();
16294
const router = useRouter();
16395

16496
const ResourceList: TListItem[] =
@@ -429,46 +361,10 @@ export const EditResource = ({ reload, data }: any) => {
429361
<Text>See Preview</Text>
430362
</div>
431363
</div>*/}
432-
<div className="flex justify-between mt-8">
433-
<Text>Fields in the Resource</Text>
434-
<div className="flex gap-4">
435-
<Button
436-
size="medium"
437-
kind="tertiary"
438-
variant="basic"
439-
onClick={function Ga() {}}
440-
>
441-
<div className="flex items-center gap-1">
442-
<Text>Refetch Fields</Text>{' '}
443-
<Icon source={Icons.info} color="interactive" />
444-
</div>
445-
</Button>
446-
447-
<Button
448-
size="medium"
449-
kind="tertiary"
450-
variant="basic"
451-
onClick={function Ga() {}}
452-
>
453-
<div className="flex items-center gap-1">
454-
<Text>Reset Fields</Text>{' '}
455-
<Icon source={Icons.info} color="interactive" />
456-
</div>
457-
</Button>
458-
</div>
459-
</div>
460-
<Text variant="headingXs" as="span" fontWeight="regular">
461-
The Field settings apply to the Resource on a master level and can not
462-
be changed in Access Models.
463-
</Text>
464-
<div className="mt-3">
465-
<DataTable
466-
columns={table.columns}
467-
rows={table.rows}
468-
hideFooter={true}
469-
defaultRowCount={10}
470-
/>
471-
</div>
364+
{resourceId &&
365+
data?.find((item: any) => item.id === resourceId).schema.length > 0 ? (
366+
<ResourceSchema resourceId={resourceId} data={data} />
367+
) : null}
472368
</div>
473369
);
474370
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import React from 'react';
2+
import { graphql } from '@/gql';
3+
import { useMutation } from '@tanstack/react-query';
4+
import { Button, DataTable, Icon, IconButton, Spinner, Text } from 'opub-ui';
5+
import { GraphQL } from '@/lib/api';
6+
import { Icons } from '@/components/icons';
7+
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+
46+
const resetSchema: any = graphql(`
47+
mutation resetFileResourceSchema($resourceId: UUID!) {
48+
resetFileResourceSchema(resourceId: $resourceId) {
49+
... on TypeResource {
50+
id
51+
schema {
52+
format
53+
description
54+
id
55+
fieldName
56+
}
57+
}
58+
}
59+
}
60+
`);
61+
62+
const { mutate, isLoading } = useMutation(
63+
(data: { resourceId: string }) => GraphQL(resetSchema, data),
64+
{
65+
onSuccess: (data) => {
66+
console.log(data, '-data');
67+
},
68+
onError: (err: any) => {
69+
console.log('Error ::: ', err);
70+
},
71+
}
72+
);
73+
74+
const setFields = () => {
75+
mutate({
76+
resourceId: resourceId,
77+
});
78+
};
79+
80+
return (
81+
<>
82+
<div className="mt-8 flex justify-between">
83+
<Text>Fields in the Resource</Text>
84+
<div className="flex gap-4">
85+
<Button
86+
size="medium"
87+
kind="tertiary"
88+
variant="basic"
89+
onClick={setFields}
90+
>
91+
<div className="flex items-center gap-1">
92+
<Text>Reset Fields</Text>{' '}
93+
<Icon source={Icons.info} color="interactive" />
94+
</div>
95+
</Button>
96+
</div>
97+
</div>
98+
<Text variant="headingXs" as="span" fontWeight="regular">
99+
The Field settings apply to the Resource on a master level and can not
100+
be changed in Access Models.
101+
</Text>
102+
<div className="mt-3">
103+
{isLoading ? (
104+
<div className=" mt-8 flex justify-center">
105+
<Spinner size={30} />
106+
</div>
107+
) : (
108+
<DataTable
109+
columns={table.columns}
110+
rows={table.rows}
111+
hideFooter={true}
112+
defaultRowCount={10}
113+
/>
114+
)}
115+
</div>
116+
</>
117+
);
118+
};

0 commit comments

Comments
 (0)