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

Create Main.js #19

Closed
wants to merge 12 commits into from
Closed

Create Main.js #19

wants to merge 12 commits into from

Conversation

zkhing
Copy link

@zkhing zkhing commented Feb 4, 2023

Contributors, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with ISSUE NUMBER | ISSUE TITLE
  • I have linked my PR to the paired issue with #123
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the acceptance criteria

Changelist

Briefly explain your PR.

Context

Link to ticket with fixes #123

Questions

Ask any questions you have for your reviewer.

@netlify
Copy link

netlify bot commented Feb 4, 2023

Deploy Preview for rainyplaytime failed.

Name Link
🔨 Latest commit 544ee29
🔍 Latest deploy log https://app.netlify.com/sites/rainyplaytime/deploys/63ffc1aa5a805100081827ef

@SallyMcGrath
Copy link
Member

fixes #6

src/Main.js Outdated
Comment on lines 5 to 10
<div>
{/* fetch the data
images
current Weather
future Weather */}
</div>
Copy link

Choose a reason for hiding this comment

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

2 things:

  • can you use a more semantic HTML element here instead of a div?
  • And how will we pass in content as a prop, inside of <Main>? Remember we will be passing whole components into Main from inside of App.js

src/components/Header/Header.jsx Outdated Show resolved Hide resolved
src/components/Header/Header.jsx Outdated Show resolved Hide resolved
Comment on lines 12 to 21
useEffect(() => {
fetch(
`https://api.openweathermap.org/data/2.5/weather?q=liverpool&units=metric&appid=3b86046cce0de3be7cfa8369f4540b37`
)
.then((res) => res.json())
.then((data) => {
setData(data);
})
.catch((e) => console.log(e.message));
}, []);
Copy link

Choose a reason for hiding this comment

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

This is better but you are still saving the data in 2 different places (lines 9 and 18). You only need one.
I think what you need to do is:

  • onChange in the text input, save the user's input, just like you've done - don't change anything here it's perfect.
  • on search button click, trigger the API call to fetch the new data (for the new city we just saved). How will you do this? useEffect is good when you want to react to a change in value, but in this particular case, you want to react to a button click instead. I recommend not using useEffect here. How about just a simple getNewWeather function?
  • Replace the word 'liverpool' with ${city} so the API call changes.

If you do all this correctly it should update the temperature for each new city (it works for me 😊)

Copy link

Choose a reason for hiding this comment

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

(If you're wondering when to use useEffect, it would be perfect if you didn't have a 'search' button. For example if you just had the text input but no button, then you would want to make a new API call each time the user types a new letter. In this case, you would still save 'city' onChange but you would add city as a dependency in the useEffect array, and the API call inside the useEffect would get triggered each time the value of city changes.
This is very inefficient because you would make too many calls to the API, so in this case it's best to move the API call to be triggered by a button click instead.)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much Lucy! It works now. I didn't realise that fetch API works without useEffect. :) Now I learned when to use useEffect.

Copy link

@LucyMac LucyMac left a comment

Choose a reason for hiding this comment

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

@zkhing please can you:

  • go to the master branch and pull to get the latest changes
  • merge the master branch into your branch.
  • and push

You'll then be ready to merge this branch into master so every one in the team will have access to your code.

@zkhing
Copy link
Author

zkhing commented Feb 21, 2023

@LucyMac I have done pull request to master. Can you please merge my pull request. Thanks

Copy link

@LucyMac LucyMac left a comment

Choose a reason for hiding this comment

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

@zkhing I'm confused, what is the difference between this PR and your other one #28?

Please delete this one if they are the same.

@zkhing zkhing closed this Mar 1, 2023
@zkhing
Copy link
Author

zkhing commented Mar 1, 2023

@zkhing I'm confused, what is the difference between this PR and your other one #28?
Please delete this one if they are the same.

@LucyMac I am sorry that making you confused. My bad and I am not familiar with git and not used to it. Sometimes I don't even know what I have done in Github. I will delete this one.

@LucyMac
Copy link

LucyMac commented Mar 2, 2023

No problem @zkhing these things appear complicated at first :)
I see what happened now. PRs work with branches: this PR was pointing from your main branch to the CYF repo's master branch, whereas the other PR (the one we're keeping) is pointing from your main branch to the CYF repo's main branch.
Since your branch was the same in both cases it means we won't lose any of your code whichever PR we keep. But we will merge your code into a different destination: CYF master or CYF main.

I'm going to delete the CYF master branch so we only have CYF main from now on. Sorry if my explanation is more confusing :D

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.

3 participants