Skip to content

Conversation

@fikretellek
Copy link

@fikretellek fikretellek commented Apr 28, 2024

Learners, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the requirements of this task

Changelist

Briefly explain your PR.

Questions

Ask any questions you have for your reviewer.

@fikretellek
Copy link
Author

react app ---> https://fe-chat-react-app.netlify.app/

Copy link

@musayuksel musayuksel left a comment

Choose a reason for hiding this comment

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

I loved your design and solution! Great job @fikretellek!
I've added some improvements that you might consider.

Comment on lines 8 to 12
useEffect(() => {
getMessages();
const interval = setInterval(() => getMessages(), 10000);
return () => clearInterval(interval);
}, []);

Choose a reason for hiding this comment

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

Great job! Efficiently using the useEffect and clearInterval

Copy link
Author

Choose a reason for hiding this comment

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

I am planning to use websocket to achieve real time communication. I'll try to implement it today. Can we have a meeting about websockets if you have time? I quite didn't understand it 😆

})
.then((response) => response.json())
.then((data) => setMessages(data.slice().reverse()));
}

Choose a reason for hiding this comment

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

Two suggestions for improvement:
First : consider using state to store the values of the name and message inputs.

Second : extract the fetch functionality into its own function to improve code organisation and maintain single responsibility.

Copy link
Author

Choose a reason for hiding this comment

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

I didn't understand the second suggestion. could you please make it more clear?

Choose a reason for hiding this comment

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

Well, we can create a single reusable fetch function to handle the fetching logic, like:

function fetchMessages(url, options) {
  return fetch(url, options)
    .then((response) => response.json())
    .then((data) => data.slice().reverse());
}

then you can use it for both getMessages and setMessages:

function getMessages() {
  fetchMessages("https://module-servers.onrender.com/messages")
    .then((data) => setMessages(data));
}



//...
const requestOptions = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(newMessage),
};

fetchMessages("https://module-servers.onrender.com/messages", requestOptions)
  .then((data) => setMessages(data));

There are different ways to do that, you can update it according to your taste ;)

</div>
<div id="messages">
{messages.map((message, index) => {
const currentUser = document.getElementById("name").value;

Choose a reason for hiding this comment

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

Same one for this part too!

Copy link
Author

Choose a reason for hiding this comment

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

done 👍

@sonarqubecloud
Copy link

sonarqubecloud bot commented May 9, 2024

Quality Gate Passed Quality Gate passed

Issues
6 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants