Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 24x 12x 12x 12x 12x 48x 48x 48x 48x 48x 48x 24x 48x 48x 48x 48x 48x 48x 48x 48x 48x 1x 1x 1x 1x 1x 1x 1x 1x | import { useQuery } from '@tanstack/react-query';
import { keyBy } from 'lodash-es';
import { useEffect, useMemo } from 'react';
import { useSnackbars } from '@allshares/studio-design-system';
import { dayjs } from '@amalia/ext/dayjs';
import { useMutation } from '@amalia/ext/react-query';
import { type Period, type PeriodFrequencyEnum } from '@amalia/payout-definition/periods/types';
import { useCurrentCompany } from '@amalia/tenants/companies/state';
import { PeriodsApiClient } from './periods.api-client';
import { periodsQueryKeys } from './periods.keys';
export const useCurrentPeriod = ({
frequency,
periodId,
enabled,
}: {
frequency?: PeriodFrequencyEnum;
periodId?: Period['id'];
enabled?: boolean;
} = {}) => {
const { data: company } = useCurrentCompany();
const { snackError } = useSnackbars();
const currentFrequency = frequency || company.statementFrequency;
const {
data: periods,
isError,
error,
...rest
} = useQuery({
queryKey: periodId ? periodsQueryKeys.periodById(periodId) : periodsQueryKeys.currentPeriod(currentFrequency),
queryFn: () =>
periodId
? PeriodsApiClient.getAllPeriods()
: PeriodsApiClient.getPeriodByDate({
dateString: dayjs.utc().format('YYYY-MM-DD'),
frequency: currentFrequency,
}).then((period) => [period]),
enabled: enabled !== false,
});
useEffect(() => {
if (isError) {
snackError(error.message);
}
}, [isError, error, snackError]);
const currentPeriod = periodId ? periods?.find(({ id }) => id === periodId) : periods?.[0];
return {
currentPeriod,
...rest,
};
};
export const usePeriod = (date?: string, frequency?: PeriodFrequencyEnum) =>
useQuery({
enabled: !!frequency && !!date,
queryKey: periodsQueryKeys.byDate(date!, frequency!),
queryFn: () => PeriodsApiClient.getPeriodByDate({ dateString: date!, frequency: frequency! }),
placeholderData: (pastValue) => pastValue ?? undefined,
});
export const usePeriods = ({ enabled = true }: { enabled?: boolean } = {}) => {
const { snackError } = useSnackbars();
const {
data: periods,
isError,
error,
...rest
} = useQuery({
queryKey: periodsQueryKeys.list(),
queryFn: () => PeriodsApiClient.getAllPeriods(),
enabled,
});
useEffect(() => {
if (isError) {
snackError(error.message);
}
}, [isError, error, snackError]);
const { periodsMap, periodsList } = useMemo(
() => ({
periodsMap: keyBy(periods ?? [], 'id'),
periodsList: (periods ?? []).sort((p1, p2) => (p1.startDate > p2.startDate ? 1 : -1)),
}),
[periods],
);
return {
periodsMap,
periodsList,
...rest,
};
};
export const useFindOrCreatePeriod = () =>
useMutation({
mutationFn: PeriodsApiClient.getPeriodByDate,
meta: {
invalidateQueries: [periodsQueryKeys.list()],
},
});
|