Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc apollo rest link flexjar #616

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 26 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"@navikt/ds-icons": "^3.4.3",
"@navikt/ds-react": "^6.8.0",
"@navikt/oasis": "^3.3.0",
"@tanstack/react-query": "^5.40.0",
"@types/uuid": "^9.0.8",
"apollo-link-rest": "^0.9.0",
"classnames": "^2.5.1",
"dayjs": "^1.11.11",
"framer-motion": "^11.2.10",
Expand Down
16 changes: 13 additions & 3 deletions src/app/api/flexjar/oppdater/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { postOppdater } from '@/app/api/flexjar/flexjar';
import { erLokal } from '@/env';
import logger from '@/logger';

export const dynamic = 'force-dynamic';

export const dynamic = 'force-dynamic'; // defaults to auto
export async function POST(request: Request, { params }: { params: { id: string } }) {
const id = params.id; // 'a', 'b', or 'c'
return postOppdater(id, request);
if (erLokal) {
logger.info(`Mocker flexjar lokalt, mottok OPPDATERT feedback: ${JSON.stringify(request.json(), null, 2)}`);

return Response.json({
id: 'fake-lokal-flexjar-id',
});
}

return postOppdater(params.id, request);
}
9 changes: 9 additions & 0 deletions src/app/api/flexjar/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { postOpprett } from '@/app/api/flexjar/flexjar';
import { erLokal } from '@/env';
import logger from '@/logger';

export const dynamic = 'force-dynamic';

export async function POST(request: Request) {
if (erLokal) {
logger.info(`Mocker flexjar lokalt, mottok feedback: ${JSON.stringify(request.json(), null, 2)}`);
return Response.json({
id: 'fake-lokal-flexjar-id',
});
}

return postOpprett(request);
}
10 changes: 10 additions & 0 deletions src/app/apollo/apolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RestLink } from 'apollo-link-rest';

import possibletypes from '@/app/apollo/possibletypes';
import { erLokal } from '@/env';
import { ApolloClient, HttpLink, InMemoryCache, TypePolicies, from } from '@apollo/client';
Expand Down Expand Up @@ -47,9 +49,17 @@ const getTypePolicies = (): TypePolicies => {
};
};

const restLink = new RestLink({
endpoints: {
sanity: 'https://z9kr8ddn.api.sanity.io/v2023-08-01/data/query/production',
flexjar: '/api/flexjar',
},
});

export const createApolloClient = () =>
new ApolloClient({
link: from([
restLink,
new RetryLink({
attempts: { max: 5 },
}),
Expand Down
8 changes: 2 additions & 6 deletions src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { ApolloProvider } from '@apollo/client';
import { useLoadingToast } from '@hooks/useLoadingToast';
import { useFetchPersonQuery } from '@state/person';
import { useSetVarsler } from '@state/varsler';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

dayjs.extend(relativeTime);
dayjs.extend(minMax);
Expand All @@ -42,7 +41,6 @@ type Props = {

export const Providers = ({ children, bruker }: PropsWithChildren<Props>): ReactElement => {
const [apolloClient] = useState(() => createApolloClient());
const [queryClient] = useState(() => new QueryClient());

const initializeState = useCallback(
({ set }: { set: SetRecoilState }) => {
Expand All @@ -52,7 +50,7 @@ export const Providers = ({ children, bruker }: PropsWithChildren<Props>): React
hydrateAllFilters(set, bruker.grupper);
hydrateSorteringForTab(set);
},
[bruker.grupper],
[bruker.grupper, bruker.ident],
);

useLayoutEffect(() => {
Expand All @@ -65,9 +63,7 @@ export const Providers = ({ children, bruker }: PropsWithChildren<Props>): React
<RecoilRoot initializeState={initializeState}>
<SyncAlerts>
<VenterPåEndringProvider>
<BrukerContext.Provider value={bruker}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</BrukerContext.Provider>
<BrukerContext.Provider value={bruker}>{children}</BrukerContext.Provider>
</VenterPåEndringProvider>
</SyncAlerts>
</RecoilRoot>
Expand Down
34 changes: 26 additions & 8 deletions src/components/flexjar/FlexjarFelles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';

import { BodyShort, Button, Textarea } from '@navikt/ds-react';

import { FeedbackPayload } from '@/external/flexjar/types';
import { useOppdaterFlexjarFeedback } from '@/external/flexjar/useOppdaterFlexjarFeedback';
import { useOpprettFlexjarFeedback } from '@/external/flexjar/useOpprettFlexjarFeedback';
import { Bold } from '@components/Bold';
import { useOppdaterFlexjarFeedback } from '@hooks/useOppdaterFlexjarFeedback';
import { useOpprettFlexjarFeedback } from '@hooks/useOpprettFlexjarFeedback';

interface FlexjarFellesProps {
feedbackId: string;
Expand Down Expand Up @@ -38,26 +39,37 @@ export function FlexjarFelles({
const [textValue, setTextValue] = useState('');
const [errorMsg, setErrorMsg] = useState<string | null>(null);
const textAreaRef = useRef(null);
const { mutate: giFeedback, data, reset } = useOpprettFlexjarFeedback();
const { mutate: oppdaterFeedback } = useOppdaterFlexjarFeedback();
const { mutate: giFeedback, data, error: opprettError, reset } = useOpprettFlexjarFeedback();
const { mutate: oppdaterFeedback, error: oppdaterError } = useOppdaterFlexjarFeedback();

const fetchFeedback = useCallback(
async (knappeklikk?: () => void): Promise<boolean> => {
if (activeState === null) {
return false;
}

const body = {
const payload: FeedbackPayload = {
feedback: textValue,
feedbackId: feedbackId,
svar: activeState,
...feedbackProps,
};

if (data?.id) {
oppdaterFeedback({ body, id: data.id, cb: knappeklikk });
oppdaterFeedback({
variables: {
id: data.id,
payload,
},
onCompleted: knappeklikk,
});
return true;
} else {
giFeedback(body);
giFeedback({
variables: {
payload,
},
});
return false;
}
},
Expand Down Expand Up @@ -112,7 +124,13 @@ export function FlexjarFelles({
<form className={styles.form}>
<Textarea
ref={textAreaRef}
error={errorMsg}
error={
errorMsg
? errorMsg
: opprettError || oppdaterError
? 'En ukjent feil oppstod, prøv igjen senere'
: undefined
}
label={getPlaceholder()}
onKeyDown={async (e) => {
if (e.key === 'Enter' && e.ctrlKey) {
Expand Down
5 changes: 5 additions & 0 deletions src/external/flexjar/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type FeedbackPayload = Record<string, unknown> & {
feedback: string;
svar: string | number;
feedbackId: string;
};
27 changes: 27 additions & 0 deletions src/external/flexjar/useOppdaterFlexjarFeedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FeedbackPayload } from '@/external/flexjar/types';
import { gql, useMutation } from '@apollo/client';

type OppdaterFeedbackVariables = { id: string; payload: FeedbackPayload };

export function useOppdaterFlexjarFeedback() {
const [mutate, { data, error }] = useMutation<unknown, OppdaterFeedbackVariables>(gql`
mutation OppdaterFlexjarFeedback($id: String!, $payload: JSON!) {
oppdaterFlexjarFeedback(id: $id, input: $payload)
@rest(
type: "FlexjarFeedback"
endpoint: "flexjar"
path: "/oppdater/{args.id}"
method: "POST"
bodyKey: "input"
) {
id
}
}
`);

return {
mutate,
data,
error,
};
}
Loading
Loading