-
Notifications
You must be signed in to change notification settings - Fork 4
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
react2-week1/eva #320
Open
yagmureva
wants to merge
1
commit into
main
Choose a base branch
from
react2-week1/eva
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
react2-week1/eva #320
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* App.css */ | ||
body { | ||
font-family: "Arial", sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f4f4f4; | ||
} | ||
|
||
.container { | ||
max-width: 800px; | ||
margin: 50px auto; | ||
padding: 20px; | ||
background-color: #fff; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
border-radius: 5px; | ||
} | ||
|
||
input { | ||
padding: 10px; | ||
font-size: 16px; | ||
margin-right: 10px; | ||
} | ||
|
||
button { | ||
padding: 10px 20px; | ||
font-size: 16px; | ||
background-color: #4caf50; | ||
color: #fff; | ||
border: none; | ||
cursor: pointer; | ||
border-radius: 5px; | ||
} | ||
|
||
button:hover { | ||
background-color: #45a049; | ||
} | ||
|
||
ul { | ||
list-style: none; | ||
padding: 0; | ||
} | ||
|
||
li { | ||
margin: 10px 0; | ||
padding: 10px; | ||
background-color: #f0f0f0; | ||
border-radius: 5px; | ||
} | ||
|
||
p { | ||
font-size: 16px; | ||
margin: 10px 0; | ||
} | ||
|
||
.loading { | ||
font-size: 18px; | ||
color: #3498db; | ||
} | ||
|
||
.error { | ||
font-size: 18px; | ||
color: #e74c3c; | ||
} | ||
|
||
.no-results { | ||
font-size: 18px; | ||
color: #2ecc71; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// App.js | ||
import React, { useState } from "react"; | ||
import { useGithubContext } from "./Context/GithubContext"; | ||
import "./App.css"; | ||
|
||
const App = () => { | ||
const { users, loading, error, searchUsers } = useGithubContext(); | ||
const [query, setQuery] = useState(""); | ||
|
||
const handleSearch = () => { | ||
searchUsers(query); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<header> | ||
<h1>Github User Searcher</h1> | ||
</header> | ||
|
||
<div> | ||
<input | ||
type="text" | ||
value={query} | ||
onChange={(e) => setQuery(e.target.value)} | ||
/> | ||
<button onClick={handleSearch}>Search</button> | ||
|
||
{loading && <p>Loading...</p>} | ||
{error && <p>Error: {error}</p>} | ||
{users.length === 0 && !loading && !error && <p>No results...</p>} | ||
|
||
<ul> | ||
{users.map((user) => ( | ||
<li key={user.id}>{user.login}</li> | ||
))} | ||
</ul> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// GithubContext.js | ||
import React, { createContext, useReducer, useContext } from "react"; | ||
|
||
const GithubContext = createContext(); | ||
|
||
const initialState = { | ||
users: [], | ||
loading: false, | ||
error: null, | ||
}; | ||
|
||
const githubReducer = (state, action) => { | ||
switch (action.type) { | ||
case "SEARCH_LOADING": | ||
return { ...state, loading: true, error: null }; | ||
case "SEARCH_SUCCESS": | ||
return { ...state, users: action.payload, loading: false, error: null }; | ||
case "SEARCH_ERROR": | ||
return { ...state, loading: false, error: action.payload }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const GithubProvider = ({ children }) => { | ||
const [state, dispatch] = useReducer(githubReducer, initialState); | ||
|
||
const searchUsers = async (query) => { | ||
try { | ||
dispatch({ type: "SEARCH_LOADING" }); | ||
const response = await fetch( | ||
`https://api.github.com/search/users?q=${query}` | ||
); | ||
const data = await response.json(); | ||
|
||
if (data.items) { | ||
dispatch({ type: "SEARCH_SUCCESS", payload: data.items }); | ||
} else { | ||
dispatch({ type: "SEARCH_ERROR", payload: "No results..." }); | ||
} | ||
} catch (error) { | ||
dispatch({ | ||
type: "SEARCH_ERROR", | ||
payload: `Error fetching: ${error.message}`, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<GithubContext.Provider value={{ ...state, searchUsers }}> | ||
{children} | ||
</GithubContext.Provider> | ||
); | ||
}; | ||
|
||
const useGithubContext = () => { | ||
const context = useContext(GithubContext); | ||
if (!context) { | ||
throw new Error("useGithubContext must be used within a GithubProvider"); | ||
} | ||
return context; | ||
}; | ||
|
||
export { GithubProvider, useGithubContext }; |
35 changes: 35 additions & 0 deletions
35
7_react/react2/week1/my-app/src/components/useGithubContext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// App.js | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember the js in the filename ( |
||
import React, { useState } from "react"; | ||
import { useGithubContext } from "../Context/GithubContext"; | ||
|
||
const App = () => { | ||
const { users, loading, error, searchUsers } = useGithubContext(); | ||
const [query, setQuery] = useState(""); | ||
|
||
const handleSearch = () => { | ||
searchUsers(query); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={query} | ||
onChange={(e) => setQuery(e.target.value)} | ||
/> | ||
<button onClick={handleSearch}>Search</button> | ||
|
||
{loading && <p>Loading...</p>} | ||
{error && <p>Error: {error}</p>} | ||
{users.length === 0 && !loading && !error && <p>No results...</p>} | ||
|
||
<ul> | ||
{users.map((user) => ( | ||
<li key={user.id}>{user.login}</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
body { | ||
margin: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
code { | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', | ||
monospace; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// index.js | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import App from "./App"; | ||
import { GithubProvider } from "./Context/GithubContext"; | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<GithubProvider> | ||
<App /> | ||
</GithubProvider> | ||
</React.StrictMode>, | ||
document.getElementById("root") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const reportWebVitals = onPerfEntry => { | ||
if (onPerfEntry && onPerfEntry instanceof Function) { | ||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { | ||
getCLS(onPerfEntry); | ||
getFID(onPerfEntry); | ||
getFCP(onPerfEntry); | ||
getLCP(onPerfEntry); | ||
getTTFB(onPerfEntry); | ||
}); | ||
} | ||
}; | ||
|
||
export default reportWebVitals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// jest-dom adds custom jest matchers for asserting on DOM nodes. | ||
// allows you to do things like: | ||
// expect(element).toHaveTextContent(/react/i) | ||
// learn more: https://github.com/testing-library/jest-dom | ||
import '@testing-library/jest-dom'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good error handling 👍