feat(ui): added org context and user context

This commit is contained in:
akhilmhdh
2023-02-10 22:03:57 +05:30
parent 52a6fe64a7
commit 8dc47110a0
6 changed files with 91 additions and 1 deletions
@@ -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;
};
@@ -0,0 +1 @@
export { OrgProvider, useOrgnization } from './OrganizationContext';