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

Unfinished project - CP #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<title>Redux intro</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body>
<div id="root">
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"classnames": "^2.2.6",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"he": "^1.2.0",
"prop-types": "^15.6.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
Expand Down
98 changes: 96 additions & 2 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,99 @@
export function fetchQuestion(){
return function(dispatch){

export function fetchQuestionAPI(){
// console.log()
return function(dispatch, getState){
// const {reduxState} = getState();
fetch(`https://opentdb.com/api.php?amount=1&type=multiple`)
.then(response => response.json())
.then(result => {
dispatch(receiveQuestions(result))
dispatch(correct_answer(result))
dispatch(fetchPhoto(result.results[0].category))
// dispatch(receiveSearch(result.results[0].category))
// console.log('hi')
})
.catch(function(error) {
// something went wrong. let's sort it out
});
}
};

export function correct_answer(result){
return {
type: 'CORRECT_ANSWER',
correct_answer: result.results[0].correct_answer
}
}

export function score(isCorrect){
console.log(isCorrect)
return {

type: 'SCORE_ADD',
answerIsCorrect: isCorrect

}

}

function fetchPhoto(category){
console.log('hi')
return function(dispatch, getState){
// const {reduxState} = getState();
fetch(`https://api.unsplash.com/search/photos?page=1&query=${category}&client_id=d1463f432cce4150640ff56ee13c1f94ec0b2993db4395bcb8913f34daeb0d48`)
.then(response => response.json())
.then(result => {
dispatch(savePhoto(result))
})
.catch(function(error) {
// something went wrong. let's sort it out
});
}
};


export function savePhoto(result){
// console.log(result.results[0].urls.regular)
return {
type: 'SAVE_PHOTO_URL',
image: result.results[0].urls.regular

}
}


export function receiveQuestions(result){
return {
type: 'RECEIVE_QUESTIONS',
questions: result.results[0]

//questions refers to a variable
//result = result of fetch
}
}


export function countDown()
{
counter = counter - 1;
window.status = counter;
if (counter == 0)
{
window.clearTimeout( timer );
timer = null;
}
else
{
timer = window.setTimeout( "countDown()", 1000);
}
}


var timer;
var counter = 10;

export function startCounting()
{
timer = window.setTimeout( "countDown()", 1000 );
window.status = counter; // show the initial value
}
12 changes: 10 additions & 2 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React from 'react';
import QuestionsContainer from '../containers/QuestionsContainer';
import ScoreContainer from '../containers/ScoreContainer';
import Nav from '../components/Nav';


class App extends React.Component {


render(){
return (
<div>
App contents go here
<div >
<Nav />
<QuestionsContainer />
<ScoreContainer/>
</div>
)
}
Expand Down
21 changes: 21 additions & 0 deletions src/components/Nav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react";

class Nav extends React.Component {

constructor() {
super();
}

render() {
return (
<div className="Nav">
<h1>THIS IS A QUIZ</h1>
</div>
)
}
}




export default Nav;
55 changes: 55 additions & 0 deletions src/components/Questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react";
import { decode } from 'he';

class Questions extends React.Component {

constructor() {
super();
}


componentDidMount(){
this.props.fetchQuestion()
}


render() {
console.log(this.props.questions)
let answerList = (this.props.questions.incorrect_answers === undefined)
?
null :


[...this.props.questions.incorrect_answers, this.props.questions.correct_answer]
.sort(function() { return 0.5 - Math.random() })
.map((answer) =>
<button className ="voting-button" key={answer} onClick={() => this.props.clickHandler(this.props.questions.correct_answer === answer)}>{answer}</button>
);

let questionDecoded = (this.props.questions.question === undefined) ? null : decode(this.props.questions.question)
// decoding HTML entities in questions using 'he' - ternary to cover initial undefined state.

return (
<div className="quiz__display"
style = {{backgroundImage: `url(${this.props.image})`}}>
<div>
<h2>{questionDecoded}</h2>
<ul>{answerList}</ul>
</div>
</div>
);
}
}

// {this.props.questions.length > 0 ?
// <div key={this.props.questions.question}>
// <h2 key={this.props.questions.question}>
// {this.props.questions.question}</h2>
// {this.props.questions.incorrect_answers}

// </div> :
// <h3>I haven't fetched yet</h3>}



export default Questions;
49 changes: 49 additions & 0 deletions src/components/Score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";

class Score extends React.Component {

constructor() {
super();
// this.mixQuestions = this.mixQuestions.bind(this);
}




render() {

// let emotion = === true ? "/static/img/basket_full.png": "/static/img/basket.png";

// if (this.props.score 0) {
// emotion = "/static/img/basket_full.png";
// } else if ( ) {
// greeting = "/static/img/basket.png";
// }

// return (
// <div className="nav">
// <img className="nav__logo" src="/static/img/logo.png" />
// <img className="nav__basket" onClick={() => this.props.openBasket()} src={basketFull}/>
// </div>




return (
<div className="score">
<div>
<h2>SCORE: {this.props.score}</h2>
{/* <img src=`{this.props.score}.jpg`/>
<img className="nav__logo" src="/img/0.jpg" />

<img src="${this.props.score}.jpg">
*/}
</div>
</div>
);
}
}



export default Score;
34 changes: 34 additions & 0 deletions src/containers/QuestionsContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { connect } from "react-redux";
import Questions from "../components/Questions";
// import { setQuery, submitQuery } from "../actions"
import { fetchQuestionAPI, score, image } from '../actions';



// fetchQuestion={this.props.fetchQuestion}

const mapStateToProps = state => {
return {
questions: state.questionsResults.questions,
image: state.image

//name of prop being passed down(object) : state:(importedreducercomponent).(key of object)
};
}



const mapDispatchToProps = dispatch => {
return {
fetchQuestion: () => dispatch(fetchQuestionAPI()),
clickHandler: (isCorrect) => {
dispatch(score(isCorrect)),
dispatch(fetchQuestionAPI())
}
};
}


export default connect(
mapStateToProps, mapDispatchToProps
)(Questions);
18 changes: 18 additions & 0 deletions src/containers/ScoreContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { connect } from "react-redux";
import Score from "../components/Score";
import { score } from '../actions';

const mapStateToProps = state => {
return {
score: state.score

//name of prop being passed down(object) : state:(importedreducercomponent).(key of object)
};
}




export default connect(
mapStateToProps
)(Score);
Binary file added src/img/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';

import thunkMiddleware from 'redux-thunk';
import { createStore, applyMiddleware } from 'redux';
import { createStore, applyMiddleware, compose } from "redux";
import { Provider } from 'react-redux';
import rootReducer from './reducers';

const store = createStore(rootReducer, applyMiddleware(
thunkMiddleware
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(rootReducer, {}, composeEnhancers(
applyMiddleware(thunkMiddleware)
));

ReactDOM.render(
Expand Down
21 changes: 21 additions & 0 deletions src/reducers/correct_answer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

function correct_answer(state = {
correct_answer: ``
}, action){
switch (action.type) {
case 'CORRECT_ANSWER':
// console.log(action.questions);



const newAnswer = Object.assign({}, state, { correct_answer: action.correct_answer});
return newAnswer

//return = action of returning the value of 'questions' in the action object.

default:
return state
}
}

export default correct_answer;
13 changes: 13 additions & 0 deletions src/reducers/gifs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function gifs(state = '', action){
switch (action.type) {

case 'GIF':
console.log(action)
return action.answerIsCorrect ? state + 1 : state -1

default:
return state
}
}

export default gifs;
Loading