1
1
import React , { Component } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
import { connect } from 'react-redux'
4
- import { logout , getCurrentProblemThunk } from '../store'
4
+ import { logout , getCurrentProblemThunk , getUserInputThunk } from '../store'
5
5
import { NavLink } from 'react-router-dom'
6
6
import Levels from './Levels'
7
7
import Editor from './CodeEditor'
8
+ import axios from 'axios'
8
9
9
10
/**
10
11
* COMPONENT
@@ -16,36 +17,48 @@ class SingleProblem extends Component {
16
17
}
17
18
18
19
componentDidMount ( ) {
19
- //load problem from the database
20
- this . props . loadProblem ( this . props . params . match . params . id )
20
+ //Find or create user <-> problem association in UserProblem table
21
+ axios . get ( `/api/users/${ this . props . userId } /${ this . props . problem . id } ` )
22
+ . then ( res => {
23
+ if ( ! res . data ) {
24
+ //create association
25
+ axios . post ( `/api/users/${ userId } /${ problemId } ` )
26
+ . then ( res => {
27
+ dispatch ( userProblemAssociated ( res . data ) )
28
+ } )
29
+ } } )
30
+ . catch ( err => console . log ( err ) )
31
+
32
+ this . props . loadProblem ( this . props . params , this . props . userId )
21
33
}
22
34
23
35
render ( ) {
24
- const { email, isLoggedIn, problem, params} = this . props
36
+ const { email, isLoggedIn, problem, params, userId , userInput } = this . props
25
37
return (
26
38
< div className = 'toc' >
27
- { /* <a href="#" onClick={handleClick}>Logout</a> */ }
28
39
< h3 > { problem ? problem . name : '' } </ h3 >
29
40
< h4 > { problem ? problem . prompt : '' } </ h4 >
30
- < Editor />
41
+ < Editor userInput = { userInput } />
31
42
< NavLink to = { '/' } >
32
43
< button > Go Back Home</ button >
33
44
</ NavLink >
34
45
</ div >
35
46
)
36
47
}
37
-
38
48
}
39
49
40
50
/**
41
51
* CONTAINER
42
52
*/
43
53
const mapState = ( state , ownprops ) => {
44
54
return {
45
- params : ownprops ,
55
+ // Params: to refactor to not use ownprops.match.params.id. Idea: put an onClick function in the Levels component to dispatch to the store
56
+ params : parseInt ( ownprops . match . params . id ) ,
46
57
problem : state . currentProblem ,
47
58
isLoggedIn : ! ! state . user . id ,
48
- email : state . user . email
59
+ email : state . user . email ,
60
+ userId : state . user . id ,
61
+ userInput : state . userInput
49
62
}
50
63
}
51
64
@@ -54,8 +67,9 @@ const mapDispatch = (dispatch) => {
54
67
handleLogOut ( ) {
55
68
dispatch ( logout ( ) )
56
69
} ,
57
- loadProblem ( id ) {
58
- dispatch ( getCurrentProblemThunk ( id ) )
70
+ loadProblem ( problemId , userId ) {
71
+ dispatch ( getUserInputThunk ( problemId , userId ) )
72
+ dispatch ( getCurrentProblemThunk ( problemId , userId ) )
59
73
}
60
74
}
61
75
}
@@ -65,7 +79,5 @@ export default connect(mapState, mapDispatch)(SingleProblem)
65
79
/**
66
80
* PROP TYPES
67
81
*/
68
- SingleProblem . propTypes = {
69
- email : PropTypes . string
70
- }
82
+
71
83
0 commit comments