Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/entities/dashboard/api/getDashboardSummary.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { dashboardUrl, get } from "@/shared/api";
import {
dashboardUrl,
get,
normalizeApiError,
} from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import type { DashboardSummaryDTO } from "../model/types";
import { dashboardSummarySchema } from "./schemas";

export async function getDashboardSummary(): Promise<DashboardSummaryDTO> {
const response = await get<BaseResponseType<unknown>>(
dashboardUrl.getSummary(),
);
try {
const response = await get<BaseResponseType<unknown>>(
dashboardUrl.getSummary(),
);

return dashboardSummarySchema.parse(response.data);
return dashboardSummarySchema.parse(response.data);
} catch (error) {
throw normalizeApiError(error);
}
}
13 changes: 13 additions & 0 deletions src/entities/machine/api/deleteMachine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
del,
machineUrl,
normalizeApiError,
} from "@/shared/api";

export async function deleteMachine(id: number): Promise<void> {
try {
await del(machineUrl.deleteMachine(id));
} catch (error) {
throw normalizeApiError(error);
}
}
84 changes: 46 additions & 38 deletions src/entities/machine/api/getMachines.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
import { get, machineUrl } from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import { mapMachines } from "../lib/mapMachine";
import type {
MachineItem,
MachineParamsType,
MachineType,
} from "../model/types";
import { machineResponseSchema } from "./schemas";

async function getMachinesByType(
machineType: MachineType,
params?: MachineParamsType,
): Promise<MachineItem[]> {
const response = await get<BaseResponseType<unknown>>(
machineUrl.getMachines(),
{
params: {
...params,
type: machineType,
import {
get,
machineUrl,
normalizeApiError,
} from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import { mapMachines } from "../lib/mapMachine";
import type {
MachineItem,
MachineParamsType,
MachineType,
} from "../model/types";
import { machineResponseSchema } from "./schemas";

async function getMachinesByType(
machineType: MachineType,
params?: MachineParamsType,
): Promise<MachineItem[]> {
const response = await get<BaseResponseType<unknown>>(
machineUrl.getMachines(),
{
params: {
...params,
type: machineType,
},
},
},
);

const parsedData = machineResponseSchema.parse(response.data);

return mapMachines(parsedData.machines);
}

export async function getMachines(
params?: MachineParamsType,
): Promise<MachineItem[]> {
const [washers, dryers] = await Promise.all([
getMachinesByType("WASHER", params),
getMachinesByType("DRYER", params),
]);

return [...washers, ...dryers];
}
);

const parsedData = machineResponseSchema.parse(response.data);

return mapMachines(parsedData.machines);
}

export async function getMachines(
params?: MachineParamsType,
): Promise<MachineItem[]> {
try {
const [washers, dryers] = await Promise.all([
getMachinesByType("WASHER", params),
getMachinesByType("DRYER", params),
]);

return [...washers, ...dryers];
} catch (error) {
throw normalizeApiError(error);
}
}
24 changes: 24 additions & 0 deletions src/entities/machine/api/updateMachineStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
machineUrl,
normalizeApiError,
put,
} from "@/shared/api";
import type { MachineConditionStatusDTO } from "../model/types";

type UpdateMachineStatusParams = {
id: number;
status: MachineConditionStatusDTO;
};

export async function updateMachineStatus({
id,
status,
}: UpdateMachineStatusParams): Promise<void> {
try {
await put(machineUrl.updateMachineStatus(id), {
status,
});
} catch (error) {
throw normalizeApiError(error);
}
}
7 changes: 4 additions & 3 deletions src/entities/machine/api/useDeleteMachine.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { del, machineQueryKeys, machineUrl } from "@/shared/api";
import { machineQueryKeys } from "@/shared/api";
import { deleteMachine } from "./deleteMachine";

export const useDeleteMachine = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (id: number) => del(machineUrl.deleteMachine(id)),
mutationFn: deleteMachine,
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: machineQueryKeys.all,
});
},
});
};
};
14 changes: 4 additions & 10 deletions src/entities/machine/api/useUpdateMachineStatus.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { machineQueryKeys, machineUrl, put } from "@/shared/api";
import type { MachineConditionStatusDTO } from "../model/types";
import { machineQueryKeys } from "@/shared/api";
import { updateMachineStatus } from "./updateMachineStatus";

export const useUpdateMachineStatus = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({
id,
status,
}: {
id: number;
status: MachineConditionStatusDTO;
}) => put(machineUrl.updateMachineStatus(id), { status }),
mutationFn: updateMachineStatus,
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: machineQueryKeys.all,
});
},
});
};
};
32 changes: 20 additions & 12 deletions src/entities/report/api/getMalfunctionReports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { get, reportUrl } from "@/shared/api";
import {
get,
normalizeApiError,
reportUrl,
} from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import type {
ReportParamsType,
Expand All @@ -9,17 +13,21 @@ import { reportResponseSchema } from "./schemas";
export const getMalfunctionReports = async (
params?: ReportParamsType,
): Promise<BaseResponseType<ReportResponseType>> => {
const response = await get<BaseResponseType<unknown>>(
reportUrl.getMalfunctionReports(),
{
params,
},
);
try {
const response = await get<BaseResponseType<unknown>>(
reportUrl.getMalfunctionReports(),
{
params,
},
);

const parsedData = reportResponseSchema.parse(response.data);
const parsedData = reportResponseSchema.parse(response.data);

return {
...response,
data: parsedData,
};
return {
...response,
data: parsedData,
};
} catch (error) {
throw normalizeApiError(error);
}
};
28 changes: 28 additions & 0 deletions src/entities/report/api/updateReportStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
normalizeApiError,
put,
reportUrl,
} from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import type { ReportStatusType } from "../model/types";
Comment thread
bom0320 marked this conversation as resolved.
Outdated

type UpdateReportStatusParams = {
id: number;
status: ReportStatusType;
};

export async function updateReportStatus({
id,
status,
}: UpdateReportStatusParams): Promise<void> {
try {
await put<BaseResponseType<null>>(
reportUrl.updateMalfunctionReportStatus(id),
{
status,
},
);
} catch (error) {
throw normalizeApiError(error);
}
}
22 changes: 11 additions & 11 deletions src/entities/report/api/useUpdateReportStatus.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { put, reportUrl, reportQueryKeys } from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import type { ReportStatusType } from "../model/types";
import { reportQueryKeys } from "@/shared/api";
import { updateReportStatus } from "./updateReportStatus";

export function useUpdateReportStatus() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: async ({ id, status }: { id: number; status: ReportStatusType }) => {
await put<BaseResponseType<null>>(reportUrl.updateMalfunctionReportStatus(id), {
status,
});
},
mutationFn: updateReportStatus,
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: reportQueryKeys.all,
});

alert("신고 상태가 변경되었습니다.");
},
onError: () => {
alert("신고 상태 변경 중 오류가 발생했습니다.");
onError: (error) => {
alert(
error instanceof Error
? error.message
: "신고 상태 변경 중 오류가 발생했습니다.",
);
},
});
}
}
16 changes: 12 additions & 4 deletions src/entities/reservation/api/deleteReservation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { del, reservationUrl } from "@/shared/api";
import {
del,
normalizeApiError,
reservationUrl,
} from "@/shared/api";

export async function deleteReservation(id: number) {
await del(reservationUrl.deleteReservation(id));
}
export async function deleteReservation(id: number): Promise<void> {
try {
await del(reservationUrl.deleteReservation(id));
} catch (error) {
throw normalizeApiError(error);
}
}
34 changes: 23 additions & 11 deletions src/entities/reservation/api/getMachineReservationHistory.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import { get, reservationUrl } from "@/shared/api";
import {
get,
normalizeApiError,
reservationUrl,
} from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import { mapMachineReservationHistory } from "../lib/mapReservationHistory";
import type {
MachineReservationHistory,
MachineReservationHistoryParamsType,
MachineReservationHistoryResponseType,
} from "../model/historyTypes";
import { machineReservationHistoryResponseSchema } from "./schemas";

export async function getMachineReservationHistory(
params?: MachineReservationHistoryParamsType,
): Promise<MachineReservationHistory | null> {
const response = await get<
BaseResponseType<MachineReservationHistoryResponseType>
>(reservationUrl.getMachineReservationHistory(), {
params,
});
try {
const response = await get<BaseResponseType<unknown>>(
reservationUrl.getMachineReservationHistory(),
{
params,
},
);

const machine = response.data.machines[0];
const parsedData =
machineReservationHistoryResponseSchema.parse(response.data);

if (!machine) return null;
const machine = parsedData.machines[0];

return mapMachineReservationHistory(machine);
}
if (!machine) return null;

return mapMachineReservationHistory(machine);
} catch (error) {
throw normalizeApiError(error);
}
}
Loading
Loading