Skip to content
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
26 changes: 16 additions & 10 deletions src/components/screens/EditProfileScreen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ const styles = `
}
`;

function Modal({ message, type = 'info', onClose }) {
const handleClick = () => onClose();
function Modal({ message, type = 'info', onClose, action }) {
const navigate = useNavigate();
const handleClick = () => {
if (action === 'mypage') navigate('/mypage');
else if (action === 'home') navigate('/');

onClose();
}

return (
<div className="fixed inset-0 flex items-center justify-center bg-black/40 z-50">
Expand Down Expand Up @@ -85,7 +91,8 @@ export default function EditProfileScreen({ onBack }) {
setEmail(data.email);
setAvatar(data.image?.imageUrl || data.avatarUrl || null);
} catch {
setModal({ message: "๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ‚", type: "error" });
setModal({ message: "๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค ", type: "error" });
navigate("/login")
}
};
fetchMyInfo();
Expand Down Expand Up @@ -134,11 +141,11 @@ export default function EditProfileScreen({ onBack }) {
{ nickname },
{ headers: { Authorization: `Bearer ${token}` } }
);
setModal({ message: "ํšŒ์›์ •๋ณด ์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ", type: "success" });
setModal({ message: "ํšŒ์›์ •๋ณด ์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ", type: "success", action : "mypage" });
setTimeout(() => {
navigate("/mypage");
onBack?.();
}, 1000);
}, 50000);
} catch {
setModal({ message: "๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”", type: "error" });
} finally {
Expand All @@ -154,12 +161,11 @@ export default function EditProfileScreen({ onBack }) {
"/member/deactivate",
{ headers: { Authorization: `Bearer ${token}` } }
);
setModal({ message: "ํšŒ์› ํƒˆํ‡ด๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ", type: "success" });
setModal({ message: "ํšŒ์› ํƒˆํ‡ด๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ", type: "success" , action :"home"});
localStorage.clear();
setTimeout(() => {
navigate("/mypage");
onBack?.();
}, 1000);
}, 50000);
} catch {
setModal({ message: "๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”", type: "error" });
} finally {
Expand Down Expand Up @@ -272,13 +278,13 @@ export default function EditProfileScreen({ onBack }) {
๋’ค๋กœ๊ฐ€๊ธฐ
</button>
</div>

{/* โœ… ๋ชจ๋‹ฌ ํ‘œ์‹œ */}

{modal && (
<Modal
message={modal.message}
type={modal.type}
onClose={() => setModal(null)}
action={modal.action}
/>
)}
</div>
Expand Down
140 changes: 53 additions & 87 deletions src/components/screens/LoginSignupScreen.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import api from '../../api/axios';
// โ†“ redux ์•ˆ ์“ฐ๋ฉด ์ด ๋ถ€๋ถ„ ์ œ๊ฑฐํ•ด๋„๋จ
import { useDispatch } from 'react-redux';
import { updateProfile, login, fetchPointInfo } from '../../store/slices/userSlice';
import kakaoBtn from '../../assets/kakao_login_medium_wide.png';
import HomeScreen from './HomeScreen';

// ํ…Œ๋งˆ ์ปฌ๋Ÿฌ
const themeColor = '#96cb6f';

// ๊ฒ€์ฆ ํ•จ์ˆ˜
// ์œ ํšจ์„ฑ ๊ฒ€์ฆ ํ•จ์ˆ˜
const validateEmail = (email) => /[^@\s]+@[^@\s]+\.[^@\s]+/.test(email);
const validatePassword = (password) => password.length >= 6;

// ์นด์นด์˜ค ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์ด๋™
// ์นด์นด์˜ค ๋กœ๊ทธ์ธ
const kakaoLogin = () => {
window.location.href = `${
import.meta.env.VITE_APP_SERVER_URL
}/oauth2/authorization/kakao`;
};


// ์ „์—ญ ์Šคํƒ€์ผ ๊ทธ๋Œ€๋กœ ์œ ์ง€
// ์Šคํƒ€์ผ
const styles = `
:root { --brand: ${themeColor}; }
*{ box-sizing: border-box; }
Expand All @@ -33,47 +31,28 @@ const styles = `
.tabs{ display:flex; gap:8px; border-bottom:1px solid #eaeaea; margin-bottom:18px; }
.tab{ flex:1; padding:12px 8px; text-align:center; font-weight:700; border:0; background:transparent; cursor:pointer; border-bottom:3px solid transparent; transition:all .2s ease; }
.tab.active{ color:var(--brand); border-bottom-color:var(--brand); }
.button.focus{ outline : none ,box-shadow:none }
.field{ margin:14px 0; }
.label{ display:block; font-weight:600; color:#333; margin-bottom:6px; transition:color .2s ease; }
.input{ width:100%; padding:12px 14px; border-radius:12px; border:2px solid #e5e7eb; outline:none;
transition:border-color .15s ease, box-shadow .15s ease, background .15s ease; }
.label{ display:block; font-weight:600; color:#333; margin-bottom:6px; }
.input{ width:100%; padding:12px 14px; border-radius:12px; border:2px solid #e5e7eb; outline:none; transition:border-color .15s ease, box-shadow .15s ease, background .15s ease; }
.input:focus{ border-color:var(--brand); box-shadow:0 0 0 4px rgba(133,193,75,.15); }
.input.filled{ background:#f9fff2; border-color:#cfe8ae; }
.input.valid{ border-color:var(--brand); }
.input.invalid{ border-color:#e11d48; box-shadow:0 0 0 4px rgba(225,29,72,.10); }
.hint{ font-size:.85rem; color:#6b7280; margin-top:6px; }
.error{ font-size:.85rem; color:#e11d48; margin-top:6px; }
.btn{ width:100%; padding:12px 14px; border-radius:12px; border:0; background:var(--brand); color:#fff; font-weight:800; cursor:pointer; margin-top:6px;}
button:focus{ outline:none; box-shadow:none; }
.btn:disabled{ opacity:.5; cursor:not-allowed; }
.kakao{ width:100%; margin-top:12px; padding:12px 14px; border-radius:12px; border:0;
background:#FEE500; color:#3C1E1E; font-weight:700; cursor:pointer; }
.valid-text {
display: block;
margin-top: 6px;
margin-left: 4px;
font-size: 0.88rem;
color: #3fa14a;
transition: color 0.2s ease;
}
.invalid-text {
display: block;
margin-top: 6px;
margin-left: 4px;
font-size: 0.88rem;
color: #d33b3b;
transition: color 0.2s ease;
}
.valid-text{ font-size:.88rem; color:#3fa14a; margin-top:6px; margin-left:4px; }
.invalid-text{ font-size:.88rem; color:#d33b3b; margin-top:6px; margin-left:4px; }
`;

function Modal({ message, type = 'info', onClose }) {
/* ๋ชจ๋‹ฌ */
function Modal({ message, type = 'info', onClose, action }) {
const navigate = useNavigate();

const handleClick = () => {
if (type === 'success') {
onClose();
} else {
onClose();
}
if (action === 'mypage') navigate('/mypage');
else if (action === 'home') navigate('/');
else if (action === 'login') navigate('/login');
onClose();
};

return (
Expand Down Expand Up @@ -101,25 +80,23 @@ function Modal({ message, type = 'info', onClose }) {
);
}

/* ------------------ ๋กœ๊ทธ์ธ / ํšŒ์›๊ฐ€์ž… ํ†ตํ•ฉ ------------------ */
/* ๋กœ๊ทธ์ธ / ํšŒ์›๊ฐ€์ž… ํ†ตํ•ฉ ํ™”๋ฉด */
export default function LoginSignupScreen({ onNavigate }) {
const [page, setPage] = useState('login');
const [userInfo, setUserInfo] = useState(null);
const [modal, setModal] = useState(null);
const dispatch = useDispatch(); // redux์—†๋Š” ์‚ฌ๋žŒ์€ ์ œ๊ฑฐ ๊ฐ€๋Šฅ
const [modal, setModal] = useState(null);
const dispatch = useDispatch();

// ๋กœ๊ทธ์ธ ์œ ์ง€
// ๋กœ๊ทธ์ธ ์œ ์ง€
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) return;

api
.get('/member/me', {
headers: { Authorization: `Bearer ${token}` },
})
.then((res) => {
setUserInfo(res.data.data);
// Redux ์ƒํƒœ ์—…๋ฐ์ดํŠธ
dispatch(login({ token }));
dispatch(
updateProfile({
Expand All @@ -130,7 +107,6 @@ export default function LoginSignupScreen({ onNavigate }) {
memberId: res.data.data.memberId,
})
);
// ํฌ์ธํŠธ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
dispatch(fetchPointInfo());
})
.catch(() => {
Expand All @@ -143,7 +119,6 @@ export default function LoginSignupScreen({ onNavigate }) {
<>
<div className="auth-wrap">
<style>{styles}</style>

<div className="card">
<div className="title">GreenMap</div>
<div className="subtitle">๊ทธ๋ฆฐ๋งต</div>
Expand All @@ -164,50 +139,52 @@ export default function LoginSignupScreen({ onNavigate }) {

{!userInfo ? (
page === 'login' ? (
<LoginForm setUserInfo={setUserInfo} setModal={setModal} onNavigate={onNavigate} />
<LoginForm
setUserInfo={setUserInfo}
setModal={setModal}
onNavigate={onNavigate}
/>
) : (
<SignupForm setPage={setPage} setModal={setModal} />
)
) : (
<HomeScreen onNavigate={onNavigate} />
)}


{!userInfo && page === 'login' && (
<button
onClick={kakaoLogin}
style={{
width: '100%',
marginTop: '12px',
borderRadius: '12px',
overflow: 'hidden',
padding: 0,
}}
>
<img
src={kakaoBtn}
alt="์นด์นด์˜ค ๋กœ๊ทธ์ธ"
style={{ width: '100%', display: 'block' }}
/>
</button>
)}

{!userInfo && page === 'login' && (
<button
onClick={kakaoLogin}
style={{
width: '100%',
marginTop: '12px',
borderRadius: '12px',
overflow: 'hidden',
padding: 0,
}}
>
<img
src={kakaoBtn}
alt="์นด์นด์˜ค ๋กœ๊ทธ์ธ"
style={{ width: '100%', display: 'block' }}
/>
</button>
)}
</div>


{modal && (
<Modal
message={modal.message}
type={modal.type}
onClose={() => setModal(null)}
action={modal.action}
/>
)}
</div>
</>
);
}

/* ------------------ ๋กœ๊ทธ์ธ ------------------ */

function LoginForm({ setUserInfo, setModal, onNavigate }) {
const dispatch = useDispatch();
const [email, setEmail] = useState('');
Expand All @@ -224,15 +201,14 @@ function LoginForm({ setUserInfo, setModal, onNavigate }) {
const res = await api.post('/member/login', { email, password });
const token = res.data.data.accessToken;
const memberId = res.data.data.memberId;

localStorage.setItem('token', token);
localStorage.setItem('memberId', memberId);

const info = await api.get('/member/me', {
headers: { Authorization: `Bearer ${token}` },
});

// Redux ์ƒํƒœ ์—…๋ฐ์ดํŠธ

dispatch(login({ token }));
dispatch(
updateProfile({
Expand All @@ -243,20 +219,10 @@ function LoginForm({ setUserInfo, setModal, onNavigate }) {
memberId: info.data.data.memberId,
})
);
// ํฌ์ธํŠธ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
dispatch(fetchPointInfo());

setUserInfo(info.data.data);
setModal({ message: '๋กœ๊ทธ์ธ ์„ฑ๊ณต!', type: 'success' });

// ๋„ค๋น„๊ฒŒ์ด์…˜ ์ฒ˜๋ฆฌ - ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ์ด๋™
setTimeout(() => {
if (onNavigate) {
onNavigate('home');
} else if (window.location.pathname === '/login') {
window.location.href = '/';
}
}, 800);
setModal({ message: '๋กœ๊ทธ์ธ ์„ฑ๊ณต!', type: 'success', action: 'home' });
} catch {
setModal({
message: '์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.',
Expand Down Expand Up @@ -300,8 +266,8 @@ function LoginForm({ setUserInfo, setModal, onNavigate }) {
);
}

/* ------------------ ํšŒ์›๊ฐ€์ž… ------------------ */
function SignupForm({ setPage, setModal }) {
const navigate = useNavigate();
const [email, setEmail] = useState('');
const [emailAvailable, setEmailAvailable] = useState(null);
const [password, setPassword] = useState('');
Expand Down Expand Up @@ -364,10 +330,10 @@ function SignupForm({ setPage, setModal }) {
try {
await api.post('/member', { email, password, nickname });
setModal({
message: 'ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต ๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”',
message: 'ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต! ๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”',
type: 'success',
action: 'login',
});
setTimeout(() => setPage('login'), 1000);
} catch {
setModal({
message: '๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”',
Expand Down Expand Up @@ -438,7 +404,7 @@ function SignupForm({ setPage, setModal }) {
);
}

/* ------------------ ์žฌ์‚ฌ์šฉ Input ------------------ */

function InputField({ label, type, value, onChange, onBlur, isValid, touched }) {
const filled = value?.length > 0;
const showInvalid = touched && !isValid && filled;
Expand Down
Loading