From ce056821a1b1d562e4c36d7ff30dfcb5e3ad3aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sarstr=C3=B6m?= Date: Mon, 22 Nov 2021 10:47:11 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Feature=20toggle=20conte?= =?UTF-8?q?xt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/skolplattformen-sthlm/App.tsx | 57 +++++++++++-------- .../components/login.component.tsx | 7 ++- .../context/feature/featureContext.tsx | 18 ++++++ .../hooks/useFeature.tsx | 12 ++++ libs/api-hjarntorget/lib/features.ts | 8 +-- libs/api-hjarntorget/lib/index.ts | 1 + libs/api-skolplattformen/lib/features.ts | 5 ++ libs/api-skolplattformen/lib/index.ts | 1 + libs/api/lib/features.ts | 9 +-- libs/api/lib/index.ts | 4 +- libs/hooks/src/feature.tsx | 27 --------- 11 files changed, 87 insertions(+), 62 deletions(-) create mode 100644 apps/skolplattformen-sthlm/context/feature/featureContext.tsx create mode 100644 apps/skolplattformen-sthlm/hooks/useFeature.tsx create mode 100644 libs/api-skolplattformen/lib/features.ts delete mode 100644 libs/hooks/src/feature.tsx diff --git a/apps/skolplattformen-sthlm/App.tsx b/apps/skolplattformen-sthlm/App.tsx index f4d29cfe8..30988d605 100644 --- a/apps/skolplattformen-sthlm/App.tsx +++ b/apps/skolplattformen-sthlm/App.tsx @@ -1,8 +1,12 @@ import * as eva from '@eva-design/eva' import AsyncStorage from '@react-native-async-storage/async-storage' import CookieManager from '@react-native-cookies/cookies' -import initSkolplattformen from '@skolplattformen/api-skolplattformen' -import initHjarntorget from '@skolplattformen/api-hjarntorget' +import initSkolplattformen, { + features as featuresSkolplattformen, +} from '@skolplattformen/api-skolplattformen' +import initHjarntorget, { + features as featuresHjarntorget, +} from '@skolplattformen/api-hjarntorget' import { ApiProvider } from '@skolplattformen/hooks' import { ApplicationProvider, IconRegistry } from '@ui-kitten/components' @@ -19,6 +23,7 @@ import useSettingsStorage from './hooks/useSettingsStorage' import { translations } from './utils/translation' import { Reporter } from '@skolplattformen/hooks' import { Api } from '@skolplattformen/api' +import { FeatureProvider } from './context/feature/featureContext' const ApiList = new Map([ // @ts-expect-error Why is fetch failing here? @@ -77,27 +82,31 @@ export default () => { const api = ApiList.get('goteborg-hjarntorget')! return ( - - - - - - - - - - - - - + + + + + + + + + + + + + + + ) } diff --git a/apps/skolplattformen-sthlm/components/login.component.tsx b/apps/skolplattformen-sthlm/components/login.component.tsx index 2118d769a..abe775387 100644 --- a/apps/skolplattformen-sthlm/components/login.component.tsx +++ b/apps/skolplattformen-sthlm/components/login.component.tsx @@ -23,6 +23,7 @@ import { } from 'react-native' import { schema } from '../app.json' import { SchoolPlatformContext } from '../context/schoolPlatform/schoolPlatformContext' +import { useFeature } from '../hooks/useFeature' import useSettingsStorage from '../hooks/useSettingsStorage' import { useTranslation } from '../hooks/useTranslation' import { Layout as LayoutStyle, Sizing, Typography } from '../styles' @@ -57,6 +58,7 @@ export const Login = () => { const [loginMethodIndex, setLoginMethodIndex] = useSettingsStorage('loginMethodIndex') + const loginBankIdSameDevice = useFeature('LOGIN_BANK_ID_SAME_DEVICE') const { currentSchoolPlatform, changeSchoolPlatform } = useContext( SchoolPlatformContext ) @@ -66,11 +68,14 @@ export const Login = () => { const valid = Personnummer.valid(personalIdNumber) const loginMethods = [ - t('auth.bankid.OpenOnThisDevice'), t('auth.bankid.OpenOnAnotherDevice'), t('auth.loginAsTestUser'), ] + if (loginBankIdSameDevice) { + loginMethods.unshift(t('auth.bankid.OpenOnThisDevice')) + } + const schoolPlatforms = [ { id: 'stockholm-skolplattformen', diff --git a/apps/skolplattformen-sthlm/context/feature/featureContext.tsx b/apps/skolplattformen-sthlm/context/feature/featureContext.tsx new file mode 100644 index 000000000..53d4f03e2 --- /dev/null +++ b/apps/skolplattformen-sthlm/context/feature/featureContext.tsx @@ -0,0 +1,18 @@ +import { Features, FeatureType } from '@skolplattformen/api' +import React from 'react' + +export const FeatureFlagsContext = React.createContext({ + LOGIN_BANK_ID_SAME_DEVICE: false, +}) + +interface Props { + features: Features +} + +export const FeatureProvider: React.FC = (props) => { + return ( + + {props.children} + + ) +} diff --git a/apps/skolplattformen-sthlm/hooks/useFeature.tsx b/apps/skolplattformen-sthlm/hooks/useFeature.tsx new file mode 100644 index 000000000..dfbf2c6a6 --- /dev/null +++ b/apps/skolplattformen-sthlm/hooks/useFeature.tsx @@ -0,0 +1,12 @@ +import React from 'react' +import { FeatureType, Features } from '@skolplattformen/api' +import { FeatureFlagsContext } from '../context/feature/featureContext' + +export const useFeature = (name: FeatureType) => { + const features = React.useContext(FeatureFlagsContext) + if (features === null) { + throw new Error('You must wrap your components in a FeatureProvider.') + } + + return features[name] +} diff --git a/libs/api-hjarntorget/lib/features.ts b/libs/api-hjarntorget/lib/features.ts index b5067cd73..471ad55ce 100644 --- a/libs/api-hjarntorget/lib/features.ts +++ b/libs/api-hjarntorget/lib/features.ts @@ -1,5 +1,5 @@ -import { Feature } from '@skolplattformen/api' +import { Features } from '@skolplattformen/api' -export const features: Feature[] = [ - { name: 'login', enabled: true }, -] \ No newline at end of file +export const features: Features = { + LOGIN_BANK_ID_SAME_DEVICE: false +} \ No newline at end of file diff --git a/libs/api-hjarntorget/lib/index.ts b/libs/api-hjarntorget/lib/index.ts index 566e5a5bd..bec6660d3 100644 --- a/libs/api-hjarntorget/lib/index.ts +++ b/libs/api-hjarntorget/lib/index.ts @@ -3,6 +3,7 @@ import { Api, FetcherOptions, Fetch, RNCookieManager, ToughCookieJar, wrapReactNativeCookieManager, wrapToughCookie } from '@skolplattformen/api' +export { features } from './features' const init = ( fetchImpl: Fetch, diff --git a/libs/api-skolplattformen/lib/features.ts b/libs/api-skolplattformen/lib/features.ts new file mode 100644 index 000000000..cc6578702 --- /dev/null +++ b/libs/api-skolplattformen/lib/features.ts @@ -0,0 +1,5 @@ +import { Features } from '@skolplattformen/api' + +export const features: Features = { + LOGIN_BANK_ID_SAME_DEVICE: true +} \ No newline at end of file diff --git a/libs/api-skolplattformen/lib/index.ts b/libs/api-skolplattformen/lib/index.ts index bca236c37..926e05439 100644 --- a/libs/api-skolplattformen/lib/index.ts +++ b/libs/api-skolplattformen/lib/index.ts @@ -3,6 +3,7 @@ import { Api, FetcherOptions, Fetch, RNCookieManager, ToughCookieJar, wrapReactNativeCookieManager, wrapToughCookie } from '@skolplattformen/api' +export { features } from './features' const init = ( fetchImpl: Fetch, diff --git a/libs/api/lib/features.ts b/libs/api/lib/features.ts index 600de8b91..c0b6392b8 100644 --- a/libs/api/lib/features.ts +++ b/libs/api/lib/features.ts @@ -1,4 +1,5 @@ -export interface Feature { - name: string; - enabled: boolean; -} \ No newline at end of file +export interface Features { + LOGIN_BANK_ID_SAME_DEVICE: boolean; +} + +export type FeatureType = keyof Features; diff --git a/libs/api/lib/index.ts b/libs/api/lib/index.ts index 00aa57dd6..c287efb2b 100644 --- a/libs/api/lib/index.ts +++ b/libs/api/lib/index.ts @@ -9,9 +9,9 @@ export { RNCookieManager, ToughCookieJar, wrapReactNativeCookieManager, - wrapToughCookie, + wrapToughCookie, } from './cookies' export { URLSearchParams } from './URLSearchParams' export { wrap }; -export { Feature } from './features' \ No newline at end of file +export { FeatureType, Features } from './features' \ No newline at end of file diff --git a/libs/hooks/src/feature.tsx b/libs/hooks/src/feature.tsx deleted file mode 100644 index 30bc8de68..000000000 --- a/libs/hooks/src/feature.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { Feature } from '@skolplattformen/api' -import React from 'react' - -const FeatureFlagsContext = React.createContext([]) - -interface Props { - features: Feature[] -} - -export const FeatureProvider: React.FC = (props) => { - return ( - - {props.children} - - ) -} - -export const useFeature = (name: string) => { - const features = React.useContext(FeatureFlagsContext) - if (features === null) { - throw new Error('You must wrap your components in a FeatureProvider.') - } - - const feature = features.find((f) => f.name === name) - - return feature && feature.enabled -}