Skip to content

Commit

Permalink
small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-ihnatovich-exa committed Jul 1, 2024
1 parent c1668a7 commit afd34e6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
7 changes: 6 additions & 1 deletion src/app/chat/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const Chat = () => {
const createNotification = () =>
setNotifications([...notifications, { id: notifications.length }]);

const deleteNotification = (id) =>
setNotifications(
notifications.filter((notification) => notification.id !== id),
);

useEffect(() => {
socket.on('messageResponse', (data) => setMessages([...messages, data]));
}, [socket, messages]);
Expand Down Expand Up @@ -65,7 +70,7 @@ const Chat = () => {
<MessagesPanelFooter socket={socket} currentUser={currentUser} />
</div>
{notifications.map(({ id }) => (
<Notification onClose={() => {}} key={id}>
<Notification onClose={() => deleteNotification(id)} key={id}>
This is a notification!
</Notification>
))}
Expand Down
1 change: 1 addition & 0 deletions src/app/chat/MessagePanelFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const MessagesPanelFooter = ({ socket, currentUser }) => {
type="text"
className="flex-1 p-2 border border-gray-300 rounded-lg bg-inherit"
placeholder="Type a message..."
value={message}
onChange={(e) => setMessage(e.target.value)}
/>
<button
Expand Down
6 changes: 2 additions & 4 deletions src/app/chat/MessagesPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const MessagesPanel = ({ messages, currentUser }) => {
return (
<div className="flex-1 flex flex-col">
<div className="p-4 border-b border-gray-200">
<h1 className="text-xl font-semibold">
Chat with {currentUser.displayName}
</h1>
<h1 className="text-xl font-semibold">Chat</h1>
</div>
<div className="flex-1 overflow-y-auto p-4">
<div className="flex flex-col space-y-4">
Expand All @@ -17,7 +15,7 @@ const MessagesPanel = ({ messages, currentUser }) => {
</div>
) : (
<div className="flex items-end justify-end" key={message.id}>
<div className="bg-gray-200 p-3 rounded-lg max-w-xs">
<div className="bg-gray-400 p-3 rounded-lg max-w-xs">
{message.text}
</div>
</div>
Expand Down
29 changes: 21 additions & 8 deletions src/app/components/Notification.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import { createPortal } from 'react-dom';
import { useState, useEffect } from 'react';

const color = {
info: 'blue',
success: 'green',
warning: 'yellow',
error: 'red',
};

let timeToDelete = 2000;

const Notification = ({ type = 'info', children, onClose }) => {
const color = {
info: 'blue',
success: 'green',
warning: 'yellow',
error: 'red',
};
useEffect(() => {
const timeoutId = setTimeout(onClose, timeToDelete);

return () => {
clearTimeout(timeoutId);
};
}, [onClose]);

return createPortal(
<div
className="z-50 overflow-hidden p-6"
className="z-50 overflow-hidden p-6 flex items-center justify-center"
style={{ background: color[type] }}
>
{children}
<button
className="bg-transparent border-0 text-white float-right cursor-pointer"
onClick={onClose}
>
<span className="text-white opacity-7 h-6 w-6 text-xl block ">x</span>
<span className="text-white opacity-7 h-6 w-6 text-xs block ml-3">
x
</span>
</button>
</div>,
document.getElementById('notifyContainer'),
Expand Down
9 changes: 0 additions & 9 deletions src/app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@
import Chat from './chat/Chat';
import Modal from './components/Modal';
import { AuthContextProvider } from './context/AuthContext';
import { useEffect, useState } from 'react';

const USERNAME = 'di_user';

export default function Home() {
const [isOpen, setIsOpen] = useState(true);

useEffect(() => {
localStorage.setItem('userName', USERNAME);
}, []);

return (
<main>
<AuthContextProvider>
Expand Down

0 comments on commit afd34e6

Please sign in to comment.