Skip to content
This repository was archived by the owner on Sep 29, 2020. It is now read-only.

NOJIRA Ta i bruk hook for rest-kall fleire stader i sak-app #1234

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion mochapack.opts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
--colors
--require @babel/polyfill
--require ./setup/setup.jsx
--include ./packages/utils-test/src/setup-enzyme.jsx
--include ./packages/utils-test/src/setup-mocha.jsx
--include ./packages/utils-test/src/setup-mocha-hook.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface OwnProps {
clear: () => void;
};
opneSokeside: () => void;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

interface StateProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface OwnProps {
hentBehandling: ({ behandlingId: number }, { keepData: boolean }) => Promise<any>;
opneSokeside: () => void;
hasFetchError: boolean;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

interface FaktaPanelInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface OwnProps {
hasFetchError: boolean;
oppdaterBehandlingVersjon: (versjon: number) => void;
oppdaterProsessStegOgFaktaPanelIUrl: (punktnavn?: string, faktanavn?: string) => void;
featureToggles: any;
featureToggles: {[key: string]: boolean};
opneSokeside: () => void;
apentFaktaPanelInfo?: { urlCode: string; textCode: string };
dispatch: Dispatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface OwnProps {
clear: () => void;
};
opneSokeside: () => void;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

interface StateProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface OwnProps {
hentBehandling: ({ behandlingId: number }, { keepData: boolean }) => Promise<any>;
opneSokeside: () => void;
hasFetchError: boolean;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

interface FaktaPanelInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface OwnProps {
hasFetchError: boolean;
oppdaterBehandlingVersjon: (versjon: number) => void;
oppdaterProsessStegOgFaktaPanelIUrl: (punktnavn?: string, faktanavn?: string) => void;
featureToggles: any;
featureToggles: {[key: string]: boolean};
opneSokeside: () => void;
apentFaktaPanelInfo?: { urlCode: string; textCode: string};
dispatch: Dispatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface OwnProps {
clear: () => void;
};
opneSokeside: () => void;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

interface StateProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface OwnProps {
hentBehandling: ({ behandlingId: number }, { keepData: boolean }) => Promise<any>;
opneSokeside: () => void;
hasFetchError: boolean;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

interface FaktaPanelInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface OwnProps {
opneSokeside: () => void;
apentFaktaPanelInfo?: { urlCode: string; textCode: string};
dispatch: Dispatch;
featureToggles: any;
featureToggles: {[key: string]: boolean};
}

const getForhandsvisCallback = (dispatch, fagsak, behandling) => (data) => {
Expand Down
7 changes: 2 additions & 5 deletions packages/rest-api-hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export { default as RestApiState } from './src/RestApiState';

export { default as getUseRestApi } from './src/local-data/useRestApi';
export { default as useRestApiRunner } from './src/local-data/useRestApiRunner';

export { RestApiStateContext, RestApiProvider } from './src/RestApiContext';
export { default as useGlobalStateRestApi } from './src/global-data/useGlobalStateRestApi';
export { default as useGlobalStateRestApiData } from './src/global-data/useGlobalStateRestApiData';

export { RestApiErrorProvider } from './src/error/RestApiErrorContext';
export { default as useRestApiError } from './src/error/useRestApiError';
export { default as useRestApiErrorDispatcher } from './src/error/useRestApiErrorDispatcher';

export { default as RestApiHooks } from './src/RestApiHooks';
1 change: 0 additions & 1 deletion packages/rest-api-hooks/src/RestApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const RestApiDispatchContext = createContext<Dispatch | undefined>(undefi

interface OwnProps {
children: ReactNode;
requestApi: any;
initialState?: {[key in string]: any};
}

Expand Down
26 changes: 26 additions & 0 deletions packages/rest-api-hooks/src/RestApiHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AbstractRequestApi } from '@fpsak-frontend/rest-api-new';

import getUseRestApi, { getUseRestApiMock } from './local-data/useRestApi';
import getUseRestApiRunner, { getUseRestApiRunnerMock } from './local-data/useRestApiRunner';
import getUseGlobalStateRestApi, { getUseGlobalStateRestApiMock } from './global-data/useGlobalStateRestApi';
import useGlobalStateRestApiData, { useGlobalStateRestApiDataMock } from './global-data/useGlobalStateRestApiData';

const initHooks = (requestApi: AbstractRequestApi) => {
if (requestApi.isMock()) {
return {
useRestApi: getUseRestApiMock(requestApi),
useRestApiRunner: getUseRestApiRunnerMock(requestApi),
useGlobalStateRestApi: getUseGlobalStateRestApiMock(requestApi),
useGlobalStateRestApiData: useGlobalStateRestApiDataMock(requestApi),
};
}

return {
useRestApi: getUseRestApi(requestApi),
useRestApiRunner: getUseRestApiRunner(requestApi),
useGlobalStateRestApi: getUseGlobalStateRestApi(requestApi),
useGlobalStateRestApiData,
};
};

export default { initHooks };
79 changes: 57 additions & 22 deletions packages/rest-api-hooks/src/global-data/useGlobalStateRestApi.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState, useEffect, useContext } from 'react';
import {
useState, useEffect, useContext, DependencyList,
} from 'react';

import { NotificationMapper, RequestApi } from '@fpsak-frontend/rest-api-new';
import { NotificationMapper, AbstractRequestApi } from '@fpsak-frontend/rest-api-new';

import useRestApiErrorDispatcher from '../error/useRestApiErrorDispatcher';
import { RestApiDispatchContext } from '../RestApiContext';
Expand All @@ -12,12 +14,37 @@ interface RestApiData<T> {
data?: T;
}

interface Options {
updateTriggers?: DependencyList;
suspendRequest?: boolean;
}

const defaultOptions = {
updateTriggers: [],
suspendRequest: false,
};

/**
* For mocking i unit-test
*/
export const getUseGlobalStateRestApiMock = (requestApi: AbstractRequestApi) => function useGlobalStateRestApi<T>(
key: string, params: any = {},
):RestApiData<T> {
return {
state: RestApiState.SUCCESS,
error: undefined,
data: requestApi.startRequest(key, params),
};
};

/**
* Hook som henter data fra backend (ved mount) og deretter lagrer i @see RestApiContext
*/
const getUseGlobalStateRestApi = (requestApi: RequestApi) => function useGlobalStateRestApi<T>(key: string, params: any = {}):RestApiData<T> {
const getUseGlobalStateRestApi = (requestApi: AbstractRequestApi) => function useGlobalStateRestApi<T>(
key: string, params: any = {}, options: Options = defaultOptions,
):RestApiData<T> {
const [data, setData] = useState({
state: RestApiState.LOADING,
state: RestApiState.NOT_STARTED,
error: undefined,
data: undefined,
});
Expand All @@ -31,25 +58,33 @@ const getUseGlobalStateRestApi = (requestApi: RequestApi) => function useGlobalS
const dispatch = useContext(RestApiDispatchContext);

useEffect(() => {
dispatch({ type: 'remove', key });

requestApi.startRequest(key, params, notif)
.then((dataRes) => {
dispatch({ type: 'success', key, data: dataRes.payload });
setData({
state: RestApiState.SUCCESS,
data: dataRes.payload,
error: undefined,
});
})
.catch((error) => {
setData({
state: RestApiState.ERROR,
data: undefined,
error,
});
if (requestApi.hasPath(key) && !options.suspendRequest) {
dispatch({ type: 'remove', key });

setData({
state: RestApiState.LOADING,
error: undefined,
data: undefined,
});
}, []);

requestApi.startRequest(key, params, notif)
.then((dataRes) => {
dispatch({ type: 'success', key, data: dataRes.payload });
setData({
state: RestApiState.SUCCESS,
data: dataRes.payload,
error: undefined,
});
})
.catch((error) => {
setData({
state: RestApiState.ERROR,
data: undefined,
error,
});
});
}
}, options.updateTriggers);

return data;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { useContext } from 'react';

import { AbstractRequestApi } from '@fpsak-frontend/rest-api-new';

import { RestApiStateContext } from '../RestApiContext';

/**
* For mocking i unit-test
*/
export const useGlobalStateRestApiDataMock = (requestApi: AbstractRequestApi) => function useGlobalStateRestApiDataa<T>(
key: string,
): T {
return requestApi.startRequest(key, {});
};

/**
* Hook som bruker respons som allerede er hentet fra backend. For å kunne bruke denne
* må @see useGlobalStateRestApi først brukes for å hente data fra backend
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useContext } from 'react';

import { RestApiStateContext } from '../RestApiContext';

/**
* Hook som bruker respons som allerede er hentet fra backend. For å kunne bruke denne
* må @see useGlobalStateRestApi først brukes for å hente data fra backend
*/
function useGlobalStateRestApiError<T>(key: string): T {
const state = useContext(RestApiStateContext);
return state[key];
}

export default useGlobalStateRestApiError;
23 changes: 19 additions & 4 deletions packages/rest-api-hooks/src/local-data/useRestApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
useState, useEffect, DependencyList,
} from 'react';

import { REQUEST_POLLING_CANCELLED, NotificationMapper, RequestApi } from '@fpsak-frontend/rest-api-new';
import { REQUEST_POLLING_CANCELLED, NotificationMapper, AbstractRequestApi } from '@fpsak-frontend/rest-api-new';

import useRestApiErrorDispatcher from '../error/useRestApiErrorDispatcher';
import RestApiState from '../RestApiState';
Expand All @@ -25,11 +25,26 @@ const defaultOptions = {
suspendRequest: false,
};

/**
* For mocking i unit-test
*/
export const getUseRestApiMock = (requestApi: AbstractRequestApi) => function useRestApi<T>(
key: string, params: any = {},
):RestApiData<T> {
return {
state: RestApiState.SUCCESS,
error: undefined,
data: requestApi.startRequest(key, params),
};
};

/**
* Hook som utfører et restkall ved mount. En kan i tillegg legge ved en dependencies-liste som kan trigge ny henting når data
* blir oppdatert. Hook returnerer rest-kallets status/resultat/feil
*/
const getUseRestApi = (requestApi: RequestApi) => function useRestApi<T>(key: string, params: any = {}, options: Options = defaultOptions):RestApiData<T> {
const getUseRestApi = (requestApi: AbstractRequestApi) => function useRestApi<T>(
key: string, params: any = {}, options: Options = defaultOptions,
):RestApiData<T> {
const [data, setData] = useState({
state: RestApiState.NOT_STARTED,
error: undefined,
Expand Down Expand Up @@ -67,14 +82,14 @@ const getUseRestApi = (requestApi: RequestApi) => function useRestApi<T>(key: st
error,
});
});
} else {
} else if (!requestApi.hasPath(key)) {
setData({
state: RestApiState.NOT_STARTED,
error: undefined,
data: undefined,
});
}
}, options.updateTriggers);
}, [...options.updateTriggers]);

return data;
};
Expand Down
31 changes: 29 additions & 2 deletions packages/rest-api-hooks/src/local-data/useRestApiRunner.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useCallback } from 'react';

import {
REQUEST_POLLING_CANCELLED, NotificationMapper, ErrorType, RequestApi,
REQUEST_POLLING_CANCELLED, NotificationMapper, ErrorType, AbstractRequestApi,
} from '@fpsak-frontend/rest-api-new';
import useRestApiErrorDispatcher from '../error/useRestApiErrorDispatcher';
import RestApiState from '../RestApiState';
Expand All @@ -15,10 +15,37 @@ interface RestApiData<T> {
cancelRequest: () => void;
}

/**
* For mocking i unit-test
*/
export const getUseRestApiRunnerMock = (requestApi: AbstractRequestApi) => function useRestApiRunner<T>(key: string):RestApiData<T> {
const [data, setData] = useState({
state: RestApiState.NOT_STARTED,
data: undefined,
error: undefined,
});

const startRequest = (params: any = {}):Promise<T> => {
setData({
state: RestApiState.SUCCESS,
data: requestApi.startRequest(key, params),
error: undefined,
});
return Promise.resolve(data, params);
};

return {
startRequest,
resetRequestData: () => undefined,
cancelRequest: () => undefined,
...data,
};
};

/**
* Hook som gir deg ein funksjon til å starte restkall, i tillegg til kallets status/resultat/feil
*/
const getUseRestApiRunner = (requestApi: RequestApi) => function useRestApiRunner<T>(key: string):RestApiData<T> {
const getUseRestApiRunner = (requestApi: AbstractRequestApi) => function useRestApiRunner<T>(key: string):RestApiData<T> {
const [data, setData] = useState({
state: RestApiState.NOT_STARTED,
data: undefined,
Expand Down
Loading