Skip to content

Commit

Permalink
Merge pull request #3 from fac20/add-user
Browse files Browse the repository at this point in the history
Add user
  • Loading branch information
amberrignell authored Sep 17, 2020
2 parents cb75d12 + df6f784 commit 4888b86
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 20 deletions.
22 changes: 9 additions & 13 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,43 @@ import AddUser from "./components/adduser";
import DisplayUser from "./components/displayuser";
import Play from "./components/play";

function App() {
const usernames = ["aissshah", "akomiqaia", "Albadylic", "Alexreid95", "Azizi-A", "CampbellDocherty", "Chloeh24", "fairyaksh", "ephieo", "glrta", "hannahgooding", "HettieM", "itsina96", "Ivo-Evans", "jackherizsmith", "jamesj-0", "jenndroid", "jessica440", "jhart5", "Jihyun-Jang", "Joepock123", "khadija-nur", "LiCern", "Lizzy-j", "mhtien", "Netceer", "oliverjam", "RihardsJ", "Roger-Heathcote", "amberrignell", "RunGT", "sofer", "tacotoemeck", "trishtracking", "yvonne-liu", "VatsKan"];


function App() {
const [difficulty, setDifficulty] = React.useState(6);
const [state, setState] = React.useState("home");
const [usernames, setUsernames] = React.useState(["aissshah", "akomiqaia", "Albadylic", "Alexreid95", "Azizi-A", "CampbellDocherty", "Chloeh24", "fairyaksh", "ephieo", "glrta", "hannahgooding", "HettieM", "itsina96", "Ivo-Evans", "jackherizsmith", "jamesj-0", "jenndroid", "jessica440", "jhart5", "Jihyun-Jang", "Joepock123", "khadija-nur", "LiCern", "Lizzy-j", "mhtien", "Netceer", "oliverjam", "RihardsJ", "Roger-Heathcote", "amberrignell", "RunGT", "sofer", "tacotoemeck", "trishtracking", "yvonne-liu", "VatsKan"]);

if (state === "home") {
return (
<div className="App">
<Header> </Header>
<Home setState={setState} usernames={usernames}></Home>
<Home setState={setState} usernames={usernames} difficulty={difficulty} setDifficulty={setDifficulty}></Home>
</div>
)
} else if (state === "play"){
console.log("state updated");
return (
<div className="App">
<Header></Header>
<Play setState={setState} usernames={usernames}></Play>
<Play setState={setState} usernames={usernames} difficulty={difficulty}></Play>
</div>
)
} else if (state === "adduser") {
return (
<div className="App">
<Header> </Header>
<AddUser setState={setState}></AddUser>
<AddUser setState={setState} usernames={usernames} setUsernames={setUsernames} ></AddUser>
</div>
)
} else if (state ==="displayuser"){
return (
<div className="App">
<Header> </Header>
<DisplayUser setState={setState}></DisplayUser>
<DisplayUser setState={setState} usernames={usernames}></DisplayUser>
</div>
)
}
// return (
// <div className="App">
// <Header> </Header>
// <Home setState={setState}></Home>

// </div>
//);
}

export default App;
24 changes: 24 additions & 0 deletions src/components/adduser.js
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;
17 changes: 16 additions & 1 deletion src/components/displayuser.js
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
9 changes: 6 additions & 3 deletions src/components/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import "../style/home.css";
import Play from "./play";
import Adduser from "./adduser";

export default function Home({setState}) {
const [difficulty, setDifficulty] = React.useState(6);
export default function Home({setState, usernames, difficulty, setDifficulty}) {


return (
<main>
Expand All @@ -16,9 +16,12 @@ export default function Home({setState}) {
<p>Difficulty: {difficulty}</p>

<button onClick={() => {
console.log("start clicked");
setState("play");
}}>Start</button>
<button onClick={() => {
setState("adduser");
}}>Add Your Own GitHub Picture!</button>

{/* // <button onClick={setState("play")}>Add a user</button> */}
</main>
)
Expand Down
94 changes: 91 additions & 3 deletions src/components/play.js
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>
</>
)


}
5 changes: 5 additions & 0 deletions src/style/adduser.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
form {
display: flex;
flex-direction: column;
}

23 changes: 23 additions & 0 deletions src/style/play.css
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
}
17 changes: 17 additions & 0 deletions src/utils/cardFlipper.js
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

1 change: 1 addition & 0 deletions src/utils/data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/utils/datahelpers.js
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;

0 comments on commit 4888b86

Please sign in to comment.