Skip to content

Commit

Permalink
fix: don't use userInfo state
Browse files Browse the repository at this point in the history
  • Loading branch information
2paperstar committed Jun 7, 2024
1 parent a39124b commit e290ec6
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions src/pages/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
Modal,
Select,
} from '@mantine/core';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import useSWR from 'swr';
import { LookBook, createLookBook } from '../api/ai';
import { updateUser } from '../api/auth';
import area from '../data/area';
import useUser from '../hooks/useUser';
import Swal from 'sweetalert2';
Expand All @@ -35,10 +34,6 @@ const ageRangeOptions = [
];

function MainPage() {
const [userInfo, setUserInfo] = useState(
{ gender: '', ageRange: '' } || null
);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [gender, setGender] = useState<string | null>('');
const [ageRange, setAgeRange] = useState<string | null>('');
const [error, setError] = useState('');
Expand All @@ -48,33 +43,12 @@ function MainPage() {
const [district, setDistrict] = useState<string>('청담동');
const [loading, setLoading] = useState(false);

const { user } = useUser();
const { user, updateUser } = useUser();
const { data: lookBookData, mutate } = useSWR<{
total: number;
list: LookBook[];
}>('/ai');

useEffect(() => {
const fetchUserInfo = async () => {
try {
if (!user) {
return null;
}

setUserInfo(user);

if (!user.gender || !user.ageRange) {
setIsModalOpen(true);
}
} catch (err) {
console.error('Failed to fetch user info:', err);
console.log(userInfo);
}
};

fetchUserInfo();
}, [user, userInfo]);

const renderTodayLookBook = () => {
if (!lookBookData) {
return (
Expand Down Expand Up @@ -170,8 +144,6 @@ function MainPage() {

try {
await updateUser({ gender, ageRange });
setUserInfo({ gender, ageRange });
setIsModalOpen(false);
} catch (err: any) {
setError(
'Failed to update user info: ' +
Expand Down Expand Up @@ -272,8 +244,8 @@ function MainPage() {
</Grid>

<Modal
opened={isModalOpen}
onClose={() => setIsModalOpen(false)}
opened={user === undefined}
onClose={() => {}}
title="당신에 대해 알고 싶어요!"
>
{error && <p style={{ color: 'red' }}>{error}</p>}
Expand Down

0 comments on commit e290ec6

Please sign in to comment.