Skip to content

Commit

Permalink
class history added
Browse files Browse the repository at this point in the history
  • Loading branch information
jw20191n committed Dec 4, 2019
1 parent 847f2ce commit 7c60411
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class App extends React.Component {
<Route exact path="/" render={() => <Home /> }/>
<Route exact path="/login" render={(routerProps) => <Login setCurrentUser={this.setCurrentUser} signOut={this.signOut} {...routerProps}/> } />
<Route exact path='/signup' render={(routerProps) => <SignUp {...routerProps} setCurrentUser={this.setCurrentUser}/> } />
<Route exact path='/student' render={(routerProps) => <StudentPage currentUser={this.state.currentUser} {...routerProps} setCurrentUser={this.setCurrentUser} /> } />
<Route exact path='/stdprofile' render={(routerProps) => <StudentProfile currentUser={this.state.currentUser} {...routerProps} /> } />
<Route exact path='/student' render={(routerProps) => <StudentPage currentUser={this.state.currentUser} {...routerProps} /> } />
<Route exact path='/stdprofile' render={(routerProps) => <StudentProfile currentUser={this.state.currentUser} setCurrentUser={this.setCurrentUser} {...routerProps} /> } />
<Route exact path='/stdhistory' render={(routerProps) => <StudentHistory currentUser={this.state.currentUser} {...routerProps} /> } />
<Route exact path='/admin' render={() => <AdminPage currentUser={this.state.currentUser}/> } />
<Route exact path='/createlesson' render={(routerProps) => <CreateLesson currentUser={this.state.currentUser} {...routerProps}/> } />
Expand Down
54 changes: 31 additions & 23 deletions src/components/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,38 @@ export default class Chat extends Component {


sendGuessright = (data) => {
console.log('in here');
if(data.user === this.props.currentUser){
let foundWord = {};
let word = data.word
// console.log(data.user.id === this.props.currentUser.id);
if(data.user.id === this.props.currentUser.id){
this.createGuessrights(data.user, word, data.drawer);
}
}


createGuessrights = (user, word, drawer) => {
let foundWord = {};

fetch('http://localhost:3001/words')
.then(resp => resp.json())
.then(resp => {
foundWord = resp.find(obj => obj.text === data.word )
console.log(foundWord.text);
fetch('http://localhost:3001/guessrights',{
method: "POST",
headers:{
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
student_id: data.user.id,
word_id: foundWord.id,
lesson_id: 1
})
}).then(resp => resp.json())
.then(data => console.log(data))
})
}
fetch('http://localhost:3001/words')
.then(resp => resp.json())
.then(resp => {
// console.log(resp);
foundWord = resp.find(obj => obj.text === word && obj.lesson_id === this.props.currentUser.lesson_id)
// console.log(foundWord);
fetch('http://localhost:3001/guessrights',{
method: "POST",
headers:{
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
student_id: user.id,
word_id: foundWord.id,
drawer_id: drawer.id,
lesson_id: this.props.currentUser.lesson_id
})
}).then(resp => resp.json())
.then(data => console.log(data))
})
}

render() {
Expand Down
6 changes: 3 additions & 3 deletions src/components/LessonDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default class LessonDetail extends Component {

componentDidUpdate(){
this.printWords();
console.log(this.state.words);
}


Expand Down Expand Up @@ -63,7 +62,8 @@ export default class LessonDetail extends Component {
.then(data => {
console.log(data);
this.setState({
words: [...this.state.words, data]
words: [...this.state.words, data],
word: ""
})
})
}
Expand All @@ -78,7 +78,7 @@ export default class LessonDetail extends Component {
<form className="auth-form" onSubmit={this.addWord}>
<div className="form-group">
<label>add word to class</label>
<input type="text" name="word" className="form-control" placeholder="please type in word you want to add" onChange={this.handleChange} value={this.state.username}/>
<input type="text" name="word" className="form-control" placeholder="please type in word you want to add" onChange={this.handleChange} value={this.state.word}/>
</div>
<button type="submit" value="submit" className="btn btn-primary" >Submit</button>
</form>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class Login extends Component {
// console.log(this.state.type)
this.props.setCurrentUser(foundUser)
if(this.state.type === "student"){
this.props.history.push('/student')
this.props.history.push('/stdprofile')
}else if (this.state.type === "admin"){
this.props.history.push('/admin')
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class SignUp extends Component {
.then(data=> {
console.log(data);
this.props.setCurrentUser(data)
this.props.history.push('/student')
this.props.history.push('/stdprofile')
})
}else{
fetch('http://localhost:3001/admins',{
Expand Down
69 changes: 54 additions & 15 deletions src/components/StudentHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ export default class StudentHistory extends Component {

state={
words: [],
drawed: [],
right: [],
wrong: []
}

componentDidMount(){
this.fetchLessonWords();
// console.log(this.props.currentUser)
}

componentDidUpdate(){
// console.log(this.state.words);
console.log(this.state.right, this.state.wrong);
console.log("words state", this.state.words);
console.log("right-->", this.state.right);
console.log("wrong-->", this.state.wrong);
console.log("drawed-->", this.state.drawed);
this.printWrongWords();
}

fetchLessonWords = () => {
Expand All @@ -39,45 +44,79 @@ export default class StudentHistory extends Component {
fetch('http://localhost:3001/guessrights')
.then(resp => resp.json())
.then(data => {
let rightWords = data.filter(word => word.student_id === this.props.currentUser.id);
rightWords = rightWords.filter(word => word.lesson_id === this.props.currentUser.lesson_id);
// console.log(data);
let lessonGuessedWords = data.filter(word => word.lesson_id === this.props.currentUser.lesson_id);
let rightWords = lessonGuessedWords.filter(word => word.student_id === this.props.currentUser.id);
// rightWords = rightWords.filter(word => word.lesson_id === this.props.currentUser.lesson_id);
console.log("rightWords--->", rightWords);

let right = [];
rightWords.forEach(wordObj=>{
this.state.words.forEach(word=>{
if(wordObj.id === word.id){
right.push(word.text);
if(wordObj.word_id === word.id){
if(!right.includes(word.text)){
right.push(word.text);
}
}
})
})
console.log("right", right);

let allWords = this.state.words.map(word => word.text);
console.log('all', allWords);
let drawedWords = [];
let drawed = lessonGuessedWords.filter(word => word.drawer_id === this.props.currentUser.id);
drawed.forEach(wordObj=>{
this.state.words.forEach(word=>{
if(wordObj.word_id === word.id){
if(!drawedWords.includes(word.text)){
drawedWords.push(word.text);
}
}
})
})
console.log('drawed', drawedWords);

let wrongWords = [];
allWords.forEach(word=>{
if(!right.includes(word)){
if(!right.includes(word) && !drawedWords.includes(word)){
wrongWords.push(word);
}
})
this.setState({
right: right,
wrong: wrongWords
wrong: wrongWords,
drawed: drawedWords
})
})

}

printWrongWords = () => {
let div = document.getElementsByClassName('history-inner')[0];
this.state.wrongWords.forEach(word => {
div.innerText += `<p>${word}</p>`
})
let div = document.getElementsByClassName('history-inner')[1];
let drawedDiv = document.getElementsByClassName('history-inner')[0];
div.innerHTML = "";
drawedDiv.innerHTML = "";

if(this.state.wrong.length === 0 ){
div.innerHTML += `<p>Good job!!!! You got all the word</p>`
}else{
this.state.wrong.forEach(word => {
div.innerHTML += `<p>${word}</p>`
})
}

this.state.drawed.forEach(word => {
drawedDiv.innerHTML += `<p>${word}</p>`
})
}

render() {
return(
<div className="std-history">
You drawed:
<div className="history-inner"></div>
Study List:
<div className="history-inner">
</div>
<div className="history-inner"></div>
</div>
)

Expand Down
43 changes: 42 additions & 1 deletion src/components/studentProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,58 @@ import React, { Component } from 'react';

export default class StudentProfile extends Component {

state = {
lesson_id: null
}


componentDidMount(){

}

handleSubmit = (event) => {
event.preventDefault();

if(this.props.currentUser){
fetch(`http://localhost:3001/students/${this.props.currentUser.id}`,{
method: "PATCH",
headers:{
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
lesson_id: this.state.lesson_id
})
}).then(resp=>resp.json())
.then(data=> {
// console.log(data);
this.props.setCurrentUser(data);
})
// this.getWords();
this.props.history.push('/student');
}
}

handleChange = (event) => {
this.setState({
lesson_id: parseInt(event.target.value)
})
}

render() {
return(
<div className="std-profile">

<form className="auth-form" onSubmit={this.handleSubmit}>
<ul className="form-ul">
<li>
<label>Lesson Id</label>
<input type="text" name="lesson_id" placeholder="lesson id" onChange={this.handleChange} value={this.state.name}/>
</li>
<li>
<input type="submit" value="submit" className="submit-btn" />
</li>
</ul>
</form>
</div>
)

Expand Down
3 changes: 2 additions & 1 deletion src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ io.on('connection', function (socket) {
}else{
usersGuessed.push(data.user);
wordGuessed[currentWord] = usersGuessed;
io.emit('guessright', { user: data.user, correct: true, word: currentWord})
io.emit('guessright', { user: data.user, drawer: currentDrawer, correct: true, word: currentWord})


//user who guess right got 10 points
userScore[data.user.username] += 10;
Expand Down

0 comments on commit 7c60411

Please sign in to comment.