1+ import React from 'react' ;
2+ import { useParams } from 'next/navigation' ;
13import { graphql } from '@/gql' ;
24import {
35 CreateFileResourceInput ,
6+ SchemaUpdateInput ,
47 UpdateFileResourceInput ,
58} 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' ;
9+ import { IconTrash } from '@tabler/icons-react' ;
10+ import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
11+ import {
12+ parseAsBoolean ,
13+ parseAsString ,
14+ useQueryState ,
15+ } from 'next-usequerystate' ;
916import {
1017 Button ,
18+ ButtonGroup ,
19+ Checkbox ,
1120 Combobox ,
21+ DataTable ,
1222 Dialog ,
1323 Divider ,
1424 DropZone ,
1525 Icon ,
26+ IconButton ,
1627 Select ,
28+ Spinner ,
1729 Text ,
1830 TextField ,
19- toast
31+ toast ,
2032} from 'opub-ui' ;
21- import React from 'react' ;
2233
23- import { Icons } from '@/components/icons' ;
2434import { GraphQL } from '@/lib/api' ;
35+ import { Icons } from '@/components/icons' ;
2536import { createResourceFilesDoc } from './ResourceDropzone' ;
26- import { ResourceSchema } from './ResourceSchema' ;
37+ import { ResourceSchema , updateSchema } from './ResourceSchema' ;
2738
2839interface TListItem {
2940 label : string ;
@@ -50,18 +61,26 @@ export const EditResource = ({ reload, data }: any) => {
5061 ` ) ;
5162
5263 const [ resourceId , setResourceId ] = useQueryState < any > ( 'id' , parseAsString ) ;
64+ const [ schema , setSchema ] = React . useState ( [ ] ) ;
5365
5466 const { mutate, isLoading } = useMutation (
55- ( data : { fileResourceInput : UpdateFileResourceInput } ) =>
56- GraphQL ( updateResourceDoc , data ) ,
67+ ( data : {
68+ fileResourceInput : UpdateFileResourceInput ;
69+ isResetSchema : boolean ;
70+ } ) => GraphQL ( updateResourceDoc , data ) ,
5771 {
58- onSuccess : ( ) => {
72+ onSuccess : ( data , variables ) => {
5973 toast ( 'File changes saved' , {
6074 action : {
6175 label : 'Dismiss' ,
6276 onClick : ( ) => { } ,
6377 } ,
6478 } ) ;
79+ if ( variables . isResetSchema ) {
80+ schemaMutation . mutate ( {
81+ resourceId : resourceId ,
82+ } ) ;
83+ }
6584 reload ( ) ;
6685 } ,
6786 onError : ( err : any ) => {
@@ -70,11 +89,35 @@ export const EditResource = ({ reload, data }: any) => {
7089 }
7190 ) ;
7291
73- const {
74- data : payload ,
75- refetch,
76- isLoading : isPending ,
77- } = useQuery < any > ( [ `fetch_schema_${ params . id } ` ] , ( ) =>
92+ const resetSchema : any = graphql ( `
93+ mutation resetFileResourceSchema($resourceId: UUID!) {
94+ resetFileResourceSchema(resourceId: $resourceId) {
95+ ... on TypeResource {
96+ id
97+ schema {
98+ format
99+ description
100+ id
101+ fieldName
102+ }
103+ }
104+ }
105+ }
106+ ` ) ;
107+
108+ const schemaMutation = useMutation (
109+ ( data : { resourceId : string } ) => GraphQL ( resetSchema , data ) ,
110+ {
111+ onSuccess : ( ) => {
112+ schemaQuery . refetch ( ) ;
113+ } ,
114+ onError : ( err : any ) => {
115+ console . log ( 'Error ::: ' , err ) ;
116+ } ,
117+ }
118+ ) ;
119+
120+ const schemaQuery = useQuery < any > ( [ `fetch_schema_${ params . id } ` ] , ( ) =>
78121 GraphQL ( fetchSchema , { datasetId : params . id } )
79122 ) ;
80123
@@ -92,6 +135,24 @@ export const EditResource = ({ reload, data }: any) => {
92135 }
93136 ` ) ;
94137
138+ const { mutate : modify } = useMutation (
139+ ( data : { input : SchemaUpdateInput } ) => GraphQL ( updateSchema , data ) ,
140+ {
141+ onSuccess : ( ) => {
142+ schemaQuery . refetch ( ) ;
143+ toast ( 'Schema Updated Successfully' , {
144+ action : {
145+ label : 'Dismiss' ,
146+ onClick : ( ) => { } ,
147+ } ,
148+ } ) ;
149+ } ,
150+ onError : ( err : any ) => {
151+ console . log ( 'Error ::: ' , err ) ;
152+ } ,
153+ }
154+ ) ;
155+
95156 const { mutate : transform } = useMutation (
96157 ( data : { fileResourceInput : CreateFileResourceInput } ) =>
97158 GraphQL ( createResourceFilesDoc , data ) ,
@@ -104,6 +165,7 @@ export const EditResource = ({ reload, data }: any) => {
104165 onClick : ( ) => { } ,
105166 } ,
106167 } ) ;
168+ schemaQuery . refetch ( ) ;
107169 reload ( ) ;
108170 } ,
109171 onError : ( err : any ) => {
@@ -112,8 +174,6 @@ export const EditResource = ({ reload, data }: any) => {
112174 }
113175 ) ;
114176
115- const router = useRouter ( ) ;
116-
117177 const ResourceList : TListItem [ ] =
118178 data . map ( ( item : any ) => ( {
119179 label : item . name ,
@@ -177,17 +237,20 @@ export const EditResource = ({ reload, data }: any) => {
177237 </ div >
178238 ) ;
179239
180- const [ resourceFile , setResourceFile ] = React . useState < File > ( ) ;
181240
182241 const onDrop = React . useCallback (
183- ( _dropFiles : File [ ] , acceptedFiles : File [ ] ) =>
184- setResourceFile ( acceptedFiles [ 0 ] ) ,
242+ ( _dropFiles : File [ ] , acceptedFiles : File [ ] ) => {
243+ mutate ( {
244+ fileResourceInput : {
245+ id : resourceId ,
246+ file : acceptedFiles [ 0 ] ,
247+ } ,
248+ isResetSchema : true ,
249+ } ) ;
250+ } ,
185251 [ ]
186252 ) ;
187-
188- const fileInput = resourceFile ? (
189- < div className = "flex " > { resourceFile . name } </ div >
190- ) : (
253+ const fileInput = (
191254 < div className = "flex" >
192255 < Text className = "break-all" >
193256 { getResourceObject ( resourceId ) ?. fileDetails . file . name . replace (
@@ -206,15 +269,23 @@ export const EditResource = ({ reload, data }: any) => {
206269 mutate ( {
207270 fileResourceInput : {
208271 id : resourceId ,
209- description : resourceDesc
210- ? resourceDesc
211- : getResourceObject ( resourceId ) ?. description ,
212- name : resourceName
213- ? resourceName
214- : getResourceObject ( resourceId ) ?. label ,
215- file : resourceFile ,
272+ description : resourceDesc || '' ,
273+ name : resourceName || '' ,
216274 } ,
275+ isResetSchema : false ,
217276 } ) ;
277+ if ( schema . length > 0 ) {
278+ const updatedScheme = schema . map ( ( item ) => {
279+ const { fieldName, ...rest } = item as any ;
280+ return rest ;
281+ } ) ;
282+ modify ( {
283+ input : {
284+ resource : resourceId ,
285+ updates : updatedScheme ,
286+ } ,
287+ } ) ;
288+ }
218289 } ;
219290
220291 return (
@@ -248,7 +319,7 @@ export const EditResource = ({ reload, data }: any) => {
248319 className = " w-1/5 justify-end"
249320 size = "medium"
250321 kind = "tertiary"
251- variant = "basic "
322+ variant = "interactive "
252323 onClick = { listViewFunction }
253324 >
254325 < div className = "flex items-center gap-2" >
@@ -356,7 +427,7 @@ export const EditResource = ({ reload, data }: any) => {
356427 />
357428 <Combobox
358429 name="to_row_number"
359- label="To Row Number"
430+ label="To Row Number"
360431 placeholder="Search Locations"
361432 list={[
362433 {
@@ -382,18 +453,44 @@ export const EditResource = ({ reload, data }: any) => {
382453 <Text>See Preview</Text>
383454 </div>
384455 </div>*/ }
385- { resourceId && payload && Object . keys ( payload ) . length > 0 ? (
386- < ResourceSchema
387- resourceId = { resourceId }
388- isPending = { isPending }
389- refetch = { refetch }
390- data = {
391- payload ?. datasetResources ?. filter (
392- ( item : any ) => item . id === resourceId
393- ) [ 0 ] ?. schema
394- }
395- />
396- ) : null }
456+ < div className = "my-8" >
457+ < div className = "flex flex-wrap justify-between" >
458+ < Text > Fields in the Resource</ Text >
459+ < Button
460+ size = "medium"
461+ kind = "tertiary"
462+ variant = "basic"
463+ onClick = { ( ) =>
464+ schemaMutation . mutate ( {
465+ resourceId : resourceId ,
466+ } )
467+ }
468+ >
469+ < div className = "flex items-center gap-1" >
470+ < Text > Reset Fields</ Text > { ' ' }
471+ < Icon source = { Icons . info } color = "interactive" />
472+ </ div >
473+ </ Button >
474+ </ div >
475+ < Text variant = "headingXs" as = "span" fontWeight = "regular" >
476+ The Field settings apply to the Resource on a master level and can not
477+ be changed in Access Models.
478+ </ Text >
479+ { schemaQuery . isLoading || schemaMutation . isLoading ? (
480+ < div className = " mt-8 flex justify-center" >
481+ < Spinner size = { 30 } />
482+ </ div >
483+ ) : resourceId && schemaQuery . data ?. datasetResources ?. filter (
484+ ( item : any ) => item . id === resourceId ) . length > 0 ? (
485+ < ResourceSchema
486+ setSchema = { setSchema }
487+ data = { schemaQuery . data ?. datasetResources ?. filter (
488+ ( item : any ) => item . id === resourceId ) [ 0 ] ?. schema }
489+ />
490+ ) : (
491+ < div className = "my-8 flex justify-center" > Click on Reset Fields </ div >
492+ ) }
493+ </ div >
397494 </ div >
398495 ) ;
399496} ;
0 commit comments