Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bdd09ed
refactor: 메인 페이지 피드 정보 받아오는 방식 변경 onSnapShot => next.js api
Nov 7, 2022
820cdd8
feat: 현재 유저 정보 받아오는 방식 변경 onSnapShot => next api
Nov 13, 2022
720fb9a
refactor: 좋아요 받아오는 방식 변경 onSnapShot => next api
Nov 19, 2022
b709431
refactor: 코멘트 정보 받아오는 방식 변경 onSnapShot => next api
Nov 20, 2022
215c231
fix: 코멘트 배열이 없을 때 발생하는 에러 처리
Nov 20, 2022
570c873
refactor: [myPage] 피드 코멘트, 좋아요 받아오는 방식 변경 onSnapShot => next api
Nov 21, 2022
07c4d85
refactor: 데이터 받아오는 방식 변경 onSnapShot => next api
Nov 23, 2022
f838c82
feat: [myPage] 피드 로딩 스켈레톤 추가
Nov 24, 2022
10fb75d
feat: [commentModal] 로딩 스켈레톤 추가
Nov 24, 2022
6ae3b9f
feat: [profilePage] 피드 로딩 스켈레톤 추가
Nov 25, 2022
bedd6ed
feat: [mainPage] 팔로우 목록 로딩 스켈레톤 추가
Nov 25, 2022
370f20d
fix: 첫 회원가입 사용자 로그인시 발생하던 오류 수정
Jun 13, 2023
1d5982e
chore: conflict 해결
Jun 13, 2023
ebdda77
Merge pull request #112 from kyw0716/fix/sign-up-error
kyw0716 Jun 13, 2023
2f3e5ed
fix: 팔로우하는 사람이 없을 때 메인 페이지 상단에 로딩 스켈레톤이 생기던 오류 수정
Jun 13, 2023
1ddfaf4
refactor: 메인 피드 정보 recoil state로 관리하기
Jun 13, 2023
2ee6006
feat: 피드 추가 낙관적 업데이트 적용
Jun 13, 2023
58d92a3
style: 타입 이름 수정
Jun 13, 2023
54b916c
feat: 피드 정보 수정 낙관적 업데이트 적용 (삭제, 비공개 여부 토글)
Jun 14, 2023
67f3541
refactor: 피드 정보 수정 낙관적 업데이트 적용 (위치, 내용, 비공개 여부 토글)
Jun 14, 2023
da2dc77
refactor: fetch => axios
Jun 15, 2023
da884ff
fix: 잘못된 axios 사용 수정
Jun 15, 2023
231903e
refactor: 팔로우 하는 유저 목록 컴포넌트 불필요한 요청 줄이기
Jun 15, 2023
bc803a2
refactor: 메인 피드 정보 불러오는 api 수정
Jun 15, 2023
1714546
fix: 피드 등록 요청에서 발생한 오류 수정
Jun 15, 2023
edf9f12
refactor: 피드 좋아요 기능 수정
Jun 16, 2023
f4059c8
chore: 잘못 지운 파일 복구
Jun 16, 2023
4bd4281
style: props 네이밍 수정
Jun 16, 2023
f16148f
chore: .env 추가
Jun 16, 2023
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
3 changes: 3 additions & 0 deletions firebase-practice/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ yarn-error.log*

# test page
test.tsx

# env
.env
19 changes: 13 additions & 6 deletions firebase-practice/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { onAuthStateChanged } from "firebase/auth"
import { authService, DBService } from "@FireBase"
import { RecoilRoot, useSetRecoilState } from "recoil"
import { darkModeState, userDataState } from "@share/recoil/recoilList"
import { UserData } from "backend/dto"
import { doc, onSnapshot } from "firebase/firestore"
import { FeedItem, UserData } from "backend/dto"
import axios from "axios"

const SetDarkMode = () => {
const setDarkRecoil = useSetRecoilState(darkModeState)
Expand All @@ -20,11 +20,18 @@ const SetDarkMode = () => {
const SetCurrnentUser = () => {
const setCurrentUser = useSetRecoilState(userDataState)
useEffect(() => {
onAuthStateChanged(authService, (user) => {
onAuthStateChanged(authService, async (user) => {
if (user) {
onSnapshot(doc(DBService, "users", `${user.uid}`), (response) => {
setCurrentUser(response.data() as UserData)
})
const userId = user.uid

const userInfo = await axios(`/api/profile?userId=${userId}`).then<
Omit<UserData, "feed">
>((res) => res.data)
const userFeeds = await axios(`/api/userFeed?userId=${userId}`).then<
FeedItem[]
>((res) => res.data)

setCurrentUser({ ...userInfo, feed: userFeeds })
}
})
}, [])
Expand Down
4 changes: 1 addition & 3 deletions firebase-practice/pages/api/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { v4 } from "uuid"
* update : commentId, oldComment, uploadTime 모두 포함한 데이터 전송
* new comment : userId, comment, feedId 만을 포함한 데이터 전송
*
*
* method : DELETE
* request url : /api/comment
* request body : {commentId, comment, uploadTime, userId, feedId}
Expand All @@ -38,15 +37,14 @@ export default async function getComment(
) {
if (req.method === "GET") {
const commentId = req.query?.commentId
console.log(commentId)
const getCommentRef = doc(DBService, "Comments", `${commentId}`)
const docSnapShot = await getDoc(getCommentRef)

if (docSnapShot.exists()) {
const data = docSnapShot.data()?.AllComments
res.status(200).json(data as Comment[])
} else {
res.status(500).json("Fail")
res.status(200).json([])
}
}
if (req.method === "POST") {
Expand Down
170 changes: 86 additions & 84 deletions firebase-practice/pages/api/feed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DBService } from "@FireBase"
import { FeedData } from "backend/dto"
import { FeedItem, UserInfo } from "backend/dto"
import {
arrayRemove,
arrayUnion,
deleteDoc,
doc,
getDoc,
setDoc,
Expand All @@ -16,12 +17,12 @@ import type { NextApiRequest, NextApiResponse } from "next"
* request url : /api/feed
* response : 메인 페이지의 피드 정보들
*
* TODO: 새로운 피드 등록과 피드 수정 기능 구분하기
* method : POST
* request url : /api/feed
* request body for new feed : {imageUrl, desc, location, isPrivate, creator}
* response : Success
*
* uploadTime이 new feed 인 경우
* method : POST
* request url : /api/feed
* request body for new feed : {imageUrl, desc, location, isPrivate, storageId, creator, uploadTime, newDesc, newLocation, newIsPrivate}
Expand All @@ -35,19 +36,39 @@ import type { NextApiRequest, NextApiResponse } from "next"

export default async function getFeed(
req: NextApiRequest,
res: NextApiResponse<FeedData[] | string>,
res: NextApiResponse<FeedItem[] | string>,
) {
if (req.method === "GET") {
const getFeedRef = doc(DBService, "mainPage", "userFeedDataAll")
const docSnapShot = await getDoc(getFeedRef)

if (docSnapShot.exists()) {
const data = docSnapShot.data()?.feed as FeedData[]
const data = docSnapShot.data()?.feed as FeedItem[]

const feedItems = await Promise.all(
data.map(async (feedItem) => {
const userId = feedItem.creator
const getUserInfoRef = doc(DBService, "users", `${userId}`)
const userInfoDocSnapShot = await getDoc(getUserInfoRef)

const userInfo = userInfoDocSnapShot.data()?.info as UserInfo

return {
...feedItem,
creator: {
...userInfo,
},
}
}),
)

res
.status(200)
.json(data.sort((a, b) => Number(a.uploadTime) - Number(b.uploadTime)))
.json(
feedItems.sort((a, b) => Number(b.uploadTime) - Number(a.uploadTime)),
)
} else {
res.status(500).json("Fail")
res.status(404).json("not found")
}
} else if (req.method === "POST") {
const {
Expand All @@ -58,89 +79,66 @@ export default async function getFeed(
storageId,
creator,
uploadTime,
newDesc,
newLocation,
newIsPrivate,
} = req.body

const firestoreAllRef = doc(DBService, "mainPage", `userFeedDataAll`)
const firestorePersonalRef = doc(DBService, "users", `${creator}`)

if (!uploadTime) {
const feed: FeedData = {
imageUrl: imageUrl,
desc: desc,
location: location,
isPrivate: isPrivate,
storageId: storageId,
creator: creator,
const firestoreRef = doc(DBService, "mainPage", `userFeedDataAll`)

const isNewFeed = uploadTime === "new feed"

if (isNewFeed) {
const feed: FeedItem = {
imageUrl,
desc,
location,
isPrivate,
storageId,
creator,
uploadTime: getCurrentTime(),
}

await updateDoc(firestoreAllRef, {
feed: arrayUnion(feed),
}).catch(async (error) => {
if (error.code === "not-found") {
await setDoc(firestoreAllRef, {
feed: [feed],
})
} else {
res.status(500).json(error.code)
}
})

await updateDoc(firestorePersonalRef, {
await updateDoc(firestoreRef, {
feed: arrayUnion(feed),
}).catch(async (error) => {
if (error.code === "not-found") {
await setDoc(firestorePersonalRef, {
setDoc(firestoreRef, {
feed: [feed],
})
} else {
res.status(500).json(error.code)
}
})

res.status(200).json("Success")
} else {
const feedToRemove: FeedData = {
imageUrl: imageUrl,
desc: desc,
location: location,
isPrivate: isPrivate,
storageId: storageId,
creator: creator,
uploadTime: uploadTime,
}
if (!isNewFeed) {
const { newDesc, newLocation, newIsPrivate } = req.body

const feedToRemove: FeedItem = {
imageUrl,
desc,
location,
isPrivate,
storageId,
creator,
uploadTime,
}
const newFeed: FeedData = {
imageUrl: imageUrl,
const newFeed: FeedItem = {
imageUrl,
desc: newDesc,
location: newLocation,
isPrivate: newIsPrivate,
storageId: storageId,
creator: creator,
uploadTime: uploadTime,
storageId,
creator,
uploadTime,
}

await updateDoc(firestoreAllRef, {
feed: arrayRemove(feedToRemove),
}).catch((error) => res.status(500).json(error.code))

await updateDoc(firestorePersonalRef, {
updateDoc(firestoreRef, {
feed: arrayRemove(feedToRemove),
}).catch((error) => {
res.status(500).json(error.code)
})

await updateDoc(firestoreAllRef, {
feed: arrayUnion(newFeed),
}).catch((error) => res.status(500).json(error.code))

await updateDoc(firestorePersonalRef, {
feed: arrayUnion(newFeed),
}).catch((error) => res.status(500).json(error.code))

res.status(200).json("Success")
.then(() => {
updateDoc(firestoreRef, {
feed: arrayUnion(newFeed),
}).catch((error) => res.status(500).json(error.code))
})
.catch((error) => res.status(500).json(error.code))
}
} else if (req.method === "DELETE") {
const {
Expand All @@ -151,33 +149,37 @@ export default async function getFeed(
storageId,
creator,
uploadTime,
} = req.body as FeedData

const firestoreAllRef = doc(DBService, "mainPage", `userFeedDataAll`)
const firestorePersonalRef = doc(DBService, "users", `${creator}`)

const feed: FeedData = {
imageUrl: imageUrl,
desc: desc,
location: location,
isPrivate: isPrivate,
storageId: storageId,
creator: creator,
uploadTime: uploadTime,
userId,
} = req.body

const feedRef = doc(DBService, "mainPage", `userFeedDataAll`)
const userRef = doc(DBService, "users", `${userId}`)
const commentRef = doc(DBService, "Comments", `${storageId}`)

const feed: Omit<FeedItem, "creator"> & { creator: string } = {
creator,
desc,
imageUrl,
isPrivate: isPrivate === "true",
location,
storageId,
uploadTime,
}

await updateDoc(firestoreAllRef, {
updateDoc(feedRef, {
feed: arrayRemove(feed),
}).catch((error) => {
res.status(500).json(error.code)
})

await updateDoc(firestorePersonalRef, {
feed: arrayRemove(feed),
updateDoc(userRef, {
likeFeedIds: arrayRemove(storageId),
}).catch((error) => {
res.status(500).json(error.code)
})

res.status(200).json("Success")
deleteDoc(commentRef).catch((error) => {
res.status(500).json(error.code)
})
}
}
Loading