Skip to content

Commit 01bce9c

Browse files
committed
Update: created a separate api route and thunk to fetch the user input, rather than eager loading it
1 parent 73a3100 commit 01bce9c

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

client/components/CodeEditor.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class CodeEditor extends Component {
2424

2525
//receiving code input as a prop from singleProblem once that is done loading from the db api call
2626
componentWillReceiveProps(nextprop){
27-
if(nextprop.codeInput && nextprop.codeInput.length){
28-
this.setState({code: nextprop.codeInput})
27+
if(nextprop.userInput && nextprop.userInput.length){
28+
this.setState({code: nextprop.userInput})
2929
}
3030
}
3131

@@ -36,7 +36,7 @@ class CodeEditor extends Component {
3636
}
3737

3838
render() {
39-
const { problemId, userId, handleSave, codeInput } = this.props;
39+
const { problemId, userId, handleSave, userInput } = this.props;
4040
return (
4141
<div>
4242
<div>
@@ -65,7 +65,7 @@ class CodeEditor extends Component {
6565

6666
const mapState = (state, ownprops) => {
6767
return {
68-
codeInput: ownprops.codeInput,
68+
userInput: ownprops.userInput,
6969
problemId: state.currentProblem.id,
7070
userId: state.user.id
7171
};

client/components/SingleProblem.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
33
import {connect} from 'react-redux'
4-
import {logout, getCurrentProblemThunk, findOrCreateUserProblem, clearCurrentProblem} from '../store'
4+
import {logout, getCurrentProblemThunk, getUserInputThunk, clearCurrentProblem} from '../store'
55
import { NavLink } from 'react-router-dom'
66
import Levels from './Levels'
77
import Editor from './CodeEditor'
@@ -28,20 +28,17 @@ class SingleProblem extends Component {
2828
})
2929
}})
3030
.catch(err => console.log(err))
31+
3132
this.props.loadProblem(this.props.params, this.props.userId)
3233
}
3334

3435
render(){
35-
const {email, isLoggedIn, problem, params, userId} = this.props
36-
let codeInput = ''
37-
if(problem.users){
38-
codeInput = problem.users[0].UserProblem.savedInput
39-
}
36+
const {email, isLoggedIn, problem, params, userId, userInput} = this.props
4037
return (
4138
<div className='toc'>
4239
<h3>{problem ? problem.name : ''}</h3>
4340
<h4>{problem ? problem.prompt : ''}</h4>
44-
<Editor codeInput={codeInput}/>
41+
<Editor userInput={userInput}/>
4542
<NavLink to={'/'}>
4643
<button>Go Back Home</button>
4744
</NavLink>
@@ -60,6 +57,7 @@ const mapState = (state, ownprops) => {
6057
isLoggedIn: !!state.user.id,
6158
email: state.user.email,
6259
userId: state.user.id,
60+
userInput: state.userInput
6361
}
6462
}
6563

@@ -69,6 +67,7 @@ const mapDispatch = (dispatch) => {
6967
dispatch(logout())
7068
},
7169
loadProblem(problemId, userId) {
70+
dispatch(getUserInputThunk(problemId, userId))
7271
dispatch(getCurrentProblemThunk(problemId, userId))
7372
},
7473
clearProblem(){

client/store/userInput.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,42 @@ import user from './user';
33

44
//ACTION TYPES
55
const SAVE_USER_INPUT = 'SAVE_USER_INPUT'
6+
const GET_USER_INPUT = 'GET_USER_INPUT'
67

78
//INITIAL STATE
89
const userInput = ''
910

1011
//ACTION CREATORS
1112
const saveUserInput = input => ({type: SAVE_USER_INPUT, input})
13+
const getUserInput = input => ({type: GET_USER_INPUT, input})
1214

1315
//THUNKS
14-
export const postUserInput = (problemId, userId, input) =>{
16+
export const postUserInput = (problemId, userId, input) => {
1517
return (dispatch) => {
16-
axios.post(`/api/problems/${problemId}/${userId}`, input)
18+
axios.post(`/api/inputs/${problemId}/${userId}`, input)
1719
.then(res => {
1820
dispatch(saveUserInput(input))
1921
})
2022
.catch(err => console.log(err))
2123
}
2224
}
2325

26+
export const getUserInputThunk = (problemId, userId) => {
27+
return (dispatch) => {
28+
axios.get(`/api/inputs/${problemId}/${userId}`)
29+
.then(input => {
30+
dispatch(getUserInput(input.data.savedInput))
31+
})
32+
.catch(err => console.log(err))
33+
}
34+
}
35+
2436
//REDUCER
2537
export default function(state = userInput, action) {
2638
switch (action.type) {
2739
case SAVE_USER_INPUT:
40+
return action.input
41+
case GET_USER_INPUT:
2842
return action.input
2943
default:
3044
return state

server/api/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = router
44
// matches all requests to /api/{whatever}/
55
router.use('/users', require('./users'))
66
router.use('/problems', require('./problems'))
7+
router.use('/inputs', require('./inputs'))
78

89
router.use((req, res, next) => {
910
const error = new Error('Not Found')

server/api/inputs.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const router = require('express').Router()
2+
const { UserProblem } = require('../db/models')
3+
module.exports = router
4+
5+
//Get the User Input
6+
router.get('/:problemId/:userId', (req, res, next) => {
7+
UserProblem.findOne({
8+
where: {
9+
problemId: req.params.problemId,
10+
userId: req.params.userId
11+
}
12+
})
13+
.then(input => res.json(input))
14+
.catch(next)
15+
})
16+
17+
//Update the UserProblem join table
18+
router.post('/:problemId/:userId', (req, res, next) => {
19+
UserProblem.findOne({
20+
where: {
21+
problemId: req.params.problemId,
22+
userId: req.params.userId
23+
}
24+
})
25+
.then(problem => { problem.update(req.body) })
26+
.then(data => { res.sendStatus(204) })
27+
.catch(next)
28+
})
29+

server/api/problems.js

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const router = require('express').Router()
2-
const { Problem, Dialog, User, UserProblem } = require('../db/models')
2+
const { Problem, Dialog } = require('../db/models')
33
module.exports = router
44

55
//get all problems
@@ -15,27 +15,8 @@ router.get('/:problemId/:userId', (req, res, next) => {
1515
where: {
1616
id: req.params.problemId,
1717
},
18-
include: [{model: Dialog}, {model: User, where: {id: req.params.userId}, through: { attributes: ['savedInput']} }]
18+
include: [{model: Dialog}]
1919
})
2020
.then(problem => res.json(problem))
2121
.catch(next)
2222
})
23-
24-
//Update the UserProblem join table
25-
router.post('/:problemId/:userId', (req, res, next) => {
26-
UserProblem.findOne({
27-
where: {
28-
problemId: req.params.problemId,
29-
userId: req.params.userId
30-
}
31-
})
32-
.then(problem => {
33-
problem.update(req.body)
34-
})
35-
.then(data => {
36-
res.sendStatus(204)
37-
})
38-
.catch(next)
39-
40-
})
41-

0 commit comments

Comments
 (0)