forked from moyeothon/2024_moyeothon_4team_FE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feature: 홈페이지 구현
- Loading branch information
Showing
8 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useState } from "react"; | ||
import styled from "styled-components"; | ||
import Modal from "@components/common/Modal"; | ||
import BackgroundImage from "/images/BackgroundImage.svg"; | ||
|
||
const StyledLetterPaper = styled.div` | ||
cursor: pointer; | ||
padding: 20px; | ||
border-radius: 8px; | ||
background-image: url(${BackgroundImage}); | ||
background-size: cover; | ||
background-repeat: no-repeat; | ||
background-position: center; | ||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2); /* 그림자 추가 */ | ||
margin-bottom: 10px; | ||
`; | ||
|
||
const LetterPaper = ({ letter, onClick }) => { | ||
const { letter: letterContent, sendAt } = letter; | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const sendDate = new Date(sendAt); | ||
const currentDate = new Date(); | ||
|
||
const handleClick = () => { | ||
if (sendDate > currentDate) { | ||
onClick("이 편지는 아직 열 수 없습니다."); | ||
} else { | ||
setIsModalOpen(true); | ||
} | ||
}; | ||
|
||
const closeModal = () => { | ||
setIsModalOpen(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<StyledLetterPaper onClick={handleClick}> | ||
{sendDate > currentDate ? "비공개 편지" : letterContent} | ||
</StyledLetterPaper> | ||
{isModalOpen && ( | ||
<Modal onClose={closeModal}> | ||
<h2>편지 내용</h2> | ||
<p>{letterContent}</p> | ||
<button onClick={closeModal}>닫기</button> | ||
</Modal> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default LetterPaper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// components/common/Modal.js | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
|
||
const Overlay = styled.div` | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
background: rgba(0, 0, 0, 0.5); /* 반투명한 배경 */ | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
z-index: 1000; | ||
`; | ||
|
||
const ModalContent = styled.div` | ||
background: white; | ||
border-radius: 8px; | ||
padding: 20px; | ||
max-width: 500px; | ||
width: 100%; | ||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3); /* 모달 창 그림자 */ | ||
position: relative; | ||
`; | ||
|
||
const CloseButton = styled.button` | ||
position: absolute; | ||
top: 10px; | ||
right: 10px; | ||
background: none; | ||
border: none; | ||
font-size: 20px; | ||
cursor: pointer; | ||
`; | ||
|
||
const Modal = ({ children, onClose }) => { | ||
return ( | ||
<Overlay onClick={onClose}> | ||
<ModalContent onClick={(e) => e.stopPropagation()}> | ||
<CloseButton onClick={onClose}>×</CloseButton> | ||
{children} | ||
</ModalContent> | ||
</Overlay> | ||
); | ||
}; | ||
|
||
export default Modal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useState, useEffect } from "react"; | ||
import { getReceivedLetters, getSentLetters } from "@services/lettergiveService"; | ||
|
||
export const usegiveLetters = (type) => { | ||
const [letters, setLetters] = useState([]); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
const fetchLetters = async () => { | ||
setLoading(true); | ||
try { | ||
const data = type === "received" ? await getReceivedLetters() : await getSentLetters(); | ||
setLetters(data); | ||
} catch (err) { | ||
setError(err); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
fetchLetters(); | ||
}, [type]); | ||
|
||
return { letters, loading, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
import * as S from "./styled"; | ||
import { useState } from "react"; | ||
import { usegiveLetters } from "@hooks/usegiveLetters"; | ||
import LetterPaper from "@components/LetterPaper"; | ||
import { toast } from "react-toastify"; | ||
import ToastOne from "@components/common/Toasts/ToastOne"; | ||
|
||
export const LetterInventoryPage = () => { | ||
const [type, setType] = useState("received"); | ||
const { letters, loading, error } = usegiveLetters(type); | ||
|
||
// 데이터 검증: letters가 배열이 아닐 경우 빈 배열로 설정 | ||
const validLetters = Array.isArray(letters) ? letters : []; | ||
|
||
const handleOptionClick = (selectedType) => { | ||
setType(selectedType); | ||
}; | ||
|
||
const showToast = (message) => { | ||
toast.info(message, { autoClose: 2000 }); | ||
}; | ||
|
||
if (loading) return <div>Loading...</div>; | ||
if (error) return <div>Error: {error.message || "An unexpected error occurred"}</div>; | ||
|
||
return ( | ||
<S.Wrapper> | ||
<S.MiniHeader> | ||
<S.HeaderOption>받은 편지</S.HeaderOption> | ||
<S.HeaderOption>보낸 편지</S.HeaderOption> | ||
<S.HeaderOption onClick={() => handleOptionClick("received")}>받은 편지</S.HeaderOption> | ||
<S.HeaderOption onClick={() => handleOptionClick("sent")}>보낸 편지</S.HeaderOption> | ||
</S.MiniHeader> | ||
<div> | ||
{validLetters.length === 0 ? ( | ||
<div>아직 편지함에 편지가 없습니다!</div> | ||
) : ( | ||
validLetters.map((letter, index) => ( | ||
<LetterPaper key={index} letter={letter} onClick={showToast} /> | ||
)) | ||
)} | ||
</div> | ||
<ToastOne /> | ||
</S.Wrapper> | ||
) | ||
} | ||
); | ||
}; | ||
|
||
export default LetterInventoryPage; | ||
export default LetterInventoryPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { instance } from "./instance"; | ||
|
||
export const getReceivedLetters = async () => { | ||
try { | ||
const response = await instance.get("/get/received/letter"); | ||
return response.data; | ||
} catch (error) { | ||
console.error("Failed to fetch received letters", error); // 에러 출력 | ||
throw error; // 에러를 다시 던짐 | ||
} | ||
}; | ||
|
||
export const getSentLetters = async () => { | ||
try { | ||
const response = await instance.get("/get/sent/letter"); | ||
return response.data; | ||
} catch (error) { | ||
console.error("Failed to fetch sent letters", error); // 에러 출력 | ||
throw error; // 에러를 다시 던짐 | ||
} | ||
}; |