
// 좋아요한 참여했Surv을 삭제하는 함수
export const deleteLikedPostsLite = async (userId: string, postId: string) => {
try {
const postRef = doc(db, `users/${userId}/liteSurveyLikedPosts`, postId);
await deleteDoc(postRef);
} catch (error) {
console.error(`좋아요한 게시글 ${postId} 삭제 실패: `, error);
// 오류를 다시 던져 상위 컴포넌트에서 처리할 수 있게 함
throw error;
}
};
//내가 좋아요한 IT 서베이
export const getLikedPostsIT = async (userId: string) => {
const likedPostsQuery = query(collection(db, `users/${userId}/itSurveyLikedPosts`), where('liked', '==', true));
const querySnapshot = await getDocs(likedPostsQuery);
const likedPosts = await Promise.all(
querySnapshot.docs.map(async documentSnapshot => {
const postId = documentSnapshot.id;
const postRef = doc(db, 'posts', postId); //
const postSnap = await getDoc(postRef);
const postData = postSnap.data();
return {
id: postId,
title: postData?.title,
content: postData?.content,
};
}),
);
return likedPosts;
};
// 좋아요한 IT 서베이를 삭제하는 함수
export const deleteLikedPostIT = async (userId: string, postId: string) => {
try {
const postRef = doc(db, `users/${userId}/itSurveyLikedPosts`, postId);
await deleteDoc(postRef);
} catch (error) {
console.error(`좋아요한 게시글 ${postId} 삭제 실패: `, error);
// 오류를 다시 던져 상위 컴포넌트에서 처리할 수 있게 함
throw error;
}
};
useEffect(() => {
if (userId) {
setIsLoading(true);
Promise.all([getUserPostsIT(userId), getUserPostLite(userId), getLikedPostsLite(userId), getLikedPostsIT(userId)])
.then(([postsIT, postsLite, likedLitePostsData, likedITPostsData]) => {
setPosts(postsIT);
setUserPostLite(postsLite);
setLikedLitePosts(likedLitePostsData);
setLikedITPosts(likedITPostsData);
})
.finally(() => {
setIsLoading(false);
});
}
}, [userId]);
// 좋아요한 게시글 삭제 핸들러
const clickDeleteLikedPostLiteHandler = async (postId: string) => {
const result = await Swal.fire({
title: '좋아요를 해제하시겠습니까?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '확인',
cancelButtonText: '취소',
});
if (result.isConfirmed) {
try {
await deleteLikedPostsLite(userId, postId); // 좋아요한 게시글 삭제 함수 호출
setLikedLitePosts(prevPosts => prevPosts.filter(post => post.id !== postId)); // UI 업데이트
Swal.fire({
title: '좋아요가 해제되었습니다.',
confirmButtonText: '확인',
confirmButtonColor: '#3085d6',
icon: 'success',
});
} catch (error) {
console.error('좋아요한 게시글 삭제 실패: ', error);
Swal.fire({
title: '좋아요 해제에 실패했습니다.',
text: '다시 시도해 주세요.',
icon: 'error',
});
}
}
};
// 좋아요한 게시글 삭제 핸들러
const clickDeleteLikedPostITHandler = async (postId: string) => {
const result = await Swal.fire({
title: '좋아요를 해제하시겠습니까?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '확인',
cancelButtonText: '취소',
});
if (result.isConfirmed) {
try {
await deleteLikedPostIT(userId, postId); // 좋아요한 게시글 삭제 함수 호출
setLikedITPosts(prevPosts => prevPosts.filter(post => post.id !== postId)); // UI 업데이트
Swal.fire({
title: '좋아요가 해제되었습니다.',
confirmButtonText: '확인',
confirmButtonColor: '#3085d6',
icon: 'success',
});
} catch (error) {
console.error('좋아요한 게시글 삭제 실패: ', error);
Swal.fire({
title: '좋아요 해제에 실패했습니다.',
text: '다시 시도해 주세요.',
icon: 'error',
});
}
}
};
User
'{ id: string; title: any; content: any; }[]' 형식의 인수는 'SetStateAction<PostIT[]>' 형식의 매개 변수에 할당될 수 없습니다.
'{ id: string; title: any; content: any; }[]' 형식은 'PostIT[]' 형식에 할당할 수 없습니다.
'deadlineDate' 속성이 '{ id: string; title: any; content: any; }' 형식에 없지만 'PostIT' 형식에서 필수입니다.
이런 타입문제는 매우 많이 겪었고...가뿐하게 이젠 해결할수 있었으면 좋겠다
오늘의 한줄평 : 병원진료가 내일인데 오늘인줄알고 간 바보가 있다....?하 ...