Skip to content

Commit

Permalink
refactor : problem api routes component
Browse files Browse the repository at this point in the history
  • Loading branch information
Happhee committed Jun 15, 2024
1 parent 10ba818 commit d4a01f5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/problem/components/AnswerChoiceButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { cn } from "@shared/utils/cn";
import ChoiceFillCircleSvg from "../ChoiceFillCircleSvg";
import { ANSWER_CHOICHE_BUTTON_INFO } from "@problem/constants/problemInfo";
import ProblemContext from "@problem/context/problemContext";
import { QUERY_KEY } from "@problem/remotes/api";
import { AnswerCheckInfo, AnswerChoiceInfo } from "@problem/types/problemInfo";

interface AnswerChoiceButtonProps extends Pick<AnswerChoiceInfo, "content"> {}
Expand All @@ -25,7 +26,7 @@ export default function AnswerChoiceButton({

const problemAnswerInfo = useMutationState({
filters: {
mutationKey: ["get-problem-answer"],
mutationKey: [QUERY_KEY.POST_PROBLEM_ANSWER],
},
select: (mutation) => mutation.state.data as AnswerCheckInfo,
});
Expand Down
3 changes: 2 additions & 1 deletion src/problem/components/ProblemTitle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from "react";
import { useMutationState, useQuery } from "@tanstack/react-query";

import { PROBLEM_TITLE_INFO } from "@problem/constants/problemInfo";
import { QUERY_KEY } from "@problem/remotes/api";
import { getProblemQueryOptions } from "@problem/remotes/getProblemQueryOptions";
import { AnswerCheckInfo } from "@problem/types/problemInfo";

Expand All @@ -18,7 +19,7 @@ export default function ProblemTitle() {
});
const problemAnswerInfo = useMutationState({
filters: {
mutationKey: ["get-problem-answer", problemIdNumber],
mutationKey: [QUERY_KEY.POST_PROBLEM_ANSWER, problemIdNumber],
},
select: (mutation) => mutation.state.data as AnswerCheckInfo,
});
Expand Down
3 changes: 2 additions & 1 deletion src/problem/components/TagList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { ApiResponse } from "@api/api-config";
import queryClient from "@api/query-client";

import Tag from "@common/components/Tag";
import { QUERY_KEY } from "@problem/remotes/api";
import { PromblemInfo } from "@problem/types/problemInfo";

export default function TagList() {
const problemInfoData = queryClient.getQueryData(["get-Problems-info"]) as
const problemInfoData = queryClient.getQueryData([QUERY_KEY.GET_PROBLEM]) as
| ApiResponse<PromblemInfo>
| undefined;

Expand Down
11 changes: 8 additions & 3 deletions src/problem/remotes/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const PROBLEM_API_ROUTES = {
problems: (articleId: number) => `/articles/${articleId}/problems`,
submitAnswer: (problemId: number) => `/problems/${problemId}`,
export const API_ROUTE = {
PROBLEM: (articleId: number) => `/articles/${articleId}/problems`,
SUBMIT_ANSWER: (problemId: number) => `/problems/${problemId}`,
};

export const QUERY_KEY = {
GET_PROBLEM: "get-problem-info",
POST_PROBLEM_ANSWER: "post-problem-answer",
};
6 changes: 3 additions & 3 deletions src/problem/remotes/getProblemQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { UseQueryOptions } from "@tanstack/react-query";

import { ApiResponse, axiosRequest } from "@api/api-config";

import { PROBLEM_API_ROUTES } from "./api";
import { API_ROUTE, QUERY_KEY } from "./api";
import { PromblemInfo } from "@problem/types/problemInfo";

export const getProblemInfo = ({
problemId,
}: ProblemInfoParams): Promise<ApiResponse<PromblemInfo>> => {
return axiosRequest("get", PROBLEM_API_ROUTES.problems(problemId));
return axiosRequest("get", API_ROUTE.PROBLEM(problemId));
};
export const getProblemQueryOptions = ({
problemId,
Expand All @@ -18,7 +18,7 @@ export const getProblemQueryOptions = ({
PromblemInfo
> => {
return {
queryKey: ["get-Problems-info"],
queryKey: [QUERY_KEY.GET_PROBLEM, problemId],
queryFn: () => getProblemInfo({ problemId }),
select: (data) => data.data,
};
Expand Down
10 changes: 3 additions & 7 deletions src/problem/remotes/postProblemAnswerOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ import { UseMutationOptions } from "@tanstack/react-query";

import { ApiResponse, axiosRequest } from "@api/api-config";

import { PROBLEM_API_ROUTES } from "./api";
import { API_ROUTE, QUERY_KEY } from "./api";
import { AnswerCheckInfo } from "@problem/types/problemInfo";

export const postProblemAnswer = (
params: ProblemAnswerParams,
body: ProblemAnswerBody,
): Promise<ApiResponse<AnswerCheckInfo>> => {
return axiosRequest(
"post",
PROBLEM_API_ROUTES.submitAnswer(params.problemId),
body,
);
return axiosRequest("post", API_ROUTE.SUBMIT_ANSWER(params.problemId), body);
};
export const postProblemAnswerMutationOptions = (
params: ProblemAnswerParams,
Expand All @@ -23,7 +19,7 @@ export const postProblemAnswerMutationOptions = (
ProblemAnswerBody
> => {
return {
mutationKey: ["get-problem-answer", params.problemId],
mutationKey: [QUERY_KEY.POST_PROBLEM_ANSWER, params.problemId],
mutationFn: (body) => postProblemAnswer(params, body),
};
};
Expand Down

0 comments on commit d4a01f5

Please sign in to comment.