Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit 38f8ddd

Browse files
authored
Merge pull request #303 from navikt/refactor/ART-07-access
Fetch Access and Bigquery
2 parents a4e88aa + e8290cd commit 38f8ddd

22 files changed

+173
-855
lines changed

components/dataproducts/access/datasetAccess.tsx

+14-46
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { useState } from 'react'
33
import { isAfter, parseISO, format } from 'date-fns'
44
import {
55
useRevokeAccessMutation,
6-
useAccessRequestsForDatasetQuery,
76
useApproveAccessRequestMutation,
87
useDenyAccessRequestMutation,
9-
useDatasetAccessQuery,
108
} from '../../../lib/schema/graphql'
119
import {
1210
Alert,
@@ -17,11 +15,11 @@ import {
1715
Table,
1816
Textarea,
1917
} from '@navikt/ds-react'
20-
import { GET_DATASET_ACCESS } from '../../../lib/queries/access/datasetAccess'
2118
import { ExternalLink } from '@navikt/ds-icons'
22-
import { GET_ACCESS_REQUESTS_FOR_DATASET } from '../../../lib/queries/accessRequest/accessRequestsForDataset'
2319
import { nb } from 'date-fns/locale'
2420
import ErrorMessage from '../../lib/error'
21+
import { useGetDataset } from '../../../lib/rest/dataproducts'
22+
import { useFetchAccessRequestsForDataset } from '../../../lib/rest/access'
2523

2624
interface AccessEntry {
2725
subject: string
@@ -239,54 +237,36 @@ const DatasetAccess = ({ id }: AccessListProps) => {
239237
const [revokeAccess] = useRevokeAccessMutation()
240238
const [approveAccessRequest] = useApproveAccessRequestMutation()
241239
const [denyAccessRequest] = useDenyAccessRequestMutation()
242-
const datasetAccessRequestsQuery = useAccessRequestsForDatasetQuery({
243-
variables: { datasetID: id },
244-
ssr: true,
245-
})
240+
const fetchAccessRequestsForDataset = useFetchAccessRequestsForDataset(id)
246241

247-
const datasetAccessQuery = useDatasetAccessQuery({
248-
variables: { id },
249-
ssr: true,
250-
})
242+
const getDataset = useGetDataset(id)
251243

252-
if (datasetAccessRequestsQuery.error)
253-
return <ErrorMessage error={datasetAccessRequestsQuery.error} />
244+
if (fetchAccessRequestsForDataset.error)
245+
return <ErrorMessage error={fetchAccessRequestsForDataset.error} />
254246
if (
255-
datasetAccessRequestsQuery.loading ||
256-
!datasetAccessRequestsQuery.data?.accessRequestsForDataset
247+
fetchAccessRequestsForDataset.loading ||
248+
!fetchAccessRequestsForDataset.data?.accessRequests
257249
)
258250
return <div />
259251

260252
const datasetAccessRequests =
261-
datasetAccessRequestsQuery.data?.accessRequestsForDataset
253+
fetchAccessRequestsForDataset.data.accessRequests as any[]
262254

263-
if (datasetAccessQuery.error)
264-
return <ErrorMessage error={datasetAccessQuery.error} />
255+
if (getDataset.error)
256+
return <ErrorMessage error={getDataset.error} />
265257
if (
266-
datasetAccessQuery.loading ||
267-
!datasetAccessQuery.data?.dataset.access
258+
getDataset.loading ||
259+
!getDataset?.dataset?.access
268260
)
269261
return <div />
270262

271-
const access = datasetAccessQuery.data.dataset.access
263+
const access = getDataset.dataset.access
272264

273265
const approveRequest = async (requestID: string) => {
274266
try {
275267
await approveAccessRequest({
276268
variables: { id: requestID },
277269
refetchQueries: [
278-
{
279-
query: GET_DATASET_ACCESS,
280-
variables: {
281-
id,
282-
},
283-
},
284-
{
285-
query: GET_ACCESS_REQUESTS_FOR_DATASET,
286-
variables: {
287-
datasetID: id,
288-
},
289-
},
290270
],
291271
})
292272
} catch (e: any) {
@@ -299,12 +279,6 @@ const DatasetAccess = ({ id }: AccessListProps) => {
299279
await denyAccessRequest({
300280
variables: { id: requestID },
301281
refetchQueries: [
302-
{
303-
query: GET_ACCESS_REQUESTS_FOR_DATASET,
304-
variables: {
305-
datasetID: id,
306-
},
307-
},
308282
],
309283
})
310284
} catch (e: any) {
@@ -319,12 +293,6 @@ const DatasetAccess = ({ id }: AccessListProps) => {
319293
await revokeAccess({
320294
variables: { id: a.id },
321295
refetchQueries: [
322-
{
323-
query: GET_DATASET_ACCESS,
324-
variables: {
325-
id,
326-
},
327-
},
328296
],
329297
})
330298
} catch (e: any) {

components/dataproducts/accessRequest/accessRequestsListForOwner.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import * as React from 'react'
22
import { useState } from 'react'
33
import {
4-
AccessRequestsForDatasetQuery,
54
useApproveAccessRequestMutation,
65
} from '../../../lib/schema/graphql'
76
import { Alert, Button, Table } from '@navikt/ds-react'
87
import { useRouter } from 'next/router'
98

109
interface AccessListProps {
11-
accessQuery: AccessRequestsForDatasetQuery | undefined
10+
accessRequests: any[]
1211
}
1312

14-
const AccessRequestsListForOwner = ({ accessQuery }: AccessListProps) => {
15-
const access = accessQuery?.accessRequestsForDataset
13+
const AccessRequestsListForOwner = ({ accessRequests }: AccessListProps) => {
1614
const [approveAccessRequest] = useApproveAccessRequestMutation()
1715
const [isDenying, setIsDenying] = useState<Array<string>>([])
1816

1917
const [formError, setFormError] = useState('')
20-
if (access?.length === 0) {
18+
if (accessRequests?.length === 0) {
2119
return <>Ingen har forespurt tilgang til produktet</>
2220
}
2321

@@ -52,7 +50,7 @@ const AccessRequestsListForOwner = ({ accessQuery }: AccessListProps) => {
5250
</Table.Row>
5351
</Table.Header>
5452
<Table.Body>
55-
{access?.map((a, i) => (
53+
{accessRequests?.map((a, i) => (
5654
<Table.Row key={i}>
5755
<Table.DataCell align="right">{a.subject}</Table.DataCell>
5856
<Table.DataCell>{a.expires}</Table.DataCell>

components/dataproducts/dataset/access/datasetAccessForOwner.tsx

-38
This file was deleted.

components/dataproducts/dataset/access/datasetAccessForUser.tsx

-54
This file was deleted.

components/dataproducts/dataset/useColumnTags.tsx

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useState } from 'react'
22
import {
33
BigQueryType,
4-
useGcpGetColumnsQuery,
54
} from '../../../lib/schema/graphql'
5+
import { useFetchBQcolumns } from '../../../lib/rest/bigquery'
66

77
export type PIITagType =
88
| 'PII_DirekteIdentifiserende'
@@ -53,13 +53,7 @@ export const useColumnTags = (
5353
new Map<string, Map<string, boolean>>()
5454
)
5555

56-
const columnsQuery = useGcpGetColumnsQuery({
57-
variables: {
58-
projectID: projectID,
59-
datasetID: datasetID,
60-
tableID: tableID,
61-
},
62-
})
56+
const fetchColumns = useFetchBQcolumns(projectID, datasetID, tableID)
6357

6458
var tableKey = buildTableKey(projectID, datasetID, tableID)
6559

@@ -69,9 +63,9 @@ export const useColumnTags = (
6963
datasetID &&
7064
tableID &&
7165
!tagsMap.has(tableKey) &&
72-
!columnsQuery.error &&
73-
!columnsQuery.loading &&
74-
columnsQuery.data
66+
!fetchColumns.error &&
67+
!fetchColumns.loading &&
68+
fetchColumns.bqColumns
7569
) {
7670
var newTagsMap = new Map<string, Map<string, PIITagType> | undefined>(
7771
tagsMap
@@ -81,7 +75,7 @@ export const useColumnTags = (
8175

8276
var newPseudoColumnsMap = new Map<string, Map<string, boolean>>(pseudoColumnsMap)
8377
var pseudoColumns = new Map<string, boolean>()
84-
columnsQuery.data.gcpGetColumns.forEach((it) =>{
78+
fetchColumns.bqColumns.forEach((it) =>{
8579
tags.set(
8680
it.name,
8781
(!!tagsFromQuery[it.name] &&
@@ -126,11 +120,11 @@ export const useColumnTags = (
126120

127121
return {
128122
columns:
129-
!columnsQuery.error && !columnsQuery.loading
130-
? (columnsQuery.data?.gcpGetColumns as ColumnType[])
123+
!fetchColumns.error && !fetchColumns.loading
124+
? (fetchColumns.bqColumns as ColumnType[])
131125
: undefined,
132-
loading: columnsQuery.loading,
133-
error: columnsQuery.error,
126+
loading: fetchColumns.loading,
127+
error: fetchColumns.error,
134128
tags: tagsMap.get(tableKey),
135129
pseudoColumns: pseudoColumnsMap.get(tableKey) || new Map<string, boolean>(),
136130
annotateColumn: annotateColumn,

components/dataproducts/datasource/dataset.tsx

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import {
22
BigQueryType,
3-
GcpGetTablesQuery,
4-
useGcpGetTablesLazyQuery,
53
} from '../../../lib/schema/graphql'
64

75
import { TreeItem } from '@mui/x-tree-view/TreeItem';
86

97
import { Loader } from '@navikt/ds-react'
108
import { ExpandFilled, NextFilled } from '@navikt/ds-icons'
119
import Tabell from '../../lib/icons/tabell'
10+
import { useFetchBQTables } from '../../../lib/rest/bigquery';
1211

13-
const DataproductTableIconMap: Record<BigQueryType, () => JSX.Element> = {
14-
materialized_view: Tabell,
15-
table: Tabell,
16-
view: Tabell,
12+
const DataproductTableIconMap: Record<string, () => JSX.Element> = {
13+
"materialized_view": Tabell,
14+
"table": Tabell,
15+
"view": Tabell,
1716
}
1817

1918
export interface DataproductSourceDatasetProps {
@@ -27,11 +26,7 @@ export const Dataset = ({
2726
datasetID,
2827
active,
2928
}: DataproductSourceDatasetProps) => {
30-
const [getTables, { data, loading, called }] = useGcpGetTablesLazyQuery({
31-
variables: { projectID, datasetID },
32-
})
33-
34-
if (active && !called) getTables()
29+
const fetchBQTables= useFetchBQTables(projectID, datasetID)
3530

3631
const loadingPlaceholder = (
3732
<TreeItem
@@ -48,14 +43,14 @@ export const Dataset = ({
4843
/>
4944
)
5045

51-
const datasetContents = (contents: GcpGetTablesQuery['gcpGetTables']) =>
52-
contents?.map(({ name, type }) => (
46+
const datasetContents = (contents: any) =>
47+
contents?.map((it: any) => (
5348
<TreeItem
5449
className="MuiTreeView-leaf"
55-
slots={{ endIcon: DataproductTableIconMap[type]}}
56-
itemId={`${projectID}/${datasetID}/${name}`}
57-
key={`${projectID}/${datasetID}/${name}`}
58-
label={name}
50+
slots={{ endIcon: DataproductTableIconMap[it.type as string]}}
51+
itemId={`${projectID}/${datasetID}/${it.name}`}
52+
key={`${projectID}/${datasetID}/${it.name}`}
53+
label={it.name}
5954
/>
6055
))
6156

@@ -65,10 +60,10 @@ export const Dataset = ({
6560
itemId={`${projectID}/${datasetID}`}
6661
label={datasetID}
6762
>
68-
{loading
63+
{fetchBQTables.loading
6964
? loadingPlaceholder
70-
: data?.gcpGetTables?.length
71-
? datasetContents(data?.gcpGetTables)
65+
: fetchBQTables.bqTables?.length
66+
? datasetContents(fetchBQTables?.bqTables)
7267
: emptyPlaceholder}
7368
</TreeItem>
7469
)

0 commit comments

Comments
 (0)