feat(dashboard): add features api.

This commit is contained in:
elforjani13
2022-03-07 00:18:58 +02:00
parent a639101e28
commit 51c7a0fcd8
6 changed files with 65 additions and 16 deletions

View File

@@ -209,6 +209,10 @@ const BRANCHES = {
BRANCH: 'BRANCH',
};
const DASHBOARD = {
DASHBOARD_META: 'DASHBOARD_META',
};
export default {
...ACCOUNTS,
...BILLS,
@@ -239,4 +243,5 @@ export default {
...WAREHOUSES,
...WAREHOUSE_TRANSFERS,
...BRANCHES,
...DASHBOARD,
};

View File

@@ -1,6 +1,8 @@
import { useEffect } from 'react';
import { useMutation, useQueryClient } from 'react-query';
import { useQueryTenant, useRequestQuery } from '../useQueryRequest';
import useApiRequest from '../useRequest';
import { useSetFeatureDashboardMeta } from '../state/feature';
import t from './types';
// Common invalidate queries.
@@ -126,8 +128,7 @@ export function useUser(id, props) {
);
}
export function useAuthenticatedAccount(props){
export function useAuthenticatedAccount(props) {
return useRequestQuery(
['AuthenticatedAccount'],
{
@@ -146,8 +147,9 @@ export function useAuthenticatedAccount(props){
* Fetches the dashboard meta.
*/
export function useDashboardMeta(props) {
return useRequestQuery(
['DashboardMeta'],
const setFeatureDashboardMeta = useSetFeatureDashboardMeta();
const state = useRequestQuery(
[t.DASHBOARD_META],
{
method: 'get',
url: 'dashboard/boot',
@@ -155,7 +157,13 @@ export function useDashboardMeta(props) {
{
select: (res) => res.data.meta,
defaultData: {},
...props
}
)
}
...props,
},
useEffect(() => {
if (state.isSuccess) {
setFeatureDashboardMeta(state.data);
}
}, [setFeatureDashboardMeta]),
);
return state;
}

View File

@@ -1,5 +1,7 @@
import { useSelector } from 'react-redux';
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { createSelector } from 'reselect';
import { setFeatureDashboardMeta } from '../../store/dashboard/dashboard.actions';
const featuresSelector = createSelector(
(state) => state.dashboard.features,
@@ -15,3 +17,17 @@ export const useFeatureCan = () => {
},
};
};
/**
* Sets features.
*/
export const useSetFeatureDashboardMeta = () => {
const dispatch = useDispatch();
return React.useCallback(
(features) => {
dispatch(setFeatureDashboardMeta(features));
},
[dispatch],
);
};