mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 12:58:13 +00:00
refactor(api): Introduce unified API layer for freight backoffice
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
import { QUERY_KEYS } from "@/constants/TANSTACK_QUEY_KEY";
|
||||
import {
|
||||
ruleEngineService,
|
||||
type RuleEngineListParams,
|
||||
} from "@/services/ruleEngine/ruleEngine.service";
|
||||
import { api } from "@/services/api";
|
||||
import type { RuleEngineListParams } from "@/services/ruleEngine/ruleEngine.service";
|
||||
import { RULE_ENGINE_SELECT_NONE } from "@/pages/ruleEngine/config/resources";
|
||||
import type {
|
||||
ApproveRatePayload,
|
||||
@@ -15,26 +12,29 @@ import type {
|
||||
|
||||
const CARGO_TYPE_PARENT_PAGE_SIZE = 500;
|
||||
|
||||
const listKey = (resource: RuleEngineResourceSlug) =>
|
||||
["rule-engine", resource] as const;
|
||||
|
||||
export const useRuleEngineList = (
|
||||
resource: RuleEngineResourceSlug,
|
||||
params: RuleEngineListParams,
|
||||
) =>
|
||||
useQuery({
|
||||
queryKey: [...QUERY_KEYS.RULE_ENGINE.list(resource), params],
|
||||
queryFn: () => ruleEngineService.list<RuleEngineRecord>(resource, params),
|
||||
});
|
||||
useQuery(
|
||||
api.ruleEngine.list.queryOptions({
|
||||
input: { resource, params },
|
||||
}),
|
||||
);
|
||||
|
||||
export const useCargoTypeParentOptions = (excludeId?: string, enabled = true) =>
|
||||
useQuery({
|
||||
queryKey: [
|
||||
...QUERY_KEYS.RULE_ENGINE.list("cargo-types"),
|
||||
"parent-options",
|
||||
excludeId ?? "",
|
||||
],
|
||||
queryKey: api.ruleEngine.list.queryKey({
|
||||
resource: "cargo-types",
|
||||
params: { page: 1, pageSize: CARGO_TYPE_PARENT_PAGE_SIZE },
|
||||
}),
|
||||
queryFn: () =>
|
||||
ruleEngineService.list<RuleEngineRecord>("cargo-types", {
|
||||
page: 1,
|
||||
pageSize: CARGO_TYPE_PARENT_PAGE_SIZE,
|
||||
api.ruleEngine.list.call({
|
||||
resource: "cargo-types",
|
||||
params: { page: 1, pageSize: CARGO_TYPE_PARENT_PAGE_SIZE },
|
||||
}),
|
||||
enabled,
|
||||
select: (result) => {
|
||||
@@ -45,7 +45,9 @@ export const useCargoTypeParentOptions = (excludeId?: string, enabled = true) =>
|
||||
const name = String(row.cargoTypeName ?? "").trim();
|
||||
const code = String(row.code ?? "").trim();
|
||||
const label =
|
||||
name && code ? `${name} (${code})` : name || code || String(row.id);
|
||||
name && code
|
||||
? `${name} (${code})`
|
||||
: name || code || String(row.id);
|
||||
return { label, value: String(row.id) };
|
||||
});
|
||||
return [noneOption, ...parents];
|
||||
@@ -53,20 +55,20 @@ export const useCargoTypeParentOptions = (excludeId?: string, enabled = true) =>
|
||||
});
|
||||
|
||||
export const useApprovalChain = (enabled: boolean) =>
|
||||
useQuery({
|
||||
queryKey: QUERY_KEYS.RULE_ENGINE.chain,
|
||||
queryFn: () => ruleEngineService.getApprovalChain(),
|
||||
enabled,
|
||||
});
|
||||
useQuery(
|
||||
api.ruleEngine.getApprovalChain.queryOptions({
|
||||
enabled,
|
||||
}),
|
||||
);
|
||||
|
||||
export const useRuleEngineMutations = (resource: RuleEngineResourceSlug) => {
|
||||
const qc = useQueryClient();
|
||||
const invalidate = () =>
|
||||
qc.invalidateQueries({ queryKey: QUERY_KEYS.RULE_ENGINE.list(resource) });
|
||||
qc.invalidateQueries({ queryKey: listKey(resource) });
|
||||
|
||||
const create = useMutation({
|
||||
mutationFn: (payload: Record<string, unknown>) =>
|
||||
ruleEngineService.create(resource, payload),
|
||||
api.ruleEngine.create.call({ resource, payload }),
|
||||
onSuccess: () => {
|
||||
toast.success("Created successfully");
|
||||
invalidate();
|
||||
@@ -81,7 +83,7 @@ export const useRuleEngineMutations = (resource: RuleEngineResourceSlug) => {
|
||||
}: {
|
||||
id: string;
|
||||
payload: Record<string, unknown>;
|
||||
}) => ruleEngineService.update(resource, id, payload),
|
||||
}) => api.ruleEngine.update.call({ resource, id, payload }),
|
||||
onSuccess: () => {
|
||||
toast.success("Updated successfully");
|
||||
invalidate();
|
||||
@@ -90,7 +92,8 @@ export const useRuleEngineMutations = (resource: RuleEngineResourceSlug) => {
|
||||
});
|
||||
|
||||
const remove = useMutation({
|
||||
mutationFn: (id: string) => ruleEngineService.remove(resource, id),
|
||||
mutationFn: (id: string) =>
|
||||
api.ruleEngine.remove.call({ resource, id }),
|
||||
onSuccess: () => {
|
||||
toast.success("Deleted successfully");
|
||||
invalidate();
|
||||
@@ -104,10 +107,10 @@ export const useRuleEngineMutations = (resource: RuleEngineResourceSlug) => {
|
||||
export const useRateWorkflow = () => {
|
||||
const qc = useQueryClient();
|
||||
const invalidate = () =>
|
||||
qc.invalidateQueries({ queryKey: QUERY_KEYS.RULE_ENGINE.list("rates") });
|
||||
qc.invalidateQueries({ queryKey: listKey("rates") });
|
||||
|
||||
const submit = useMutation({
|
||||
mutationFn: (id: string) => ruleEngineService.submitRate(id),
|
||||
mutationFn: (id: string) => api.ruleEngine.submitRate.call({ id }),
|
||||
onSuccess: () => {
|
||||
toast.success("Rate submitted for approval");
|
||||
invalidate();
|
||||
@@ -116,8 +119,13 @@ export const useRateWorkflow = () => {
|
||||
});
|
||||
|
||||
const approve = useMutation({
|
||||
mutationFn: ({ id, payload }: { id: string; payload: ApproveRatePayload }) =>
|
||||
ruleEngineService.approveRate(id, payload),
|
||||
mutationFn: ({
|
||||
id,
|
||||
payload,
|
||||
}: {
|
||||
id: string;
|
||||
payload: ApproveRatePayload;
|
||||
}) => api.ruleEngine.approveRate.call({ id, payload }),
|
||||
onSuccess: () => {
|
||||
toast.success("Rate approved");
|
||||
invalidate();
|
||||
|
||||
Reference in New Issue
Block a user