Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
571 changes: 571 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"private": true,
"version": "0.0.0",
"type": "module",
"homepage": "https://wendyww9.github.io/react-chatlog/",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"test": "vitest --run"
"test": "vitest --run",
"predeploy": "npm run build && cp build/index.html build/404.html",
"deploy": "gh-pages -d build"
},
"dependencies": {
"luxon": "^2.5.2",
Expand All @@ -30,6 +33,7 @@
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.12",
"eslint-plugin-vitest-globals": "^1.5.0",
"gh-pages": "^6.3.0",
"globals": "^15.11.0",
"jest-extended": "^4.0.2",
"jsdom": "^25.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}

#App span {
display: inline-block
display: inline-block;
}

.red {
Expand Down
41 changes: 38 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
import './App.css';
import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog.jsx';
import { useState } from 'react';

const App = () => {
const [chatData, setChatData] = useState(chatMessages);
const setChatLiked = (id) => {
setChatData(chatData => {
return chatData.map(chat => {
return (chat.id === id) ? { ...chat, liked: !chat.liked } : chat;
});
});
};

const likeCount = chatData.reduce((totalLikes, currentChat) => {
return totalLikes + (currentChat.liked ? 1 : 0);
}, 0);

const extractUniqueSenders = (chatLog) => {
const uniqueSenders = [];

for (const message of chatLog) {
if (!uniqueSenders.includes(message.sender)) {
uniqueSenders.push(message.sender);
}
}

return uniqueSenders;
};
const uniqueSenders = extractUniqueSenders(chatData);

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>{`Chat between ${uniqueSenders[0]} and ${uniqueSenders[1]}`}</h1>
<section>
<p className='widget' id="heartWidget">{likeCount} ❤️s</p>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={chatData}
onLike={setChatLiked}
uniqueSenders={uniqueSenders}
/>
</main>
</div>
);
Expand Down
8 changes: 8 additions & 0 deletions src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ button {
text-align: left;
}

.chat-entry.local .entry-body {
color: green;
}

.chat-entry.local .entry-time {
text-align: right;
}
Expand All @@ -76,6 +80,10 @@ button {
text-align: right;
}

.chat-entry.remote .entry-body{
color: blue;
}

.chat-entry.remote .entry-bubble {
background-color: #e0ffff;
margin-left: auto;
Expand Down
30 changes: 22 additions & 8 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import PropTypes from 'prop-types';
import './ChatEntry.css';
import TimeStamp from './TimeStamp';



const ChatEntry = (props) => {
const nameColor = props.liked ? '❤️' : '🤍';

const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<li className={`chat-entry ${props.position}`}>
<h2 className="entry-name">{props.sender}</h2>
<section className="entry-bubble">
<p>Replace with body of ChatEntry</p>
<p className="entry-time">Replace with TimeStamp component</p>
<button className="like">🤍</button>
<p className='entry-body'>{props.body}</p>
<p className="entry-time">
<TimeStamp time={props.timeStamp}></TimeStamp>
</p>
<button className="like" onClick={() => props.onLike(props.id)}>{nameColor}</button>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this pattern of sending just the id to props.onLike since it keeps all the state management and message object creation confined to App.

</section>
</div>
</li>
);
};

ChatEntry.propTypes = {
// Fill with correct proptypes
id:PropTypes.number.isRequired,
sender:PropTypes.string.isRequired,
body:PropTypes.string.isRequired,
timeStamp:PropTypes.string.isRequired,
liked:PropTypes.bool.isRequired,
onLike:PropTypes.func.isRequired,
position:PropTypes.string.isRequired,
};

export default ChatEntry;
2 changes: 2 additions & 0 deletions src/components/ChatEntry.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('Wave 01: ChatEntry', () => {
body="Get out by 8am. I'll count the silverware"
timeStamp="2018-05-18T22:12:03Z"
liked={false}
onLike={() => {}}
position="local"
/>
);
});
Expand Down
1 change: 1 addition & 0 deletions src/components/ChatLog.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.chat-log {
margin: auto;
max-width: 50rem;
list-style-type: none;
}
47 changes: 47 additions & 0 deletions src/components/ChatLog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import './ChatLog.css';
import ChatEntry from './ChatEntry';
import PropTypes from 'prop-types';

const ChatLog = ({entries, onLike, uniqueSenders}) => {
const getPosition = (sender) => {
if (!uniqueSenders || uniqueSenders.length < 2) return 'local';
return sender === uniqueSenders[1] ? 'remote' : 'local';
};

const chatEntryComponents = entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onLike={onLike}
position={getPosition(entry.sender)}>
</ChatEntry>
);
});

return (
<ul className='chat-log'>
{chatEntryComponents}
</ul>
);
};

ChatLog.propTypes = {
entries:PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
sender:PropTypes.string.isRequired,
body:PropTypes.string.isRequired,
timeStamp:PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
})
).isRequired,
onLike:PropTypes.func.isRequired,
uniqueSenders:PropTypes.array.isRequired,
};

export default ChatLog;
4 changes: 2 additions & 2 deletions src/components/ChatLog.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const LOG = [

describe('Wave 02: ChatLog', () => {
beforeEach(() => {
render(<ChatLog entries={LOG} />);
render(<ChatLog entries={LOG} onLike={() => {}} uniqueSenders={[]}/>);
});

test('renders without crashing and shows all the names', () => {
Expand All @@ -66,7 +66,7 @@ describe('Wave 02: ChatLog', () => {
});

test('renders an empty list without crashing', () => {
const element = render(<ChatLog entries={[]} />);
const element = render(<ChatLog entries={[]} onLike={() => {}} uniqueSenders={[]}/>);
expect(element).not.toBeNull();
});
});
6 changes: 5 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export default defineConfig({
globals: true,
setupFiles: ['./vitest.setup.js'],
},
})
base: '/react-chatlog/',
build: {
outDir: 'build', // Or 'dist' if you've changed the default
},
});