Skip to content

Commit

Permalink
[DOP-22969] rewrite SelectedGroupProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Zabilsya committed Jan 17, 2025
1 parent c0ff15b commit ad5d2d4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/entities/group/api/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useGetGroup';
export * from './useDeleteGroupUser';
export * from './useGetInitialGroup';
14 changes: 14 additions & 0 deletions src/entities/group/api/hooks/useGetInitialGroup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useQuery, UseQueryResult } from '@tanstack/react-query';

import { GetGroupRequest, Group } from '../../types';
import { GroupQueryKey } from '../../keys';
import { groupService } from '../../groupService';

/** Hook for getting group info from backend */
export const useGetInitialGroup = ({ id }: GetGroupRequest): UseQueryResult<Group> => {
return useQuery({
queryKey: [GroupQueryKey.GET_GROUP, id],
queryFn: () => groupService.getGroup({ id }),
enabled: !!id,
});
};
2 changes: 1 addition & 1 deletion src/entities/group/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SELECTED_GROUP_CONTEXT_INITIAL_VALUE: SelectedGroupContextProps = {

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

export const SELECTED_GROUP_LOCAL_STORAGE_KEY = 'SELECTED_GROUP';
export const SELECTED_GROUP_ID_LOCAL_STORAGE_KEY = 'SELECTED_GROUP';

export const USER_ROLE_IN_GROUP_SELECT_OPTIONS = prepareOptionsForSelect<keyof typeof UserRole>({
data: ['Guest', 'Developer', 'Maintainer'],
Expand Down
23 changes: 16 additions & 7 deletions src/entities/group/providers/SelectedGroupProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import React, { PropsWithChildren, useState } from 'react';
import React, { PropsWithChildren, useEffect, useState } from 'react';

import { getInitialSelectedGroup } from '../../utils';
import { Group } from '../../api';
import { SELECTED_GROUP_LOCAL_STORAGE_KEY, SelectedGroupContext } from '../../constants';
import { Group, useGetInitialGroup } from '../../api';
import { SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, SelectedGroupContext } from '../../constants';

export const SelectedGroupProvider = ({ children }: PropsWithChildren) => {
const [selectedGroup, setSelectedGroup] = useState<Group | null>(() => getInitialSelectedGroup());
const { data: initialGroup } = useGetInitialGroup({
id: Number(localStorage.getItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY)),
});

const [selectedGroup, setSelectedGroup] = useState<Group | null>(null);

useEffect(() => {
if (initialGroup) {
setSelectedGroup(initialGroup);
}
}, [initialGroup]);

const handleSelectGroup = (group: Group) => {
setSelectedGroup(group);
localStorage.setItem(SELECTED_GROUP_LOCAL_STORAGE_KEY, JSON.stringify(group));
localStorage.setItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, group.data.id.toString());
};

const handleCleanGroup = () => {
setSelectedGroup(null);
localStorage.removeItem(SELECTED_GROUP_LOCAL_STORAGE_KEY);
localStorage.removeItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY);
};

const contextValue = {
Expand Down
24 changes: 0 additions & 24 deletions src/entities/group/utils/getInitialSelectedGroup/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/entities/group/utils/index.ts

This file was deleted.

0 comments on commit ad5d2d4

Please sign in to comment.