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

Commit de0b286

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/follow-redirects-1.15.6
2 parents 43a7d09 + 55a270e commit de0b286

File tree

14 files changed

+126
-392
lines changed

14 files changed

+126
-392
lines changed

components/lib/MoveModal.tsx

+4-12
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import {
55
Heading,
66
Select,
77
} from '@navikt/ds-react'
8-
import {
9-
SearchType,
10-
useSearchContentWithOptionsQuery,
11-
} from '../../lib/schema/graphql'
128
import LoaderSpinner from './spinner'
139
import { useState } from 'react'
1410
import ErrorMessage from './error'
11+
import { useSearch } from '../../lib/rest/search'
1512

1613
interface MoveModalProps {
1714
open: boolean
@@ -31,15 +28,10 @@ export const MoveModal = ({
3128
undefined
3229
)
3330
const [error, setError] = useState<string | undefined>(undefined)
34-
const search = useSearchContentWithOptionsQuery({
35-
variables: {
36-
options: { types: ['dataproduct'] as SearchType[], groups: [group] },
37-
},
38-
fetchPolicy: 'network-only',
39-
})
31+
const search = useSearch({ types: ['dataproduct'], groups: [group] })
4032

4133
if (search.error) return <ErrorMessage error={search.error} />
42-
if (search.loading || !search.data?.search) return <LoaderSpinner />
34+
if (search.loading || !search.data?.results) return <LoaderSpinner />
4335

4436
return (
4537
<Modal open={open} onClose={onCancel} header={{heading: "Flytt datasett"}}>
@@ -56,7 +48,7 @@ export const MoveModal = ({
5648
onChange={(event) => setDataproductID(event.target.value)}
5749
>
5850
<option value="">Velg dataprodukt</option>
59-
{search.data?.search?.map(
51+
{search.data?.results?.map(
6052
(dp) =>
6153
currentDataproductID !== dp.result.id && (
6254
<option value={dp.result.id} key={dp.result.id}>

components/search/resultList.tsx

+12-34
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { QueryResult } from '@apollo/client'
2-
import {
3-
Exact,
4-
SearchContentWithOptionsQuery,
5-
SearchOptions,
6-
useDeleteStoryMutation,
7-
} from '../../lib/schema/graphql'
82
import ErrorMessage from '../lib/error'
93
import LoaderSpinner from '../lib/spinner'
104
import SearchResultLink from './searchResultLink'
@@ -15,16 +9,15 @@ import { USER_INFO } from '../../lib/queries/userInfo/userInfo'
159
import { UserState } from '../../lib/context'
1610
import { useSearchTeamKatalogen } from '../../lib/rest/teamkatalogen'
1711
import { useGetProductAreas } from '../../lib/rest/productAreas'
12+
import { SearchResult } from '../../lib/rest/search'
13+
import { useDeleteStoryMutation } from '../../lib/schema/graphql'
1814

1915
const Results = ({ children }: { children: React.ReactNode }) => (
2016
<div className="results">{children}</div>
2117
)
2218

2319
type ResultListInterface = {
24-
search?: QueryResult<
25-
SearchContentWithOptionsQuery,
26-
Exact<{ options: SearchOptions }>
27-
>
20+
search?: {data: SearchResult|undefined, loading: boolean, error: any}
2821
dataproducts?: {
2922
__typename?: 'Dataproduct' | undefined
3023
id: string
@@ -64,23 +57,6 @@ const ResultList = ({
6457
searchParam,
6558
updateQuery,
6659
}: ResultListInterface) => {
67-
useEffect(() => {
68-
if (!!searchParam) {
69-
if (
70-
search?.data?.search.filter(
71-
(d) => d.result.__typename === 'Dataproduct'
72-
).length == 0
73-
) {
74-
updateQuery?.({ ...searchParam, preferredType: 'story' })
75-
} else if (
76-
search?.data?.search.filter((d) => d.result.__typename === 'Story')
77-
.length == 0
78-
) {
79-
updateQuery?.({ ...searchParam, preferredType: 'dataproduct' })
80-
}
81-
}
82-
// eslint-disable-next-line react-hooks/exhaustive-deps
83-
}, [search])
8460
const [deleteStoryQuery] = useDeleteStoryMutation()
8561
const userInfo = useContext(UserState)
8662
const { searchResult: teamkatalogen } = useSearchTeamKatalogen()
@@ -96,6 +72,8 @@ const ResultList = ({
9672
]
9773
})
9874

75+
const isDataProduct = (item: any) => !!item.datasets
76+
9977
const getTeamKatalogenInfo = (item: any) => {
10078
const getTeamID = (url: string) => {
10179
var urlComponents = url?.split("/")
@@ -115,11 +93,11 @@ const ResultList = ({
11593

11694
if (error) return <ErrorMessage error={error} />
11795
if (loading || !data) return <LoaderSpinner />
118-
const dataproducts = data.search.filter(
119-
(d) => d.result.__typename === 'Dataproduct'
96+
const dataproducts = data.results.filter(
97+
(d) => isDataProduct(d.result)
12098
)
121-
const datastories = data.search.filter(
122-
(d) => d.result.__typename === 'Story'
99+
const datastories = data.results.filter(
100+
(d) => !isDataProduct(d.result)
123101
)
124102

125103
//dataproducts.forEach((d) => console.log(getTeamKatalogenInfo(d.result)?.teamkatalogenTeam))
@@ -147,7 +125,7 @@ const ResultList = ({
147125
{datastories.map(
148126
(it, idx) =>
149127
(
150-
it.result.__typename === 'Story' && (
128+
!isDataProduct(it.result) && (
151129
<SearchResultLink
152130
key={idx}
153131
name={it.result.name}
@@ -169,7 +147,7 @@ const ResultList = ({
169147
<Tabs.Panel className="flex flex-col gap-4" value="dataproduct">
170148
{dataproducts.map(
171149
(d, idx) =>
172-
d.result.__typename === 'Dataproduct' && (
150+
isDataProduct(d.result) && (
173151
<SearchResultLink
174152
key={idx}
175153
group={d.result.owner}
@@ -184,7 +162,7 @@ const ResultList = ({
184162
)}
185163
</Tabs.Panel>
186164
</Tabs>
187-
{data.search.length == 0 && 'ingen resultater'}
165+
{data.results.length == 0 && 'ingen resultater'}
188166
</Results>
189167
)
190168
}

components/search/searchResultLink.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ export interface SearchResultProps {
2727
description?: string
2828
datasets?: {
2929
name: string
30-
datasource: {
31-
lastModified: string,
32-
}
30+
dataSourceLastModified: string
3331
}[]
3432
teamkatalogenTeam?: string,
3533
productArea?: string,
@@ -140,7 +138,7 @@ export const SearchResultLink = ({
140138
{datasets.map((ds, index) => (
141139
<div key={index}>
142140
<p dangerouslySetInnerHTML={{ __html: ds.name.replaceAll("_", "_<wbr>") }} />
143-
<Detail className="text-text-subtle">Sist oppdatert: {humanizeDate(ds.datasource.lastModified)}</Detail>
141+
<Detail className="text-text-subtle">Sist oppdatert: {humanizeDate(ds.dataSourceLastModified)}</Detail>
144142
</div>
145143
))}
146144
</div>

lib/queries/search/resultCount.tsx

-10
This file was deleted.

lib/queries/search/searchResult.ts

-35
This file was deleted.

lib/queries/search/searchResultWithOptions.ts

-48
This file was deleted.

lib/rest/restApi.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
1+
import { SearchOptions } from "./search"
2+
13
const isServer = typeof window === 'undefined'
24

35
export const apiUrl = () => {
4-
if (process.env.NEXT_PUBLIC_ENV === 'development') {
5-
return 'http://localhost:8080/api'
6-
}
7-
return isServer ? 'http://nada-backend/api' : '/api'
6+
if (process.env.NEXT_PUBLIC_ENV === 'development') {
7+
return 'http://localhost:8080/api'
8+
}
9+
return isServer ? 'http://nada-backend/api' : '/api'
810
}
911

1012
export const getDataproductUrl = (id: string) => `${apiUrl()}/dataproducts/${id}`
1113
export const getDatasetUrl = (id: string) => `${apiUrl()}/datasets/${id}`
1214
export const getProductAreasUrl = () => `${apiUrl()}/productareas`
1315
export const getProductAreaUrl = (id: string) => `${apiUrl()}/productareas/${id}`
14-
export const searchTeamKatalogenUrl = (gcpGroups?: string[]) =>
15-
{
16-
const parameters = gcpGroups?.length ? gcpGroups.map(group => `gcpGroups=${encodeURIComponent(group)}`).join('&'): ''
16+
export const searchTeamKatalogenUrl = (gcpGroups?: string[]) => {
17+
const parameters = gcpGroups?.length ? gcpGroups.map(group => `gcpGroups=${encodeURIComponent(group)}`).join('&') : ''
1718
const query = parameters ? `?${parameters}` : ''
1819
return `${apiUrl()}/teamkatalogen${query}`
1920
}
2021

22+
export const searchUrl = (options: SearchOptions) => {
23+
let queryParams: string[] = [];
24+
25+
// Helper function to add array-based options
26+
const addArrayOptions = (optionArray: string[] | undefined, paramName: string) => {
27+
if (optionArray && optionArray.length) {
28+
queryParams.push(`${paramName}=${optionArray.reduce((s, p) => `${s ? `${s},` : ""}${encodeURIComponent(p)}`)}`)
29+
}
30+
};
31+
32+
// Adding array-based options
33+
addArrayOptions(options.keywords, 'keywords');
34+
addArrayOptions(options.groups, 'groups');
35+
addArrayOptions(options.teamIDs, 'teamIDs');
36+
addArrayOptions(options.services, 'services');
37+
addArrayOptions(options.types, 'types');
38+
39+
// Adding single-value options
40+
if (options.text) queryParams.push(`text=${encodeURIComponent(options.text)}`);
41+
if (options.limit !== undefined) queryParams.push(`limit=${options.limit}`);
42+
if (options.offset !== undefined) queryParams.push(`offset=${options.offset}`);
43+
44+
const query = queryParams.length ? `?${queryParams.join('&')}` : '';
45+
return `${apiUrl()}/search${query}`;
46+
};
2147

lib/rest/search.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useEffect, useState } from "react"
2+
import { searchUrl } from "./restApi"
3+
4+
export interface SearchOptions {
5+
// Freetext search
6+
text?: string
7+
// Filter on keyword
8+
keywords?: string[]
9+
// Filter on group
10+
groups?: string[]
11+
//Filter on team_id
12+
teamIDs?: string[]
13+
// Filter on enabled services
14+
services?: string[]
15+
// Filter on types
16+
types?: string[]
17+
18+
limit?: number
19+
offset?: number
20+
}
21+
22+
export interface SearchResult{
23+
results: any[]
24+
}
25+
26+
const search = async (o: SearchOptions)=>{
27+
const url = searchUrl(o);
28+
const options = {
29+
method: 'GET',
30+
headers: {
31+
'Content-Type': 'application/json',
32+
},
33+
}
34+
return fetch(url, options)
35+
}
36+
37+
export const useSearch = (o: SearchOptions)=>{
38+
const [data, setData] = useState<SearchResult|undefined>(undefined)
39+
const [loading, setLoading] = useState(false)
40+
const [error, setError] = useState(null)
41+
42+
43+
useEffect(()=>{
44+
search(o).then((res)=> res.json())
45+
.then((data)=>
46+
{
47+
setError(null)
48+
setData(data)
49+
})
50+
.catch((err)=>{
51+
setError(err)
52+
setData(undefined)
53+
}).finally(()=>{
54+
setLoading(false)
55+
})
56+
}, [o?JSON.stringify(o):""])
57+
58+
return {data, loading, error}
59+
}

0 commit comments

Comments
 (0)