Skip to content

Commit 20b2040

Browse files
committed
eager loading associations to render levels component on front end
1 parent 66e55da commit 20b2040

File tree

8 files changed

+64
-19
lines changed

8 files changed

+64
-19
lines changed

client/components/Levels.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import {connect} from 'react-redux'
4+
import {withRouter, Link} from 'react-router-dom'
5+
import {logout} from '../store'
6+
7+
8+
const Levels = (props) => {
9+
console.log("levels props", props)
10+
const {problems} = props
11+
return (
12+
<div>
13+
{
14+
problems && problems.map(problem => {
15+
return (
16+
<div key={problem.id}>
17+
Name: {problem.name}
18+
Level: {problem.level}
19+
Problem Number: {problem.problemNumber}
20+
</div>
21+
)
22+
})
23+
}
24+
</div>
25+
)
26+
}
27+
28+
29+
export default Levels

client/components/main.js

-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ import { Login, Signup } from "./auth-form";
99
import { Router } from "react-router";
1010
import history from "../history";
1111

12-
/**
13-
* COMPONENT
14-
* The Main component is our 'picture frame' - it displays the navbar and anything
15-
* else common to our entire app. The 'picture' inside the frame is the space
16-
* rendered out by the component's `children`.
17-
*/
18-
// const Main = (props) => {
19-
// const {children, handleClick, isLoggedIn} = props
2012

2113
class Main extends React.Component {
2214
constructor(props) {

client/components/user-home.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ import PropTypes from 'prop-types'
33
import {connect} from 'react-redux'
44
import {withRouter, Link} from 'react-router-dom'
55
import {logout} from '../store'
6+
import Levels from './Levels'
67

78

89
/**
910
* COMPONENT
1011
*/
1112
export const UserHome = (props) => {
12-
const {email, isLoggedIn, handleClick} = props
13-
console.log("userhome")
13+
const {email, isLoggedIn, handleClick, userProblems} = props
14+
console.log("userhome renders with these props", props)
1415
return (
1516
<div>
1617
<h3>Welcome, {email}</h3>
18+
<h2>Which chapter would you like to select?</h2>
19+
<Levels problems={userProblems} />
1720
</div>
1821
)
1922
}
@@ -23,6 +26,8 @@ export const UserHome = (props) => {
2326
*/
2427
const mapState = (state) => {
2528
return {
29+
// storeProps: state,
30+
userProblems: state.user.problems,
2631
isLoggedIn: !!state.user.id,
2732
email: state.user.email
2833
}

client/store/currentProblem.js

Whitespace-only changes.

script/seed.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function seed () {
2323
])
2424

2525
const associations = await Promise.all([
26-
users[0].addProblem(problems[0])
26+
problems[0].addUser(users[0])
2727
])
2828

2929
console.log(`seeded ${users.length} users`)

server/api/users.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module.exports = router
44

55
router.get('/', (req, res, next) => {
66
User.findAll({
7-
attributes: ['id', 'email']
7+
attributes: ['id', 'email'],
88
})
99
.then(users => res.json(users))
1010
.catch(next)
1111
});
1212

13-
//creates an association on the UserProblem join table by finding theuser and the problem, returning them via promise.all, and using sequelize association method to set the user on the problem
13+
14+
//creates an association on the UserProblem join table
1415
router.post('/:userId/:level/:problemNumber', (req, res, next) => {
16+
//need promise.all here because need both values for addUser
1517
Promise.all(
1618
[User.findOne({
1719
where: {
@@ -26,8 +28,8 @@ router.post('/:userId/:level/:problemNumber', (req, res, next) => {
2628
})]
2729
)
2830
.then(([user, problem]) => {
29-
//the aforementioned sequelize method
30-
user.addProblem(problem)
31+
//addUser is a sequelize method
32+
problem.addUser(user)
3133
})
3234
.then(() => res.sendStatus(200))
3335
.catch(next)

server/auth/index.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const router = require('express').Router()
2-
const User = require('../db/models/user')
2+
const {User, Problem} = require('../db/models')
33
module.exports = router
44

55
router.post('/login', (req, res, next) => {
6-
User.findOne({where: {email: req.body.email}})
6+
User.findOne({
7+
where: {
8+
email: req.body.email
9+
},
10+
//gets all problems that user is associated with
11+
include: [{model: Problem}]
12+
})
713
.then(user => {
814
if (!user) {
915
res.status(401).send('User not found')
@@ -35,8 +41,18 @@ router.post('/logout', (req, res) => {
3541
res.redirect('/')
3642
})
3743

38-
router.get('/me', (req, res) => {
39-
res.json(req.user)
44+
router.get('/me', (req, res, next) => {
45+
User.findOne({
46+
where: {
47+
id: req.user.id
48+
},
49+
//loads any problems associated with the user
50+
include: [{
51+
model: Problem
52+
}]
53+
})
54+
.then(user => res.json(user))
55+
.catch(next)
4056
})
4157

4258
router.use('/google', require('./google'))

server/db/models/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const UserProblem = require('./userProblem')
1010
*/
1111
Dialog.belongsTo(Problem)
1212
Problem.hasMany(Dialog)
13+
1314
User.belongsToMany(Problem, {through: UserProblem})
1415
Problem.belongsToMany(User, {through: UserProblem})
1516

0 commit comments

Comments
 (0)