Skip to content

Commit

Permalink
Merge pull request #74 from 9oormDari/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
karpitony authored Sep 28, 2024
2 parents d023113 + fa0580f commit ebf756f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 54 deletions.
114 changes: 73 additions & 41 deletions src/components/Goalpage/GoormScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Rainbow1 from '../../assets/GoalPage/Rainbow1.svg';
import Rainbow2 from '../../assets/GoalPage/Rainbow2.svg';
import CloudComponent from './GoormScreen/CloudComponent';
import { API } from '../../lib/api/index.ts';
import useIdStore from '../../components/idStore.ts';

interface Cloud {
cloudType: string;
Expand All @@ -16,21 +17,52 @@ interface Cloud {

export default function GoormScreen() {
const [stage, setStage] = useState<number>(0);
const [targetStage, setTargetStage] = useState<number>(0); // λͺ©ν‘œ μŠ€ν…Œμ΄μ§€ μƒνƒœ μΆ”κ°€

const [clouds, setClouds] = useState<Cloud[]>([]);
const id = useIdStore((state) => state.id);

// idκ°€ 변경될 λ•Œ 데이터λ₯Ό κ°€μ Έμ˜€λŠ” ν•¨μˆ˜
useEffect(() => {
const fetchUserData = async () => {
if (!id) return; // idκ°€ μ—†μœΌλ©΄ μ‹€ν–‰ν•˜μ§€ μ•ŠμŒ

try {
const response = await API.User.getUserStep(id);
if (response.status === 'OK' && response.data) {
console.log("currentStep", response.data.currentStep);
setTargetStage(response.data.currentStep); // λͺ©ν‘œ μŠ€ν…Œμ΄μ§€ μ„€μ •
setStage(0); // 초기 μŠ€ν…Œμ΄μ§€λ₯Ό 0으둜 μ„€μ •
} else {
console.error('응닡 μƒνƒœκ°€ OKκ°€ μ•„λ‹ˆκ±°λ‚˜ 데이터가 μ—†μŠ΅λ‹ˆλ‹€.');
setTargetStage(0);
setStage(0);
}
} catch (error) {
console.error('λ‚΄ 정보λ₯Ό λΆˆλŸ¬μ˜€λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€:', error);
setTargetStage(0);
setStage(0);
}
};

fetchUserData();
}, [id]);

useEffect(() => {
const fetchMyData = async () => {
try {
const response = await API.User.currentStep();

if (response.status === 'OK' && response.data) {
setStage(response.data.currentStep);
setStage(0);
setTargetStage(response.data.currentStep);
console.log('ν˜„μž¬ μŠ€ν…Œμ΄μ§€:', response.data.currentStep);
} else {
console.error(
'응닡 μƒνƒœκ°€ OKκ°€ μ•„λ‹ˆκ±°λ‚˜ 데이터가 μ—†μŠ΅λ‹ˆλ‹€.'
);
setStage(0);
setTargetStage(0);
}
} catch (error) {
console.error(
Expand All @@ -44,54 +76,54 @@ export default function GoormScreen() {
fetchMyData();
}, []);

useEffect(() => {
const newClouds: Cloud[] = [
{
cloudType: stage === 1 ? ColorCloud : BlueCloud,
colStart: 1,
rowStart: 1,
hidden: stage < 1,
},
{
cloudType: stage === 2 ? ColorCloud : BlueCloud,
colStart: 2,
rowStart: 2,
hidden: stage < 2,
},
{
cloudType: stage === 3 ? ColorCloud : BlueCloud,
colStart: 3,
rowStart: 1,
hidden: stage < 3,
},
{
cloudType: stage === 4 ? ColorCloud : BlueCloud,
colStart: 4,
rowStart: 2,
hidden: stage < 4,
},
];

setClouds(newClouds); // μƒˆλ‘œμš΄ clouds λ°°μ—΄λ‘œ μ—…λ°μ΄νŠΈ
}, [stage]); // stageκ°€ 변경될 λ•Œλ§ˆλ‹€ μ‹€ν–‰

// μž„μ‹œλ‘œ 집어넣은 ꡬ름 단계 증가 ν•¨μˆ˜
const increaseStage = (stage:number) => {
setStage((prev) => (prev < stage ? prev + 1 : prev));
};
// stageκ°€ 변경될 λ•Œ clouds μƒνƒœ μ—…λ°μ΄νŠΈ
useEffect(() => {
console.log("Stage updated:", stage);
const updateClouds = () => {
const newClouds: Cloud[] = [
{
cloudType: stage === 1 ? ColorCloud : BlueCloud,
colStart: 1,
rowStart: 1,
hidden: stage < 1,
},
{
cloudType: stage === 2 ? ColorCloud : BlueCloud,
colStart: 2,
rowStart: 2,
hidden: stage < 2,
},
{
cloudType: stage === 3 ? ColorCloud : BlueCloud,
colStart: 3,
rowStart: 1,
hidden: stage < 3,
},
{
cloudType: stage === 4 ? ColorCloud : BlueCloud,
colStart: 4,
rowStart: 2,
hidden: stage < 4,
},
];

setClouds(newClouds); // μƒˆλ‘œμš΄ clouds λ°°μ—΄λ‘œ μ—…λ°μ΄νŠΈ
};

updateClouds();
}, [stage, targetStage]); // stageκ°€ 변경될 λ•Œλ§ˆλ‹€ μ‹€ν–‰

// ꡬ름 μ¦κ°€μ‹œν‚€λŠ”λ° 천천히 μ¦κ°€ν•˜λ„λ‘ μ„€μ •
useEffect(() => {
if (stage === 0) {
return;
} else if (stage < 4) { // μ΅œλŒ€ μŠ€ν…Œμ΄μ§€κ°€ 4라면
if (stage <= targetStage) { // λͺ©ν‘œ μŠ€ν…Œμ΄μ§€λ₯Ό λ„˜μ§€ μ•Šλ„λ‘ μ„€μ •
const timer = setTimeout(() => {
increaseStage(stage);
}, 1500);
setStage((prev) => (prev < targetStage ? prev + 1 : prev)); // λͺ©ν‘œ μŠ€ν…Œμ΄μ§€λ₯Ό λ„˜μ§€ μ•Šλ„λ‘ 증가
}, 1000);

return () => clearTimeout(timer);
}
}, [stage]);
}, [stage, targetStage]);

return (
<div
Expand Down
27 changes: 19 additions & 8 deletions src/components/Goalpage/MemberList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ interface Member {
export default function MemberList() {
const [isModalVisible, setIsModalVisible] = useState(false);
const [isSeeMyImageModalVisible, setIsSeeMyImageModalVisible] = useState(false);
const [canOpenSeeMyImageModal, setCanOpenSeeMyImageModal] = useState(false);
const [members, setMembers] = useState<Member[]>([
{ id: '1', username: 'κΉ€', profileUrl: '' },
{ id: '2', username: 'λ°•', profileUrl: '' },
{ id: '3', username: '졜', profileUrl: '' },
{ id: '1', username: 'Empty Data', profileUrl: '' },
{ id: '2', username: 'Empty Data', profileUrl: '' },
{ id: '3', username: 'Empty Data', profileUrl: '' },
]);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
Expand All @@ -28,22 +29,28 @@ export default function MemberList() {
const fetchMyData = async () => {
try {
const response = await API.User.getMyInfo();
setMyId(response.username);
setMyName(response.nickname);
console.log(myId, myName);
console.log(response);
setMyId(response.id);
setMyName(response.nickname);
} catch (error) {
console.error('λ‚΄ 정보λ₯Ό λΆˆλŸ¬μ˜€λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€:', error);
}
};
fetchMyData();
}, []);

useEffect(() => {
// myId와 myName이 μ—…λ°μ΄νŠΈλ˜λ©΄ canOpenSeeMyImageModal μƒνƒœλ₯Ό true둜 λ³€κ²½
if (myId && myName) {
setCanOpenSeeMyImageModal(true);
}
}, [myId, myName]);

useEffect(() => {
const fetchMemberList = async () => {
setLoading(true);
try {
const response = await API.User.getGroupMemberList();

if (response.status === 'OK' && response.data) {
const memberList: Member[] = response.data;
setMembers(memberList);
Expand All @@ -67,7 +74,11 @@ export default function MemberList() {
};

const openSeeMyImageModal = () => {
setIsSeeMyImageModalVisible(true);
if (canOpenSeeMyImageModal) {
setIsSeeMyImageModalVisible(true);
} else {
alert('λ‚΄ 정보λ₯Ό λΆˆλŸ¬μ˜€λŠ” μ€‘μž…λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.');
}
}

const closeSeeMyImageModal = () => {
Expand Down
10 changes: 7 additions & 3 deletions src/components/Goalpage/MemberList/MemberCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ShowImageModal from './ShowImageModal';
import { useState } from 'react';
import cn from '../../../lib/cn';
import useIdStore from '../../idStore';

interface MemberCardProps {
id: string; // νšŒμ›μ„ 식별할 수 μžˆλŠ” 고유 ID
Expand All @@ -10,11 +11,15 @@ interface MemberCardProps {

export default function MemberCard({ id, name, profileUrl }: MemberCardProps) {
const [isModalOpen, setIsModalOpen] = useState(false);
const setId = useIdStore((state) => state.setId);

// λͺ¨λ‹¬μ„ μ—΄κ³  idλ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜
const handleOpenModal = () => {
setIsModalOpen(true);
setId(id); // zustand μŠ€ν† μ–΄μ˜ idλ₯Ό μ„€μ •
setIsModalOpen(true); // λͺ¨λ‹¬ μ—΄κΈ°
};

// λͺ¨λ‹¬μ„ λ‹«λŠ” ν•¨μˆ˜
const handleCloseModal = () => {
setIsModalOpen(false);
};
Expand All @@ -35,11 +40,10 @@ export default function MemberCard({ id, name, profileUrl }: MemberCardProps) {
'w-16 md:w-32 h-8 md:h-12 bg-slate-400 text-white rounded-lg',
'hover:bg-slate-700 transition text-sm md:text-xl font-semibold'
)}
onClick={handleOpenModal}
onClick={handleOpenModal} // λͺ¨λ‹¬ 열기와 id 섀정을 ν•˜λ‚˜μ˜ ν•¨μˆ˜λ‘œ 처리
>
보러 κ°€κΈ°
</button>

{isModalOpen && (
<ShowImageModal
memberId={id}
Expand Down
25 changes: 25 additions & 0 deletions src/components/idStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// idStore.ts
import { create } from 'zustand';

// μƒνƒœ νƒ€μž…μ„ μ •μ˜ν•©λ‹ˆλ‹€.
interface IdState {
id: string;
}

// μ•‘μ…˜ νƒ€μž…μ„ μ •μ˜ν•©λ‹ˆλ‹€.
interface IdActions {
setId: (newId: string) => void;
}

// μƒνƒœμ™€ μ•‘μ…˜μ„ ν•©μΉœ νƒ€μž…μ„ μ •μ˜ν•©λ‹ˆλ‹€.
type IdStore = IdState & IdActions;

// μƒνƒœλ₯Ό κ΄€λ¦¬ν•˜λŠ” zustand μŠ€ν† μ–΄λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
const useIdStore = create<IdStore>((set) => ({
id: '', // 초기 μƒνƒœ κ°’

// setId ν•¨μˆ˜λŠ” μƒˆλ‘œμš΄ id 값을 λ°›μ•„μ„œ μƒνƒœλ₯Ό μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.
setId: (newId) => set(() => ({ id: newId })),
}));

export default useIdStore;
14 changes: 12 additions & 2 deletions src/lib/api/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export namespace __User {
tokenOn: true,
});
}
export async function currentStep() {
const url = `${BASE_URL}/user/current-step`;
export async function getUserStep(id:string) {
const url = `${BASE_URL}/user/current-step/${id}`;

return fetchData({
url,
Expand Down Expand Up @@ -130,4 +130,14 @@ export namespace __User {
tokenOn: true,
});
}

export async function currentStep() {
const url = `${BASE_URL}/user/current-step`;

return fetchData({
url,
method: 'GET',
tokenOn: true,
});
}
}

0 comments on commit ebf756f

Please sign in to comment.