Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/dropdown zustand-persist 추가 필요없는 파일 삭제 #52

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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,
},
),
);