From 011af9fa76e8ce9eb84585d47e01bf177c6bdde2 Mon Sep 17 00:00:00 2001 From: aashimgarg Date: Tue, 28 May 2024 14:45:16 +0530 Subject: [PATCH 1/7] add editable fields with mutations --- .../[id]/edit/components/EditResource.tsx | 48 +++++-- .../[id]/edit/components/ResourceSchema.tsx | 125 +++++++++++++++++- 2 files changed, 159 insertions(+), 14 deletions(-) diff --git a/app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditResource.tsx b/app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditResource.tsx index 9de92497..0795a4af 100644 --- a/app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditResource.tsx +++ b/app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditResource.tsx @@ -1,29 +1,39 @@ import { graphql } from '@/gql'; import { CreateFileResourceInput, + SchemaUpdateInput, UpdateFileResourceInput, } from '@/gql/generated/graphql'; -import { useMutation, useQuery } from '@tanstack/react-query'; -import { parseAsString, useQueryState } from 'next-usequerystate'; -import { useParams, useRouter } from 'next/navigation'; +import { IconTrash } from '@tabler/icons-react'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { + parseAsBoolean, + parseAsString, + useQueryState, +} from 'next-usequerystate'; import { Button, + ButtonGroup, + Checkbox, Combobox, + DataTable, Dialog, Divider, DropZone, Icon, + IconButton, Select, Text, TextField, - toast + toast, } from 'opub-ui'; -import React from 'react'; -import { Icons } from '@/components/icons'; import { GraphQL } from '@/lib/api'; +import { Icons } from '@/components/icons'; import { createResourceFilesDoc } from './ResourceDropzone'; -import { ResourceSchema } from './ResourceSchema'; +import { ResourceSchema, updateSchema } from './ResourceSchema'; +import { useParams } from 'next/navigation'; +import React from 'react'; interface TListItem { label: string; @@ -50,6 +60,7 @@ export const EditResource = ({ reload, data }: any) => { `); const [resourceId, setResourceId] = useQueryState('id', parseAsString); + const [schema, setSchema] = React.useState([]); const { mutate, isLoading } = useMutation( (data: { fileResourceInput: UpdateFileResourceInput }) => @@ -92,6 +103,18 @@ export const EditResource = ({ reload, data }: any) => { } `); + const { mutate: modify } = useMutation( + (data: { input: SchemaUpdateInput }) => GraphQL(updateSchema, data), + { + onSuccess: (data) => { + console.log(data); + }, + onError: (err: any) => { + console.log('Error ::: ', err); + }, + } + ); + const { mutate: transform } = useMutation( (data: { fileResourceInput: CreateFileResourceInput }) => GraphQL(createResourceFilesDoc, data), @@ -112,8 +135,6 @@ export const EditResource = ({ reload, data }: any) => { } ); - const router = useRouter(); - const ResourceList: TListItem[] = data.map((item: any) => ({ label: item.name, @@ -215,6 +236,14 @@ export const EditResource = ({ reload, data }: any) => { file: resourceFile, }, }); + if (schema.length > 0) { + modify({ + input: { + resource: resourceId, + updates: schema, + }, + }); + } }; return ( @@ -384,6 +413,7 @@ export const EditResource = ({ reload, data }: any) => { */} {resourceId && payload && Object.keys(payload).length > 0 ? ( { + const [description, setDescription] = React.useState(value || ''); + + const handleChange = (text: string) => { + setDescription(text); + handleFieldChange('description', text, rowIndex); + }; + + return ( + handleChange(e)} + /> + ); +}; + export const ResourceSchema = ({ + setSchema, resourceId, isPending, data, refetch, }: any) => { - + + const transformedData = data.map((item: any) => ({ + schemaId: parseInt(item.id, 10), + format: item.format, + description: item.description, + })); + const [updatedData, setUpdatedData] = React.useState(transformedData); + + React.useEffect(() => { + if (data && data.length > 0) { + setUpdatedData(transformedData); + } + }, [data]); + + const handleFieldChange = ( + field: string, + newValue: string, + rowIndex: any + ) => { + setUpdatedData((prev: any) => { + const newData = [...prev]; + newData[rowIndex] = { + ...newData[rowIndex], + [field]: newValue, + }; + return newData; + }); + }; + + setSchema(updatedData); + const resetSchema: any = graphql(` mutation resetFileResourceSchema($resourceId: UUID!) { resetFileResourceSchema(resourceId: $resourceId) { @@ -42,6 +121,17 @@ export const ResourceSchema = ({ } ); + const options = [ + { + label: 'Integer', + value: 'INTEGER', + }, + { + label: 'String', + value: 'STRING', + }, + ]; + const generateColumnData = () => { return [ { @@ -51,16 +141,41 @@ export const ResourceSchema = ({ { accessorKey: 'description', header: 'DESCRIPTION', + cell: (info: any) => { + const rowIndex = info.row.index; + const description = updatedData[rowIndex]?.description || ''; + return ( + + ); + }, }, { accessorKey: 'format', header: 'FORMAT', + cell: (info: any) => { + const rowIndex = info.row.index; + const format = updatedData[rowIndex]?.format || ''; + return ( +