feat(ui): added org context and user context

This commit is contained in:
akhilmhdh
2023-02-08 23:06:37 +05:30
parent 52a6fe64a7
commit 8dc47110a0
6 changed files with 91 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
import { createContext, ReactNode, useContext, useMemo } from 'react';
import { useGetOrganization } from '@app/hooks/api';
import { Organization } from '@app/hooks/api/types';
import { useWorkspace } from '../WorkspaceContext';
type TOrgContext = {
orgs?: Organization[];
currentOrg?: Organization;
isLoading: boolean;
};
const OrgContext = createContext<TOrgContext | null>(null);
type Props = {
children: ReactNode;
};
export const OrgProvider = ({ children }: Props): JSX.Element => {
const { currentWorkspace } = useWorkspace();
const { data: userOrgs, isLoading } = useGetOrganization();
const currentWsOrgID = currentWorkspace?.organization;
// memorize the workspace details for the context
const value = useMemo<TOrgContext>(
() => ({
org: userOrgs,
currentOrg: (userOrgs || []).find(({ _id }) => _id === currentWsOrgID),
isLoading
}),
[currentWsOrgID, userOrgs, isLoading]
);
return <OrgContext.Provider value={value}>{children}</OrgContext.Provider>;
};
export const useOrgnization = () => {
const ctx = useContext(OrgContext);
if (!ctx) {
throw new Error('useOrgnization to be used within <OrgContext.Provider>');
}
return ctx;
};

View File

@@ -0,0 +1 @@
export { OrgProvider, useOrgnization } from './OrganizationContext';

View File

@@ -0,0 +1,38 @@
import { createContext, ReactNode, useContext, useMemo } from 'react';
import { useGetUser } from '@app/hooks/api';
import { User } from '@app/hooks/api/types';
type TUserContext = {
user: User;
isLoading: boolean;
};
const UserContext = createContext<TUserContext | null>(null);
type Props = {
children: ReactNode;
};
export const UserProvider = ({ children }: Props): JSX.Element => {
const { data, isLoading } = useGetUser();
// memorize the workspace details for the context
const value = useMemo<TUserContext>(() => {
return {
user: data!,
isLoading
};
}, [data, isLoading]);
return <UserContext.Provider value={value}>{children}</UserContext.Provider>;
};
export const useUser = () => {
const ctx = useContext(UserContext);
if (!ctx) {
throw new Error('useUser has to be used within <UserContext.Provider>');
}
return ctx;
};

View File

@@ -0,0 +1 @@
export { UserProvider, useUser } from './UserContext';

View File

@@ -23,9 +23,10 @@ export const WorkspaceProvider = ({ children }: Props): JSX.Element => {
// memorize the workspace details for the context
const value = useMemo<TWorkspaceContext>(() => {
const wsId = workspaceId || localStorage.getItem('projectData.id');
return {
workspaces: ws || [],
currentWorkspace: (ws || []).find(({ _id: id }) => id === workspaceId),
currentWorkspace: (ws || []).find(({ _id: id }) => id === wsId),
isLoading
};
}, [ws, workspaceId, isLoading]);

View File

@@ -1,2 +1,5 @@
export { AuthProvider } from './AuthContext';
export { OrgProvider, useOrgnization } from './OrganizationContext';
export { SubscriptionProvider, useSubscription } from './SubscriptionContext';
export { UserProvider, useUser } from './UserContext';
export { useWorkspace, WorkspaceProvider } from './WorkspaceContext';