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

Commit 334e52c

Browse files
authored
Merge pull request #299 from navikt/refactor/ART06-keywords-and-bugfix
bugfix and use keywordslist query
2 parents 7b95515 + bbbbd47 commit 334e52c

File tree

16 files changed

+52
-91
lines changed

16 files changed

+52
-91
lines changed

components/dataproducts/dataset/dataset.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface EntryProps {
3333
const Dataset = ({ datasetID, userInfo, isOwner, dataproduct }: EntryProps) => {
3434
const [edit, setEdit] = useState(false)
3535
const {dataset, loading, error} = useGetDataset(datasetID)
36-
const accessType = findAccessType(userInfo?.groups, dataset, isOwner)
36+
const accessType = findAccessType(userInfo?.googleGroups, dataset, isOwner)
3737

3838
if(loading || !dataset){
3939
return <LoaderSpinner></LoaderSpinner>

components/dataproducts/dataset/datasetSourceForm.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export const DataproductSourceForm = ({
3333
register('bigquery.dataset')
3434
register('bigquery.table')
3535

36-
const teamProjects = userInfo?.gcpProjects
37-
.filter((project: any) => project.group.email == team)
36+
const teamProjects = userInfo?.gcpProjects?.filter((project: any) => project.group.email == team)
3837
.map((group: any) => group.id)
3938

4039
const handleNodeSelect = (e: any, node: string) => {

components/dataproducts/newDataproductForm.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export const NewDataproductForm = () => {
166166
({ group }: { group: { name: string } }) => (
167167
<option
168168
value={
169-
userInfo?.groups.filter((g:any) => g.name === group.name)[0]
169+
userInfo?.googleGroups.filter((g:any) => g.Name === group.name)[0]
170170
.email
171171
}
172172
key={group.name}

components/header/user.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const backendHost = () => {
1010

1111
export default function User() {
1212
const userData = useContext(UserState)
13-
const userOfNada = userData?.googleGroups.find((gr: any) => gr.name === 'nada')
13+
const userOfNada = userData?.googleGroups.find((gr: any) => gr.Name === 'nada')
1414

1515
const router = useRouter()
1616
return userData ? (

components/insightProducts/newInsightProduct.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export const NewInsightProductForm = () => {
209209
({ group }: { group: { name: string } }) => (
210210
<option
211211
value={
212-
userData?.groups.filter((g: any) => g.name === group.name)[0]
212+
userData?.googleGroups.filter((g: any) => g.Name === group.name)[0]
213213
.email
214214
}
215215
key={group.name}

components/lib/tagsSelector.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useContext } from 'react'
44
import { ActionMeta, StylesConfig, ThemeConfig } from 'react-select'
55
import CreatableSelect from 'react-select/creatable'
66
import { UserState } from '../../lib/context'
7-
import { useKeywordsQuery } from '../../lib/schema/graphql'
87
import TagPill from './tagPill'
8+
import { fetchKeywords, useFetchKeywords } from '../../lib/rest/keywords'
99

1010
export interface TagsSelectorProps {
1111
onAdd: (value: string) => void
@@ -59,18 +59,17 @@ const theme: ThemeConfig = (t) => ({
5959
})
6060

6161
const useBuildTagOptionsList = () => {
62-
const { data, error } = useKeywordsQuery()
62+
const keywordsList = useFetchKeywords()
63+
6364
const userData = useContext(UserState)
6465
let tagsMap = new Map(
6566
userData?.dataproducts
6667
?.flatMap((it: any) => it.keywords)
6768
.map((it: any) => [it, 100000])
6869
)
69-
if (!error) {
70-
data?.keywords.forEach((it: any) =>
70+
keywordsList?.keywordItems.forEach((it: any) =>
7171
tagsMap.set(it.keyword, it.count + (tagsMap.get(it.keyword) || 0))
7272
)
73-
}
7473
return Array.from(tagsMap.entries())
7574
.sort((l: any, r: any) => r[1] - l[1])
7675
.map((it) => it[0])

components/stories/newStory.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export const NewStoryForm = () => {
268268
({ group }: { group: { name: string } }) => (
269269
<option
270270
value={
271-
userData?.groups.filter((g: any) => g.name === group.name)[0]
271+
userData?.googleGroups.filter((g: any) => g.Name === group.name)[0]
272272
.email
273273
}
274274
key={group.name}

lib/queries/keywords/keywords.ts

-10
This file was deleted.

lib/rest/keywords.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect, useState } from "react"
2+
import { fetchKeywordsUrl } from "./restApi"
3+
4+
export const fetchKeywords = async () => {
5+
const url = fetchKeywordsUrl()
6+
return fetch(url)
7+
}
8+
9+
export const useFetchKeywords = () => {
10+
const [keywordsList, setKeywordsList] = useState<any>(undefined)
11+
useEffect(() => {
12+
fetchKeywords().then((res) => res.json())
13+
.then((keywordsList) => {
14+
setKeywordsList(keywordsList)
15+
})
16+
.catch((err) => {
17+
setKeywordsList({
18+
keywordItems: [],
19+
})
20+
})
21+
}, [])
22+
return keywordsList
23+
}

lib/rest/restApi.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const getDatasetUrl = (id: string) => `${apiUrl()}/datasets/${id}`
1414
export const getProductAreasUrl = () => `${apiUrl()}/productareas`
1515
export const getProductAreaUrl = (id: string) => `${apiUrl()}/productareas/${id}`
1616
export const fetchUserDataUrl = () => `${apiUrl()}/userData`
17+
export const fetchKeywordsUrl = () => `${apiUrl()}/keywords`
1718
export const searchTeamKatalogenUrl = (gcpGroups?: string[]) => {
1819
const parameters = gcpGroups?.length ? gcpGroups.map(group => `gcpGroups=${encodeURIComponent(group)}`).join('&') : ''
1920
const query = parameters ? `?${parameters}` : ''

lib/schema/graphql.ts

-45
Original file line numberDiff line numberDiff line change
@@ -1386,11 +1386,6 @@ export type InsightProductQueryVariables = Exact<{
13861386

13871387
export type InsightProductQuery = { __typename?: 'Query', insightProduct: { __typename?: 'InsightProduct', id: string, name: string, description: string, created: any, lastModified?: any | null, type: string, link: string, keywords: Array<string>, group: string, teamkatalogenURL?: string | null, productAreaID?: string | null, teamID?: string | null } };
13881388

1389-
export type KeywordsQueryVariables = Exact<{ [key: string]: never; }>;
1390-
1391-
1392-
export type KeywordsQuery = { __typename?: 'Query', keywords: Array<{ __typename?: 'Keyword', keyword: string, count: number }> };
1393-
13941389
export type UpdateKeywordsMutationVariables = Exact<{
13951390
input: UpdateKeywords;
13961391
}>;
@@ -2579,46 +2574,6 @@ export type InsightProductQueryHookResult = ReturnType<typeof useInsightProductQ
25792574
export type InsightProductLazyQueryHookResult = ReturnType<typeof useInsightProductLazyQuery>;
25802575
export type InsightProductSuspenseQueryHookResult = ReturnType<typeof useInsightProductSuspenseQuery>;
25812576
export type InsightProductQueryResult = Apollo.QueryResult<InsightProductQuery, InsightProductQueryVariables>;
2582-
export const KeywordsDocument = gql`
2583-
query Keywords {
2584-
keywords {
2585-
keyword
2586-
count
2587-
}
2588-
}
2589-
`;
2590-
2591-
/**
2592-
* __useKeywordsQuery__
2593-
*
2594-
* To run a query within a React component, call `useKeywordsQuery` and pass it any options that fit your needs.
2595-
* When your component renders, `useKeywordsQuery` returns an object from Apollo Client that contains loading, error, and data properties
2596-
* you can use to render your UI.
2597-
*
2598-
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
2599-
*
2600-
* @example
2601-
* const { data, loading, error } = useKeywordsQuery({
2602-
* variables: {
2603-
* },
2604-
* });
2605-
*/
2606-
export function useKeywordsQuery(baseOptions?: Apollo.QueryHookOptions<KeywordsQuery, KeywordsQueryVariables>) {
2607-
const options = {...defaultOptions, ...baseOptions}
2608-
return Apollo.useQuery<KeywordsQuery, KeywordsQueryVariables>(KeywordsDocument, options);
2609-
}
2610-
export function useKeywordsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<KeywordsQuery, KeywordsQueryVariables>) {
2611-
const options = {...defaultOptions, ...baseOptions}
2612-
return Apollo.useLazyQuery<KeywordsQuery, KeywordsQueryVariables>(KeywordsDocument, options);
2613-
}
2614-
export function useKeywordsSuspenseQuery(baseOptions?: Apollo.SuspenseQueryHookOptions<KeywordsQuery, KeywordsQueryVariables>) {
2615-
const options = {...defaultOptions, ...baseOptions}
2616-
return Apollo.useSuspenseQuery<KeywordsQuery, KeywordsQueryVariables>(KeywordsDocument, options);
2617-
}
2618-
export type KeywordsQueryHookResult = ReturnType<typeof useKeywordsQuery>;
2619-
export type KeywordsLazyQueryHookResult = ReturnType<typeof useKeywordsLazyQuery>;
2620-
export type KeywordsSuspenseQueryHookResult = ReturnType<typeof useKeywordsSuspenseQuery>;
2621-
export type KeywordsQueryResult = Apollo.QueryResult<KeywordsQuery, KeywordsQueryVariables>;
26222577
export const UpdateKeywordsDocument = gql`
26232578
mutation updateKeywords($input: UpdateKeywords!) {
26242579
updateKeywords(input: $input)

pages/admin/tags/index.tsx

+7-8
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import InnerContainer from '../../../components/lib/innerContainer'
2222
import LoaderSpinner from '../../../components/lib/spinner'
2323
import TagPill from '../../../components/lib/tagPill'
2424
import {
25-
useKeywordsQuery,
2625
useUpdateKeywordsMutation,
2726
} from '../../../lib/schema/graphql'
2827
import { useFetchUserData } from '../../../lib/rest/userData'
28+
import { useFetchKeywords } from '../../../lib/rest/keywords'
2929

3030
const TagsCleaner = () => {
31-
const kw = useKeywordsQuery()
31+
const kw = useFetchKeywords()
3232
const [tagsInUse, setTagsInUse] = useState([] as string[])
3333
const [tagsObsolete, setTagsObsolete] = useState([] as string[])
3434
const [checkStatement1, setCheckStatement1] = useState(false)
@@ -51,15 +51,15 @@ const TagsCleaner = () => {
5151
return <InnerContainer>Failed to fetch user information</InnerContainer>
5252
}
5353

54-
const notMemberOfNada = !userData.data.userInfo.groups.find(
55-
(g: any) => g.name === 'nada'
54+
const notMemberOfNada = !userData.data.googleGroups.find(
55+
(g: any) => g.Name === 'nada'
5656
)
5757
if (notMemberOfNada) {
5858
return <InnerContainer>Permission denied</InnerContainer>
5959
}
6060

61-
if (!tagsInUse.length && !tagsObsolete.length && !kw.loading && !!kw.data) {
62-
setTagsInUse(kw.data.keywords.map((it) => it.keyword))
61+
if (!tagsInUse.length && !tagsObsolete.length && kw?.keywordItems) {
62+
setTagsInUse(kw.keywordItems.map((it:any) => it.keyword))
6363
}
6464

6565
const ToggleTag = (tag: string) => {
@@ -181,8 +181,7 @@ const TagsCleaner = () => {
181181
<Heading className="mt-2" spacing level="1" size="medium">
182182
Tags Cleanup
183183
</Heading>
184-
{kw.loading && <LoaderSpinner></LoaderSpinner>}
185-
{!kw.loading && !!kw.data && (
184+
{!!kw?.keywordItems && (
186185
<div>
187186
<Alert variant="info">
188187
Click tags below to move them between left and right panel.

pages/dataproduct/[id]/[slug]/[[...page]].tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ const Dataproduct = () => {
3636
const userInfo = useContext(UserState)
3737

3838
const isOwner =
39-
userInfo?.groups === undefined
39+
!userInfo?.googleGroups
4040
? false
41-
: userInfo.groups.some(
42-
(g: Group) => g.email === dataproduct?.owner.group
41+
: userInfo.googleGroups.some(
42+
(g: any) => g.Email === dataproduct?.owner.group
4343
)
4444

4545
useEffect(() => {

pages/dataproduct/[id]/[slug]/edit.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ const DataproductEdit = () => {
4949
if (loading || !dataproduct) return <LoaderSpinner />
5050

5151
const isOwner =
52-
userInfo?.groups === undefined
52+
!userInfo?.googleGroups
5353
? false
54-
: userInfo.groups.some(
55-
(g: Group) => g.email === dataproduct?.owner?.group
54+
: userInfo.googleGroups.some(
55+
(g: any) => g.Email === dataproduct?.owner?.group
5656
)
5757

5858
const onDelete = async () => {

pages/index.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { GetServerSideProps } from 'next'
22
import { addApolloState, initializeApollo } from '../lib/apollo'
33
import {
4-
KeywordsDocument,
54
MetabaseProudctsDocument,
65
} from '../lib/schema/graphql'
76
import { useRouter } from 'next/router'
@@ -137,9 +136,6 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
137136
await apolloClient.query({
138137
query: MetabaseProudctsDocument,
139138
})
140-
await apolloClient.query({
141-
query: KeywordsDocument,
142-
})
143139

144140
return addApolloState(apolloClient, {
145141
props: {},

pages/search.tsx

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import { SearchPanel } from '../components/search/searchPanel'
1010

1111
import {
1212
Exact,
13-
KeywordsQuery,
1413
SearchType,
15-
useKeywordsQuery,
1614
} from '../lib/schema/graphql'
1715
import { useGetProductAreas } from '../lib/rest/productAreas'
1816
import { useSearch } from '../lib/rest/search'
17+
import { useFetchKeywords } from '../lib/rest/keywords'
1918

2019
export interface SearchParam {
2120
[s: string]: string | string[]
@@ -118,14 +117,14 @@ const buildProductAreaFiltersTree = (
118117
)
119118

120119
const buildKeywordsFiltersTree = (
121-
queryResult: QueryResult<KeywordsQuery, Exact<{ [key: string]: never }>>,
120+
keywordsList: any,
122121
pickedFilters: string[]
123122
) => {
124-
return queryResult.loading || !queryResult.data
123+
return !keywordsList?.keywordItems
125124
? ({} as FilterTreeNode)
126125
: (Object.fromEntries(
127126
new Map(
128-
queryResult.data.keywords.map((it) => [
127+
keywordsList.keywordItems.map((it: any) => [
129128
`${it.keyword} (${it.count})`,
130129
!!pickedFilters.find((f) => `${it.keyword} (${it.count})` === f),
131130
])
@@ -156,7 +155,7 @@ export type FilterType = 'Områder' | 'Nøkkelord'
156155

157156
const Search = () => {
158157
const {productAreas, loading, error} = useGetProductAreas()
159-
const kw = useKeywordsQuery()
158+
const kw = useFetchKeywords()
160159
const [teamNameToID, teamIDToName] = loading || error? [new Map, new Map]: buildTeamIDMaps(productAreas)
161160

162161
const router = useRouter()

0 commit comments

Comments
 (0)