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

Commit b595f77

Browse files
committed
client
1 parent 8c4e204 commit b595f77

13 files changed

+57
-351
lines changed

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/story/createStory.ts

-9
This file was deleted.

lib/queries/story/deleteInsightProduct.ts

-7
This file was deleted.

lib/queries/story/deleteStory.ts

-7
This file was deleted.

lib/queries/story/story.ts

-18
This file was deleted.

lib/queries/story/updateStory.ts

-27
This file was deleted.

lib/rest/insightProducts.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from "react"
2-
import { createInsightProductUrl, fetchTemplate, getInsightProductUrl, postTemplate, putTemplate, updateInsightProductUrl } from "./restApi"
2+
import { createInsightProductUrl, deleteTemplate, fetchTemplate, getInsightProductUrl, postTemplate, putTemplate, updateInsightProductUrl } from "./restApi"
33

44
const getInsightProduct = async (id: string) => {
55
const url = getInsightProductUrl(id)
@@ -39,4 +39,9 @@ export const createInsightProduct = async (insp: any) => {
3939
export const updateInsightProduct = async (id: string, insp: any) => {
4040
const url = updateInsightProductUrl(id)
4141
return putTemplate(url, insp).then((res)=>res.json())
42+
}
43+
44+
export const deleteInsightProduct= async (id: string) => {
45+
const url = updateInsightProductUrl(id)
46+
return deleteTemplate(url).then((res)=>res.json())
4247
}

lib/rest/restApi.ts

+5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ export const deleteDatasetUrl = (id: string) => `${apiUrl()}/datasets/${id}`
2121
export const updateDatasetUrl = (id: string) => `${apiUrl()}/datasets/${id}`
2222
export const getAccessiblePseudoDatasetsUrl = () => `${apiUrl()}/datasets/pseudo/accessible`
2323

24+
export const createStoryUrl = () => `${apiUrl()}/stories/new`
25+
export const updateStoryUrl = (id: string) => `${apiUrl()}/stories/${id}`
26+
export const deleteStoryUrl = (id: string) => `${apiUrl()}/stories/${id}`
27+
2428
export const getProductAreasUrl = () => `${apiUrl()}/productareas`
2529
export const getProductAreaUrl = (id: string) => `${apiUrl()}/productareas/${id}`
2630
export const fetchUserDataUrl = () => `${apiUrl()}/userData`
2731

2832
export const getInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}`
2933
export const createInsightProductUrl = () => `${apiUrl()}/insightProducts/new`
3034
export const updateInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}`
35+
export const deleteInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}`
3136

3237
export const fetchKeywordsUrl = () => `${apiUrl()}/keywords`
3338
export const updateKeywordsUrl = () => `${apiUrl()}/keywords`

lib/rest/stories.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { th } from "date-fns/locale"
2+
import { createStoryUrl, deleteTemplate, postTemplate, putTemplate, updateStoryUrl } from "./restApi"
3+
import { isThenable } from "next/dist/client/components/router-reducer/router-reducer-types"
4+
5+
export const createStory = (newStory: any, files: any[]) => {
6+
const formData = new FormData()
7+
files.forEach((file, idx) => {
8+
formData.append(`files[${idx}][path]`, file.path)
9+
formData.append(`files[${idx}[file]`, file.file)
10+
})
11+
formData.append('story', JSON.stringify(newStory))
12+
return postTemplate(createStoryUrl(), formData).then((res) => res.json())
13+
}
14+
15+
export const updateStory =(storyId: string, updatedStory: any) => {
16+
return putTemplate(updateStoryUrl(storyId), updatedStory).then((res) => res.json())
17+
}
18+
19+
export const deleteStory = (storyId: string) => {
20+
return deleteTemplate(updateStoryUrl(storyId), {isDeleted: true}).then((res) => res.json())
21+
}

0 commit comments

Comments
 (0)