Skip to content

Commit

Permalink
implement get user chats in app b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 27, 2024
1 parent e2cea66 commit 9e74fd3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 5 deletions.
29 changes: 29 additions & 0 deletions staff/marti-herms/project/G-HUB/app/logic/getUserChats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { validate, errors } from 'com'

const { SystemError } = errors

export default (targetUserId) => {
validate.string(targetUserId, 'targetUserId')

return fetch(`${import.meta.env.VITE_API_URL}/chats/${targetUserId}`, {
headers: { Authorization: `Bearer ${sessionStorage.token}` }
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 200) {
return response.json()
.then(chats => chats)
}

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
2 changes: 2 additions & 0 deletions staff/marti-herms/project/G-HUB/app/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import editUserUsername from './editUserUsername.js'
import openChat from './openChat.js'
import sendMessage from './sendMessage.js'
import getChatMessages from './getChatMessages.js'
import getUserChats from './getUserChats.js'

const logic = {
deleteReview,
Expand All @@ -36,6 +37,7 @@ const logic = {
getGameReviews,
getUser,
getUserAvatar,
getUserChats,
getUserFavs,
getUserLibrary,
getUserUsername,
Expand Down
5 changes: 4 additions & 1 deletion staff/marti-herms/project/G-HUB/app/logic/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export default (chatId, content) => {

return fetch(`${import.meta.env.VITE_API_URL}/chat/${chatId}/messages`, {
method: 'POST',
headers: { Authorization: `Bearer ${sessionStorage.token}` },
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ content })
})
.catch(error => { throw new SystemError(error.message) })
Expand Down
13 changes: 12 additions & 1 deletion staff/marti-herms/project/G-HUB/app/src/home/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ export default function Chat({ onOpenChat }) {
useEffect(() => {
try {
if (userId === loggedInUser) {
logic.getUserFollowing(userId)
logic.getUserChats(userId)
.then(chats => {
const promises = chats.map(chat => {
const index = chat.participants[0] === userId ? 1 : 0

return logic.getUser(chat.participants[index])
})

return Promise.all(promises)
})
.then(users => setUsers(users))
.catch(error => {
console.error(error)
Expand Down Expand Up @@ -92,6 +101,8 @@ export default function Chat({ onOpenChat }) {

setMessage(messageInput.value)

console.log(chat)

try {
logic.sendMessage(chat, message)
.then(() => {
Expand Down
2 changes: 0 additions & 2 deletions staff/marti-herms/project/G-HUB/app/src/home/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ export default function Game({ makeReviewVisibility, onCancel }) {
try {
logic.makeReview(gameId, comment, rate || 0)
.then(() => {
console.log('hello')

onCancel()

loadReviews()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RiUserFollowLine as FollowIcon, RiUserUnfollowLine as UnfollowIcon } from 'react-icons/ri'
import { IoIosSend as SendIcon } from 'react-icons/io'


import Container from '../library/Container'
import Avatar from '../library/Avatar'
import Paragraph from '../library/Paragraph'
Expand Down
3 changes: 3 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/getUserChats.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default (userId, targetUserId) => {
chat.id = chat._id.toString()
delete chat._id

chat.participants[0] = chat.participants[0].toString()
chat.participants[1] = chat.participants[1].toString()

return chat
})

Expand Down

0 comments on commit 9e74fd3

Please sign in to comment.