mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
feat(BS|PL): integrate report query with location query.
This commit is contained in:
@@ -72,6 +72,7 @@
|
|||||||
"postcss-preset-env": "6.7.0",
|
"postcss-preset-env": "6.7.0",
|
||||||
"postcss-rtl": "^1.7.3",
|
"postcss-rtl": "^1.7.3",
|
||||||
"postcss-safe-parser": "4.0.1",
|
"postcss-safe-parser": "4.0.1",
|
||||||
|
"query-string": "^7.1.1",
|
||||||
"ramda": "^0.27.1",
|
"ramda": "^0.27.1",
|
||||||
"react": "^16.12.0",
|
"react": "^16.12.0",
|
||||||
"react-app-polyfill": "^1.0.6",
|
"react-app-polyfill": "^1.0.6",
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import FinancialStatementHeader from '../../FinancialStatements/FinancialStateme
|
|||||||
|
|
||||||
import { compose, transformToForm } from 'utils';
|
import { compose, transformToForm } from 'utils';
|
||||||
import {
|
import {
|
||||||
getBalanceSheetHeaderDefaultValues,
|
|
||||||
getBalanceSheetHeaderValidationSchema,
|
getBalanceSheetHeaderValidationSchema,
|
||||||
|
getDefaultBalanceSheetQuery,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,7 +31,7 @@ function BalanceSheetHeader({
|
|||||||
// #withBalanceSheetActions
|
// #withBalanceSheetActions
|
||||||
toggleBalanceSheetFilterDrawer: toggleFilterDrawer,
|
toggleBalanceSheetFilterDrawer: toggleFilterDrawer,
|
||||||
}) {
|
}) {
|
||||||
const defaultValues = getBalanceSheetHeaderDefaultValues();
|
const defaultValues = getDefaultBalanceSheetQuery();
|
||||||
|
|
||||||
// Filter form initial values.
|
// Filter form initial values.
|
||||||
const initialValues = transformToForm(
|
const initialValues = transformToForm(
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import * as Yup from 'yup';
|
|||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
|
|
||||||
import { transformToForm } from 'utils';
|
import { transformToForm } from 'utils';
|
||||||
import { useLocationQuery, useMutateLocationQuery } from 'hooks';
|
import { useAppQueryString } from 'hooks';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the default balance sheet query.
|
* Retrieves the default balance sheet query.
|
||||||
@@ -27,8 +27,8 @@ export const getDefaultBalanceSheetQuery = () => ({
|
|||||||
previousPeriodPercentageChange: false,
|
previousPeriodPercentageChange: false,
|
||||||
|
|
||||||
// Percentage columns.
|
// Percentage columns.
|
||||||
percentageColumn: false,
|
percentageOfColumn: false,
|
||||||
percentageRow: false,
|
percentageOfRow: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,10 +36,7 @@ export const getDefaultBalanceSheetQuery = () => ({
|
|||||||
*/
|
*/
|
||||||
export const useBalanceSheetQuery = () => {
|
export const useBalanceSheetQuery = () => {
|
||||||
// Retrieves location query.
|
// Retrieves location query.
|
||||||
const locationQuery = useLocationQuery();
|
const [locationQuery, setLocationQuery] = useAppQueryString();
|
||||||
|
|
||||||
// Mutates the location query.
|
|
||||||
const { mutate: setLocationQuery } = useMutateLocationQuery();
|
|
||||||
|
|
||||||
// Merges the default filter query with location URL query.
|
// Merges the default filter query with location URL query.
|
||||||
const query = React.useMemo(() => {
|
const query = React.useMemo(() => {
|
||||||
@@ -47,7 +44,7 @@ export const useBalanceSheetQuery = () => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...defaultQuery,
|
...defaultQuery,
|
||||||
...transformToForm(Object.fromEntries([...locationQuery]), defaultQuery),
|
...transformToForm(locationQuery, defaultQuery),
|
||||||
};
|
};
|
||||||
}, [locationQuery]);
|
}, [locationQuery]);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import moment from 'moment';
|
|||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
|
|
||||||
import { useMutateLocationQuery, useLocationQuery } from 'hooks';
|
import { useAppQueryString } from 'hooks';
|
||||||
import { transformToForm } from 'utils';
|
import { transformToForm } from 'utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,10 +38,7 @@ export const getDefaultProfitLossQuery = () => ({
|
|||||||
*/
|
*/
|
||||||
export const useProfitLossSheetQuery = () => {
|
export const useProfitLossSheetQuery = () => {
|
||||||
// Retrieves location query.
|
// Retrieves location query.
|
||||||
const locationQuery = useLocationQuery();
|
const [locationQuery, setLocationQuery] = useAppQueryString();
|
||||||
|
|
||||||
// Mutate the location query.
|
|
||||||
const { mutate: setLocationQuery } = useMutateLocationQuery();
|
|
||||||
|
|
||||||
// Merges the default query with location query.
|
// Merges the default query with location query.
|
||||||
const query = React.useMemo(() => {
|
const query = React.useMemo(() => {
|
||||||
@@ -49,7 +46,7 @@ export const useProfitLossSheetQuery = () => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...defaultQuery,
|
...defaultQuery,
|
||||||
...transformToForm(Object.fromEntries([...locationQuery]), defaultQuery),
|
...transformToForm(locationQuery, defaultQuery),
|
||||||
};
|
};
|
||||||
}, [locationQuery]);
|
}, [locationQuery]);
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import useAutofocus from './useAutofocus';
|
|||||||
import { useLocalStorage } from './utils/useLocalStorage';
|
import { useLocalStorage } from './utils/useLocalStorage';
|
||||||
|
|
||||||
export * from './utils';
|
export * from './utils';
|
||||||
|
export * from './useQueryString';
|
||||||
|
|
||||||
export function useIsValuePassed(value, compatatorValue) {
|
export function useIsValuePassed(value, compatatorValue) {
|
||||||
const cache = useRef([value]);
|
const cache = useRef([value]);
|
||||||
@@ -54,29 +55,3 @@ export function useMemorizedColumnsWidths(tableName) {
|
|||||||
};
|
};
|
||||||
return [get, save, handleColumnResizing];
|
return [get, save, handleColumnResizing];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the URL location search params.
|
|
||||||
*/
|
|
||||||
export const useLocationQuery = () => {
|
|
||||||
const { search } = useLocation();
|
|
||||||
|
|
||||||
return useMemo(() => {
|
|
||||||
return new URLSearchParams(search);
|
|
||||||
}, [search]);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mutates the URL location params.
|
|
||||||
*/
|
|
||||||
export const useMutateLocationQuery = () => {
|
|
||||||
const location = useLocation();
|
|
||||||
const history = useHistory();
|
|
||||||
|
|
||||||
return {
|
|
||||||
mutate: (query) => {
|
|
||||||
const params = new URLSearchParams(query).toString();
|
|
||||||
history.push({ pathname: location.pathname, search: params.toString() });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|||||||
89
src/hooks/useQueryString.ts
Normal file
89
src/hooks/useQueryString.ts
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
|
||||||
|
import {
|
||||||
|
ParseOptions,
|
||||||
|
ParsedQuery,
|
||||||
|
StringifyOptions,
|
||||||
|
parse,
|
||||||
|
stringify,
|
||||||
|
} from 'query-string';
|
||||||
|
import { useHistory } from 'react-router';
|
||||||
|
|
||||||
|
export interface QueryStringResult {
|
||||||
|
[0]: ParsedQuery;
|
||||||
|
[1]: Dispatch<SetStateAction<Record<string, any>>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
type NavigateCallback = (
|
||||||
|
pathnameWithParams: string,
|
||||||
|
pathname: string,
|
||||||
|
stringifedParams: string,
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query string.
|
||||||
|
* @param {Location} location
|
||||||
|
* @param {NavigateCallback} navigate
|
||||||
|
* @param {ParseOptions} parseOptions
|
||||||
|
* @param {StringifyOptions} stringifyOptions
|
||||||
|
* @returns {QueryStringResult}
|
||||||
|
*/
|
||||||
|
export function useQueryString(
|
||||||
|
location: Location,
|
||||||
|
navigate: NavigateCallback,
|
||||||
|
parseOptions?: ParseOptions,
|
||||||
|
stringifyOptions?: StringifyOptions,
|
||||||
|
): QueryStringResult {
|
||||||
|
const isFirst = useRef(true);
|
||||||
|
const [state, setState] = useState(parse(location.search, parseOptions));
|
||||||
|
|
||||||
|
useEffect((): void => {
|
||||||
|
if (isFirst.current) {
|
||||||
|
isFirst.current = false;
|
||||||
|
} else {
|
||||||
|
const pathname = location.pathname;
|
||||||
|
const stringifedParams = stringify(state, stringifyOptions);
|
||||||
|
const pathnameWithParams = pathname + '?' + stringifedParams;
|
||||||
|
|
||||||
|
navigate(pathnameWithParams, pathname, stringifedParams);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const setQuery: typeof setState = (values): void => {
|
||||||
|
const nextState = typeof values === 'function' ? values(state) : values;
|
||||||
|
setState(
|
||||||
|
(state): ParsedQuery => ({
|
||||||
|
...state,
|
||||||
|
...nextState,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
return [state, setQuery];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query string hook integrate with react router of the application.
|
||||||
|
* @param {NavigateCallback} navigate
|
||||||
|
* @param {ParseOptions} parseOptions
|
||||||
|
* @returns {QueryStringResult}
|
||||||
|
*/
|
||||||
|
export const useAppQueryString = (
|
||||||
|
navigate: NavigateCallback,
|
||||||
|
parseOptions: ParseOptions = {},
|
||||||
|
): QueryStringResult => {
|
||||||
|
const history = useHistory();
|
||||||
|
|
||||||
|
return useQueryString(
|
||||||
|
window.location,
|
||||||
|
(pathnameWithParams, pathname, stringifiedParams) => {
|
||||||
|
history.push({ pathname, search: stringifiedParams });
|
||||||
|
|
||||||
|
navigate && navigate(pathnameWithParams, pathname, stringifiedParams);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
parseNumbers: true,
|
||||||
|
parseBooleans: true,
|
||||||
|
...parseOptions,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user