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
49 changes: 49 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Deploy

on:
push:
branches:
- main

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4

- name: Install dependencies
uses: bahmutov/npm-install@v1

- name: Build project
run: npm run build

- name: Upload production-ready build files
uses: actions/upload-artifact@v4
with:
name: production-files
path: ./dist

deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'

steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: production-files
path: ./dist

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
22 changes: 20 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import React, { useState } from "react";
import './App.css';
import ChatLog from './components/ChatLog';
import chat from './data/messages.json';

const App = () => {
const [chatMessages, setChatMessages] = useState(chat);

Choose a reason for hiding this comment

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

👍


const toggleLike = (id) => {
const updatedMessages = chatMessages.map((entry) => {

Choose a reason for hiding this comment

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

Nice use of map here to both handle making a new list so that React sees the message data has changed, and make new data for the clicked message with its like status toggled.

if (entry.id === id) {
return { ...entry, liked: !entry.liked };

Choose a reason for hiding this comment

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

We showed this approach in class, but technically, we're mixing a few responsibilities here. rather than this function needing to know how to change the liked status itself, we could move this update logic to a helper function. This would better mirror how we eventually update records when there's an API call involved.

In this project, our messages are very simple objects, but if we had more involved operations, it could be worthwhile to create an actual class with methods to work with them, or at least have a set of dedicated helper functions to centralize any such mutation logic.

} else {
return entry;
}
});
setChatMessages(updatedMessages);
};

const totalLikes = chatMessages.filter((entry) => entry.liked).length;

Choose a reason for hiding this comment

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

Great idea to filter down the data to the liked messages and use the list length as the count!

This approach works, but you'll typically see JS devs use reduce to calculate a value like this:

  const calculateTotalLikeCount = (chatData) => {
    return chatData.reduce((acc, chat) => {
      return chat.liked ? acc + 1 : acc;
    }, 0);
  };


return (
<div id="App">
<header>
<h1>Application title</h1>

Choose a reason for hiding this comment

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

An element missing from your project is the header "Chat Between X and Y". Here you have "Application Title".

@dniph How would you create a header that reflects the screenshot below? It can be done by hardcoding the string in the h1 tag, but I'm wondering how would you programmatically get the names of the participants in the chat?

Please respond to this comment with your answer, thank you!

image

<p>{totalLikes} ❤️s</p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatMessages} onLikeToggle={toggleLike} />
</main>
</div>
);
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 './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({ id, sender, body, timeStamp, liked, onLikeToggle }) => {
// Función que se ejecuta al hacer clic en el botón del corazón
const handleLikeClick = () => {
onLikeToggle(id);
};
Comment on lines +7 to +9

Choose a reason for hiding this comment

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

👍


const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${sender === 'Vladimir' ? 'local' : 'remote'}`}>
<h2 className="entry-name">{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>{body}</p>
<TimeStamp time={timeStamp} />

Choose a reason for hiding this comment

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

Nice job using the provided TimeStamp component

<button className="like" onClick={handleLikeClick}>
{liked ? '❤️' : '🤍'}
</button>
</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,
onLikeToggle: PropTypes.func.isRequired,
};

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

const ChatLog = ({ entries, onLikeToggle }) => {
const ChatEntries = entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onLikeToggle={onLikeToggle}
/>
);
});

return <div className='chat-log'>{ChatEntries}</div>;
};

ChatLog.propTypes = {
entries: PropTypes.array.isRequired,
onLikeToggle: PropTypes.func.isRequired
};

export default ChatLog;
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
base: '/react-chatlog/',
test: {
// jest config here
reporters: ['verbose'],
Expand Down