Skip to content

Commit a8967b5

Browse files
ZabilsyaZabilsya
andauthored
[DOP-22969] save selected group in localstorage (#60)
* [DOP-22969] save selected group in localstorage * [DOP-22969] rewrite SelectedGroupProvider --------- Co-authored-by: Zabilsya <[email protected]>
1 parent 760277f commit a8967b5

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './useGetGroup';
22
export * from './useDeleteGroupUser';
3+
export * from './useGetInitialGroup';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useQuery, UseQueryResult } from '@tanstack/react-query';
2+
3+
import { GetGroupRequest, Group } from '../../types';
4+
import { GroupQueryKey } from '../../keys';
5+
import { groupService } from '../../groupService';
6+
7+
/** Hook for getting group info from backend */
8+
export const useGetInitialGroup = ({ id }: GetGroupRequest): UseQueryResult<Group> => {
9+
return useQuery({
10+
queryKey: [GroupQueryKey.GET_GROUP, id],
11+
queryFn: () => groupService.getGroup({ id }),
12+
enabled: !!id,
13+
});
14+
};

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_ID_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: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
import React, { PropsWithChildren, useState } from 'react';
1+
import React, { PropsWithChildren, useEffect, useState } from 'react';
22

3-
import { Group } from '../../api';
4-
import { SelectedGroupContext } from '../../constants';
3+
import { Group, useGetInitialGroup } from '../../api';
4+
import { SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, SelectedGroupContext } from '../../constants';
55

66
export const SelectedGroupProvider = ({ children }: PropsWithChildren) => {
7+
const { data: initialGroup } = useGetInitialGroup({
8+
id: Number(localStorage.getItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY)),
9+
});
10+
711
const [selectedGroup, setSelectedGroup] = useState<Group | null>(null);
812

13+
useEffect(() => {
14+
if (initialGroup) {
15+
setSelectedGroup(initialGroup);
16+
}
17+
}, [initialGroup]);
18+
19+
const handleSelectGroup = (group: Group) => {
20+
setSelectedGroup(group);
21+
localStorage.setItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, group.data.id.toString());
22+
};
23+
24+
const handleCleanGroup = () => {
25+
setSelectedGroup(null);
26+
localStorage.removeItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY);
27+
};
28+
929
const contextValue = {
1030
group: selectedGroup,
11-
selectGroup: setSelectedGroup,
12-
cleanGroup: () => setSelectedGroup(null),
31+
selectGroup: handleSelectGroup,
32+
cleanGroup: handleCleanGroup,
1333
};
1434

1535
return <SelectedGroupContext.Provider value={contextValue}>{children}</SelectedGroupContext.Provider>;

0 commit comments

Comments
 (0)