diff --git a/api/scripts/generate/all.js b/api/scripts/generate/all.js index 0477e155..61cf2906 100644 --- a/api/scripts/generate/all.js +++ b/api/scripts/generate/all.js @@ -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) } }) diff --git a/api/scripts/generate/article.js b/api/scripts/generate/article.js index a84c4628..9f235a1c 100644 --- a/api/scripts/generate/article.js +++ b/api/scripts/generate/article.js @@ -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(), + } } diff --git a/api/scripts/generate/comment.js b/api/scripts/generate/comment.js index 70e78d9c..57c24ccc 100644 --- a/api/scripts/generate/comment.js +++ b/api/scripts/generate/comment.js @@ -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(), + } } diff --git a/api/scripts/generate/index.js b/api/scripts/generate/index.js index ad761745..b70cfdd8 100644 --- a/api/scripts/generate/index.js +++ b/api/scripts/generate/index.js @@ -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) { diff --git a/api/scripts/generate/user.js b/api/scripts/generate/user.js index 927ad49a..2112ca4a 100644 --- a/api/scripts/generate/user.js +++ b/api/scripts/generate/user.js @@ -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(), + } } diff --git a/api/src/models/article.js b/api/src/models/article.js index 8d717d7c..fd49213d 100644 --- a/api/src/models/article.js +++ b/api/src/models/article.js @@ -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'}, } } diff --git a/api/src/routes/api/articles.js b/api/src/routes/api/articles.js index c28aede3..f0b98b4b 100644 --- a/api/src/routes/api/articles.js +++ b/api/src/routes/api/articles.js @@ -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 } diff --git a/client/package-scripts.js b/client/package-scripts.js index ae7d67b3..582ad717 100644 --- a/client/package-scripts.js +++ b/client/package-scripts.js @@ -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: { diff --git a/client/src/middleware.js b/client/src/middleware.js index cc603365..def9b894 100644 --- a/client/src/middleware.js +++ b/client/src/middleware.js @@ -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 => { @@ -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}) } @@ -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) } diff --git a/client/src/reducers/article-list.js b/client/src/reducers/article-list.js index d941a80f..cafd07cc 100644 --- a/client/src/reducers/article-list.js +++ b/client/src/reducers/article-list.js @@ -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': @@ -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 {} diff --git a/client/src/reducers/auth.js b/client/src/reducers/auth.js index 3ff9d3b9..482420e6 100644 --- a/client/src/reducers/auth.js +++ b/client/src/reducers/auth.js @@ -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': @@ -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} diff --git a/client/src/shared/agent.js b/client/src/shared/agent.js index 825519ee..bff59a10 100644 --- a/client/src/shared/agent.js +++ b/client/src/shared/agent.js @@ -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 + } }, } diff --git a/cypress/e2e/users_spec.js b/cypress/e2e/users_spec.js index a9c47aee..b18995a5 100644 --- a/cypress/e2e/users_spec.js +++ b/cypress/e2e/users_spec.js @@ -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}`) } diff --git a/yarn.lock b/yarn.lock index 52112633..72ad4887 100644 --- a/yarn.lock +++ b/yarn.lock @@ -18,7 +18,7 @@ acorn-jsx@^3.0.0: dependencies: acorn "^3.0.4" -acorn@4.0.4: +acorn@4.0.4, acorn@^4.0.3, acorn@^4.0.4: version "4.0.4" resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.4.tgz#17a8d6a7a6c4ef538b814ec9abac2779293bf30a" @@ -26,10 +26,6 @@ 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" @@ -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"