diff --git a/src/entities/group/api/hooks/index.ts b/src/entities/group/api/hooks/index.ts index 0e5de58d..6cd4257a 100644 --- a/src/entities/group/api/hooks/index.ts +++ b/src/entities/group/api/hooks/index.ts @@ -1,2 +1,3 @@ export * from './useGetGroup'; export * from './useDeleteGroupUser'; +export * from './useGetInitialGroup'; diff --git a/src/entities/group/api/hooks/useGetInitialGroup/index.ts b/src/entities/group/api/hooks/useGetInitialGroup/index.ts new file mode 100644 index 00000000..e5387a1d --- /dev/null +++ b/src/entities/group/api/hooks/useGetInitialGroup/index.ts @@ -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 => { + return useQuery({ + queryKey: [GroupQueryKey.GET_GROUP, id], + queryFn: () => groupService.getGroup({ id }), + enabled: !!id, + }); +}; diff --git a/src/entities/group/constants.ts b/src/entities/group/constants.ts index 79404e4a..041e9294 100644 --- a/src/entities/group/constants.ts +++ b/src/entities/group/constants.ts @@ -12,6 +12,8 @@ const SELECTED_GROUP_CONTEXT_INITIAL_VALUE: SelectedGroupContextProps = { export const SelectedGroupContext = createContext(SELECTED_GROUP_CONTEXT_INITIAL_VALUE); +export const SELECTED_GROUP_ID_LOCAL_STORAGE_KEY = 'SELECTED_GROUP'; + export const USER_ROLE_IN_GROUP_SELECT_OPTIONS = prepareOptionsForSelect({ data: ['Guest', 'Developer', 'Maintainer'], renderLabel: (data) => data, diff --git a/src/entities/group/guards/checkIsGroup/index.ts b/src/entities/group/guards/checkIsGroup/index.ts new file mode 100644 index 00000000..8237ac73 --- /dev/null +++ b/src/entities/group/guards/checkIsGroup/index.ts @@ -0,0 +1,18 @@ +import { Group } from '../../api'; + +/** + * Guard for checking correctness of the type of the group variable read from localStorage + * + * @returns - Initial selected group or null + */ +export const checkIsGroup = (group: unknown): group is Group => { + const checkingGroup = group as Group; + + return ( + !!checkingGroup?.data && + !!checkingGroup.data?.id && + !!checkingGroup.data?.name && + !!checkingGroup.data?.owner_id && + !!checkingGroup?.role + ); +}; diff --git a/src/entities/group/guards/index.ts b/src/entities/group/guards/index.ts new file mode 100644 index 00000000..49d606aa --- /dev/null +++ b/src/entities/group/guards/index.ts @@ -0,0 +1 @@ +export * from './checkIsGroup'; diff --git a/src/entities/group/providers/SelectedGroupProvider/index.tsx b/src/entities/group/providers/SelectedGroupProvider/index.tsx index 77a0545e..f698aba2 100644 --- a/src/entities/group/providers/SelectedGroupProvider/index.tsx +++ b/src/entities/group/providers/SelectedGroupProvider/index.tsx @@ -1,15 +1,35 @@ -import React, { PropsWithChildren, useState } from 'react'; +import React, { PropsWithChildren, useEffect, useState } from 'react'; -import { Group } from '../../api'; -import { SelectedGroupContext } from '../../constants'; +import { Group, useGetInitialGroup } from '../../api'; +import { SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, SelectedGroupContext } from '../../constants'; export const SelectedGroupProvider = ({ children }: PropsWithChildren) => { + const { data: initialGroup } = useGetInitialGroup({ + id: Number(localStorage.getItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY)), + }); + const [selectedGroup, setSelectedGroup] = useState(null); + useEffect(() => { + if (initialGroup) { + setSelectedGroup(initialGroup); + } + }, [initialGroup]); + + const handleSelectGroup = (group: Group) => { + setSelectedGroup(group); + localStorage.setItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY, group.data.id.toString()); + }; + + const handleCleanGroup = () => { + setSelectedGroup(null); + localStorage.removeItem(SELECTED_GROUP_ID_LOCAL_STORAGE_KEY); + }; + const contextValue = { group: selectedGroup, - selectGroup: setSelectedGroup, - cleanGroup: () => setSelectedGroup(null), + selectGroup: handleSelectGroup, + cleanGroup: handleCleanGroup, }; return {children};