Skip to content

Commit 2aed439

Browse files
author
Zabilsya
committed
[DOP-22969] save selected group in localstorage
1 parent 6f05b15 commit 2aed439

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

src/entities/group/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const SELECTED_GROUP_CONTEXT_INITIAL_VALUE: SelectedGroupContextProps = {
1212

1313
export const SelectedGroupContext = createContext<SelectedGroupContextProps>(SELECTED_GROUP_CONTEXT_INITIAL_VALUE);
1414

15+
export const SELECTED_GROUP_LOCAL_STORAGE_KEY = 'SELECTED_GROUP';
16+
1517
export const USER_ROLE_IN_GROUP_SELECT_OPTIONS = prepareOptionsForSelect<keyof typeof UserRole>({
1618
data: ['Guest', 'Developer', 'Maintainer'],
1719
renderLabel: (data) => data,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
};

src/entities/group/guards/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './checkIsGroup';

src/entities/group/providers/SelectedGroupProvider/index.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import React, { PropsWithChildren, useState } from 'react';
22

3+
import { getInitialSelectedGroup } from '../../utils';
34
import { Group } from '../../api';
4-
import { SelectedGroupContext } from '../../constants';
5+
import { SELECTED_GROUP_LOCAL_STORAGE_KEY, SelectedGroupContext } from '../../constants';
56

67
export 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>;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
};

src/entities/group/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './getInitialSelectedGroup';

0 commit comments

Comments
 (0)