feat(GeneralLedger): location query.

This commit is contained in:
a.bouhuolia
2022-03-23 18:18:34 +02:00
parent 3faa765a07
commit 2e743f2232
4 changed files with 60 additions and 22 deletions

View File

@@ -15,9 +15,8 @@ import { GeneralLedgerBody } from './GeneralLedgerBody';
import withGeneralLedgerActions from './withGeneralLedgerActions'; import withGeneralLedgerActions from './withGeneralLedgerActions';
import { transformFilterFormToQuery } from 'containers/FinancialStatements/common'; import { useGeneralLedgerQuery } from './common';
import { compose } from 'utils'; import { compose } from 'utils';
import { getDefaultGeneralLedgerQuery } from './common';
/** /**
* General Ledger (GL) sheet. * General Ledger (GL) sheet.
@@ -26,9 +25,8 @@ function GeneralLedger({
// #withGeneralLedgerActions // #withGeneralLedgerActions
toggleGeneralLedgerFilterDrawer, toggleGeneralLedgerFilterDrawer,
}) { }) {
const [filter, setFilter] = useState({ // General ledger query.
...getDefaultGeneralLedgerQuery(), const { query, setLocationQuery } = useGeneralLedgerQuery();
});
// Handle financial statement filter change. // Handle financial statement filter change.
const handleFilterSubmit = useCallback( const handleFilterSubmit = useCallback(
@@ -38,9 +36,9 @@ function GeneralLedger({
fromDate: moment(filter.fromDate).format('YYYY-MM-DD'), fromDate: moment(filter.fromDate).format('YYYY-MM-DD'),
toDate: moment(filter.toDate).format('YYYY-MM-DD'), toDate: moment(filter.toDate).format('YYYY-MM-DD'),
}; };
setFilter(parsedFilter); setLocationQuery(parsedFilter);
}, },
[setFilter], [setLocationQuery],
); );
// Hide the filter drawer once the page unmount. // Hide the filter drawer once the page unmount.
@@ -52,13 +50,13 @@ function GeneralLedger({
); );
return ( return (
<GeneralLedgerProvider query={transformFilterFormToQuery(filter)}> <GeneralLedgerProvider query={query}>
<GeneralLedgerActionsBar /> <GeneralLedgerActionsBar />
<DashboardPageContent> <DashboardPageContent>
<FinancialStatement> <FinancialStatement>
<GeneralLedgerHeader <GeneralLedgerHeader
pageFilter={filter} pageFilter={query}
onSubmitFilter={handleFilterSubmit} onSubmitFilter={handleFilterSubmit}
/> />
<GeneralLedgerSheetLoadingBar /> <GeneralLedgerSheetLoadingBar />

View File

@@ -14,6 +14,7 @@ import GeneralLedgerHeaderDimensionsPanel from './GeneralLedgerHeaderDimensionsP
import withGeneralLedger from './withGeneralLedger'; import withGeneralLedger from './withGeneralLedger';
import withGeneralLedgerActions from './withGeneralLedgerActions'; import withGeneralLedgerActions from './withGeneralLedgerActions';
import { getDefaultGeneralLedgerQuery } from './common';
import { compose, transformToForm, saveInvoke } from 'utils'; import { compose, transformToForm, saveInvoke } from 'utils';
/** /**
@@ -31,11 +32,7 @@ function GeneralLedgerHeader({
isFilterDrawerOpen, isFilterDrawerOpen,
}) { }) {
// Default values. // Default values.
const defaultValues = { const defaultValues = getDefaultGeneralLedgerQuery();
fromDate: moment().toDate(),
toDate: moment().toDate(),
branchesIds: [],
};
// Initial values. // Initial values.
const initialValues = transformToForm( const initialValues = transformToForm(
@@ -43,30 +40,25 @@ function GeneralLedgerHeader({
...pageFilter, ...pageFilter,
fromDate: moment(pageFilter.fromDate).toDate(), fromDate: moment(pageFilter.fromDate).toDate(),
toDate: moment(pageFilter.toDate).toDate(), toDate: moment(pageFilter.toDate).toDate(),
branchesIds: [],
}, },
defaultValues, defaultValues,
); );
// Validation schema. // Validation schema.
const validationSchema = Yup.object().shape({ const validationSchema = Yup.object().shape({
dateRange: Yup.string().optional(), dateRange: Yup.string().optional(),
fromDate: Yup.date().required(), fromDate: Yup.date().required(),
toDate: Yup.date().min(Yup.ref('fromDate')).required(), toDate: Yup.date().min(Yup.ref('fromDate')).required(),
}); });
// Handle form submit. // Handle form submit.
const handleSubmit = (values, { setSubmitting }) => { const handleSubmit = (values, { setSubmitting }) => {
saveInvoke(onSubmitFilter, values); saveInvoke(onSubmitFilter, values);
toggleDisplayFilterDrawer(); toggleDisplayFilterDrawer(false);
setSubmitting(false); setSubmitting(false);
}; };
// Handle cancel button click. // Handle cancel button click.
const handleCancelClick = () => { const handleCancelClick = () => {
toggleDisplayFilterDrawer(false); toggleDisplayFilterDrawer(false);
}; };
// Handle drawer close action. // Handle drawer close action.
const handleDrawerClose = () => { const handleDrawerClose = () => {
toggleDisplayFilterDrawer(false); toggleDisplayFilterDrawer(false);

View File

@@ -1,6 +1,8 @@
import React, { createContext, useContext } from 'react'; import React, { createContext, useContext } from 'react';
import FinancialReportPage from '../FinancialReportPage'; import FinancialReportPage from '../FinancialReportPage';
import { useGeneralLedgerSheet, useAccounts } from 'hooks/query'; import { useGeneralLedgerSheet } from 'hooks/query';
import { transformFilterFormToQuery } from '../common';
const GeneralLedgerContext = createContext(); const GeneralLedgerContext = createContext();
@@ -8,12 +10,18 @@ const GeneralLedgerContext = createContext();
* General ledger provider. * General ledger provider.
*/ */
function GeneralLedgerProvider({ query, ...props }) { function GeneralLedgerProvider({ query, ...props }) {
// Transformes the report query to request query.
const requestQuery = React.useMemo(
() => transformFilterFormToQuery(query),
[query],
);
const { const {
data: generalLedger, data: generalLedger,
isFetching, isFetching,
isLoading, isLoading,
refetch, refetch,
} = useGeneralLedgerSheet(query, { keepPreviousData: true }); } = useGeneralLedgerSheet(requestQuery, { keepPreviousData: true });
const provider = { const provider = {
generalLedger, generalLedger,

View File

@@ -1,6 +1,12 @@
import React from 'react';
import intl from 'react-intl-universal'; import intl from 'react-intl-universal';
import moment from 'moment'; import moment from 'moment';
import { castArray } from 'lodash';
import { useAppQueryString } from 'hooks';
import { transformToForm } from 'utils';
// Filters accounts options.
export const filterAccountsOptions = [ export const filterAccountsOptions = [
{ {
key: 'all-accounts', key: 'all-accounts',
@@ -25,5 +31,39 @@ export const getDefaultGeneralLedgerQuery = () => {
toDate: moment().endOf('year').format('YYYY-MM-DD'), toDate: moment().endOf('year').format('YYYY-MM-DD'),
basis: 'accural', basis: 'accural',
filterByOption: 'with-transactions', filterByOption: 'with-transactions',
branchesIds: [],
}; };
}; };
/**
* Parses general ledger query of browser location.
*/
const parseGeneralLedgerQuery = (locationQuery) => {
const defaultQuery = getDefaultGeneralLedgerQuery();
const transformed = {
...defaultQuery,
...transformToForm(locationQuery, defaultQuery),
};
return {
...transformed,
// Ensures the branches ids is always array.
branchesIds: castArray(transformed.branchesIds),
};
};
/**
* Retrieves the general ledger location query.
*/
export const useGeneralLedgerQuery = () => {
// Retrieves location query.
const [locationQuery, setLocationQuery] = useAppQueryString();
// Merges the default filter query with location URL query.
const query = React.useMemo(
() => parseGeneralLedgerQuery(locationQuery),
[locationQuery],
);
return { query, locationQuery, setLocationQuery };
};