-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from fac20/add-user
Add user
- Loading branch information
Showing
10 changed files
with
223 additions
and
20 deletions.
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
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,24 @@ | ||
import React from "react"; | ||
import "../style/adduser.css"; | ||
|
||
function AddUser({setState, usernames, setUsernames}) { | ||
console.log(usernames) | ||
return ( | ||
<form> | ||
<label htmlFor="username"> | ||
Enter your GitHub username: | ||
</label> | ||
<input type="text" id="username"></input> | ||
<button type="submit" onClick={(event) => { | ||
event.preventDefault(); | ||
let newValue = document.getElementById("username").value | ||
let newArray = usernames.push(newValue) | ||
setUsernames(newArray) | ||
console.log(usernames) | ||
setState("displayuser"); | ||
}}>Generate a card with my username </button> | ||
</form> | ||
) | ||
} | ||
|
||
export default AddUser; |
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 |
---|---|---|
@@ -1 +1,16 @@ | ||
|
||
import React from "react" | ||
import "../style/displayuser.css"; | ||
|
||
function DisplayUser({usernames, setState}) { | ||
console.log(usernames[0]) | ||
return ( | ||
<> | ||
<h1>{usernames}</h1> | ||
<button onClick={() => { | ||
setState("home"); | ||
}}>Back to Home</button> | ||
</> | ||
) | ||
} | ||
|
||
export default DisplayUser |
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
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 |
---|---|---|
@@ -1,10 +1,98 @@ | ||
import React from "react"; | ||
import "../style/play.css"; | ||
import "../style/play.css"; | ||
import fetchData from "../utils/datahelpers"; | ||
import timer from "../utils/cardFlipper"; | ||
|
||
|
||
export default function Play({ usernames, difficulty }) { | ||
const [data, setData] = React.useState("Hi"); | ||
const [counter, setCounter] = React.useState("1"); | ||
let [flippedState, setFlippedState] = React.useState(0); | ||
let previousCard = "" | ||
const [result, setResult] = React.useState(null); | ||
|
||
|
||
React.useEffect(() => { | ||
fetchData(difficulty, usernames) | ||
.then(res => { | ||
console.log(res); | ||
const allData = res.map(user => | ||
( | ||
<button onClick={(event) => { | ||
if (event.target.className === "user-card hidden"){ | ||
event.target.classList.remove("hidden"); | ||
event.target.classList.add("flipped"); | ||
} else{ | ||
event.target.parentNode.classList.remove("hidden"); | ||
event.target.parentNode.classList.add("flipped"); | ||
} | ||
console.log(event.target) | ||
setFlippedState(flippedState => flippedState + 1); | ||
console.log("flippedState", flippedState); | ||
|
||
|
||
|
||
|
||
if (flippedState === 1) { | ||
const flippedCards = document.querySelectorAll(".flipped"); | ||
console.log(flippedCards[0].childNodes[0].src) | ||
if (flippedCards[0].childNodes[0].src === flippedCards[1].childNodes[0].src ) { | ||
console.log("you win"); | ||
setResult("You Win"); | ||
} else { | ||
console.log("you lose"); | ||
setResult("You Lose"); | ||
} | ||
} | ||
|
||
// if (flippedState) { | ||
// console.log(event.target) | ||
// //comparison with previousCard | ||
// } else { | ||
// // save value to previousCard | ||
// } | ||
// //flippedState = !flippedState | ||
}} className="user-card" > | ||
<img src={user.avatar_url} alt="user avatar"></img> | ||
<h3>{user.login}</h3> | ||
</button> | ||
) | ||
) | ||
return allData; | ||
}) | ||
.then(JSX => { | ||
setData(JSX); | ||
timer(setCounter, counter); | ||
}) | ||
|
||
|
||
}, [flippedState, result]) | ||
|
||
if (counter === 0) { | ||
document.querySelectorAll(".user-card").forEach(x => { | ||
x.classList.add("hidden"); | ||
setCounter(null) | ||
}) | ||
} | ||
|
||
return(<h1>{usernames.sort(() => Math.random() - 0.5)}</h1>) | ||
|
||
// document.querySelectorAll(".user-card").forEach(card => { | ||
// card.addEventListener("click", (event) => { | ||
// event.target.classList.remove("hidden"); | ||
// // event.target.removeEventListener() | ||
|
||
} | ||
// } | ||
|
||
|
||
|
||
|
||
return ( | ||
<> | ||
{counter ? <h1>{counter}</h1> : null} | ||
{result ? <h1>{result}</h1> : null} | ||
<div className="play-grid">{data}</div> | ||
</> | ||
) | ||
|
||
|
||
} |
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 @@ | ||
form { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
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,23 @@ | ||
.play-grid{ | ||
display: grid; | ||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); | ||
background-color: #f7ede2; | ||
} | ||
|
||
.user-card{ | ||
border: 3px solid #f28482; | ||
border-radius: 3rem; | ||
margin: 2rem; | ||
padding: 0.5rem; | ||
background-color: #f5cac3; | ||
} | ||
|
||
img { | ||
border-radius: 1.2rem; | ||
max-width: 100%; | ||
} | ||
|
||
.hidden > img, .hidden > h3 { | ||
opacity: 0; | ||
fill: #f28482 | ||
} |
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,17 @@ | ||
import React from "react"; | ||
|
||
const timer = (setCounter, counter) => { | ||
let interval = setInterval(() => { | ||
if (counter === 0) { | ||
console.log("finished") | ||
clearInterval(interval) | ||
|
||
} else { | ||
counter = counter -1; | ||
setCounter(counter); | ||
} | ||
}, 1000) | ||
} | ||
|
||
export default timer | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
const checkResponse = (response) => { | ||
if (response.status !== 200) { | ||
console.log(`Error with the request! ${response.status}`); | ||
return; | ||
} | ||
//console.log(response); | ||
return response.json(); | ||
}; | ||
|
||
const fetchData = (difficulty, usernames) => { | ||
let sortedArray = getRandomUsers(difficulty, usernames); | ||
let promiseArray = sortedArray.map(username => { | ||
return fetch(`https://api.github.com/users/${username}`) | ||
.then(checkResponse) | ||
.catch( err => { | ||
throw new Error (`fetch user failed ${err}`); | ||
}) | ||
}) | ||
|
||
return Promise.all(promiseArray); | ||
|
||
} | ||
|
||
const getRandomUsers = (difficulty, usernames) => { | ||
let sortedArray = usernames.sort(() => Math.random() - 0.5); | ||
let randomised = sortedArray.slice(0, difficulty/2) | ||
let randomisedDouble = randomised.concat(randomised); | ||
return randomisedDouble.sort(() => Math.random() - 0.5); | ||
} | ||
|
||
export default fetchData; |