-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98ceec2
commit 95c8fd2
Showing
15 changed files
with
273 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum FeatureToggles { | ||
NY_ARBEIDSSOKER_REGISTRERING_URL = 'modiacontextholder.ny-arbeidssoker-registrering-url' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as React from 'react'; | ||
import { getJson, lagModiacontextholderUrl } from '../redux/api'; | ||
import { FeatureToggles } from './FeatureToggles'; | ||
import { useQuery, UseQueryResult } from '@tanstack/react-query'; | ||
export type FeatureTogglesResponse = { | ||
[key in FeatureToggles]: boolean; | ||
}; | ||
|
||
const url = () => { | ||
const queryParams = Object.values(FeatureToggles) | ||
.map((it) => `id=${it}`) | ||
.join('&'); | ||
|
||
return `${lagModiacontextholderUrl()}/api/featuretoggle/?${queryParams}`; | ||
}; | ||
|
||
interface FetchError { | ||
error: string; | ||
} | ||
|
||
type ReactElement = React.ReactElement | null; | ||
|
||
export interface Config<T> { | ||
ifPending: ReactElement | (() => ReactElement); | ||
ifError: ReactElement | ((error: any) => ReactElement); | ||
ifData: (data: T) => ReactElement; | ||
} | ||
|
||
export type RendererOrConfig<T> = | ||
| ((data: T) => ReactElement) | ||
| (Partial<Config<T>> & Pick<Config<T>, 'ifData'>); | ||
export type DefaultConfig = Omit<Config<any>, 'ifData'>; | ||
|
||
export function applyDefaults<T>( | ||
defaults: DefaultConfig, | ||
renderer: RendererOrConfig<T> | ||
): Config<T> { | ||
if (typeof renderer === 'function') { | ||
return { ...defaults, ifData: renderer }; | ||
} else { | ||
return { ...defaults, ...renderer }; | ||
} | ||
} | ||
|
||
export function useRest<TData = unknown, TError = unknown>( | ||
response: UseQueryResult<TData, TError>, | ||
config: Config<TData> | ||
): ReactElement { | ||
if (response.isLoading) { | ||
if (typeof config.ifPending === 'function') { | ||
return config.ifPending(); | ||
} else { | ||
return config.ifPending; | ||
} | ||
} else if (response.isError) { | ||
if (typeof config.ifError === 'function') { | ||
return config.ifError(response.error); | ||
} else { | ||
return config.ifError; | ||
} | ||
} else { | ||
return config.ifData(response.data!); | ||
} | ||
} | ||
|
||
const defaults: DefaultConfig = { | ||
ifPending: <div>Loading...</div>, | ||
ifError: <div>Klarte ikke laste inn feature toggles</div> | ||
}; | ||
|
||
const resource = { | ||
queryKey: ['featureToggles'], | ||
useFetch(): UseQueryResult<FeatureTogglesResponse, FetchError> { | ||
return useQuery({ queryKey: this.queryKey, queryFn: () => getJson(url()) }); | ||
}, | ||
useRenderer(renderer: RendererOrConfig<FeatureTogglesResponse>) { | ||
const response = this.useFetch(); | ||
return useRest(response, applyDefaults(defaults, renderer)); | ||
} | ||
}; | ||
|
||
export default resource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { FeatureToggles } from './FeatureToggles'; | ||
import featureToggleResource from './featureToggleResource'; | ||
|
||
export const useFeatureToggle = (toggleId: FeatureToggles) => { | ||
const toggles = featureToggleResource.useFetch(); | ||
if (toggles.isLoading) { | ||
return { pending: true }; | ||
} else if (toggles.isError) { | ||
return { pending: true }; | ||
} else if (toggles.data) { | ||
return { pending: false, isOn: toggles.data[toggleId] }; | ||
} | ||
return { pending: false, isOn: false }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters