mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 07:40:32 +00:00
feat: dashboard meta boot.
This commit is contained in:
@@ -1,42 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import { Ability } from '@casl/ability';
|
|
||||||
import { createContextualCan } from '@casl/react';
|
|
||||||
|
|
||||||
import {
|
|
||||||
Item_Abilities,
|
|
||||||
AbilitySubject,
|
|
||||||
Inventory_Adjustment_Abilities,
|
|
||||||
Estimate_Abilities,
|
|
||||||
Invoice_Abilities,
|
|
||||||
Receipt_Abilities,
|
|
||||||
PaymentReceive,
|
|
||||||
Bill_Abilities,
|
|
||||||
Payment_Made_Abilities,
|
|
||||||
Customer_Abilities,
|
|
||||||
Vendor_Abilities,
|
|
||||||
Account_Abilities,
|
|
||||||
} from '../common/abilityOption';
|
|
||||||
|
|
||||||
export const AbilityContext = React.createContext();
|
|
||||||
export const Can = createContextualCan(AbilityContext.Consumer);
|
|
||||||
|
|
||||||
const AbilityContextProvider = (props) => {
|
|
||||||
const ability = new Ability([
|
|
||||||
{
|
|
||||||
subject: [AbilitySubject.Account],
|
|
||||||
action: [Account_Abilities.Create],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
subject: [AbilitySubject.Invoice],
|
|
||||||
action: [],
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AbilityContext.Provider value={ability}>
|
|
||||||
{props.children}
|
|
||||||
</AbilityContext.Provider>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default AbilityContextProvider;
|
|
||||||
@@ -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 React from 'react';
|
||||||
import * as R from 'ramda';
|
import * as R from 'ramda';
|
||||||
|
import {
|
||||||
import { useUser, useCurrentOrganization } from '../../hooks/query';
|
useAuthenticatedAccount,
|
||||||
|
useCurrentOrganization,
|
||||||
|
useDashboardMeta,
|
||||||
|
} from '../../hooks/query';
|
||||||
import { useSplashLoading } from '../../hooks/state';
|
import { useSplashLoading } from '../../hooks/state';
|
||||||
import { useWatch, useWatchImmediate, useWhen } from '../../hooks';
|
import { useWatch, useWatchImmediate, useWhen } from '../../hooks';
|
||||||
|
|
||||||
import withAuthentication from '../../containers/Authentication/withAuthentication';
|
|
||||||
|
|
||||||
import { setCookie, getCookie } from '../../utils';
|
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.
|
* Dashboard async booting.
|
||||||
*/
|
*/
|
||||||
function DashboardBootJSX({ authenticatedUserId }) {
|
export function DashboardBoot({ authenticatedUserId }) {
|
||||||
// Fetches the current user's organization.
|
// Fetches the current user's organization.
|
||||||
const {
|
const {
|
||||||
isSuccess: isCurrentOrganizationSuccess,
|
isSuccess: isCurrentOrganizationSuccess,
|
||||||
@@ -22,7 +44,9 @@ function DashboardBootJSX({ authenticatedUserId }) {
|
|||||||
|
|
||||||
// Authenticated user.
|
// Authenticated user.
|
||||||
const { isSuccess: isAuthUserSuccess, isLoading: isAuthUserLoading } =
|
const { isSuccess: isAuthUserSuccess, isLoading: isAuthUserLoading } =
|
||||||
useUser(authenticatedUserId);
|
useAuthenticatedAccount();
|
||||||
|
|
||||||
|
useDashboardMetaBoot();
|
||||||
|
|
||||||
// Initial locale cookie value.
|
// Initial locale cookie value.
|
||||||
const localeCookie = getCookie('locale');
|
const localeCookie = getCookie('locale');
|
||||||
@@ -88,9 +112,3 @@ function DashboardBootJSX({ authenticatedUserId }) {
|
|||||||
);
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DashboardBoot = R.compose(
|
|
||||||
withAuthentication(({ authenticatedUserId }) => ({
|
|
||||||
authenticatedUserId,
|
|
||||||
})),
|
|
||||||
)(DashboardBootJSX);
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import AbilityContextProvider from '../../components/Can';
|
import { DashboardAbilityProvider } from '../../components';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dashboard provider.
|
* Dashboard provider.
|
||||||
*/
|
*/
|
||||||
export default function DashboardProvider({ children }) {
|
export default function DashboardProvider({ children }) {
|
||||||
return <AbilityContextProvider>{children}</AbilityContextProvider>;
|
return <DashboardAbilityProvider>{children}</DashboardAbilityProvider>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
export * from './SplashScreen';
|
export * from './SplashScreen';
|
||||||
export * from './DashboardBoot';
|
export * from './DashboardBoot';
|
||||||
export * from './DashboardThemeProvider';
|
export * from './DashboardThemeProvider';
|
||||||
|
export * from './DashboardAbilityProvider';
|
||||||
@@ -59,7 +59,6 @@ import AvaterCell from './AvaterCell';
|
|||||||
|
|
||||||
import { ItemsMultiSelect } from './Items';
|
import { ItemsMultiSelect } from './Items';
|
||||||
import MoreMenuItems from './MoreMenutItems';
|
import MoreMenuItems from './MoreMenutItems';
|
||||||
import { Can } from './Can';
|
|
||||||
|
|
||||||
export * from './Dialog';
|
export * from './Dialog';
|
||||||
export * from './Menu';
|
export * from './Menu';
|
||||||
@@ -157,5 +156,4 @@ export {
|
|||||||
Card,
|
Card,
|
||||||
AvaterCell,
|
AvaterCell,
|
||||||
MoreMenuItems,
|
MoreMenuItems,
|
||||||
Can,
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import React from 'react';
|
|
||||||
import { useAbility } from '@casl/react';
|
import { useAbility } from '@casl/react';
|
||||||
import { AbilityContext } from '../../components/Can';
|
import { AbilityContext } from '../../components';
|
||||||
|
|
||||||
export const useAbilityContext = () => useAbility(AbilityContext);
|
|
||||||
|
|
||||||
|
export const useAbilityContext = () => useAbility(AbilityContext);
|
||||||
Reference in New Issue
Block a user