Skip to content

Commit 1fcc70c

Browse files
authored
Merge pull request #83 from CivicDataLab/ResourceListView-Search
fix: modal and search input for ResourceListView
2 parents eab503c + 23d1f3b commit 1fcc70c

4 files changed

Lines changed: 52 additions & 30 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export const createResourceFilesDoc: any = graphql(`
119119
id
120120
created
121121
name
122+
type
122123
}
123124
}
124125
`);

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

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import {
1414
DataTable,
1515
Dialog,
1616
DropZone,
17+
Icon,
1718
IconButton,
1819
SearchInput,
1920
Spinner,
2021
Text,
22+
TextField,
2123
toast,
2224
} from 'opub-ui';
2325

@@ -26,6 +28,13 @@ import { formatDate } from '@/lib/utils';
2628
import { Icons } from '@/components/icons';
2729
import { createResourceFilesDoc } from './DistributionList';
2830

31+
type FilteredRow = {
32+
name_of_resource: string;
33+
type: string;
34+
date_added: string;
35+
id: string;
36+
};
37+
2938
export const ResourceListView = ({ data, refetch }: any) => {
3039
const updateResourceList: any = graphql(`
3140
mutation deleteFileResource($resourceId: UUID!) {
@@ -35,10 +44,14 @@ export const ResourceListView = ({ data, refetch }: any) => {
3544

3645
const [resourceId, setResourceId] = useQueryState('id', parseAsString);
3746

38-
const { mutate, isLoading } = useMutation(
47+
const { mutate } = useMutation(
3948
(data: { resourceId: string }) => GraphQL(updateResourceList, data),
4049
{
41-
onSuccess: () => {
50+
onSuccess: (data, variables) => {
51+
const updatedFilteredRows = filteredRows.filter(
52+
(row: any) => row.id !== variables.resourceId
53+
);
54+
setFilteredRows(updatedFilteredRows);
4255
refetch();
4356
toast('Resource Deleted Successfully', {
4457
action: {
@@ -58,6 +71,18 @@ export const ResourceListView = ({ data, refetch }: any) => {
5871
GraphQL(createResourceFilesDoc, data),
5972
{
6073
onSuccess: (data: any) => {
74+
const updatedRows = data.createFileResources.map((item: any) => ({
75+
name_of_resource: item.name,
76+
type: item.type,
77+
date_added: formatDate(item.created),
78+
id: item.id,
79+
}));
80+
81+
setFilteredRows((prevRows: FilteredRow[]) => [
82+
...prevRows,
83+
...updatedRows,
84+
]);
85+
6186
refetch();
6287
toast('Resource Added Successfully', {
6388
action: {
@@ -136,16 +161,17 @@ export const ResourceListView = ({ data, refetch }: any) => {
136161
})) || [],
137162
};
138163

139-
//const [filteredRows, setFilteredRows] = React.useState(table.rows);
164+
const [filteredRows, setFilteredRows] = React.useState<FilteredRow[]>(
165+
table.rows
166+
);
140167

141-
// const handleSearchChange = (e:string) => {
142-
// console.log(e, 'e');
143-
// const searchTerm = e.toLowerCase();
144-
// const filtered = table.rows.filter((row:any) =>
145-
// row.name_of_resource.toLowerCase().includes(searchTerm)
146-
// );
147-
// setFilteredRows(filtered);
148-
// };
168+
const handleSearchChange = (e: string) => {
169+
const searchTerm = e.toLowerCase();
170+
const filtered = table.rows.filter((row: any) =>
171+
row.name_of_resource.toLowerCase().includes(searchTerm)
172+
);
173+
setFilteredRows(filtered);
174+
};
149175

150176
const filteredColumns = table.columns.filter(
151177
(column) => column.accessorKey !== 'id'
@@ -177,38 +203,33 @@ export const ResourceListView = ({ data, refetch }: any) => {
177203
return (
178204
<div className="mt-3">
179205
<div className="my-8 flex items-center gap-6 px-4">
180-
{/* <Text>
181-
Showing {table.rows.length} of {table.rows.length} resources
182-
</Text> */}
183-
{/* <SearchInput
206+
<Text>
207+
Showing {filteredRows.length} of {filteredRows.length} resources
208+
</Text>
209+
<SearchInput
184210
className="w-1/2 "
185211
placeholder="Search in Resources"
186212
label="Search"
187213
name="Search"
188-
onChange={function Ga() {} }
189-
/> */}
190-
{/* <Dialog>
214+
onChange={(e) => handleSearchChange(e)}
215+
/>
216+
<Dialog>
191217
<Dialog.Trigger>
192218
<Button size="medium" className=" mx-5">
193219
ADD NEW RESOURCE
194220
</Button>
195221
</Dialog.Trigger>
196222
<Dialog.Content title={'Add New Resource'}>
197-
<DropZone
198-
name="file_upload"
199-
allowMultiple={true}
200-
onDrop={dropZone}
201-
>
223+
<DropZone name="file_upload" allowMultiple={true} onDrop={dropZone}>
202224
{uploadedFile}
203225
{file.length === 0 && <DropZone.FileUpload />}
204226
</DropZone>
205227
</Dialog.Content>
206-
</Dialog> */}
228+
</Dialog>
207229
</div>
208230
<DataTable
209-
addToolbar
210231
columns={filteredColumns}
211-
rows={table.rows}
232+
rows={filteredRows}
212233
hideFooter={true}
213234
hideSelection={true}
214235
defaultRowCount={10}

gql/generated/gql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const documents = {
2020
"\n mutation createAccessModel($accessModelInput: AccessModelInput!) {\n createAccessModel(accessModelInput: $accessModelInput) {\n __typename\n ... on TypeAccessModel {\n id\n description\n name\n type\n }\n }\n }\n": types.CreateAccessModelDocument,
2121
"\n query accessModelResources($datasetId: UUID!) {\n accessModelResources(datasetId: $datasetId) {\n modelResources {\n resource {\n name\n description\n id\n }\n }\n id\n name\n description\n type\n created\n modified\n }\n }\n": types.AccessModelResourcesDocument,
2222
"\n query getResources($filters: DatasetFilter) {\n datasets(filters: $filters) {\n resources {\n id\n dataset {\n pk\n }\n type\n name\n description\n created\n fileDetails {\n id\n resource {\n pk\n }\n file {\n name\n path\n url\n }\n size\n created\n modified\n }\n }\n }\n }\n": types.GetResourcesDocument,
23-
"\n mutation readFiles($fileResourceInput: CreateFileResourceInput!) {\n createFileResources(fileResourceInput: $fileResourceInput) {\n id\n created\n name\n }\n }\n": types.ReadFilesDocument,
23+
"\n mutation readFiles($fileResourceInput: CreateFileResourceInput!) {\n createFileResources(fileResourceInput: $fileResourceInput) {\n id\n created\n name\n type\n }\n }\n": types.ReadFilesDocument,
2424
"\n query datasetTitleQuery($filters: DatasetFilter) {\n datasets(filters: $filters) {\n id\n title\n created\n }\n }\n": types.DatasetTitleQueryDocument,
2525
"\n mutation SaveTitle($updateDatasetInput: UpdateDatasetInput!) {\n updateDataset(updateDatasetInput: $updateDatasetInput) {\n __typename\n ... on TypeDataset {\n id\n title\n created\n }\n ... on OperationInfo {\n messages {\n kind\n message\n }\n }\n }\n }\n": types.SaveTitleDocument,
2626
"\n query MetaDataQuery {\n metadata {\n id\n label\n dataStandard\n urn\n dataType\n options\n validator\n type\n model\n enabled\n }\n }\n": types.MetaDataQueryDocument,
@@ -78,7 +78,7 @@ export function graphql(source: "\n query getResources($filters: DatasetFilter)
7878
/**
7979
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
8080
*/
81-
export function graphql(source: "\n mutation readFiles($fileResourceInput: CreateFileResourceInput!) {\n createFileResources(fileResourceInput: $fileResourceInput) {\n id\n created\n name\n }\n }\n"): (typeof documents)["\n mutation readFiles($fileResourceInput: CreateFileResourceInput!) {\n createFileResources(fileResourceInput: $fileResourceInput) {\n id\n created\n name\n }\n }\n"];
81+
export function graphql(source: "\n mutation readFiles($fileResourceInput: CreateFileResourceInput!) {\n createFileResources(fileResourceInput: $fileResourceInput) {\n id\n created\n name\n type\n }\n }\n"): (typeof documents)["\n mutation readFiles($fileResourceInput: CreateFileResourceInput!) {\n createFileResources(fileResourceInput: $fileResourceInput) {\n id\n created\n name\n type\n }\n }\n"];
8282
/**
8383
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
8484
*/

gql/generated/graphql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ export type ReadFilesMutationVariables = Exact<{
625625
}>;
626626

627627

628-
export type ReadFilesMutation = { __typename?: 'Mutation', createFileResources: Array<{ __typename?: 'TypeResource', id: any, created: any, name: string }> };
628+
export type ReadFilesMutation = { __typename?: 'Mutation', createFileResources: Array<{ __typename?: 'TypeResource', id: any, created: any, name: string, type: ApiResourceTypeEnum }> };
629629

630630
export type DatasetTitleQueryQueryVariables = Exact<{
631631
filters?: InputMaybe<DatasetFilter>;
@@ -701,7 +701,7 @@ export const ResourcesDocument = {"kind":"Document","definitions":[{"kind":"Oper
701701
export const CreateAccessModelDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"createAccessModel"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"accessModelInput"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"AccessModelInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createAccessModel"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"accessModelInput"},"value":{"kind":"Variable","name":{"kind":"Name","value":"accessModelInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"__typename"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"TypeAccessModel"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"type"}}]}}]}}]}}]} as unknown as DocumentNode<CreateAccessModelMutation, CreateAccessModelMutationVariables>;
702702
export const AccessModelResourcesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"accessModelResources"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"datasetId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UUID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"accessModelResources"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"datasetId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"datasetId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"modelResources"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"resource"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}},{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"type"}},{"kind":"Field","name":{"kind":"Name","value":"created"}},{"kind":"Field","name":{"kind":"Name","value":"modified"}}]}}]}}]} as unknown as DocumentNode<AccessModelResourcesQuery, AccessModelResourcesQueryVariables>;
703703
export const GetResourcesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"getResources"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"filters"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"DatasetFilter"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"datasets"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filters"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"resources"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"dataset"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pk"}}]}},{"kind":"Field","name":{"kind":"Name","value":"type"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"created"}},{"kind":"Field","name":{"kind":"Name","value":"fileDetails"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"resource"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pk"}}]}},{"kind":"Field","name":{"kind":"Name","value":"file"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"path"}},{"kind":"Field","name":{"kind":"Name","value":"url"}}]}},{"kind":"Field","name":{"kind":"Name","value":"size"}},{"kind":"Field","name":{"kind":"Name","value":"created"}},{"kind":"Field","name":{"kind":"Name","value":"modified"}}]}}]}}]}}]}}]} as unknown as DocumentNode<GetResourcesQuery, GetResourcesQueryVariables>;
704-
export const ReadFilesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"readFiles"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"fileResourceInput"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"CreateFileResourceInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createFileResources"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"fileResourceInput"},"value":{"kind":"Variable","name":{"kind":"Name","value":"fileResourceInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"created"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}}]} as unknown as DocumentNode<ReadFilesMutation, ReadFilesMutationVariables>;
704+
export const ReadFilesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"readFiles"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"fileResourceInput"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"CreateFileResourceInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createFileResources"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"fileResourceInput"},"value":{"kind":"Variable","name":{"kind":"Name","value":"fileResourceInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"created"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"type"}}]}}]}}]} as unknown as DocumentNode<ReadFilesMutation, ReadFilesMutationVariables>;
705705
export const DatasetTitleQueryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"datasetTitleQuery"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"filters"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"DatasetFilter"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"datasets"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filters"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"created"}}]}}]}}]} as unknown as DocumentNode<DatasetTitleQueryQuery, DatasetTitleQueryQueryVariables>;
706706
export const SaveTitleDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"SaveTitle"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"updateDatasetInput"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UpdateDatasetInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateDataset"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"updateDatasetInput"},"value":{"kind":"Variable","name":{"kind":"Name","value":"updateDatasetInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"__typename"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"TypeDataset"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"created"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"OperationInfo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"messages"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"kind"}},{"kind":"Field","name":{"kind":"Name","value":"message"}}]}}]}}]}}]}}]} as unknown as DocumentNode<SaveTitleMutation, SaveTitleMutationVariables>;
707707
export const MetaDataQueryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"MetaDataQuery"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"metadata"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"label"}},{"kind":"Field","name":{"kind":"Name","value":"dataStandard"}},{"kind":"Field","name":{"kind":"Name","value":"urn"}},{"kind":"Field","name":{"kind":"Name","value":"dataType"}},{"kind":"Field","name":{"kind":"Name","value":"options"}},{"kind":"Field","name":{"kind":"Name","value":"validator"}},{"kind":"Field","name":{"kind":"Name","value":"type"}},{"kind":"Field","name":{"kind":"Name","value":"model"}},{"kind":"Field","name":{"kind":"Name","value":"enabled"}}]}}]}}]} as unknown as DocumentNode<MetaDataQueryQuery, MetaDataQueryQueryVariables>;

0 commit comments

Comments
 (0)