Skip to content

Commit 5213c14

Browse files
committed
add schema fetch query
1 parent c9e9550 commit 5213c14

3 files changed

Lines changed: 131 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: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
import { GraphQL } from '@/lib/api';
4040
import { Icons } from '@/components/icons';
4141
import { createResourceFilesDoc } from './DistributionList';
42+
import { ResourceSchema } from './ResourceSchema';
4243

4344
interface TListItem {
4445
label: string;
@@ -49,6 +50,7 @@ interface TListItem {
4950
}
5051

5152
export const EditResource = ({ reload, data }: any) => {
53+
const params = useParams();
5254

5355
const updateResourceDoc: any = graphql(`
5456
mutation updateFileResource($fileResourceInput: UpdateFileResourceInput!) {
@@ -63,22 +65,6 @@ export const EditResource = ({ reload, data }: any) => {
6365
}
6466
`);
6567

66-
const resetSchema: any = graphql(`
67-
mutation resetFileResourceSchema($resourceId: UUID!) {
68-
resetFileResourceSchema(resourceId: $resourceId) {
69-
... on TypeResource {
70-
id
71-
schema {
72-
format
73-
description
74-
id
75-
fieldName
76-
}
77-
}
78-
}
79-
}
80-
`);
81-
8268
const [resourceId, setResourceId] = useQueryState<any>('id', parseAsString);
8369

8470
const { mutate, isLoading } = useMutation(
@@ -120,61 +106,6 @@ export const EditResource = ({ reload, data }: any) => {
120106
}
121107
);
122108

123-
const table = {
124-
columns: [
125-
{
126-
accessorKey: 'field_key',
127-
header: 'FIELD KEY',
128-
},
129-
{
130-
accessorKey: 'display_name',
131-
header: 'DISPLAY NAME',
132-
},
133-
{
134-
accessorKey: 'description',
135-
header: 'DESCRIPTION',
136-
},
137-
{
138-
accessorKey: 'format',
139-
header: 'FORMAT',
140-
},
141-
{
142-
header: 'DELETE',
143-
cell: ({ row }: any) => (
144-
<IconButton
145-
size="medium"
146-
icon={Icons.delete}
147-
color="interactive"
148-
onClick={(e) => console.log(row.original)}
149-
>
150-
Delete
151-
</IconButton>
152-
),
153-
},
154-
],
155-
rows: [
156-
{
157-
field_key: 'date',
158-
display_name: 'Date',
159-
description: 'Date on which measurements are taken',
160-
format: 'Date',
161-
},
162-
{
163-
field_key: 'date',
164-
display_name: 'Date',
165-
description: 'Date on which measurements are taken',
166-
format: 'Date',
167-
},
168-
{
169-
field_key: 'date',
170-
display_name: 'Date',
171-
description: 'Date on which measurements are taken',
172-
format: 'Date',
173-
},
174-
],
175-
};
176-
177-
const params = useParams();
178109
const router = useRouter();
179110

180111
const ResourceList: TListItem[] =
@@ -442,46 +373,10 @@ export const EditResource = ({ reload, data }: any) => {
442373
<Text>See Preview</Text>
443374
</div>
444375
</div>*/}
445-
<div className="flex justify-between mt-8">
446-
<Text>Fields in the Resource</Text>
447-
<div className="flex gap-4">
448-
<Button
449-
size="medium"
450-
kind="tertiary"
451-
variant="basic"
452-
onClick={function Ga() {}}
453-
>
454-
<div className="flex items-center gap-1">
455-
<Text>Refetch Fields</Text>{' '}
456-
<Icon source={Icons.info} color="interactive" />
457-
</div>
458-
</Button>
459-
460-
<Button
461-
size="medium"
462-
kind="tertiary"
463-
variant="basic"
464-
onClick={function Ga() {}}
465-
>
466-
<div className="flex items-center gap-1">
467-
<Text>Reset Fields</Text>{' '}
468-
<Icon source={Icons.info} color="interactive" />
469-
</div>
470-
</Button>
471-
</div>
472-
</div>
473-
<Text variant="headingXs" as="span" fontWeight="regular">
474-
The Field settings apply to the Resource on a master level and can not
475-
be changed in Access Models.
476-
</Text>
477-
<div className="mt-3">
478-
<DataTable
479-
columns={table.columns}
480-
rows={table.rows}
481-
hideFooter={true}
482-
defaultRowCount={10}
483-
/>
484-
</div>
376+
{resourceId &&
377+
data?.find((item: any) => item.id === resourceId).schema.length > 0 ? (
378+
<ResourceSchema resourceId={resourceId} data={data} />
379+
) : null}
485380
</div>
486381
);
487382
};
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)