Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sphinx - Nikki #72

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
78 changes: 75 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,86 @@
import { useState } from 'react';
import './App.css';
import ChatLog from './components/ChatLog';
import messageData from './data/messages.json';

const App = () => {
const [messages, setMessages] = useState(messageData);
const [likedCount, setLikedCount] = useState(0);
const [remoteColor, setRemoteColor] = useState();
const [localColor, setLocalColor] = useState();

const toggleLikedMessage = (messageId) => {
const updatedMessages = messages.map((message) => {
return message.id === messageId
? { ...message, liked: !message.liked }
: message;
});

const likedList = updatedMessages.filter((message) => message.liked);

setLikedCount(likedList.length);
setMessages(updatedMessages);
};

const participantList = [];
messages.forEach((message) => {
if (!participantList.includes(message.sender)) {
participantList.push(message.sender);
}
});

const colorBtns = (senderLocation) => {
const colors = [
{ '🔴': 'red' },
{ '🟠': 'orange' },
{ '🟡': 'yellow' },
{ '🟢': 'green' },
{ '🔵': 'blue' },
{ '🟣': 'purple' }
];
return colors.map((color, index) => {
return Object.entries(color).map(([colorIcon, colorName]) => {
return (
<button
key={index}
onClick={() => {
senderLocation === 'local' ? setLocalColor(colorName) : setRemoteColor(colorName);
}}
className='widget'>{colorIcon}
</button>
);
});
});
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<span>
<h1>Chat between </h1>{' '}
<h1 className={localColor}>{participantList[0]}</h1>{' '}
<h1>and</h1>{' '}
<h1 className={remoteColor}>{participantList[1]}</h1>
</span>
<section>
<span className='widget'>
<h3 className={localColor}>{participantList[0]}&apos;s color:</h3>
{colorBtns('local')}
</span>
<p className='widget' id='heartWidget'>{likedCount} ❤️s</p>
<span className='widget'>
<h3 className={remoteColor}>{participantList[1]}&apos;s color:</h3>
{colorBtns('remote')}
</span>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={messages}
onMessageLiked={toggleLikedMessage}
localColor={localColor}
remoteColor={remoteColor}
/>
</main>
</div>
);
Expand Down
40 changes: 32 additions & 8 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const isRemote = props.sender === 'Estragon' ? true : false;
const senderLocation = isRemote
? 'chat-entry remote'
: 'chat-entry local';
const senderColor = isRemote
? props.remoteColor
: props.localColor;

const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of 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>
<div className={senderLocation}>
<section>
<h2 className="entry-name">{props.sender}</h2>
<section className="entry-bubble">
<p className={senderColor}>{props.body}</p>
<p className="entry-time"><TimeStamp time={props.timeStamp} /></p>
<button
className="like"
onClick={
() => props.onMessageLiked(props.id)
}>{props.liked ? '❤️' : '🤍'}
</button>
</section>
</section>
</div>
);
};

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,
onMessageLiked: PropTypes.func.isRequired,
localColor: PropTypes.string,
remoteColor: PropTypes.string,
};

export default ChatEntry;
40 changes: 40 additions & 0 deletions src/components/ChatLog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry';

function ChatLog(props) {
const chatMessages = props.entries.map((message) => {
return (
<div key={message.id} className="chat-log">
<ChatEntry
id={message.id}
sender={message.sender}
body={message.body}
timeStamp={message.timeStamp}
liked={message.liked}
onMessageLiked={props.onMessageLiked}
localColor={props.localColor}
remoteColor={props.remoteColor}
/>
</div>
);
});

return chatMessages;
}

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,
onMessageLiked: PropTypes.func.isRequired,
localColor: PropTypes.string,
remoteColor: PropTypes.string,
};

export default ChatLog;