From 51c7a0fcd8cf3dc3624dbbdf824cc23b28f97934 Mon Sep 17 00:00:00 2001 From: elforjani13 <39470382+elforjani13@users.noreply.github.com> Date: Mon, 7 Mar 2022 00:18:58 +0200 Subject: [PATCH] feat(dashboard): add features api. --- src/hooks/query/types.js | 5 +++++ src/hooks/query/users.js | 24 ++++++++++++++++-------- src/hooks/state/feature.js | 18 +++++++++++++++++- src/store/dashboard/dashboard.actions.js | 18 +++++++++++++----- src/store/dashboard/dashboard.reducer.js | 15 +++++++++++++-- src/store/dashboard/dashboard.types.js | 1 + 6 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/hooks/query/types.js b/src/hooks/query/types.js index 46e4bb607..62107f0ff 100644 --- a/src/hooks/query/types.js +++ b/src/hooks/query/types.js @@ -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, }; diff --git a/src/hooks/query/users.js b/src/hooks/query/users.js index e42c87c05..4752c3e11 100644 --- a/src/hooks/query/users.js +++ b/src/hooks/query/users.js @@ -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 - } - ) -} \ No newline at end of file + ...props, + }, + useEffect(() => { + if (state.isSuccess) { + setFeatureDashboardMeta(state.data); + } + }, [setFeatureDashboardMeta]), + ); + return state; +} diff --git a/src/hooks/state/feature.js b/src/hooks/state/feature.js index 4ffe96954..ab422608f 100644 --- a/src/hooks/state/feature.js +++ b/src/hooks/state/feature.js @@ -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], + ); +}; diff --git a/src/store/dashboard/dashboard.actions.js b/src/store/dashboard/dashboard.actions.js index d1ef01274..5d6222faa 100644 --- a/src/store/dashboard/dashboard.actions.js +++ b/src/store/dashboard/dashboard.actions.js @@ -67,11 +67,10 @@ export function closeDrawer(name, payload) { export function toggleExpendSidebar(toggle) { return { type: t.SIDEBAR_EXPEND_TOGGLE, - payload: { toggle } + payload: { toggle }, }; } - export function appIsLoading(toggle) { return { type: t.APP_IS_LOADING, @@ -83,7 +82,7 @@ export function appIntlIsLoading(toggle) { return { type: t.APP_INTL_IS_LOADING, payload: { isLoading: toggle }, - }; + }; } /** @@ -101,5 +100,14 @@ export function splashStartLoading() { export function splashStopLoading() { return { type: t.SPLASH_STOP_LOADING, - } -} \ No newline at end of file + }; +} + +export const setFeatureDashboardMeta = ({ features }) => { + return { + type: t.SET_FEATURE_DASHBOARD_META, + payload: { + features, + }, + }; +}; diff --git a/src/store/dashboard/dashboard.reducer.js b/src/store/dashboard/dashboard.reducer.js index a512de8f8..424bf9e0c 100644 --- a/src/store/dashboard/dashboard.reducer.js +++ b/src/store/dashboard/dashboard.reducer.js @@ -3,6 +3,7 @@ import { isUndefined, isNumber } from 'lodash'; import t from 'store/types'; import { persistReducer, purgeStoredState } from 'redux-persist'; import storage from 'redux-persist/lib/storage'; +import { useDashboardMeta } from '../../hooks/query'; const initialState = { pageTitle: '', @@ -20,8 +21,8 @@ const initialState = { appIsLoading: true, appIntlIsLoading: true, features: { - branches: true, - warehouses: true, + // branches: true, + // warehouses: true, }, }; @@ -115,6 +116,16 @@ const reducerInstance = createReducer(initialState, { } }, + [t.SET_FEATURE_DASHBOARD_META]: (state, action) => { + const { features } = action.payload; + const _data = {}; + + features.forEach((feature) => { + _data[feature.name] = feature.is_accessible; + }); + state.features = _data; + }, + [t.SPLASH_STOP_LOADING]: (state) => { state.splashScreenLoading -= 1; state.splashScreenLoading = Math.max(state.splashScreenLoading, 0); diff --git a/src/store/dashboard/dashboard.types.js b/src/store/dashboard/dashboard.types.js index d63cddd01..7374a0437 100644 --- a/src/store/dashboard/dashboard.types.js +++ b/src/store/dashboard/dashboard.types.js @@ -16,4 +16,5 @@ export default { SET_DASHBOARD_BACK_LINK: 'SET_DASHBOARD_BACK_LINK', SPLASH_START_LOADING: 'SPLASH_START_LOADING', SPLASH_STOP_LOADING: 'SPLASH_STOP_LOADING', + SET_FEATURE_DASHBOARD_META: 'SET_FEATURE_DASHBOARD_META', };