|
1 | 1 | import { UUID } from 'crypto'; |
2 | | -import React, { useEffect } from 'react'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
3 | 3 | import Link from 'next/link'; |
4 | 4 | import { useParams } from 'next/navigation'; |
5 | 5 | import { graphql } from '@/gql'; |
6 | 6 | import { useMutation, useQuery } from '@tanstack/react-query'; |
7 | | -import { Button, DataTable, IconButton, Spinner, Text, toast } from 'opub-ui'; |
| 7 | +import { |
| 8 | + Button, |
| 9 | + DataTable, |
| 10 | + IconButton, |
| 11 | + SearchInput, |
| 12 | + Spinner, |
| 13 | + Text, |
| 14 | + toast, |
| 15 | +} from 'opub-ui'; |
8 | 16 |
|
9 | 17 | import { GraphQL } from '@/lib/api'; |
10 | 18 | import { formatDate, toTitleCase } from '@/lib/utils'; |
@@ -54,11 +62,16 @@ const AccessModelList: React.FC<AccessModelListProps> = ({ |
54 | 62 | }) |
55 | 63 | ); |
56 | 64 |
|
| 65 | + const [filteredRows, setFilteredRows] = useState<any[]>([]); |
| 66 | + |
57 | 67 | useEffect(() => { |
58 | 68 | refetch(); |
59 | | - }, [list]); |
| 69 | + if (data?.accessModelResources) { |
| 70 | + setFilteredRows(data.accessModelResources); |
| 71 | + } |
| 72 | + }, [data, list]); |
60 | 73 |
|
61 | | - const { mutate } = useMutation( |
| 74 | + const { mutate, isLoading: deleteLoading } = useMutation( |
62 | 75 | (data: { accessModelId: UUID }) => GraphQL(deleteAccessModel, data), |
63 | 76 | { |
64 | 77 | onSuccess: () => { |
@@ -126,24 +139,39 @@ const AccessModelList: React.FC<AccessModelListProps> = ({ |
126 | 139 | })); |
127 | 140 | }; |
128 | 141 |
|
| 142 | + const handleSearchChange = (e: string) => { |
| 143 | + const searchTerm = e.toLowerCase(); |
| 144 | + const filtered = data?.accessModelResources.filter((row: any) => |
| 145 | + row.name.toLowerCase().includes(searchTerm) |
| 146 | + ); |
| 147 | + setFilteredRows(filtered || []); |
| 148 | + }; |
| 149 | + |
129 | 150 | return ( |
130 | 151 | <div> |
131 | | - {!data || isLoading ? ( |
| 152 | + {!data || isLoading || deleteLoading ? ( |
132 | 153 | <div className=" mt-8 flex justify-center"> |
133 | 154 | <Spinner /> |
134 | 155 | </div> |
135 | 156 | ) : ( |
136 | 157 | <> |
137 | | - <div className="flex items-center justify-between p-4"> |
| 158 | + <div className=" my-6 flex flex-wrap items-center justify-between px-3 py-4"> |
138 | 159 | <Text> |
139 | | - Showing {data.accessModelResources?.length} Access Types |
| 160 | + Showing {data?.accessModelResources?.length || 0} Access Types |
140 | 161 | </Text> |
| 162 | + <SearchInput |
| 163 | + className="w-1/2 " |
| 164 | + placeholder="Search in Resources" |
| 165 | + label="Search" |
| 166 | + name="Search" |
| 167 | + onChange={(e) => handleSearchChange(e)} |
| 168 | + /> |
141 | 169 | <Button onClick={(e) => setList(false)}>Add Access Type</Button> |
142 | 170 | </div> |
143 | 171 |
|
144 | 172 | <DataTable |
145 | 173 | columns={generateColumnData()} |
146 | | - rows={generateTableData(data.accessModelResources)} |
| 174 | + rows={generateTableData(filteredRows)} |
147 | 175 | hideSelection |
148 | 176 | truncate |
149 | 177 | hideFooter |
|
0 commit comments