diff --git a/src/actions/setLastLocationAction.js b/src/actions/setLastLocationAction.js new file mode 100644 index 00000000..697f472d --- /dev/null +++ b/src/actions/setLastLocationAction.js @@ -0,0 +1,5 @@ +import { SET_LAST_LOCATION } from '../types' + +export let setLastLocation = props => dispatch => { + dispatch({ type: SET_LAST_LOCATION, payload: props.location.pathname }) +} diff --git a/src/containers/LogIn.js b/src/containers/LogIn.js index 546eca43..5f4b0a4b 100644 --- a/src/containers/LogIn.js +++ b/src/containers/LogIn.js @@ -17,7 +17,7 @@ export class LogIn extends Component { await this.props .postLogInInfo({ email, password }) .then(() => { - this.props.history.push('/') + this.props.history.push(this.props.lastLocation) iziToast.show({ theme: 'light', title: 'Success', @@ -92,7 +92,7 @@ export class LogIn extends Component { } } -const mapStateToProps = store => ({ loggedInUser: store.loggedInUser }) +const mapStateToProps = store => ({ loggedInUser: store.loggedInUser, lastLocation: store.lastLocation }) export default connect( mapStateToProps, { postLogInInfo } diff --git a/src/containers/ProjectsList.js b/src/containers/ProjectsList.js index bef2f01c..0b650e5e 100644 --- a/src/containers/ProjectsList.js +++ b/src/containers/ProjectsList.js @@ -2,6 +2,7 @@ import React, { Component, Fragment } from 'react' import { Header, Card, Grid } from 'semantic-ui-react' import { connect } from 'react-redux' import { fetchProjects } from '../actions/getProjectsAction' +import { setLastLocation } from '../actions/setLastLocationAction' import { Link } from 'react-router-dom' import Select from 'react-select' import Project from '../components/Project' @@ -37,7 +38,8 @@ export class ProjectsList extends Component { } else { this.paginateProjects(this.props.projects) } - }; + this.props.setLastLocation(this.props) + } componentWillReceiveProps (nextProps) { if ( @@ -223,5 +225,5 @@ export class ProjectsList extends Component { const mapStateToProps = store => ({ projects: store.projects, error: store.error }) export default connect( mapStateToProps, - { fetchProjects } + { fetchProjects, setLastLocation } )(ProjectsList) diff --git a/src/containers/UsersList.js b/src/containers/UsersList.js index ad80b639..fbe59385 100644 --- a/src/containers/UsersList.js +++ b/src/containers/UsersList.js @@ -4,6 +4,7 @@ import Paginate from '../components/Paginate' import PaginationLinks from '../components/PaginationLinks' import { connect } from 'react-redux' import { fetchUsers } from '../actions/getUsersAction' +import { setLastLocation } from '../actions/setLastLocationAction' import User from '../components/User' import '../assets/UsersList.css' export class UsersList extends Component { @@ -25,6 +26,7 @@ export class UsersList extends Component { } else { this.normalizeUsers(this.props.users) } + this.props.setLastLocation(this.props) } componentWillReceiveProps (nextProps) { @@ -105,5 +107,5 @@ export class UsersList extends Component { const mapStateToProps = store => ({ users: store.users }) export default connect( mapStateToProps, - { fetchUsers } + { fetchUsers, setLastLocation } )(UsersList) diff --git a/src/index.js b/src/index.js index b5df0def..ad431a7c 100644 --- a/src/index.js +++ b/src/index.js @@ -18,9 +18,14 @@ render( - + - + { + return + }} + /> diff --git a/src/reducers/initialState.js b/src/reducers/initialState.js index 6ebfecab..a8d7e312 100644 --- a/src/reducers/initialState.js +++ b/src/reducers/initialState.js @@ -3,5 +3,6 @@ export default { loggedInUser: {}, signedUpUser: {}, projects: [], - error: [] + error: [], + lastLocation: '' } diff --git a/src/reducers/lastLocationReducer.js b/src/reducers/lastLocationReducer.js new file mode 100644 index 00000000..27bb42af --- /dev/null +++ b/src/reducers/lastLocationReducer.js @@ -0,0 +1,13 @@ +import { SET_LAST_LOCATION } from '../types' +import initialState from './initialState' + +const lastLocationReducer = (state = initialState.lastLocation, action) => { + switch (action.type) { + case SET_LAST_LOCATION: + return action.payload + default: + return state + } +} + +export default lastLocationReducer diff --git a/src/store/index.js b/src/store/index.js index 012b775d..c24c838a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -5,6 +5,7 @@ import projects from '../reducers/projectsReducer' import loggedInUser from '../reducers/loggedInUserReducer' import signedUpUser from '../reducers/signedUpUserReducer' import error from '../reducers/errorReducer' +import lastLocation from '../reducers/lastLocationReducer' const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose const rootReducer = combineReducers({ @@ -12,7 +13,8 @@ const rootReducer = combineReducers({ projects, loggedInUser, signedUpUser, - error + error, + lastLocation }) export default createStore( diff --git a/src/tests/actions/setLastLocationAction.test.js b/src/tests/actions/setLastLocationAction.test.js new file mode 100644 index 00000000..b25570ad --- /dev/null +++ b/src/tests/actions/setLastLocationAction.test.js @@ -0,0 +1,20 @@ +import thunk from 'redux-thunk' +import configureMockStore from 'redux-mock-store' +import { SET_LAST_LOCATION } from '../../types' +import { setLastLocation } from '../../actions/setLastLocationAction' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) +let store + +describe('setLastLocation action', () => { + beforeEach(() => { + store = mockStore({}) + }) + + it('sets last location to /users', () => { + let props = { location: { pathname: '/users' } } + store.dispatch(setLastLocation(props)) + expect(store.getActions()).toEqual([{ type: SET_LAST_LOCATION, payload: '/users' }]) + }) +}) diff --git a/src/tests/containers/ProjectsList.test.js b/src/tests/containers/ProjectsList.test.js index dfce17a2..f42e1b2a 100644 --- a/src/tests/containers/ProjectsList.test.js +++ b/src/tests/containers/ProjectsList.test.js @@ -18,7 +18,8 @@ describe('ProjectsList', () => { }, 300) }), filteredProjectsList: null, - error: false + error: false, + setLastLocation: () => {} } wrapper = mount( @@ -51,6 +52,7 @@ describe('ProjectsList', () => { {}} + setLastLocation={() => {}} /> ) wrapper.setState( @@ -89,7 +91,7 @@ describe('ProjectsList', () => { it("shouldn't render a Project component without projects", () => { const wrapper = mount( - {}} /> + {}} setLastLocation={() => {}} /> ) expect(wrapper.find('Project')).toHaveLength(0) @@ -97,7 +99,7 @@ describe('ProjectsList', () => { it('should test componentWillReceiveProps', () => { const wrapper = shallow( - {}} /> + {}} setLastLocation={() => {}} /> ) wrapper.setProps({ projects: [{ id: 1, languages: [] }] }) expect(wrapper.instance().state.projects).toEqual({ @@ -107,7 +109,7 @@ describe('ProjectsList', () => { it('should call normalizeFilteredProjects', () => { const wrapper = shallow( - {}} /> + {}} setLastLocation={() => {}} /> ) wrapper.setProps({ projects: projectsFixture }) expect(wrapper.instance().state.projects).toEqual( @@ -131,7 +133,7 @@ describe('ProjectsList', () => { it('adds error to the state if fetchProjects fails', async () => { const wrapper = shallow( - {}} /> + {}} setLastLocation={() => {}} /> ) await wrapper.instance().componentDidMount() expect(wrapper.state().error).toEqual(true) diff --git a/src/tests/containers/UserList.test.js b/src/tests/containers/UserList.test.js index 19828194..f5ea475f 100644 --- a/src/tests/containers/UserList.test.js +++ b/src/tests/containers/UserList.test.js @@ -18,6 +18,7 @@ describe('UsersList', () => { }, 300) }) } + setLastLocation={() => {}} /> ) @@ -66,20 +67,20 @@ describe('UsersList', () => { it("shouldn't render a Project component without users", () => { const wrapper = mount( - {}} /> + {}} setLastLocation={() => {}} /> ) expect(wrapper.find('User')).toHaveLength(0) }) it('should test componentWillReceiveProps', () => { - const wrapper = shallow( {}} />) + const wrapper = shallow( {}} setLastLocation={() => {}} />) wrapper.setProps({ users: ['something'] }) expect(wrapper.instance().state.users).toEqual({ '1': ['something'] }) }) it('should test componentWillReceiveProps', () => { - const wrapper = shallow( {}} />) + const wrapper = shallow( {}} setLastLocation={() => {}} />) wrapper.setProps(usersFixture) expect(wrapper.instance().state.users[1][0]).toEqual(usersFixture[0]) }) diff --git a/src/tests/reducers/lastLocationReducer.test.js b/src/tests/reducers/lastLocationReducer.test.js new file mode 100644 index 00000000..652f372c --- /dev/null +++ b/src/tests/reducers/lastLocationReducer.test.js @@ -0,0 +1,17 @@ +import lastLocationReducer from '../../reducers/lastLocationReducer' +import { SET_LAST_LOCATION } from '../../types' + +describe('reduces a user', () => { + it('defaults to empty projects if none are passed in', () => { + expect(lastLocationReducer(undefined, {})).toEqual('') + }) + + it('reduces the signed in user', () => { + expect( + lastLocationReducer([], { + type: SET_LAST_LOCATION, + payload: { lastLocation: '/users' } + }) + ).toEqual({ lastLocation: '/users' }) + }) +}) diff --git a/src/tests/store/error.test.js b/src/tests/store/error.test.js index b925d352..acc09455 100644 --- a/src/tests/store/error.test.js +++ b/src/tests/store/error.test.js @@ -7,12 +7,6 @@ describe('Store', () => { message: 'Network Error' }) - expect(store.getState()).toEqual({ - users: [], - projects: [], - loggedInUser: {}, - signedUpUser: {}, - error: ['Network Error'] - }) + expect(store.getState().error).toEqual(['Network Error']) }) }) diff --git a/src/tests/store/store.test.js b/src/tests/store/store.test.js index 17bb4ad1..0b800d09 100644 --- a/src/tests/store/store.test.js +++ b/src/tests/store/store.test.js @@ -12,7 +12,8 @@ describe('Store', () => { loggedInUser: {}, signedUpUser: {}, error: [], - projects: [] + projects: [], + lastLocation: '' }) }) }) diff --git a/src/types/index.js b/src/types/index.js index a95d1c93..6fc9625a 100644 --- a/src/types/index.js +++ b/src/types/index.js @@ -3,3 +3,4 @@ export const GET_PROJECTS = 'GET_PROJECTS' export const POST_LOGIN_INFO = 'POST_LOGIN_INFO' export const POST_SIGNUP_INFO = 'POST_SIGNUP_INFO' export const FETCH_PROJECTS_FAILURE = 'FETCH_PROJECTS_FAILURE' +export const SET_LAST_LOCATION = 'SET_LAST_LOCATION'