Skip to content

Commit 348d9d5

Browse files
author
Zabilsya
committed
[DOP-22969] rewrite SelectedGroupProvider
1 parent 2aed439 commit 348d9d5

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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';
15+
export const SELECTED_GROUP_ID_LOCAL_STORAGE_KEY = 'SELECTED_GROUP';
1616

1717
export const USER_ROLE_IN_GROUP_SELECT_OPTIONS = prepareOptionsForSelect<keyof typeof UserRole>({
1818
data: ['Guest', 'Developer', 'Maintainer'],

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
import React, { PropsWithChildren, useState } from 'react';
1+
import React, { PropsWithChildren, useEffect, useState } from 'react';
22

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

76
export const SelectedGroupProvider = ({ children }: PropsWithChildren) => {
8-
const [selectedGroup, setSelectedGroup] = useState<Group | null>(() => getInitialSelectedGroup());
7+
const { data: initialGroup } = useGetInitialGroup({
8+
id: Number(localStorage.getItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY)),
9+
});
10+
11+
const [selectedGroup, setSelectedGroup] = useState<Group | null>(null);
12+
13+
useEffect(() => {
14+
if (initialGroup) {
15+
setSelectedGroup(initialGroup);
16+
}
17+
}, [initialGroup]);
918

1019
const handleSelectGroup = (group: Group) => {
1120
setSelectedGroup(group);
12-
localStorage.setItem(SELECTED_GROUP_LOCAL_STORAGE_KEY, JSON.stringify(group));
21+
localStorage.setItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, group.data.id.toString());
1322
};
1423

1524
const handleCleanGroup = () => {
1625
setSelectedGroup(null);
17-
localStorage.removeItem(SELECTED_GROUP_LOCAL_STORAGE_KEY);
26+
localStorage.removeItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY);
1827
};
1928

2029
const contextValue = {

src/entities/group/utils/getInitialSelectedGroup/index.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/entities/group/utils/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)