File tree Expand file tree Collapse file tree 6 files changed +61
-4
lines changed
providers/SelectedGroupProvider Expand file tree Collapse file tree 6 files changed +61
-4
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ const SELECTED_GROUP_CONTEXT_INITIAL_VALUE: SelectedGroupContextProps = {
1212
1313export const SelectedGroupContext = createContext < SelectedGroupContextProps > ( SELECTED_GROUP_CONTEXT_INITIAL_VALUE ) ;
1414
15+ export const SELECTED_GROUP_LOCAL_STORAGE_KEY = 'SELECTED_GROUP' ;
16+
1517export const USER_ROLE_IN_GROUP_SELECT_OPTIONS = prepareOptionsForSelect < keyof typeof UserRole > ( {
1618 data : [ 'Guest' , 'Developer' , 'Maintainer' ] ,
1719 renderLabel : ( data ) => data ,
Original file line number Diff line number Diff line change 1+ import { Group } from '../../api' ;
2+
3+ /**
4+ * Guard for checking correctness of the type of the group variable read from localStorage
5+ *
6+ * @returns - Initial selected group or null
7+ */
8+ export const checkIsGroup = ( group : unknown ) : group is Group => {
9+ const checkingGroup = group as Group ;
10+
11+ return (
12+ ! ! checkingGroup ?. data &&
13+ ! ! checkingGroup . data ?. id &&
14+ ! ! checkingGroup . data ?. name &&
15+ ! ! checkingGroup . data ?. owner_id &&
16+ ! ! checkingGroup ?. role
17+ ) ;
18+ } ;
Original file line number Diff line number Diff line change 1+ export * from './checkIsGroup' ;
Original file line number Diff line number Diff line change 11import React , { PropsWithChildren , useState } from 'react' ;
22
3+ import { getInitialSelectedGroup } from '../../utils' ;
34import { Group } from '../../api' ;
4- import { SelectedGroupContext } from '../../constants' ;
5+ import { SELECTED_GROUP_LOCAL_STORAGE_KEY , SelectedGroupContext } from '../../constants' ;
56
67export const SelectedGroupProvider = ( { children } : PropsWithChildren ) => {
7- const [ selectedGroup , setSelectedGroup ] = useState < Group | null > ( null ) ;
8+ const [ selectedGroup , setSelectedGroup ] = useState < Group | null > ( ( ) => getInitialSelectedGroup ( ) ) ;
9+
10+ const handleSelectGroup = ( group : Group ) => {
11+ setSelectedGroup ( group ) ;
12+ localStorage . setItem ( SELECTED_GROUP_LOCAL_STORAGE_KEY , JSON . stringify ( group ) ) ;
13+ } ;
14+
15+ const handleCleanGroup = ( ) => {
16+ setSelectedGroup ( null ) ;
17+ localStorage . removeItem ( SELECTED_GROUP_LOCAL_STORAGE_KEY ) ;
18+ } ;
819
920 const contextValue = {
1021 group : selectedGroup ,
11- selectGroup : setSelectedGroup ,
12- cleanGroup : ( ) => setSelectedGroup ( null ) ,
22+ selectGroup : handleSelectGroup ,
23+ cleanGroup : handleCleanGroup ,
1324 } ;
1425
1526 return < SelectedGroupContext . Provider value = { contextValue } > { children } </ SelectedGroupContext . Provider > ;
Original file line number Diff line number Diff line change 1+ import { Group } from '../../api' ;
2+ import { checkIsGroup } from '../../guards' ;
3+ import { SELECTED_GROUP_LOCAL_STORAGE_KEY } from '../../constants' ;
4+
5+ /**
6+ * Util for getting initial selected group from localStorage
7+ *
8+ * @returns - Initial selected group or null
9+ */
10+ export const getInitialSelectedGroup = ( ) : Group | null => {
11+ let group = localStorage . getItem ( SELECTED_GROUP_LOCAL_STORAGE_KEY ) ;
12+ if ( ! group ) {
13+ return null ;
14+ }
15+ try {
16+ group = JSON . parse ( group ) ;
17+ if ( checkIsGroup ( group ) ) {
18+ return group ;
19+ }
20+ return null ;
21+ } catch {
22+ return null ;
23+ }
24+ } ;
Original file line number Diff line number Diff line change 1+ export * from './getInitialSelectedGroup' ;
You can’t perform that action at this time.
0 commit comments