From 8c4e20467a340162b1a390c31e6dde2da47e70f4 Mon Sep 17 00:00:00 2001 From: "meng.zhu@nav.no" Date: Fri, 31 May 2024 10:08:48 +0200 Subject: [PATCH 1/2] insight product --- .../insightProducts/editInsightProduct.tsx | 15 +- .../insightProducts/newInsightProduct.tsx | 22 +- lib/queries/groupStats/groupStats.ts | 10 - .../insightProducts/createInsightProduct.ts | 9 - .../insightProducts/editInsightProduct.ts | 31 --- lib/queries/insightProducts/insightProduct.ts | 20 -- lib/rest/insightProducts.ts | 42 ++++ lib/rest/restApi.ts | 4 + lib/schema/graphql.ts | 212 ------------------ pages/insightProduct/edit.tsx | 9 +- 10 files changed, 66 insertions(+), 308 deletions(-) delete mode 100644 lib/queries/groupStats/groupStats.ts delete mode 100644 lib/queries/insightProducts/createInsightProduct.ts delete mode 100644 lib/queries/insightProducts/editInsightProduct.ts delete mode 100644 lib/queries/insightProducts/insightProduct.ts create mode 100644 lib/rest/insightProducts.ts diff --git a/components/insightProducts/editInsightProduct.tsx b/components/insightProducts/editInsightProduct.tsx index 4ff8face..56087dcb 100644 --- a/components/insightProducts/editInsightProduct.tsx +++ b/components/insightProducts/editInsightProduct.tsx @@ -17,7 +17,7 @@ import * as yup from 'yup' import { useContext, useState } from 'react' import TagsSelector from '../lib/tagsSelector' import { UserState } from "../../lib/context"; -import { useUpdateInsightProductMetadataMutation } from '../../lib/schema/graphql' +import { updateInsightProduct } from '../../lib/rest/insightProducts' const schema = yup.object().shape({ name: yup.string().nullable().required('Skriv inn navnet på innsiktsproduktet'), @@ -50,8 +50,9 @@ export const EditInsightProductMetadataForm = ({ id, name, description, type, li const [teamID, setTeamID] = useState('') const userInfo = useContext(UserState) const [isPrivacyCheckboxChecked, setIsPrivacyCheckboxChecked] = useState(false) + const [error, setError] = useState(undefined) + const [loading, setLoading] = useState(false) - const [updateInsightProductQuery, { loading, error }] = useUpdateInsightProductMetadataMutation() const { register, handleSubmit, @@ -95,8 +96,6 @@ export const EditInsightProductMetadataForm = ({ id, name, description, type, li const onSubmit = async (data: any) => { const editInsightProductData = { - variables: { - id: id, name: data.name, description: data.description, type: data.type, @@ -106,17 +105,21 @@ export const EditInsightProductMetadataForm = ({ id, name, description, type, li productAreaID: productAreaID, teamID: teamID, group: data.group, - }, } - updateInsightProductQuery(editInsightProductData).then(() => { + setLoading(true) + updateInsightProduct(id, editInsightProductData).then(() => { + setError(undefined) amplitudeLog('skjema fullført', { skjemanavn: 'endre-innsiktsprodukt' }) router.back() }).catch(e => { console.log(e) + setError(e) amplitudeLog('skjemainnsending feilet', { skjemanavn: 'endre-innsiktsprodukt', }) + }).finally(() => { + setLoading(false) }) } diff --git a/components/insightProducts/newInsightProduct.tsx b/components/insightProducts/newInsightProduct.tsx index d92a3154..87c1bb4e 100644 --- a/components/insightProducts/newInsightProduct.tsx +++ b/components/insightProducts/newInsightProduct.tsx @@ -17,7 +17,7 @@ import * as yup from 'yup' import { ChangeEvent, useContext, useState } from 'react' import TagsSelector from '../lib/tagsSelector' import { UserState } from "../../lib/context"; -import { CREATE_INSIGHT_PRODUCT } from '../../lib/queries/insightProducts/createInsightProduct' +import { createInsightProduct } from '../../lib/rest/insightProducts' const schema = yup.object().shape({ name: yup.string().nullable().required('Skriv inn navnet på innsiktsproduktet'), @@ -28,7 +28,7 @@ const schema = yup.object().shape({ link: yup .string() .required('Du må legge til en lenke til innsiktsproduktet') - .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'), + .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'), group: yup.string().required('Du må skrive inn en gruppe for innsiktsproduktet') }) @@ -48,6 +48,7 @@ export const NewInsightProductForm = () => { const [teamID, setTeamID] = useState('') const userData = useContext(UserState) const [isPrivacyCheckboxChecked, setIsPrivacyCheckboxChecked] = useState(false) + const [backendError, setBackendError] = useState(undefined) const handlePrivacyCheckboxChange = () => { setIsPrivacyCheckboxChecked(!isPrivacyCheckboxChecked) @@ -93,8 +94,6 @@ export const NewInsightProductForm = () => { const onSubmit = async (data: any) => { const inputData = { - variables: { - input: { name: data.name, description: valueOrNull(data.description), keywords: data.keywords, @@ -104,15 +103,15 @@ export const NewInsightProductForm = () => { link: data.link, type: data.type, group: data.group, - }, - }, - refetchQueries: ['searchContent'], } try { await createInsightProduct(inputData) + setBackendError(undefined) amplitudeLog('skjema fullført', { skjemanavn: 'ny-innsiktsprodukt' }) + router.push('/user/insightProducts') } catch (e) { + setBackendError(new Error('Internal server error')) amplitudeLog('skjemainnsending feilet', { skjemanavn: 'ny-innsiktsprodukt', }) @@ -121,15 +120,6 @@ export const NewInsightProductForm = () => { } - const [createInsightProduct, { loading, error: backendError }] = useMutation( - CREATE_INSIGHT_PRODUCT, - { - onCompleted: (data) => { - router.push("/") - }, - } - ) - const onCancel = () => { amplitudeLog( 'Klikker på: Avbryt', diff --git a/lib/queries/groupStats/groupStats.ts b/lib/queries/groupStats/groupStats.ts deleted file mode 100644 index 44c5e5c9..00000000 --- a/lib/queries/groupStats/groupStats.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { gql } from 'graphql-tag' - -export const GET_GROUPSTATS = gql` - query groupStats { - groupStats { - email - dataproducts - } - } -` diff --git a/lib/queries/insightProducts/createInsightProduct.ts b/lib/queries/insightProducts/createInsightProduct.ts deleted file mode 100644 index 2a3fce3e..00000000 --- a/lib/queries/insightProducts/createInsightProduct.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { gql } from 'graphql-tag' - -export const CREATE_INSIGHT_PRODUCT = gql` - mutation createInsightProduct($input: NewInsightProduct!) { - createInsightProduct(input: $input) { - id - } - } -` \ No newline at end of file diff --git a/lib/queries/insightProducts/editInsightProduct.ts b/lib/queries/insightProducts/editInsightProduct.ts deleted file mode 100644 index 4862c618..00000000 --- a/lib/queries/insightProducts/editInsightProduct.ts +++ /dev/null @@ -1,31 +0,0 @@ -import gql from "graphql-tag"; - -export const UPDATE_INSIGHT_PRODUCT_METADATA = gql` - mutation updateInsightProductMetadata( - $id: ID! - $name: String! - $description: String! - $type: String! - $link: String! - $keywords: [String!]! - $teamkatalogenURL: String - $productAreaID: String - $teamID: String - $group: String! - ) { - updateInsightProductMetadata( - id: $id - name: $name - description: $description - type: $type - link: $link - keywords: $keywords - teamkatalogenURL: $teamkatalogenURL - productAreaID: $productAreaID - teamID: $teamID - group: $group - ) { - id - } - } -` \ No newline at end of file diff --git a/lib/queries/insightProducts/insightProduct.ts b/lib/queries/insightProducts/insightProduct.ts deleted file mode 100644 index 4c232fef..00000000 --- a/lib/queries/insightProducts/insightProduct.ts +++ /dev/null @@ -1,20 +0,0 @@ -import gql from "graphql-tag"; - -export const GET_INSIGHT_PRODUCT = gql` - query insightProduct($id: ID!) { - insightProduct(id: $id) { - id - name - description - created - lastModified - type - link - keywords - group - teamkatalogenURL - productAreaID - teamID - } - } -` \ No newline at end of file diff --git a/lib/rest/insightProducts.ts b/lib/rest/insightProducts.ts new file mode 100644 index 00000000..7c5506c9 --- /dev/null +++ b/lib/rest/insightProducts.ts @@ -0,0 +1,42 @@ +import { useEffect, useState } from "react" +import { createInsightProductUrl, fetchTemplate, getInsightProductUrl, postTemplate, putTemplate, updateInsightProductUrl } from "./restApi" + +const getInsightProduct = async (id: string) => { + const url = getInsightProductUrl(id) + return fetchTemplate(url) +} + +export const useGetInsightProduct = (id: string)=>{ + const [insightProduct, setInsightProduct] = useState(null) + const [loading, setLoading] = useState(false) + const [error, setError] = useState(undefined) + + + useEffect(()=>{ + if(!id) return + getInsightProduct(id).then((res)=> res.json()) + .then((data)=> + { + setError(undefined) + setInsightProduct(data) + }) + .catch((err)=>{ + setError(err) + setInsightProduct(null) + }).finally(()=>{ + setLoading(false) + }) + }, [id]) + + return {insightProduct, loading, error} +} + +export const createInsightProduct = async (insp: any) => { + const url = createInsightProductUrl() + return postTemplate(url, insp).then((res)=>res.json()) +} + +export const updateInsightProduct = async (id: string, insp: any) => { + const url = updateInsightProductUrl(id) + return putTemplate(url, insp).then((res)=>res.json()) +} \ No newline at end of file diff --git a/lib/rest/restApi.ts b/lib/rest/restApi.ts index 26704e32..91a3a196 100644 --- a/lib/rest/restApi.ts +++ b/lib/rest/restApi.ts @@ -25,6 +25,10 @@ export const getProductAreasUrl = () => `${apiUrl()}/productareas` export const getProductAreaUrl = (id: string) => `${apiUrl()}/productareas/${id}` export const fetchUserDataUrl = () => `${apiUrl()}/userData` +export const getInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}` +export const createInsightProductUrl = () => `${apiUrl()}/insightProducts/new` +export const updateInsightProductUrl = (id: string) => `${apiUrl()}/insightProducts/${id}` + export const fetchKeywordsUrl = () => `${apiUrl()}/keywords` export const updateKeywordsUrl = () => `${apiUrl()}/keywords` diff --git a/lib/schema/graphql.ts b/lib/schema/graphql.ts index 65b26ad6..b7bc8e47 100644 --- a/lib/schema/graphql.ts +++ b/lib/schema/graphql.ts @@ -1180,41 +1180,6 @@ export type UserInfo = { stories: Array; }; -export type GroupStatsQueryVariables = Exact<{ [key: string]: never; }>; - - -export type GroupStatsQuery = { __typename?: 'Query', groupStats: Array<{ __typename?: 'GroupStats', email: string, dataproducts: number }> }; - -export type CreateInsightProductMutationVariables = Exact<{ - input: NewInsightProduct; -}>; - - -export type CreateInsightProductMutation = { __typename?: 'Mutation', createInsightProduct: { __typename?: 'InsightProduct', id: string } }; - -export type UpdateInsightProductMetadataMutationVariables = Exact<{ - id: Scalars['ID']['input']; - name: Scalars['String']['input']; - description: Scalars['String']['input']; - type: Scalars['String']['input']; - link: Scalars['String']['input']; - keywords: Array | Scalars['String']['input']; - teamkatalogenURL?: InputMaybe; - productAreaID?: InputMaybe; - teamID?: InputMaybe; - group: Scalars['String']['input']; -}>; - - -export type UpdateInsightProductMetadataMutation = { __typename?: 'Mutation', updateInsightProductMetadata: { __typename?: 'InsightProduct', id: string } }; - -export type InsightProductQueryVariables = Exact<{ - id: Scalars['ID']['input']; -}>; - - -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, group: string, teamkatalogenURL?: string | null, productAreaID?: string | null, teamID?: string | null } }; - export type JoinableViewQueryVariables = Exact<{ id: Scalars['ID']['input']; }>; @@ -1290,183 +1255,6 @@ export type UserInfoAccessableDataproductQueryVariables = Exact<{ [key: string]: export type UserInfoAccessableDataproductQuery = { __typename?: 'Query', userInfo: { __typename?: 'UserInfo', accessable: { __typename?: 'AccessibleDatasets', owned: Array<{ __typename: 'Dataset', id: string, name: string, description: string, created: any, lastModified: any, owner: { __typename?: 'Owner', group: string, teamkatalogenURL?: string | null } }>, granted: Array<{ __typename: 'Dataset', id: string, name: string, description: string, created: any, lastModified: any, owner: { __typename?: 'Owner', group: string, teamkatalogenURL?: string | null } }> } } }; -export const GroupStatsDocument = gql` - query groupStats { - groupStats { - email - dataproducts - } -} - `; - -/** - * __useGroupStatsQuery__ - * - * To run a query within a React component, call `useGroupStatsQuery` and pass it any options that fit your needs. - * When your component renders, `useGroupStatsQuery` returns an object from Apollo Client that contains loading, error, and data properties - * you can use to render your UI. - * - * @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; - * - * @example - * const { data, loading, error } = useGroupStatsQuery({ - * variables: { - * }, - * }); - */ -export function useGroupStatsQuery(baseOptions?: Apollo.QueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useQuery(GroupStatsDocument, options); - } -export function useGroupStatsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useLazyQuery(GroupStatsDocument, options); - } -export function useGroupStatsSuspenseQuery(baseOptions?: Apollo.SuspenseQueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useSuspenseQuery(GroupStatsDocument, options); - } -export type GroupStatsQueryHookResult = ReturnType; -export type GroupStatsLazyQueryHookResult = ReturnType; -export type GroupStatsSuspenseQueryHookResult = ReturnType; -export type GroupStatsQueryResult = Apollo.QueryResult; -export const CreateInsightProductDocument = gql` - mutation createInsightProduct($input: NewInsightProduct!) { - createInsightProduct(input: $input) { - id - } -} - `; -export type CreateInsightProductMutationFn = Apollo.MutationFunction; - -/** - * __useCreateInsightProductMutation__ - * - * To run a mutation, you first call `useCreateInsightProductMutation` within a React component and pass it any options that fit your needs. - * When your component renders, `useCreateInsightProductMutation` returns a tuple that includes: - * - A mutate function that you can call at any time to execute the mutation - * - An object with fields that represent the current status of the mutation's execution - * - * @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2; - * - * @example - * const [createInsightProductMutation, { data, loading, error }] = useCreateInsightProductMutation({ - * variables: { - * input: // value for 'input' - * }, - * }); - */ -export function useCreateInsightProductMutation(baseOptions?: Apollo.MutationHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useMutation(CreateInsightProductDocument, options); - } -export type CreateInsightProductMutationHookResult = ReturnType; -export type CreateInsightProductMutationResult = Apollo.MutationResult; -export type CreateInsightProductMutationOptions = Apollo.BaseMutationOptions; -export const UpdateInsightProductMetadataDocument = gql` - mutation updateInsightProductMetadata($id: ID!, $name: String!, $description: String!, $type: String!, $link: String!, $keywords: [String!]!, $teamkatalogenURL: String, $productAreaID: String, $teamID: String, $group: String!) { - updateInsightProductMetadata( - id: $id - name: $name - description: $description - type: $type - link: $link - keywords: $keywords - teamkatalogenURL: $teamkatalogenURL - productAreaID: $productAreaID - teamID: $teamID - group: $group - ) { - id - } -} - `; -export type UpdateInsightProductMetadataMutationFn = Apollo.MutationFunction; - -/** - * __useUpdateInsightProductMetadataMutation__ - * - * To run a mutation, you first call `useUpdateInsightProductMetadataMutation` within a React component and pass it any options that fit your needs. - * When your component renders, `useUpdateInsightProductMetadataMutation` returns a tuple that includes: - * - A mutate function that you can call at any time to execute the mutation - * - An object with fields that represent the current status of the mutation's execution - * - * @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2; - * - * @example - * const [updateInsightProductMetadataMutation, { data, loading, error }] = useUpdateInsightProductMetadataMutation({ - * variables: { - * id: // value for 'id' - * name: // value for 'name' - * description: // value for 'description' - * type: // value for 'type' - * link: // value for 'link' - * keywords: // value for 'keywords' - * teamkatalogenURL: // value for 'teamkatalogenURL' - * productAreaID: // value for 'productAreaID' - * teamID: // value for 'teamID' - * group: // value for 'group' - * }, - * }); - */ -export function useUpdateInsightProductMetadataMutation(baseOptions?: Apollo.MutationHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useMutation(UpdateInsightProductMetadataDocument, options); - } -export type UpdateInsightProductMetadataMutationHookResult = ReturnType; -export type UpdateInsightProductMetadataMutationResult = Apollo.MutationResult; -export type UpdateInsightProductMetadataMutationOptions = Apollo.BaseMutationOptions; -export const InsightProductDocument = gql` - query insightProduct($id: ID!) { - insightProduct(id: $id) { - id - name - description - created - lastModified - type - link - keywords - group - teamkatalogenURL - productAreaID - teamID - } -} - `; - -/** - * __useInsightProductQuery__ - * - * To run a query within a React component, call `useInsightProductQuery` and pass it any options that fit your needs. - * When your component renders, `useInsightProductQuery` returns an object from Apollo Client that contains loading, error, and data properties - * you can use to render your UI. - * - * @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; - * - * @example - * const { data, loading, error } = useInsightProductQuery({ - * variables: { - * id: // value for 'id' - * }, - * }); - */ -export function useInsightProductQuery(baseOptions: Apollo.QueryHookOptions & ({ variables: InsightProductQueryVariables; skip?: boolean; } | { skip: boolean; }) ) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useQuery(InsightProductDocument, options); - } -export function useInsightProductLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useLazyQuery(InsightProductDocument, options); - } -export function useInsightProductSuspenseQuery(baseOptions?: Apollo.SuspenseQueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useSuspenseQuery(InsightProductDocument, options); - } -export type InsightProductQueryHookResult = ReturnType; -export type InsightProductLazyQueryHookResult = ReturnType; -export type InsightProductSuspenseQueryHookResult = ReturnType; -export type InsightProductQueryResult = Apollo.QueryResult; export const JoinableViewDocument = gql` query JoinableView($id: ID!) { joinableView(id: $id) { diff --git a/pages/insightProduct/edit.tsx b/pages/insightProduct/edit.tsx index 92516f63..1371eea6 100644 --- a/pages/insightProduct/edit.tsx +++ b/pages/insightProduct/edit.tsx @@ -1,22 +1,23 @@ import { Loader } from "@navikt/ds-react"; import { useRouter } from "next/router"; import { EditInsightProductMetadataFields, EditInsightProductMetadataForm } from "../../components/insightProducts/editInsightProduct"; -import { useInsightProductQuery } from "../../lib/schema/graphql"; +import { use } from "react"; +import { useGetInsightProduct } from "../../lib/rest/insightProducts"; const EditInsightProductPage = () => { const router = useRouter(); const id = router.query.id; - const insightProductQuery = useInsightProductQuery({ variables: { id: id as string } }); + const insightProductQuery = useGetInsightProduct(id as string); if (insightProductQuery.error) { return
{insightProductQuery.error.message}
; } - if (insightProductQuery.loading || !insightProductQuery.data) { + if (insightProductQuery.loading || !insightProductQuery.insightProduct) { return ; } - const insightProduct = insightProductQuery.data.insightProduct; + const insightProduct = insightProductQuery.insightProduct; const formFields: EditInsightProductMetadataFields = { id: id as string, From b595f77dece59da8552023f0b387e78d23276ca9 Mon Sep 17 00:00:00 2001 From: "meng.zhu@nav.no" Date: Fri, 31 May 2024 23:35:34 +0200 Subject: [PATCH 2/2] client --- components/search/resultList.tsx | 10 +- components/search/searchResultLink.tsx | 11 +- components/stories/editStoryMetadata.tsx | 15 +- components/stories/newStory.tsx | 34 ++-- lib/queries/story/createStory.ts | 9 - lib/queries/story/deleteInsightProduct.ts | 7 - lib/queries/story/deleteStory.ts | 7 - lib/queries/story/story.ts | 18 -- lib/queries/story/updateStory.ts | 27 --- lib/rest/insightProducts.ts | 7 +- lib/rest/restApi.ts | 5 + lib/rest/stories.ts | 21 ++ lib/schema/graphql.ts | 237 ---------------------- 13 files changed, 57 insertions(+), 351 deletions(-) delete mode 100644 lib/queries/story/createStory.ts delete mode 100644 lib/queries/story/deleteInsightProduct.ts delete mode 100644 lib/queries/story/deleteStory.ts delete mode 100644 lib/queries/story/story.ts delete mode 100644 lib/queries/story/updateStory.ts create mode 100644 lib/rest/stories.ts diff --git a/components/search/resultList.tsx b/components/search/resultList.tsx index 8bb64987..b46f7e90 100644 --- a/components/search/resultList.tsx +++ b/components/search/resultList.tsx @@ -8,7 +8,7 @@ import { UserState } from '../../lib/context' import { useSearchTeamKatalogen } from '../../lib/rest/teamkatalogen' import { useGetProductAreas } from '../../lib/rest/productAreas' import { SearchResult } from '../../lib/rest/search' -import { useDeleteStoryMutation } from '../../lib/schema/graphql' +import { deleteStory } from '../../lib/rest/stories' const Results = ({ children }: { children: React.ReactNode }) => (
{children}
@@ -58,15 +58,9 @@ const ResultList = ({ searchParam, updateQuery, }: ResultListInterface) => { - const [deleteStoryQuery] = useDeleteStoryMutation() const userInfo = useContext(UserState) const { searchResult: teamkatalogen } = useSearchTeamKatalogen() const { productAreas } = useGetProductAreas() - const deleteStory = (id: string) => deleteStoryQuery({ - variables: { - id: id - }, - }) const isDataProduct = (item: any) => !!item.datasets @@ -197,7 +191,7 @@ const ResultList = ({ keywords={s.keywords} editable={true} description={s.description} - deleteResource={deleteStory} + deleteResource={()=> deleteStory(s.id)} /> ))} diff --git a/components/search/searchResultLink.tsx b/components/search/searchResultLink.tsx index b2d29f8c..f21275e4 100644 --- a/components/search/searchResultLink.tsx +++ b/components/search/searchResultLink.tsx @@ -4,12 +4,12 @@ import ReactMarkdown from 'react-markdown' import { PluggableList } from 'react-markdown/lib' import remarkGfm from 'remark-gfm' import { CoApplicant, Table } from '@navikt/ds-icons' -import { useDeleteInsightProductMutation, useDeleteStoryMutation } from '../../lib/schema/graphql' import humanizeDate from '../../lib/humanizeDate' import DeleteModal from '../lib/deleteModal' import { useState } from 'react' import { useRouter } from 'next/router' import TagPill from '../lib/tagPill' +import { deleteInsightProduct } from '../../lib/rest/insightProducts' export interface SearchResultProps { resourceType?: string @@ -55,7 +55,6 @@ export const SearchResultLink = ({ const owner = teamkatalogenTeam || group?.group const router = useRouter(); const [error, setError] = useState(undefined) - const [deleteInsightProductMutation] = useDeleteInsightProductMutation(); const editResource = () => { if (resourceType == 'datafortelling') { @@ -66,14 +65,6 @@ export const SearchResultLink = ({ } const openDeleteModal = () => setModal(true) - const deleteInsightProduct = (id: string)=>{ - return deleteInsightProductMutation({ - variables:{ - id: id - }, - }) - } - const confirmDelete = () => { const deletePromise = resourceType == "innsiktsprodukt"? deleteInsightProduct(id || ''): diff --git a/components/stories/editStoryMetadata.tsx b/components/stories/editStoryMetadata.tsx index 0b580eda..3d79dd9c 100644 --- a/components/stories/editStoryMetadata.tsx +++ b/components/stories/editStoryMetadata.tsx @@ -15,7 +15,7 @@ import * as yup from 'yup' import { useContext, useState } from 'react' import TagsSelector from '../lib/tagsSelector' import {UserState} from "../../lib/context"; -import { useUpdateStoryMetadataMutation } from '../../lib/schema/graphql' +import { updateStory } from '../../lib/rest/stories' const schema = yup.object().shape({ name: yup.string().nullable().required('Skriv inn navnet på datafortellingen'), @@ -40,7 +40,8 @@ export const EditStoryMetadataForm = ({id, name, description, keywords, teamkata const [productAreaID, setProductAreaID] = useState('') const [teamID, setTeamID] = useState('') const userInfo = useContext(UserState) - const [updateStoryQuery, {loading, error}] = useUpdateStoryMetadataMutation() + const [error, setError] = useState(undefined) + const [loading, setLoading] = useState(false) const { register, handleSubmit, @@ -77,8 +78,6 @@ export const EditStoryMetadataForm = ({id, name, description, keywords, teamkata const onSubmit = async (data: any) => { const editStoryData = { - variables: { - id: id, name: data.name, description: data.description, keywords: data.keywords, @@ -86,17 +85,21 @@ export const EditStoryMetadataForm = ({id, name, description, keywords, teamkata productAreaID: productAreaID, teamID: teamID, group: data.group, - }, } - updateStoryQuery(editStoryData).then(()=>{ + setLoading(true) + updateStory(id, editStoryData).then(()=>{ + setError(undefined) amplitudeLog('skjema fullført', { skjemanavn: 'endre-datafortelling' }) router.push("/user/stories") }).catch(e=>{ + setError(e) console.log(e) amplitudeLog('skjemainnsending feilet', { skjemanavn: 'endre-datafortelling', }) + }).finally(()=>{ + setLoading(false) }) } diff --git a/components/stories/newStory.tsx b/components/stories/newStory.tsx index 75c9b177..53247d16 100644 --- a/components/stories/newStory.tsx +++ b/components/stories/newStory.tsx @@ -11,11 +11,12 @@ import * as yup from 'yup'; import { ChangeEvent, useContext, useRef, useState } from 'react'; import TagsSelector from '../lib/tagsSelector'; import { UserState } from '../../lib/context'; -import { CREATE_STORY } from '../../lib/queries/story/createStory'; import { TreeItem } from '@mui/x-tree-view/TreeItem'; import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView'; import { FileTextFillIcon, FolderFillIcon, TrashIcon } from '@navikt/aksel-icons'; import { UploadFile } from '../../lib/schema/graphql'; +import { createStory } from '../../lib/rest/stories'; +import { set } from 'lodash'; const schema = yup.object().shape({ name: yup.string().nullable().required('Skriv inn navnet på datafortellingen'), @@ -41,6 +42,7 @@ export const NewStoryForm = () => { const [storyFiles, setStoryFiles] = useState([]); const singleFileInputRef = useRef(null); const folderFileInputRef = useRef(null); + const [error, setError] = useState(undefined); const handleSingleFileClick = () => { /* @ts-expect-error */ @@ -88,13 +90,11 @@ export const NewStoryForm = () => { const valueOrNull = (val: string) => (val == '' ? null : val); const onSubmit = async (data: any) => { - const uploadData = { - variables: { - files: storyFiles.map(it=>({ + const files= storyFiles.map(it=>({ path: fixRelativePath(it), file: it, - })), - input: { + })) + const storyInput= { name: data.name, description: valueOrNull(data.description), keywords: data.keywords, @@ -102,30 +102,22 @@ export const NewStoryForm = () => { productAreaID: productAreaID, teamID: teamID, group: data.group, - }, - }, - refetchQueries: ['searchContent', 'userInfoDetails'], - }; + } try { - await createStory(uploadData); + const data = await createStory(storyInput, files); + setError(undefined); amplitudeLog('skjema fullført', { skjemanavn: 'ny-datafortelling' }); + router.push(`/story/${data.id}`); } catch (e) { + setError(e as Error); amplitudeLog('skjemainnsending feilet', { skjemanavn: 'ny-datafortelling', }) console.log(e) } } - - const [createStory, { loading, error: backendError }] = useMutation( - CREATE_STORY, - { - onCompleted: (data) => { - router.push(`/story/${data.createStory.id}`); - }, - }, - ) + const onCancel = () => { amplitudeLog( @@ -316,7 +308,7 @@ export const NewStoryForm = () => { {renderTree(generateFileTree(storyFiles))} )} - {backendError && } + {error && }