Skip to content

Commit

Permalink
feat(): [Room] added room
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurArakelyan committed Oct 19, 2023
1 parent 534058c commit ecf5707
Show file tree
Hide file tree
Showing 39 changed files with 637 additions and 19 deletions.
8 changes: 5 additions & 3 deletions chat-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ const io = new Server(server, {
});

app.get('/', (req, res) => {
res.send('<h1>Chat Server</h1>');
res.send('<h1>Chat Server Example</h1>');
});

io.on('connection', (socket) => {
socket.on('chat', (message) => {
socket.broadcast.emit('chat', message);
socket.on('create chat', (id) => {
socket.on(`chat:${id}`, (message) => {
socket.broadcast.emit(`chat:${id}`, message);
});
});

socket.on('disconnect', () => {
Expand Down
2 changes: 1 addition & 1 deletion chat-server/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "chat-server",
"name": "websockets-chat-example-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down
15 changes: 15 additions & 0 deletions chat-server/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 2,
"builds": [
{
"src": "index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "index.js"
}
]
}
4 changes: 2 additions & 2 deletions chat/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chat",
"version": "0.1.0",
"name": "websockets-chat-example",
"version": "1.0.0",
"private": true,
"dependencies": {
"@hookform/resolvers": "^3.3.2",
Expand Down
3 changes: 2 additions & 1 deletion chat/src/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Routes, Route } from 'react-router-dom';

import { Home } from 'pages';
import { Home, Room } from 'pages';

const App = () => {
return (
<div className="App">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/room/:id" element={<Room />} />
</Routes>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions chat/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// pages
export { default as MyMessage } from './pages/Room/MyMessage';
export { default as UserMessage } from './pages/Room/UserMessage';
export { default as RoomForm } from './pages/Room/RoomForm';
61 changes: 61 additions & 0 deletions chat/src/components/pages/Room/MyMessage/MyMessage.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.my-message-wrapper {
display: flex;
justify-content: flex-end;
}

.my-message {
position: relative;
margin-right: 20px;
margin-bottom: 10px;
padding: 10px 10px 20px 10px;
background-color: #f8e896;
min-width: 20%;
max-width: 60%;
width: fit-content;
text-align: left;
font: 400 .9em 'Open Sans', sans-serif;
border: 1px solid #dfd087;
border-radius: 10px;
}
.my-message::after {
content: '';
position: absolute;
width: 0;
height: 0;
border-top: 15px solid #f8e896;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
top: 0;
right: -15px;
}
.my-message::before {
content: '';
position: absolute;
width: 0;
height: 0;
border-top: 17px solid #dfd087;
border-left: 16px solid transparent;
border-right: 16px solid transparent;
top: -1px;
right: -17px;
}

.my-message__text {
padding: 0;
margin: 0;
}

.my-message__date {
position: absolute;
font-size: 0.85em;
font-weight: 300;
margin-top: 10px;
bottom: 3px;
right: 5px;
}

@media (max-width: 800px) {
.my-message {
min-width: 60%;
}
}
25 changes: 25 additions & 0 deletions chat/src/components/pages/Room/MyMessage/MyMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FC } from 'react';

import { formatMessageDate } from 'helpers';

import { IMyMessageProps } from './types';

import styles from './MyMessage.module.scss';

const MyMessage: FC<IMyMessageProps> = ({ message }) => {
return (
<div className={styles['my-message-wrapper']}>
<div className={styles['my-message']}>
<p className={styles['my-message__text']}>
{message.message}
</p>

<span className={styles['my-message__date']}>
{formatMessageDate(message.date)}
</span>
</div>
</div>
);
};

export default MyMessage;
3 changes: 3 additions & 0 deletions chat/src/components/pages/Room/MyMessage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MyMessage from './MyMessage';

export default MyMessage;
5 changes: 5 additions & 0 deletions chat/src/components/pages/Room/MyMessage/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IMessage, IMessageWithFrom } from 'types';

export interface IMyMessageProps {
message: IMessage | IMessageWithFrom;
}
11 changes: 11 additions & 0 deletions chat/src/components/pages/Room/RoomForm/RoomForm.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.room-form {
display: flex;
justify-content: center;
width: 100%;
padding: 12px 20px 16px 20px;
}

.room-form__input {
flex: 1;
margin-right: 20px !important;
}
46 changes: 46 additions & 0 deletions chat/src/components/pages/Room/RoomForm/RoomForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FC } from 'react';
import { Button, TextField } from '@material-ui/core';

import { UseRoomFormContainerType } from './useRoomForm';

import styles from './RoomForm.module.scss';
import { Controller } from 'react-hook-form';

const RoomForm: FC<UseRoomFormContainerType> = ({ control, handleSubmit, handleFormSubmit }) => {

return (
<form
autoComplete="off"
className={styles['room-form']}
onSubmit={handleSubmit(handleFormSubmit)}
>
<Controller
control={control}
name="message"
render={({ field: { onChange, value } }) => {
return (
<TextField
onChange={onChange}
value={value}
label="Message"
className={styles['room-form__input']}
/>
);
}}
/>

<Button
type="submit"
variant="contained"
color="primary"
aria-label="Send"
>
<svg className="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true" fill="#ffffff">
<path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" />
</svg>
</Button>
</form>
);
};

export default RoomForm;
13 changes: 13 additions & 0 deletions chat/src/components/pages/Room/RoomForm/RoomFormContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import RoomForm from './RoomForm';

import useRoomForm from './useRoomForm';

const RoomFormContainer = () => {
const props = useRoomForm();

return (
<RoomForm {...props} />
);
};

export default RoomFormContainer;
3 changes: 3 additions & 0 deletions chat/src/components/pages/Room/RoomForm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import RoomForm from './RoomFormContainer';

export default RoomForm;
40 changes: 40 additions & 0 deletions chat/src/components/pages/Room/RoomForm/useRoomForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';

import { useAppDispatch } from 'store';
import { sendMessage } from 'store/room/room.actions';

import { roomChatSchema } from 'utilities';

import { IMessageData } from 'types';

const useRoomForm = () => {
const dispatch = useAppDispatch();

const {
handleSubmit,
control,
reset,
} = useForm<IMessageData>({
defaultValues: {
message: '',
},
resolver: yupResolver(roomChatSchema),
});

const handleFormSubmit = (data: IMessageData) => {
reset();

dispatch(sendMessage(data));
};

return {
handleSubmit,
handleFormSubmit,
control,
};
};

export type UseRoomFormContainerType = ReturnType<typeof useRoomForm>;

export default useRoomForm;
75 changes: 75 additions & 0 deletions chat/src/components/pages/Room/UserMessage/UserMessage.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.user-message {
display: flex;
}

.user-message__avatar {
width: 32px !important;
height: 32px !important;
background-color: #ff5722 !important;
color: #ffffff !important;
}

.user-message__content-wrapper {
width: 100%;
}

.user-message__content {
position: relative;
margin-left: 20px;
margin-bottom: 10px;
padding: 10px 10px 20px 10px;
background-color: #A8DDFD;
min-width: 20%;
max-width: 60%;
width: fit-content;
text-align: left;
font: 400 .9em 'Open Sans', sans-serif;
border: 1px solid #97C6E3;
border-radius: 10px;
}
.user-message__content::after {
content: '';
position: absolute;
width: 0;
height: 0;
border-top: 15px solid #A8DDFD;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
top: 0;
left: -15px;
}
.user-message__content::before {
content: '';
position: absolute;
width: 0;
height: 0;
border-top: 17px solid #97C6E3;
border-left: 16px solid transparent;
border-right: 16px solid transparent;
top: -1px;
left: -17px;
}

.user-message__display-name {
margin-left: 20px;
}

.user-message__text {
padding: 0;
margin: 0;
}

.user-message__date {
position: absolute;
font-size: 0.85em;
font-weight: 300;
margin-top: 10px;
bottom: 3px;
right: 5px;
}

@media (max-width: 800px) {
.user-message__content {
min-width: 60%;
}
}
Loading

0 comments on commit ecf5707

Please sign in to comment.