File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
web-app/src/services/admin Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ import * as React from 'react'
2+
3+ type Props = {
4+ children : React . ReactElement
5+ }
6+
7+ type State = {
8+ adminMode : boolean
9+ }
10+
11+ type Action = { type : 'ADMIN_MODE_ON' | 'ADMIN_MODE_OFF' }
12+
13+ const AdminContext = React . createContext < { state : State ; dispatch : ( action : Action ) => void } > ( {
14+ state : { adminMode : false } ,
15+ dispatch : ( ) => { } ,
16+ } )
17+
18+ export const AdminProvider = ( props : Props ) => {
19+ const [ state , dispatch ] = React . useReducer (
20+ ( state : State , action : Action ) => {
21+ switch ( action . type ) {
22+ case 'ADMIN_MODE_ON' :
23+ return { ...state , adminMode : true }
24+ case 'ADMIN_MODE_OFF' :
25+ return { ...state , adminMode : false }
26+ default :
27+ throw new Error ( )
28+ }
29+ } ,
30+ { adminMode : false } ,
31+ )
32+ return < AdminContext . Provider value = { { state, dispatch } } > { props . children } </ AdminContext . Provider >
33+ }
34+
35+ export const AdminConsumer = AdminContext . Consumer
You can’t perform that action at this time.
0 commit comments