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

캘린더 CRUD API 연결 #173

Merged
merged 1 commit into from
Dec 9, 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
78 changes: 44 additions & 34 deletions src/components/CalendarInfoModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import {
export default function CalendarInfo({ calendar, onClose, userId }) {
// 통합 상태 관리
const [calendarState, setCalendarState] = useState({
title: calendar.calendar_name,
detailMemo: calendar.calendar_description,
name: calendar.name,
description: calendar.description,
isPublic: calendar.is_public,
calColor: calendar.calendar_color,
visitCode: calendar.invitation_code,
color: calendar.color,
invitation_code: calendar.invitation_code,
admins: calendar.admins,
});
const { loggedIn, userInfo, setLoggedIn } = useAuth();
const { userInfo } = useAuth();
{
calendar?.admins?.length > 0 ? calendar.admins.join(", ") : "나만의 캘린더";
}
const [isEdit, setIsEdit] = useState(false);

// 상태 변경 핸들러
Expand All @@ -36,20 +40,21 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
// 상태 초기화 함수
const resetState = () => {
setCalendarState({
title: calendar.calendar_name,
detailMemo: calendar.calendar_description,
name: calendar.name,
description: calendar.description,
isPublic: calendar.is_public,
calColor: calendar.calendar_color,
visitCode: calendar.invitation_code,
color: calendar.color,
invitation_code: calendar.invitation_code,
admins: calendar.admins,
});
setIsEdit(false);
};

// 컴포넌트가 마운트될 때 creator_id와 userId 확인
// creator_id와 userId 확인
useEffect(() => {
// console.log("Calendar Creator ID:", calendar.creator);
// console.log("Current User ID:", userInfo.user_id);
// console.log(calendar);
console.log("Calendar Creator ID:", calendar.creator);
console.log("Current User ID:", userInfo.user_id);
console.log(calendar);
}, [calendar.creator_id, userId]);

// 저장
Expand All @@ -59,10 +64,11 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
const response = await instance.patch(
`/api/calendars/${calendar.calendar_id}/`,
{
calendar_name: calendarState.title,
calendar_description: calendarState.detailMemo,
name: calendarState.name,
description: calendarState.description,
is_public: calendarState.isPublic,
calendar_color: calendarState.calColor,
color: calendarState.color,
creator: calendarState.creator_id,
},

{
Expand Down Expand Up @@ -108,28 +114,32 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
}
};

//관리자 +a 계산
const visibleAdmins = calendar?.admins?.slice(0, 3) || [];
const remainingAdminsCount = calendar?.admins?.length - visibleAdmins.length;

return (
<div className="flex h-[29rem] w-[43rem] translate-x-[3rem] justify-center rounded-[1.25rem] bg-eventoWhite p-[2.8rem] shadow-xl shadow-lightGray/50">
<FaXmark
size={25}
className="absolute right-[1.2rem] top-[1.2rem] cursor-pointer text-darkGray"
onClick={onClose}
/>
<div className="flex w-full flex-col">
<div className="flex flex-col w-full">
<div className="mb-[2.8rem] flex justify-between">
{isEdit ? (
<input
type="text"
value={calendarState.title}
value={calendarState.name}
className="h-[3.8rem] w-[35rem] rounded-md bg-lightGray/20 text-[3em] font-bold text-darkGray focus:outline-none"
onChange={(e) => updateState("title", e.target.value)}
onChange={(e) => updateState("name", e.target.value)}
/>
) : (
<div
className="flex h-[3.8rem] w-[35rem] items-center rounded-md text-[3em] font-bold focus:outline-none"
style={{ color: calendarState.calColor }}
style={{ color: calendarState.color }}
>
{calendarState.title}
{calendarState.name}
</div>
)}
</div>
Expand All @@ -138,9 +148,9 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
<div className="mb-[0.75rem] text-[1rem] font-bold text-eventoPurple">
멤버
</div>
<div className="w-[40rem] text-[1.1rem] font-semibold text-darkGray">
{calendar?.members?.length > 0
? calendar.members.map((member) => member.nickname).join(", ")
<div className="w-[40rem] text-[0.9rem] text-darkGray/90">
{calendar?.admins?.length > 0
? calendar.admins.join(", ")
: "나만의 캘린더"}
</div>
</div>
Expand All @@ -157,9 +167,9 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
<div className="flex">
<input
type="text"
value={calendarState.detailMemo}
value={calendarState.description}
className="mb-[3rem] mr-[2rem] flex h-[1.5rem] w-[18rem] items-center rounded-md bg-lightGray/20 text-[1.1rem] font-semibold text-darkGray"
onChange={(e) => updateState("detailMemo", e.target.value)}
onChange={(e) => updateState("description", e.target.value)}
/>
<div className="ml-[0.2rem] flex h-[2rem] w-[10rem]">
{[
Expand All @@ -173,11 +183,11 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
].map(({ color, label }) => (
<button
key={label}
onClick={() => updateState("calColor", color)}
onClick={() => updateState("color", color)}
className="relative mr-[0.3rem] flex h-[1rem] w-[1rem] items-center justify-center"
style={{ backgroundColor: color }}
>
{calendarState.calColor === color && (
{calendarState.color === color && (
<FaCheck className="absolute text-[0.6rem] text-eventoWhite" />
)}
</button>
Expand All @@ -187,11 +197,11 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
) : (
<div className="flex">
<div className="mb-[3rem] flex h-[1.5rem] w-[20rem] items-center text-[1.1rem] font-semibold text-darkGray">
{calendarState.detailMemo}
{calendarState.description}
</div>
<div
className="ml-[0.2rem] h-[1rem] w-[1rem] items-center"
style={{ backgroundColor: calendarState.calColor }}
style={{ backgroundColor: calendarState.color }}
/>
</div>
)}
Expand Down Expand Up @@ -225,7 +235,7 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
)}
</div>
<div className="pr-[0.5rem] text-[1.1rem] font-semibold text-darkGray">
{calendarState.visitCode}
{calendarState.invitation_code}
</div>
</div>
) : (
Expand All @@ -234,11 +244,11 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
{calendarState.isPublic ? "공개" : "비공개"}
</div>
<div className="pr-[0.5rem] text-[1.1rem] font-semibold text-darkGray">
{calendarState.visitCode}
{calendarState.invitation_code}
</div>
<CopyToClipboard
className="cursor-pointer text-eventoPurple active:text-lightGray"
text={calendarState.visitCode}
text={calendarState.invitation_code}
>
<FaRegCopy />
</CopyToClipboard>
Expand Down Expand Up @@ -266,7 +276,7 @@ export default function CalendarInfo({ calendar, onClose, userId }) {
className="cursor-pointer text-[1.5rem] text-darkGray"
onClick={() => setIsEdit(true)}
/>
{calendar.creator === userInfo.user_id && (
{calendar.creator_id === userInfo.user_id && (
<FaTrashCan
className="cursor-pointer text-[1.5rem] text-darkGray"
onClick={handleDelete}
Expand Down
7 changes: 3 additions & 4 deletions src/components/CreateCalendarModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ export default function CreateCalendar({ onClose }) {
if (!title) {
throw new Error("캘린더 제목을 입력해주세요.");
}

const response = await instance.post(
"/api/calendars/",
{
calendar_name: title,
calendar_description: detailMemo,
name: title,
description: detailMemo,
is_public: isPublic,
calendar_color: calColor,
color: calColor,
},
{
headers: {
Expand Down
21 changes: 11 additions & 10 deletions src/components/SideBarLeft.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ export default function SideBarLeft() {
const response = await axios.patch(
`/api/calendars/${id}`,
{
calendar_name: myCalendars.calendar_name,
calendar_description: myCalendars.calendar_description,
calendar_name: myCalendars.name,
calendar_description: myCalendars.description,
is_public: myCalendars.is_public,
calendar_color: myCalendars.calendar_color,
calendar_color: myCalendars.color,
admins: myCalendars.admins,
isactive: checked[id],
},
{
Expand Down Expand Up @@ -176,22 +177,22 @@ export default function SideBarLeft() {
{checked[calendar.calendar_id] ? (
<FaCheckSquare
className="text-[0.93rem]"
style={{ color: calendar.calendar_color }}
style={{ color: calendar.color }}
/>
) : (
<FaRegSquare
className="text-[0.93rem]"
style={{ color: calendar.calendar_color }}
style={{ color: calendar.color }}
/>
)}
</div>
<label
htmlFor={calendar.calendar_id}
className="cursor-pointer text-[0.9rem] font-medium"
style={{ color: calendar.calendar_color }}
style={{ color: calendar.color }}
onClick={() => handleViewCalendar(calendar)}
>
{calendar.calendar_name}
{calendar.name}
</label>
</li>
))}
Expand Down Expand Up @@ -230,9 +231,9 @@ export default function SideBarLeft() {
htmlFor={calendar.calendar_id}
className="flex items-center text-[0.9rem] font-medium text-eventoPurpleBase"
>
{calendar.calendar_name}
{calendar.name}
<span className="ml-2 text-[0.7rem] font-light text-darkGray">
{calendar.calendar_description}
{calendar.description}
</span>
</label>
</li>
Expand All @@ -257,11 +258,11 @@ export default function SideBarLeft() {
<CalendarInfo
calendar={selectedCalendar}
onClose={() => setCalendarInfoOpen(false)}
onSave={handleSaveCalendar}
userId={userInfo?.user_id}
/>
</div>
)}

<div className="fixed bottom-[3rem] left-[4rem] flex flex-col items-center justify-center">
<ul className="space-y-[.8rem]">
{(Array.isArray(dday) ? dday : []).map((item, index) => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default function Calendar() {
async function fetchCalInfo() {
try {
const token = localStorage.getItem("token"); // 로컬 스토리지에서 토큰 가져오기
const response = await instance.get("/api/calendars/admins", {
const response = await instance.get("/api/calendars/admin", {
headers: {
Authorization: `Bearer ${token}`,
},
Expand Down