forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Started on provider for selecting platform + feature to
- Loading branch information
1 parent
ba45cfd
commit 1aaaeb6
Showing
9 changed files
with
122 additions
and
35 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
apps/skolplattformen-sthlm/context/schoolPlatform/schoolPlatformContext.tsx
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,37 @@ | ||
import useSettingsStorage from '../../hooks/useSettingsStorage' | ||
import React, { createContext } from 'react' | ||
|
||
interface SchoolPlatformProps { | ||
currentSchoolPlatform: string | ||
changeSchoolPlatform: (platform: string) => void | ||
} | ||
|
||
const defaultState: SchoolPlatformProps = { | ||
currentSchoolPlatform: 'stockholm-skolplattformen', | ||
changeSchoolPlatform: (platform: string) => | ||
console.log('DEBUG ONLY: changing to', platform), | ||
} | ||
|
||
export const SchoolPlatformProvider: React.FC = ({ children }) => { | ||
const [currentSchoolPlatform, setCurrentSchoolPlatform] = useSettingsStorage( | ||
'currentSchoolPlatform' | ||
) | ||
|
||
const changeSchoolPlatform = (platform: string) => { | ||
setCurrentSchoolPlatform(platform) | ||
} | ||
|
||
return ( | ||
<SchoolPlatformContext.Provider | ||
value={{ | ||
currentSchoolPlatform, | ||
changeSchoolPlatform, | ||
}} | ||
> | ||
{children} | ||
</SchoolPlatformContext.Provider> | ||
) | ||
} | ||
|
||
export const SchoolPlatformContext = | ||
createContext<SchoolPlatformProps>(defaultState) |
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,5 @@ | ||
import { Feature } from '@skolplattformen/api' | ||
|
||
export const features: Feature[] = [ | ||
{ name: 'login', enabled: true }, | ||
] |
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,4 @@ | ||
export interface Feature { | ||
name: string; | ||
enabled: boolean; | ||
} |
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,27 @@ | ||
import { Feature } from '@skolplattformen/api' | ||
import React from 'react' | ||
|
||
const FeatureFlagsContext = React.createContext<Feature[]>([]) | ||
|
||
interface Props { | ||
features: Feature[] | ||
} | ||
|
||
export const FeatureProvider: React.FC<Props> = (props) => { | ||
return ( | ||
<FeatureFlagsContext.Provider value={props.features} {...props}> | ||
{props.children} | ||
</FeatureFlagsContext.Provider> | ||
) | ||
} | ||
|
||
export const useFeature = (name: string) => { | ||
const features = React.useContext<Feature[]>(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 | ||
} |