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 | 205x 59x 59x 59x 59x 59x 205x 58x 59x 59x 10x 16x 10x 205x 44x 205x 11x 11x 11x 205x 33x 33x 47x | import type { AggregatedDebt, AggregatedDebts } from "~app/trpc-types";
import type { CurrencyCode } from "~app/utils/currency";
import type { PeerId } from "~db/ids";
import { upsertInArray } from "~utils/array";
import type {
ControllerContext,
ControllerWith,
SnapshotFn,
UpdateFn,
} from "../../types";
import { applyUpdateFnWithRevert, getUpdatedData, withRef } from "../utils";
type Controller = ControllerWith<{
procedure: ControllerContext["trpc"]["debts"]["getAllPeer"];
}>;
const updateAllSums =
({ queryClient, procedure }: Controller, peerId: PeerId) =>
(updater: UpdateFn<AggregatedDebt[]>) =>
withRef<AggregatedDebts | undefined>((ref) => {
queryClient.setQueryData(procedure.queryKey({ peerId }), (prevDebts) => {
ref.current = prevDebts;
return getUpdatedData(prevDebts, (prevData) => ({
...prevData,
items: updater(prevData.items),
}));
});
});
const updateSum =
(controller: Controller, peerId: PeerId, currencyCode: CurrencyCode) =>
(updater: UpdateFn<number>) =>
withRef<AggregatedDebt | undefined>((ref) => {
updateAllSums(
controller,
peerId,
)((prevDebts) =>
upsertInArray<(typeof prevDebts)[number]>(
prevDebts,
(entry) => entry.currencyCode === currencyCode,
(entry) => ({ ...entry, sum: updater(entry.sum) }),
{ currencyCode, sum: 0 },
ref,
),
);
}).current?.sum;
const invalidate =
({ queryClient, procedure }: Controller) =>
(peerId: PeerId) =>
queryClient.invalidateQueries(procedure.queryFilter({ peerId }));
export const getController = ({ queryClient, trpc }: ControllerContext) => {
const controller = { queryClient, procedure: trpc.debts.getAllPeer };
return {
update: (
peerId: PeerId,
currencyCode: CurrencyCode,
updater: UpdateFn<number>,
) => updateSum(controller, peerId, currencyCode)(updater),
invalidate: invalidate(controller),
};
};
export const getRevertController = ({
queryClient,
trpc,
}: ControllerContext) => {
const controller = { queryClient, procedure: trpc.debts.getAllPeer };
return {
update: (
peerId: PeerId,
currencyCode: CurrencyCode,
updater: UpdateFn<number>,
revertUpdater: SnapshotFn<number>,
) =>
applyUpdateFnWithRevert(
updateSum(controller, peerId, currencyCode),
updater,
revertUpdater,
),
invalidate: invalidate(controller),
};
};
|