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

Commit 8c4e204

Browse files
committed
insight product
1 parent 91e8ad0 commit 8c4e204

File tree

10 files changed

+66
-308
lines changed

10 files changed

+66
-308
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',

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.

lib/queries/insightProducts/insightProduct.ts

-20
This file was deleted.

lib/rest/insightProducts.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useEffect, useState } from "react"
2+
import { createInsightProductUrl, fetchTemplate, getInsightProductUrl, postTemplate, putTemplate, updateInsightProductUrl } from "./restApi"
3+
4+
const getInsightProduct = async (id: string) => {
5+
const url = getInsightProductUrl(id)
6+
return fetchTemplate(url)
7+
}
8+
9+
export const useGetInsightProduct = (id: string)=>{
10+
const [insightProduct, setInsightProduct] = useState<any>(null)
11+
const [loading, setLoading] = useState(false)
12+
const [error, setError] = useState<Error| undefined>(undefined)
13+
14+
15+
useEffect(()=>{
16+
if(!id) return
17+
getInsightProduct(id).then((res)=> res.json())
18+
.then((data)=>
19+
{
20+
setError(undefined)
21+
setInsightProduct(data)
22+
})
23+
.catch((err)=>{
24+
setError(err)
25+
setInsightProduct(null)
26+
}).finally(()=>{
27+
setLoading(false)
28+
})
29+
}, [id])
30+
31+
return {insightProduct, loading, error}
32+
}
33+
34+
export const createInsightProduct = async (insp: any) => {
35+
const url = createInsightProductUrl()
36+
return postTemplate(url, insp).then((res)=>res.json())
37+
}
38+
39+
export const updateInsightProduct = async (id: string, insp: any) => {
40+
const url = updateInsightProductUrl(id)
41+
return putTemplate(url, insp).then((res)=>res.json())
42+
}

lib/rest/restApi.ts

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export const getProductAreasUrl = () => `${apiUrl()}/productareas`
2525
export const getProductAreaUrl = (id: string) => `${apiUrl()}/productareas/${id}`
2626
export const fetchUserDataUrl = () => `${apiUrl()}/userData`
2727

28+
export const getInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}`
29+
export const createInsightProductUrl = () => `${apiUrl()}/insightProducts/new`
30+
export const updateInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}`
31+
2832
export const fetchKeywordsUrl = () => `${apiUrl()}/keywords`
2933
export const updateKeywordsUrl = () => `${apiUrl()}/keywords`
3034

0 commit comments

Comments
 (0)