Skip to content

Commit

Permalink
Merge pull request #52 from LikeLion-KNU/refactor/dropdown
Browse files Browse the repository at this point in the history
Refactor/dropdown zustand-persist 추가 필요없는 파일 삭제
  • Loading branch information
toothlessdev authored Sep 11, 2024
2 parents 3065c5a + cb069b8 commit d67bce9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 53 deletions.
36 changes: 0 additions & 36 deletions src/hooks/useAxios.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/hooks/useSubmitResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useUserInfo } from "@/store/store";

export const useSubmitResult = () => {
const [isPending, setIsPending] = useState<boolean>(false);
const major = useUserInfo((state) => state.major);
const department = useUserInfo((state) => state.department);

const navigate = useNavigate();
const [searchParams] = useSearchParams();
Expand All @@ -15,7 +15,7 @@ export const useSubmitResult = () => {
const mbti = searchParams.get("type");
console.log(searchParams);

api.post("/stats", { department: major, mbti })
api.post("/stats", { department: department, mbti })
.then(() => {
navigate(`/result?type=${mbti}`);
})
Expand Down
2 changes: 1 addition & 1 deletion src/pages/AnalyticsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface top10Response {
top: [string, number][];
}

const renderData = (data: top10Response | null, isPending: boolean, isError: any) => {
const renderData = (data: top10Response | null, isPending: boolean, isError: boolean) => {
if (isPending) return <Text size="m">로딩중...</Text>;
if (isError) return <Text size="m">오류가 발생했습니다.</Text>;
if (!data) {
Expand Down
13 changes: 7 additions & 6 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function HomePage() {
const userInfo = useUserInfo((state) => state);

const [isOpen, setIsOpen] = useState<boolean>(false);
const [selectedMajor, setSelectedMajor] = useState<string>("");
const [selectedDepartment, setSelectedDepartment] = useState<string>(userInfo.department);

const handleSideBar = () => {
setIsOpen(!isOpen);
Expand All @@ -44,16 +44,16 @@ export default function HomePage() {
toast.error("이름을 입력해주세용");
return;
}
if (!userInfo.major) {
if (!userInfo.department) {
toast.error("단과대학을 입력해주세용");
return;
}
navigate("/select");
};

useEffect(() => {
userInfo.setMajor(selectedMajor);
}, [selectedMajor, userInfo.setMajor]);
userInfo.setDepartment(selectedDepartment);
}, [selectedDepartment, userInfo.setDepartment]);

return (
<HomePageWrapper>
Expand Down Expand Up @@ -98,13 +98,14 @@ export default function HomePage() {
height="45px"
placeholder="이름을 입력하세용"
onChange={(e) => userInfo.setName(e.currentTarget.value)}
defaultValue={userInfo.name}
/>
<DropDown
color="primary"
width="242px"
height="45px"
selectedDepartment={selectedMajor}
setSelectedDepartment={setSelectedMajor}
selectedDepartment={selectedDepartment}
setSelectedDepartment={setSelectedDepartment}
/>
<Text size="xs" color="#6E6E6E">
개인정보는 외부에 공유되지 않으니 안심하세용
Expand Down
26 changes: 18 additions & 8 deletions src/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { create } from "zustand";

import { persist } from "zustand/middleware";

type UserProps = {
name: string;
major: string;
department: string;
};

type UserAction = {
setName: (name: UserProps["name"]) => void;
setMajor: (major: UserProps["major"]) => void;
setDepartment: (department: UserProps["department"]) => void;
};

export const useUserInfo = create<UserProps & UserAction>()((set) => ({
name: "",
major: "",
setName: (name) => set(() => ({ name: name })),
setMajor: (major) => set(() => ({ major: major })),
}));
export const useUserInfo = create<UserProps & UserAction>()(
persist(
(set) => ({
name: "",
department: "",
setName: (name) => set(() => ({ name: name })),
setDepartment: (department) => set(() => ({ department: department })),
}),
{
name: "user-storage",
getStorage: () => localStorage,
},
),
);

0 comments on commit d67bce9

Please sign in to comment.