Skip to content

Commit

Permalink
generating data correctly again
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Mar 10, 2017
1 parent 44c6697 commit b896ddf
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 65 deletions.
1 change: 0 additions & 1 deletion api/scripts/generate/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function generateDocumentObjects(firstUser) {
const followees = getRandomUsers(users)
followees.forEach(followee => {
if (followee !== user) {
// console.log(JSON.stringify(followee))
user.following.push(followee._id)
}
})
Expand Down
16 changes: 5 additions & 11 deletions api/scripts/generate/article.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import faker from 'faker'
import mongoose from 'mongoose'
import generateArticleData
from '../../src/models/__tests__/helpers/generate/article'
import getArticleSchema from '../../src/models/article'
import {commonProps} from './utils'

const Article = mongoose.model('Article', getArticleSchema())

export default createArticle

function createArticle(author) {
return Object.assign(
new Article({
...commonProps(author.createdAt),
...generateArticleData(author),
}),
{_id: faker.random.uuid()},
)
return {
...commonProps(author.createdAt),
...generateArticleData(author),
_id: mongoose.Types.ObjectId(),
}
}
21 changes: 8 additions & 13 deletions api/scripts/generate/comment.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import faker from 'faker'
import mongoose from 'mongoose'
import getCommentSchema from '../../src/models/comment'
import faker from 'faker'
import {commonProps} from './utils'

const Comment = mongoose.model('Comment', getCommentSchema())

export default createComment

function createComment(article, author) {
return Object.assign(
new Comment({
...commonProps(article.createdAt),
article: article._id,
author: author._id,
body: faker.lorem.sentences(),
}),
{_id: faker.random.uuid()},
)
return {
...commonProps(article.createdAt),
article: article._id,
author: author._id,
body: faker.lorem.sentences(),
_id: mongoose.Types.ObjectId(),
}
}
13 changes: 10 additions & 3 deletions api/scripts/generate/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/* eslint no-process-exit:0 */
import mongoose from 'mongoose'
import chalk from 'chalk'
import getUserSchema from '../../src/models/user'
import getArticleSchema from '../../src/models/article'
import getCommentSchema from '../../src/models/comment'
import generateDocumentObjects from './all'

const User = mongoose.model('User')
const Article = mongoose.model('Article')
const Comment = mongoose.model('Comment')
// use native promises
mongoose.Promise = Promise

const User = mongoose.model('User', getUserSchema())
const Article = mongoose.model('Article', getArticleSchema())
const Comment = mongoose.model('Comment', getCommentSchema())


let connectPromise
if (process.env.MONGODB_URI) {
Expand Down
16 changes: 5 additions & 11 deletions api/scripts/generate/user.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import faker from 'faker'
import mongoose from 'mongoose'
import getUserSchema from '../../src/models/user'
import generateUserData from '../../src/models/__tests__/helpers/generate/user'
import {commonProps} from './utils'

const User = mongoose.model('User', getUserSchema())

export default createUser

function createUser(overrides = {}) {
return Object.assign(
new User({
...commonProps(),
...generateUserData(overrides),
}),
{_id: faker.random.uuid()},
)
return {
...commonProps(),
...generateUserData(overrides),
_id: mongoose.Types.ObjectId(),
}
}
4 changes: 3 additions & 1 deletion api/src/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ function getArticleSchema(User) {
tagList: this.tagList,
favorited: user ? user.isFavorite(this._id) : false,
favoritesCount: this.favoritesCount,
author: this.author.toProfileJSONFor(user),
author: this.author ?
this.author.toProfileJSONFor(user) :
{username: 'userRemoved'},
}
}

Expand Down
5 changes: 1 addition & 4 deletions api/src/routes/api/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ function getArticlesRouter() {
User.findOne({username: req.query.favorited}) :
null,
])
.then(results => {
const author = results[0]
const favoriter = results[1]

.then(([author, favoriter]) => {
if (author) {
query.author = author._id
}
Expand Down
2 changes: 1 addition & 1 deletion client/package-scripts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {series, rimraf, commonTags, crossEnv, concurrent} = require('nps-utils')
module.exports = {
scripts: {
dev: crossEnv('PORT=8080 react-scripts start'),
dev: crossEnv(`PORT=${process.env.PORT || '8080'} react-scripts start`),
build: 'react-scripts build',
default: 'pushstate-server build',
test: {
Expand Down
11 changes: 8 additions & 3 deletions client/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const promiseMiddleware = store => next => action => {
return
}
action.payload = res || {}
store.dispatch({type: 'ASYNC_END', promise: action.payload})
if (!action.skipTracking) {
store.dispatch({type: 'ASYNC_END', promise: action.payload})
}
store.dispatch(action)
},
error => {
Expand All @@ -23,7 +25,7 @@ const promiseMiddleware = store => next => action => {
return
}
action.error = true
action.payload = error.response.body
action.payload = error.response.data
if (!action.skipTracking) {
store.dispatch({type: 'ASYNC_END', promise: action.payload})
}
Expand All @@ -39,7 +41,10 @@ const promiseMiddleware = store => next => action => {

const localStorageMiddleware = () => next => action => {
if (action.type === 'REGISTER' || action.type === 'LOGIN') {
if (!action.error) {
if (action.error) {
window.localStorage.removeItem('jwt')
agent.setToken(null)
} else {
window.localStorage.setItem('jwt', action.payload.user.token)
agent.setToken(action.payload.user.token)
}
Expand Down
24 changes: 17 additions & 7 deletions client/src/reducers/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ export default (state = {}, action) => {
tag: action.tag,
currentPage: 0,
}
case 'HOME_PAGE_LOADED':
case 'HOME_PAGE_LOADED': {
if (action.error) {
return {}
}
const [
{tags},
{articles, articlesCount},
] = action.payload
return {
...state,
tags: action.payload[0].tags,
articles: action.payload[1].articles,
articlesCount: action.payload[1].articlesCount,
tags,
articles,
articlesCount,
currentPage: 0,
tab: action.tab,
}
}
case 'HOME_PAGE_UNLOADED':
return {}
case 'CHANGE_TAB':
Expand All @@ -52,13 +60,15 @@ export default (state = {}, action) => {
tag: null,
}
case 'PROFILE_PAGE_LOADED':
case 'PROFILE_FAVORITES_PAGE_LOADED':
case 'PROFILE_FAVORITES_PAGE_LOADED': {
const [, {articles, articlesCount} = {}] = action.payload || []
return {
...state,
articles: action.payload[1].articles,
articlesCount: action.payload[1].articlesCount,
articles,
articlesCount,
currentPage: 0,
}
}
case 'PROFILE_PAGE_UNLOADED':
case 'PROFILE_FAVORITES_PAGE_UNLOADED':
return {}
Expand Down
5 changes: 3 additions & 2 deletions client/src/reducers/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default (state = {inProgress: false, errors: null}, action) => {
const defaultState = {inProgress: false, errors: null}
export default (state = defaultState, action) => {
switch (action.type) {
case 'LOGIN':
case 'REGISTER':
Expand All @@ -9,7 +10,7 @@ export default (state = {inProgress: false, errors: null}, action) => {
}
case 'LOGIN_PAGE_UNLOADED':
case 'REGISTER_PAGE_UNLOADED':
return {}
return {...defaultState}
case 'ASYNC_START':
if (action.subtype === 'LOGIN' || action.subtype === 'REGISTER') {
return {...state, inProgress: true}
Expand Down
6 changes: 5 additions & 1 deletion client/src/shared/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default {
Profile,
Tags,
setToken: token => {
api.defaults.headers.common.authorization = `Token ${token}`
if (token) {
api.defaults.headers.common.authorization = `Token ${token}`
} else {
delete api.defaults.headers.common.authorization
}
},
}
2 changes: 1 addition & 1 deletion cypress/e2e/users_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ function verifyLoggedIn(username) {
return cy
.get(sel('profile-link'))
.should('contain.text', username)
.and('have.attr', 'href', `@${username}`)
.and('have.attr', 'href', `#/@${username}`)
}
8 changes: 2 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ acorn-jsx@^3.0.0:
dependencies:
acorn "^3.0.4"

[email protected]:
[email protected], acorn@^4.0.3, acorn@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.4.tgz#17a8d6a7a6c4ef538b814ec9abac2779293bf30a"

acorn@^3.0.4:
version "3.3.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a"

acorn@^4.0.3, acorn@^4.0.4:
version "4.0.11"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.11.tgz#edcda3bd937e7556410d42ed5860f67399c794c0"

ajv-keywords@^1.0.0, ajv-keywords@^1.1.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"
Expand Down Expand Up @@ -3852,7 +3848,7 @@ xtend@^4.0.0, xtend@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

"xvfb@github:cypress-io/node-xvfb":
xvfb@cypress-io/node-xvfb:
version "0.3.0"
resolved "https://codeload.github.com/cypress-io/node-xvfb/tar.gz/22e3783c31d81ebe64d8c0df491ea00cdc74726a"

Expand Down

0 comments on commit b896ddf

Please sign in to comment.