forked from b00tc4mp/isdi-bootcamp-202405
-
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.
implement chat and messages logic and view in app b00tc4mp#84
- Loading branch information
Showing
14 changed files
with
296 additions
and
22 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
29 changes: 29 additions & 0 deletions
29
staff/marti-herms/project/G-HUB/app/logic/getChatMessages.js
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,29 @@ | ||
import { validate, errors } from 'com' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (chatId) => { | ||
validate.string(chatId, 'chatId') | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/chat/${chatId}/messages`, { | ||
headers: { Authorization: `Bearer ${sessionStorage.token}` } | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 200) { | ||
return response.json() | ||
.then(messages => messages) | ||
} | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
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,29 @@ | ||
import { validate, errors } from 'com' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (targetUserId) => { | ||
validate.string(targetUserId, 'targetUserId') | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/chat/${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(chatId => chatId) | ||
} | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
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,31 @@ | ||
import { validate, errors } from 'com' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (chatId, content) => { | ||
validate.string(chatId, 'chatId') | ||
validate.string(content, 'content') | ||
|
||
if (content.trim().length === 0) throw new Error('empty content') | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/chat/${chatId}/messages`, { | ||
method: 'POST', | ||
headers: { Authorization: `Bearer ${sessionStorage.token}` }, | ||
body: JSON.stringify({ content }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 200) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
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,141 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useParams } from 'react-router-dom' | ||
import { IoIosSend as SendIcon } from 'react-icons/io' | ||
|
||
import Container from '../library/Container' | ||
import Form from '../library/Form' | ||
import Input from '../library/Input' | ||
import Button from '../library/Button' | ||
import Avatar from '../library/Avatar' | ||
import Paragraph from '../library/Paragraph' | ||
|
||
import Message from './Message' | ||
|
||
import useContext from '../context' | ||
import extractPayloadFromToken from '../../util/extractPayloadFromToken' | ||
|
||
import defaultAvatar from '../../images/defaultAvatar.svg' | ||
|
||
import logic from '../../logic' | ||
|
||
export default function Chat({ onOpenChat }) { | ||
const [users, setUsers] = useState([]) | ||
const [chat, setChat] = useState(null) | ||
const [messages, setMessages] = useState([]) | ||
const { alert } = useContext() | ||
|
||
const { sub: loggedInUser } = extractPayloadFromToken(sessionStorage.token) | ||
|
||
const { userId } = useParams() | ||
|
||
useEffect(() => { | ||
try { | ||
if (userId === loggedInUser) { | ||
logic.getUserFollowing(userId) | ||
.then(users => setUsers(users)) | ||
.catch(error => { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
}) | ||
} else { | ||
logic.openChat(userId) | ||
.then(chatId => setChat(chatId)) | ||
.catch(error => { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
}) | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
} | ||
}, [userId]) | ||
|
||
useEffect(() => { | ||
let intervalId | ||
if (chat) { | ||
loadMessages() | ||
intervalId = setInterval(() => { | ||
loadMessages() | ||
}, 1000) | ||
} | ||
return () => { | ||
if (intervalId) { | ||
clearInterval(intervalId) | ||
setChat(null) | ||
} | ||
} | ||
}, [chat]) | ||
|
||
const handleSendMessage = (event) => { | ||
event.preventDefault() | ||
|
||
const form = event.target | ||
|
||
const messageInput = form['message-input'] | ||
|
||
const message = messageInput.value | ||
|
||
try { | ||
logic.sendMessage(chat, message) | ||
.then(() => { | ||
loadMessages() | ||
}) | ||
.catch(error => { | ||
console.error(error) | ||
|
||
alert(error) | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
} | ||
} | ||
|
||
const loadMessages = () => { | ||
try { | ||
logic.getChatMessages(chat) | ||
.then(messages => setMessages(messages.reverse())) | ||
.catch(error => { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
} | ||
} | ||
|
||
const handleOpenChat = (userId) => { | ||
onOpenChat(userId) | ||
} | ||
|
||
return <Container className='w-full h-full flex flex-col pb-[110px]' > | ||
{userId !== loggedInUser ? <> | ||
<Container className='w-full h-full flex flex-col'> | ||
{messages.map(message => <Message key={message.id} message={message} />)} | ||
</Container> | ||
<Form className='fixed w-full h-[50px] bottom-[60px] dark:bg-[#1e1e1e] flex flex-row justify-center items-center gap-2' onSubmit={handleSendMessage}> | ||
<Input id='message-input' /> | ||
<Button className='bg-blue-800 rounded-full h-[40px] aspect-square' ><SendIcon className='w-8 h-8 ml-0.5 mt-1 text-white' /></Button> | ||
</Form> | ||
</> : | ||
<Container className='flex flex-col items-center w-full h-[50px]'> | ||
{users.map(user => | ||
<article key={user.id} className='flex flex-row w-full items-center justify-between p-3 border-y border-solid border-slate-700 dark:bg-black'> | ||
<Container className='flex flex-row items-center'> | ||
<Avatar className='w-2/12 h-2/12' url={user.avatar || defaultAvatar} /> | ||
<Paragraph className='text-xl font-bold'>{user.username}</Paragraph> | ||
</Container> | ||
<Button className='h-[40px]' onClick={() => handleOpenChat(user.id)} ><SendIcon className='w-8 h-8 ml-0.5 mt-1 text-white' /></Button> | ||
</article> | ||
)} | ||
</Container>} | ||
</Container > | ||
} |
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,22 @@ | ||
import Container from '../library/Container' | ||
import Paragraph from '../library/Paragraph' | ||
|
||
import extractPayloadFromToken from '../../util/extractPayloadFromToken' | ||
|
||
export default function Message({ message }) { | ||
const { sub: userId } = extractPayloadFromToken(sessionStorage.token) | ||
|
||
return (message.author.id === userId) ? | ||
<> | ||
<Paragraph className='text-xs relative top-[25px] self-end mr-5 opacity-50'>{message.author.username}</Paragraph> | ||
<Container className='max-w-[60%] min-w-[20%] h-auto p-1 rounded-full mt-3 mx-2 bg-blue-800 self-end' > | ||
<Paragraph>{message.content}</Paragraph> | ||
</Container> | ||
</> : | ||
<> | ||
<Paragraph className='text-xs relative top-[25px] self-start ml-5 opacity-50'>{message.author.username}</Paragraph> | ||
<Container className='max-w-[60%] min-w-[20%] h-auto p-1 rounded-full mt-3 mx-2 bg-green-700 self-start' > | ||
<Paragraph>{message.content}</Paragraph> | ||
</Container> | ||
</> | ||
} |
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
Oops, something went wrong.