Skip to content

Commit

Permalink
feat: 🎸 Feature toggle context
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorlarsson committed Nov 22, 2021
1 parent aa98a40 commit ce05682
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 62 deletions.
57 changes: 33 additions & 24 deletions apps/skolplattformen-sthlm/App.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<string, Api>([
// @ts-expect-error Why is fetch failing here?
Expand Down Expand Up @@ -77,27 +82,31 @@ export default () => {
const api = ApiList.get('goteborg-hjarntorget')!

return (
<SchoolPlatformProvider>
<ApiProvider api={api} storage={AsyncStorage} reporter={reporter}>
<SafeAreaProvider>
<StatusBar
backgroundColor={colorScheme === 'dark' ? '#2E3137' : '#FFF'}
barStyle={colorScheme === 'dark' ? 'light-content' : 'dark-content'}
translucent
/>
<IconRegistry icons={EvaIconsPack} />
<ApplicationProvider
{...eva}
// @ts-expect-error Unknown error
customMapping={customMapping}
theme={colorScheme === 'dark' ? darkTheme : lightTheme}
>
<LanguageProvider cache={true} data={translations}>
<AppNavigator />
</LanguageProvider>
</ApplicationProvider>
</SafeAreaProvider>
</ApiProvider>
</SchoolPlatformProvider>
<FeatureProvider features={featuresHjarntorget}>
<SchoolPlatformProvider>
<ApiProvider api={api} storage={AsyncStorage} reporter={reporter}>
<SafeAreaProvider>
<StatusBar
backgroundColor={colorScheme === 'dark' ? '#2E3137' : '#FFF'}
barStyle={
colorScheme === 'dark' ? 'light-content' : 'dark-content'
}
translucent
/>
<IconRegistry icons={EvaIconsPack} />
<ApplicationProvider
{...eva}
// @ts-expect-error Unknown error
customMapping={customMapping}
theme={colorScheme === 'dark' ? darkTheme : lightTheme}
>
<LanguageProvider cache={true} data={translations}>
<AppNavigator />
</LanguageProvider>
</ApplicationProvider>
</SafeAreaProvider>
</ApiProvider>
</SchoolPlatformProvider>
</FeatureProvider>
)
}
7 changes: 6 additions & 1 deletion apps/skolplattformen-sthlm/components/login.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
)
Expand All @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions apps/skolplattformen-sthlm/context/feature/featureContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Features, FeatureType } from '@skolplattformen/api'
import React from 'react'

export const FeatureFlagsContext = React.createContext<Features>({
LOGIN_BANK_ID_SAME_DEVICE: false,
})

interface Props {
features: Features
}

export const FeatureProvider: React.FC<Props> = (props) => {
return (
<FeatureFlagsContext.Provider value={props.features} {...props}>
{props.children}
</FeatureFlagsContext.Provider>
)
}
12 changes: 12 additions & 0 deletions apps/skolplattformen-sthlm/hooks/useFeature.tsx
Original file line number Diff line number Diff line change
@@ -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<Features>(FeatureFlagsContext)
if (features === null) {
throw new Error('You must wrap your components in a FeatureProvider.')
}

return features[name]
}
8 changes: 4 additions & 4 deletions libs/api-hjarntorget/lib/features.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Feature } from '@skolplattformen/api'
import { Features } from '@skolplattformen/api'

export const features: Feature[] = [
{ name: 'login', enabled: true },
]
export const features: Features = {
LOGIN_BANK_ID_SAME_DEVICE: false
}
1 change: 1 addition & 0 deletions libs/api-hjarntorget/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Api, FetcherOptions, Fetch, RNCookieManager,
ToughCookieJar,
wrapReactNativeCookieManager,
wrapToughCookie } from '@skolplattformen/api'
export { features } from './features'

const init = (
fetchImpl: Fetch,
Expand Down
5 changes: 5 additions & 0 deletions libs/api-skolplattformen/lib/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Features } from '@skolplattformen/api'

export const features: Features = {
LOGIN_BANK_ID_SAME_DEVICE: true
}
1 change: 1 addition & 0 deletions libs/api-skolplattformen/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Api, FetcherOptions, Fetch, RNCookieManager,
ToughCookieJar,
wrapReactNativeCookieManager,
wrapToughCookie } from '@skolplattformen/api'
export { features } from './features'

const init = (
fetchImpl: Fetch,
Expand Down
9 changes: 5 additions & 4 deletions libs/api/lib/features.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Feature {
name: string;
enabled: boolean;
}
export interface Features {
LOGIN_BANK_ID_SAME_DEVICE: boolean;
}

export type FeatureType = keyof Features;
4 changes: 2 additions & 2 deletions libs/api/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export {
RNCookieManager,
ToughCookieJar,
wrapReactNativeCookieManager,
wrapToughCookie,
wrapToughCookie,
} from './cookies'
export { URLSearchParams } from './URLSearchParams'

export { wrap };
export { Feature } from './features'
export { FeatureType, Features } from './features'
27 changes: 0 additions & 27 deletions libs/hooks/src/feature.tsx

This file was deleted.

0 comments on commit ce05682

Please sign in to comment.