Skip to content

Commit

Permalink
refactor: 필터링 방식 변경
Browse files Browse the repository at this point in the history
유요한 쿼리 파라미터만 api 요청으로 전달
  • Loading branch information
smb0123 committed Dec 1, 2024
1 parent 6b0f4ff commit b4769fe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/components/common/Filter/Filter.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
margin: auto;
}

.allType {
.all {
@include nsnFont(1.5rem, 400, normal);
background-color: white;
padding: 1rem 0rem;
Expand Down
4 changes: 3 additions & 1 deletion src/components/common/Filter/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export default function Filter({ searchParams, handleFilter }: FilterProps) {
const selectedOptions = searchParams.get(categoryName)?.split(",") || [];
const isSelected = selectedOptions.includes(option);
const displayOption =
categoryName === "postStatus"
categoryName === "all"
? "전체"
: categoryName === "postStatus"
? option === "RECRUITING"
? "모집중"
: option === "FINISHED"
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/Filter/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const Categories = {
allType: ["전체"],
all: ["true"],
disabilityType: ["시각장애", "청각장애", "지적장애", "지체장애", "자폐성장애", "뇌병변장애", "정신장애"],
assistanceType: ["식사", "이동", "학습"],
postStatus: ["RECRUITING", "FINISHED"],
Expand Down
6 changes: 4 additions & 2 deletions src/components/common/Pagenation/apis/getHelpMeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default async function getPagenationItems(
) {
const url = allType
? `posts?post-type=${postType}&page=${page}&size=${limit}&sorted=modifiedAt,DESC`
: `posts?post-type=${postType}&page=${page}&size=${limit}&sorted=modifiedAt,DESC&post-status=${postStatus}&disability-type=${disabilityType}&assistance-type=${assistanceType}`;

: `posts?post-type=${postType}&page=${page}&size=${limit}&sorted=modifiedAt,DESC` +
(postStatus ? `&post-status=${postStatus}` : "") +
(disabilityType ? `&disability-type=${disabilityType}` : "") +
(assistanceType ? `&assistance-type=${assistanceType}` : "");
const { data } = await axiosInstance.get(url);

return data;
Expand Down
51 changes: 28 additions & 23 deletions src/components/page-layout/helpMeLayout/components/helpMeLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect } from "react";

import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { all } from "axios";
import classNames from "classnames/bind";

import Link from "next/link";
Expand All @@ -24,7 +23,7 @@ export default function HelpMeLayout() {
const params = new URLSearchParams(router.query as any);

const currentPage = params.get("page");
const allType = params.get("allType") ?? "";
const all = params.get("all") ?? "";
const disabilityType = params.get("disabilityType") ?? "";
const assistanceType = params.get("assistanceType") ?? "";
const postStatus = params.get("postStatus") ?? "";
Expand All @@ -41,9 +40,9 @@ export default function HelpMeLayout() {

const { data } = useQuery({
queryKey: ["post", page, disabilityType, assistanceType, postStatus],
queryFn: () => getPagenationItems("TAKER", page, 8, allType, postStatus, disabilityType, assistanceType),
queryFn: () => getPagenationItems("TAKER", page, 8, all, postStatus, disabilityType, assistanceType),
placeholderData: keepPreviousData,
enabled: !!allType || !!disabilityType || !!assistanceType || !!postStatus,
enabled: !!all || !!disabilityType || !!assistanceType || !!postStatus,
});

const handleFilter = (category: string, optionId: string) => {
Expand All @@ -58,44 +57,50 @@ export default function HelpMeLayout() {
let assistanceTypeList = selectedAssistanceType ? selectedAssistanceType.split(",") : [];
let postStatusList = selectedPostStatus ? selectedPostStatus.split(",") : [];

if (category === "allType") {
if (allTypeList.includes(optionId)) {
allTypeList = allTypeList.filter((e) => e !== optionId);
searchParams.set("allType", allTypeList.join(","));
searchParams.delete("allType");
} else {
allTypeList.push(optionId);
searchParams.set("allType", allTypeList.join(","));
searchParams.delete("disabilityType");
searchParams.delete("assistanceType");
searchParams.delete("postStatus");
}
if (category === "all") {
allTypeList.push(optionId);
searchParams.set("all", allTypeList.join(","));
searchParams.delete("disabilityType");
searchParams.delete("assistanceType");
searchParams.delete("postStatus");
} else if (category === "disabilityType") {
if (disabilityTypeList.includes(optionId)) {
disabilityTypeList = disabilityTypeList.filter((e) => e !== optionId);
searchParams.set("disabilityType", disabilityTypeList.join(","));
if (disabilityTypeList.length === 0) {
searchParams.delete("disabilityType");
} else {
searchParams.set("disabilityType", disabilityTypeList.join(","));
}
} else {
disabilityTypeList.push(optionId);
searchParams.set("disabilityType", disabilityTypeList.join(","));
searchParams.delete("allType");
searchParams.delete("all");
}
} else if (category === "assistanceType") {
if (assistanceTypeList.includes(optionId)) {
assistanceTypeList = assistanceTypeList.filter((e) => e !== optionId);
searchParams.set("assistanceType", assistanceTypeList.join(","));
if (assistanceTypeList.length === 0) {
searchParams.delete("assistanceType");
} else {
searchParams.set("assistanceType", assistanceTypeList.join(","));
}
} else {
assistanceTypeList.push(optionId);
searchParams.set("assistanceType", assistanceTypeList.join(","));
searchParams.delete("allType");
searchParams.delete("all");
}
} else if (category === "postStatus") {
if (postStatusList.includes(optionId)) {
postStatusList = postStatusList.filter((e) => e !== optionId);
searchParams.set("postStatus", postStatusList.join(","));
if (postStatusList.length === 0) {
searchParams.delete("postStatus");
} else {
searchParams.set("postStatus", postStatusList.join(","));
}
} else {
postStatusList.push(optionId);
searchParams.set("postStatus", postStatusList.join(","));
searchParams.delete("allType");
searchParams.delete("all");
}
}

Expand All @@ -107,7 +112,7 @@ export default function HelpMeLayout() {

useEffect(() => {
if (!params.has("allType")) {
params.set("allType", "전체");
params.set("all", "true");
router.replace(`${router.pathname}?${params.toString()}`);
}
}, []);
Expand Down

0 comments on commit b4769fe

Please sign in to comment.