mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
feat: dashboard meta boot.
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
import React from 'react';
|
||||
import { useUser } from 'hooks/query';
|
||||
import withAuthentication from '../../containers/Authentication/withAuthentication';
|
||||
|
||||
const AuthenticatedUserContext = React.createContext();
|
||||
|
||||
function AuthenticatedUserComponent({ authenticatedUserId, children }) {
|
||||
const { data: user, ...restProps } = useUser(authenticatedUserId);
|
||||
|
||||
return (
|
||||
<AuthenticatedUserContext.Provider
|
||||
value={{
|
||||
user,
|
||||
...restProps,
|
||||
}}
|
||||
children={children}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const AuthenticatedUser = withAuthentication(
|
||||
({ authenticatedUserId }) => ({
|
||||
authenticatedUserId,
|
||||
}),
|
||||
)(AuthenticatedUserComponent);
|
||||
|
||||
export const useAuthenticatedUser = () =>
|
||||
React.useContext(AuthenticatedUserContext);
|
||||
25
src/components/Dashboard/DashboardAbilityProvider.js
Normal file
25
src/components/Dashboard/DashboardAbilityProvider.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import React from 'react';
|
||||
import { Ability } from '@casl/ability';
|
||||
import { createContextualCan } from '@casl/react';
|
||||
import { useDashboardMeta } from '../../hooks/query';
|
||||
|
||||
export const AbilityContext = React.createContext();
|
||||
export const Can = createContextualCan(AbilityContext.Consumer);
|
||||
|
||||
/**
|
||||
* Dashboard ability provider.
|
||||
*/
|
||||
export function DashboardAbilityProvider({ children }) {
|
||||
const {
|
||||
data: { abilities },
|
||||
} = useDashboardMeta();
|
||||
|
||||
// Ability instance.
|
||||
const ability = new Ability([]);
|
||||
|
||||
return (
|
||||
<AbilityContext.Provider value={ability}>
|
||||
{children}
|
||||
</AbilityContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -1,18 +1,40 @@
|
||||
import React from 'react';
|
||||
import * as R from 'ramda';
|
||||
|
||||
import { useUser, useCurrentOrganization } from '../../hooks/query';
|
||||
import {
|
||||
useAuthenticatedAccount,
|
||||
useCurrentOrganization,
|
||||
useDashboardMeta,
|
||||
} from '../../hooks/query';
|
||||
import { useSplashLoading } from '../../hooks/state';
|
||||
import { useWatch, useWatchImmediate, useWhen } from '../../hooks';
|
||||
|
||||
import withAuthentication from '../../containers/Authentication/withAuthentication';
|
||||
|
||||
import { setCookie, getCookie } from '../../utils';
|
||||
|
||||
/**
|
||||
* Boots dashboard meta.
|
||||
*/
|
||||
function useDashboardMetaBoot() {
|
||||
const {
|
||||
data: dashboardMeta,
|
||||
isLoading: isDashboardMetaLoading,
|
||||
isSuccess: isDashboardMetaSuccess,
|
||||
} = useDashboardMeta();
|
||||
|
||||
const [startLoading, stopLoading] = useSplashLoading();
|
||||
|
||||
useWatchImmediate((value) => {
|
||||
value && startLoading();
|
||||
}, isDashboardMetaLoading);
|
||||
|
||||
useWatchImmediate(() => {
|
||||
isDashboardMetaSuccess && stopLoading();
|
||||
}, isDashboardMetaSuccess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard async booting.
|
||||
*/
|
||||
function DashboardBootJSX({ authenticatedUserId }) {
|
||||
export function DashboardBoot({ authenticatedUserId }) {
|
||||
// Fetches the current user's organization.
|
||||
const {
|
||||
isSuccess: isCurrentOrganizationSuccess,
|
||||
@@ -22,7 +44,9 @@ function DashboardBootJSX({ authenticatedUserId }) {
|
||||
|
||||
// Authenticated user.
|
||||
const { isSuccess: isAuthUserSuccess, isLoading: isAuthUserLoading } =
|
||||
useUser(authenticatedUserId);
|
||||
useAuthenticatedAccount();
|
||||
|
||||
useDashboardMetaBoot();
|
||||
|
||||
// Initial locale cookie value.
|
||||
const localeCookie = getCookie('locale');
|
||||
@@ -88,9 +112,3 @@ function DashboardBootJSX({ authenticatedUserId }) {
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
export const DashboardBoot = R.compose(
|
||||
withAuthentication(({ authenticatedUserId }) => ({
|
||||
authenticatedUserId,
|
||||
})),
|
||||
)(DashboardBootJSX);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import React from 'react';
|
||||
import AbilityContextProvider from '../../components/Can';
|
||||
import { DashboardAbilityProvider } from '../../components';
|
||||
|
||||
/**
|
||||
* Dashboard provider.
|
||||
*/
|
||||
export default function DashboardProvider({ children }) {
|
||||
return <AbilityContextProvider>{children}</AbilityContextProvider>;
|
||||
return <DashboardAbilityProvider>{children}</DashboardAbilityProvider>;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './SplashScreen';
|
||||
export * from './DashboardBoot';
|
||||
export * from './DashboardThemeProvider';
|
||||
export * from './DashboardAbilityProvider';
|
||||
Reference in New Issue
Block a user