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

Commit c7c80e2

Browse files
authored
Merge pull request #326 from navikt/refactor/ART12-insightproduct-and-story
Refactor/art12 insightproduct and story
2 parents 91e8ad0 + b595f77 commit c7c80e2

20 files changed

+122
-658
lines changed

components/insightProducts/editInsightProduct.tsx

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as yup from 'yup'
1717
import { useContext, useState } from 'react'
1818
import TagsSelector from '../lib/tagsSelector'
1919
import { UserState } from "../../lib/context";
20-
import { useUpdateInsightProductMetadataMutation } from '../../lib/schema/graphql'
20+
import { updateInsightProduct } from '../../lib/rest/insightProducts'
2121

2222
const schema = yup.object().shape({
2323
name: yup.string().nullable().required('Skriv inn navnet på innsiktsproduktet'),
@@ -50,8 +50,9 @@ export const EditInsightProductMetadataForm = ({ id, name, description, type, li
5050
const [teamID, setTeamID] = useState<string>('')
5151
const userInfo = useContext(UserState)
5252
const [isPrivacyCheckboxChecked, setIsPrivacyCheckboxChecked] = useState(false)
53+
const [error, setError] = useState<Error | undefined>(undefined)
54+
const [loading, setLoading] = useState(false)
5355

54-
const [updateInsightProductQuery, { loading, error }] = useUpdateInsightProductMetadataMutation()
5556
const {
5657
register,
5758
handleSubmit,
@@ -95,8 +96,6 @@ export const EditInsightProductMetadataForm = ({ id, name, description, type, li
9596

9697
const onSubmit = async (data: any) => {
9798
const editInsightProductData = {
98-
variables: {
99-
id: id,
10099
name: data.name,
101100
description: data.description,
102101
type: data.type,
@@ -106,17 +105,21 @@ export const EditInsightProductMetadataForm = ({ id, name, description, type, li
106105
productAreaID: productAreaID,
107106
teamID: teamID,
108107
group: data.group,
109-
},
110108
}
111109

112-
updateInsightProductQuery(editInsightProductData).then(() => {
110+
setLoading(true)
111+
updateInsightProduct(id, editInsightProductData).then(() => {
112+
setError(undefined)
113113
amplitudeLog('skjema fullført', { skjemanavn: 'endre-innsiktsprodukt' })
114114
router.back()
115115
}).catch(e => {
116116
console.log(e)
117+
setError(e)
117118
amplitudeLog('skjemainnsending feilet', {
118119
skjemanavn: 'endre-innsiktsprodukt',
119120
})
121+
}).finally(() => {
122+
setLoading(false)
120123
})
121124
}
122125

components/insightProducts/newInsightProduct.tsx

+6-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as yup from 'yup'
1717
import { ChangeEvent, useContext, useState } from 'react'
1818
import TagsSelector from '../lib/tagsSelector'
1919
import { UserState } from "../../lib/context";
20-
import { CREATE_INSIGHT_PRODUCT } from '../../lib/queries/insightProducts/createInsightProduct'
20+
import { createInsightProduct } from '../../lib/rest/insightProducts'
2121

2222
const schema = yup.object().shape({
2323
name: yup.string().nullable().required('Skriv inn navnet på innsiktsproduktet'),
@@ -28,7 +28,7 @@ const schema = yup.object().shape({
2828
link: yup
2929
.string()
3030
.required('Du må legge til en lenke til innsiktsproduktet')
31-
.url('Lenken må være en gyldig URL'), // Add this line to validate the link as a URL type: yup.string().required('Du må velge en type for innsiktsproduktet'),
31+
.url('Lenken må være en gyldig URL, fks. https://valid.url.to.page'), // Add this line to validate the link as a URL type: yup.string().required('Du må velge en type for innsiktsproduktet'),
3232
group: yup.string().required('Du må skrive inn en gruppe for innsiktsproduktet')
3333
})
3434

@@ -48,6 +48,7 @@ export const NewInsightProductForm = () => {
4848
const [teamID, setTeamID] = useState<string>('')
4949
const userData = useContext(UserState)
5050
const [isPrivacyCheckboxChecked, setIsPrivacyCheckboxChecked] = useState(false)
51+
const [backendError, setBackendError] = useState<Error | undefined>(undefined)
5152

5253
const handlePrivacyCheckboxChange = () => {
5354
setIsPrivacyCheckboxChecked(!isPrivacyCheckboxChecked)
@@ -93,8 +94,6 @@ export const NewInsightProductForm = () => {
9394

9495
const onSubmit = async (data: any) => {
9596
const inputData = {
96-
variables: {
97-
input: {
9897
name: data.name,
9998
description: valueOrNull(data.description),
10099
keywords: data.keywords,
@@ -104,15 +103,15 @@ export const NewInsightProductForm = () => {
104103
link: data.link,
105104
type: data.type,
106105
group: data.group,
107-
},
108-
},
109-
refetchQueries: ['searchContent'],
110106
}
111107

112108
try {
113109
await createInsightProduct(inputData)
110+
setBackendError(undefined)
114111
amplitudeLog('skjema fullført', { skjemanavn: 'ny-innsiktsprodukt' })
112+
router.push('/user/insightProducts')
115113
} catch (e) {
114+
setBackendError(new Error('Internal server error'))
116115
amplitudeLog('skjemainnsending feilet', {
117116
skjemanavn: 'ny-innsiktsprodukt',
118117
})
@@ -121,15 +120,6 @@ export const NewInsightProductForm = () => {
121120

122121
}
123122

124-
const [createInsightProduct, { loading, error: backendError }] = useMutation(
125-
CREATE_INSIGHT_PRODUCT,
126-
{
127-
onCompleted: (data) => {
128-
router.push("/")
129-
},
130-
}
131-
)
132-
133123
const onCancel = () => {
134124
amplitudeLog(
135125
'Klikker på: Avbryt',

components/search/resultList.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { UserState } from '../../lib/context'
88
import { useSearchTeamKatalogen } from '../../lib/rest/teamkatalogen'
99
import { useGetProductAreas } from '../../lib/rest/productAreas'
1010
import { SearchResult } from '../../lib/rest/search'
11-
import { useDeleteStoryMutation } from '../../lib/schema/graphql'
11+
import { deleteStory } from '../../lib/rest/stories'
1212

1313
const Results = ({ children }: { children: React.ReactNode }) => (
1414
<div className="results">{children}</div>
@@ -58,15 +58,9 @@ const ResultList = ({
5858
searchParam,
5959
updateQuery,
6060
}: ResultListInterface) => {
61-
const [deleteStoryQuery] = useDeleteStoryMutation()
6261
const userInfo = useContext(UserState)
6362
const { searchResult: teamkatalogen } = useSearchTeamKatalogen()
6463
const { productAreas } = useGetProductAreas()
65-
const deleteStory = (id: string) => deleteStoryQuery({
66-
variables: {
67-
id: id
68-
},
69-
})
7064

7165
const isDataProduct = (item: any) => !!item.datasets
7266

@@ -197,7 +191,7 @@ const ResultList = ({
197191
keywords={s.keywords}
198192
editable={true}
199193
description={s.description}
200-
deleteResource={deleteStory}
194+
deleteResource={()=> deleteStory(s.id)}
201195
/>
202196
))}
203197
</Results>

components/search/searchResultLink.tsx

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import ReactMarkdown from 'react-markdown'
44
import { PluggableList } from 'react-markdown/lib'
55
import remarkGfm from 'remark-gfm'
66
import { CoApplicant, Table } from '@navikt/ds-icons'
7-
import { useDeleteInsightProductMutation, useDeleteStoryMutation } from '../../lib/schema/graphql'
87
import humanizeDate from '../../lib/humanizeDate'
98
import DeleteModal from '../lib/deleteModal'
109
import { useState } from 'react'
1110
import { useRouter } from 'next/router'
1211
import TagPill from '../lib/tagPill'
12+
import { deleteInsightProduct } from '../../lib/rest/insightProducts'
1313

1414
export interface SearchResultProps {
1515
resourceType?: string
@@ -55,7 +55,6 @@ export const SearchResultLink = ({
5555
const owner = teamkatalogenTeam || group?.group
5656
const router = useRouter();
5757
const [error, setError] = useState<string | undefined>(undefined)
58-
const [deleteInsightProductMutation] = useDeleteInsightProductMutation();
5958

6059
const editResource = () => {
6160
if (resourceType == 'datafortelling') {
@@ -66,14 +65,6 @@ export const SearchResultLink = ({
6665
}
6766
const openDeleteModal = () => setModal(true)
6867

69-
const deleteInsightProduct = (id: string)=>{
70-
return deleteInsightProductMutation({
71-
variables:{
72-
id: id
73-
},
74-
})
75-
}
76-
7768
const confirmDelete = () => {
7869
const deletePromise = resourceType == "innsiktsprodukt"?
7970
deleteInsightProduct(id || ''):

components/stories/editStoryMetadata.tsx

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as yup from 'yup'
1515
import { useContext, useState } from 'react'
1616
import TagsSelector from '../lib/tagsSelector'
1717
import {UserState} from "../../lib/context";
18-
import { useUpdateStoryMetadataMutation } from '../../lib/schema/graphql'
18+
import { updateStory } from '../../lib/rest/stories'
1919

2020
const schema = yup.object().shape({
2121
name: yup.string().nullable().required('Skriv inn navnet på datafortellingen'),
@@ -40,7 +40,8 @@ export const EditStoryMetadataForm = ({id, name, description, keywords, teamkata
4040
const [productAreaID, setProductAreaID] = useState<string>('')
4141
const [teamID, setTeamID] = useState<string>('')
4242
const userInfo = useContext(UserState)
43-
const [updateStoryQuery, {loading, error}] = useUpdateStoryMetadataMutation()
43+
const [error, setError] = useState<Error | undefined>(undefined)
44+
const [loading, setLoading] = useState(false)
4445
const {
4546
register,
4647
handleSubmit,
@@ -77,26 +78,28 @@ export const EditStoryMetadataForm = ({id, name, description, keywords, teamkata
7778

7879
const onSubmit = async (data: any) => {
7980
const editStoryData = {
80-
variables: {
81-
id: id,
8281
name: data.name,
8382
description: data.description,
8483
keywords: data.keywords,
8584
teamkatalogenURL: data.teamkatalogenURL,
8685
productAreaID: productAreaID,
8786
teamID: teamID,
8887
group: data.group,
89-
},
9088
}
9189

92-
updateStoryQuery(editStoryData).then(()=>{
90+
setLoading(true)
91+
updateStory(id, editStoryData).then(()=>{
92+
setError(undefined)
9393
amplitudeLog('skjema fullført', { skjemanavn: 'endre-datafortelling' })
9494
router.push("/user/stories")
9595
}).catch(e=>{
96+
setError(e)
9697
console.log(e)
9798
amplitudeLog('skjemainnsending feilet', {
9899
skjemanavn: 'endre-datafortelling',
99100
})
101+
}).finally(()=>{
102+
setLoading(false)
100103
})
101104
}
102105

components/stories/newStory.tsx

+13-21
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import * as yup from 'yup';
1111
import { ChangeEvent, useContext, useRef, useState } from 'react';
1212
import TagsSelector from '../lib/tagsSelector';
1313
import { UserState } from '../../lib/context';
14-
import { CREATE_STORY } from '../../lib/queries/story/createStory';
1514
import { TreeItem } from '@mui/x-tree-view/TreeItem';
1615
import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView';
1716
import { FileTextFillIcon, FolderFillIcon, TrashIcon } from '@navikt/aksel-icons';
1817
import { UploadFile } from '../../lib/schema/graphql';
18+
import { createStory } from '../../lib/rest/stories';
19+
import { set } from 'lodash';
1920

2021
const schema = yup.object().shape({
2122
name: yup.string().nullable().required('Skriv inn navnet på datafortellingen'),
@@ -41,6 +42,7 @@ export const NewStoryForm = () => {
4142
const [storyFiles, setStoryFiles] = useState<File[]>([]);
4243
const singleFileInputRef = useRef(null);
4344
const folderFileInputRef = useRef(null);
45+
const [error, setError] = useState<Error | undefined>(undefined);
4446

4547
const handleSingleFileClick = () => {
4648
/* @ts-expect-error */
@@ -88,44 +90,34 @@ export const NewStoryForm = () => {
8890
const valueOrNull = (val: string) => (val == '' ? null : val);
8991

9092
const onSubmit = async (data: any) => {
91-
const uploadData = {
92-
variables: {
93-
files: storyFiles.map<UploadFile>(it=>({
93+
const files= storyFiles.map<UploadFile>(it=>({
9494
path: fixRelativePath(it),
9595
file: it,
96-
})),
97-
input: {
96+
}))
97+
const storyInput= {
9898
name: data.name,
9999
description: valueOrNull(data.description),
100100
keywords: data.keywords,
101101
teamkatalogenURL: data.teamkatalogenURL,
102102
productAreaID: productAreaID,
103103
teamID: teamID,
104104
group: data.group,
105-
},
106-
},
107-
refetchQueries: ['searchContent', 'userInfoDetails'],
108-
};
105+
}
109106

110107
try {
111-
await createStory(uploadData);
108+
const data = await createStory(storyInput, files);
109+
setError(undefined);
112110
amplitudeLog('skjema fullført', { skjemanavn: 'ny-datafortelling' });
111+
router.push(`/story/${data.id}`);
113112
} catch (e) {
113+
setError(e as Error);
114114
amplitudeLog('skjemainnsending feilet', {
115115
skjemanavn: 'ny-datafortelling',
116116
})
117117
console.log(e)
118118
}
119119
}
120-
121-
const [createStory, { loading, error: backendError }] = useMutation(
122-
CREATE_STORY,
123-
{
124-
onCompleted: (data) => {
125-
router.push(`/story/${data.createStory.id}`);
126-
},
127-
},
128-
)
120+
129121

130122
const onCancel = () => {
131123
amplitudeLog(
@@ -316,7 +308,7 @@ export const NewStoryForm = () => {
316308
{renderTree(generateFileTree(storyFiles))}
317309
</SimpleTreeView>
318310
)}
319-
{backendError && <ErrorMessage error={backendError} />}
311+
{error && <ErrorMessage error={error} />}
320312
<div className="flex flex-row gap-4 mb-16">
321313
<Button type="button" variant="secondary" onClick={onCancel}>
322314
Avbryt

lib/queries/groupStats/groupStats.ts

-10
This file was deleted.

lib/queries/insightProducts/createInsightProduct.ts

-9
This file was deleted.

lib/queries/insightProducts/editInsightProduct.ts

-31
This file was deleted.

0 commit comments

Comments
 (0)