From 50671ed027f874992ac50cfb21e123f579440737 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Mon, 19 Feb 2018 20:24:37 +0100 Subject: Remove action creators --- src/client/react/actions/search.js | 39 ------------------ src/client/react/actions/view.js | 51 ------------------------ src/client/react/components/container/Results.js | 3 +- src/client/react/components/container/Search.js | 15 ++++--- src/client/react/components/container/View.js | 24 ++++++++++- src/client/react/index.js | 3 +- src/client/react/reducers/search.test.js | 35 +++++++--------- 7 files changed, 46 insertions(+), 124 deletions(-) delete mode 100644 src/client/react/actions/search.js delete mode 100644 src/client/react/actions/view.js diff --git a/src/client/react/actions/search.js b/src/client/react/actions/search.js deleted file mode 100644 index e894e84..0000000 --- a/src/client/react/actions/search.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (C) 2018 Noah Loomans - * - * This file is part of rooster.hetmml.nl. - * - * rooster.hetmml.nl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * rooster.hetmml.nl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rooster.hetmml.nl. If not, see . - * - */ - -export const setUser = user => ({ - type: 'SEARCH/SET_USER', - user, -}); - -export const inputChange = searchText => ({ - type: 'SEARCH/INPUT_CHANGE', - searchText, -}); - -/** - * Change the selected result. - * @param {+1/-1} relativeChange usually +1 or -1, the change relative to the - * current result. - */ -export const changeSelectedResult = relativeChange => ({ - type: 'SEARCH/CHANGE_SELECTED_RESULT', - relativeChange, -}); diff --git a/src/client/react/actions/view.js b/src/client/react/actions/view.js deleted file mode 100644 index 986d8f2..0000000 --- a/src/client/react/actions/view.js +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (C) 2018 Noah Loomans - * - * This file is part of rooster.hetmml.nl. - * - * rooster.hetmml.nl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * rooster.hetmml.nl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rooster.hetmml.nl. If not, see . - * - */ - -// eslint-disable-next-line import/prefer-default-export -export const fetchSchedule = (user, week) => (dispatch) => { - dispatch({ - type: 'VIEW/FETCH_SCHEDULE_REQUEST', - user, - week, - }); - - fetch(`/get/${user}?week=${week}`).then( - // success - (r) => { - r.text().then((htmlStr) => { - dispatch({ - type: 'VIEW/FETCH_SCHEDULE_SUCCESS', - user, - week, - htmlStr, - }); - }); - }, - - // error - () => { - dispatch({ - type: 'VIEW/FETCH_SCHEDULE_FAILURE', - week, - user, - }); - }, - ); -}; diff --git a/src/client/react/components/container/Results.js b/src/client/react/components/container/Results.js index b36abb2..0f08d7a 100644 --- a/src/client/react/components/container/Results.js +++ b/src/client/react/components/container/Results.js @@ -25,7 +25,6 @@ import { connect } from 'react-redux'; import { withRouter } from 'react-router-dom'; import users from '../../users'; -import { setUser } from '../../actions/search'; import { userFromMatch } from '../../lib/url'; import Result from '../presentational/Result'; @@ -73,7 +72,7 @@ class Results extends React.Component { // EDGE CASE: The user is set if the user changes, but it doesn't // change if the result is already the one we are viewing. // Therefor, we need to dispatch the SET_USER command manually. - this.props.dispatch(setUser(user)); + this.props.dispatch({ type: 'SEARCH/SET_USER', user }); } else { this.props.history.push(`/${userId}`); } diff --git a/src/client/react/components/container/Search.js b/src/client/react/components/container/Search.js index b84502c..1c4ca4f 100644 --- a/src/client/react/components/container/Search.js +++ b/src/client/react/components/container/Search.js @@ -27,7 +27,6 @@ import { withRouter } from 'react-router-dom'; import SearchIcon from 'react-icons/lib/md/search'; import { userFromMatch } from '../../lib/url'; -import { setUser, inputChange, changeSelectedResult } from '../../actions/search'; import users from '../../users'; import Results from './Results'; @@ -65,13 +64,13 @@ class Search extends React.Component { componentDidMount() { const urlUser = userFromMatch(this.props.match); - this.props.dispatch(setUser(urlUser)); + this.props.dispatch({ type: 'SEARCH/SET_USERS', urlUser }); } componentWillReceiveProps(nextProps) { if (nextProps.match !== this.props.match) { const urlUser = userFromMatch(nextProps.match); - this.props.dispatch(setUser(urlUser)); + this.props.dispatch({ type: 'SEARCH/SET_USERS', urlUser }); } } @@ -94,17 +93,17 @@ class Search extends React.Component { switch (event.key) { case 'ArrowUp': event.preventDefault(); - this.props.dispatch(changeSelectedResult(-1)); + this.props.dispatch({ type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 }); break; case 'ArrowDown': event.preventDefault(); - this.props.dispatch(changeSelectedResult(+1)); + this.props.dispatch({ type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 }); break; case 'Escape': event.preventDefault(); - this.props.dispatch(setUser(urlUser)); + this.props.dispatch({ type: 'SEARCH/SET_USERS', urlUser }); break; case 'Enter': @@ -113,7 +112,7 @@ class Search extends React.Component { // EDGE CASE: The user is set if the user changes, but it doesn't // change if the result is already the one we are viewing. // Therefor, we need to dispatch the SET_USER command manually. - this.props.dispatch(setUser(urlUser)); + this.props.dispatch({ type: 'SEARCH/SET_USERS', urlUser }); } else if (result) { this.props.history.push(`/${result}`); } @@ -153,7 +152,7 @@ class Search extends React.Component { dispatch(inputChange(event.target.value))} + onChange={event => dispatch({ type: 'SEARCH/INPUT_CHANGE', searchText: event.target.value })} onKeyDown={this.onKeyDown} value={searchText} placeholder="Zoeken" diff --git a/src/client/react/components/container/View.js b/src/client/react/components/container/View.js index 938614e..28aaad3 100644 --- a/src/client/react/components/container/View.js +++ b/src/client/react/components/container/View.js @@ -24,7 +24,6 @@ import { connect } from 'react-redux'; import { withRouter } from 'react-router-dom'; import { userFromMatch, weekFromLocation } from '../../lib/url'; -import { fetchSchedule } from '../../actions/view'; import extractSchedule from '../../lib/extractSchedule'; import Schedule from '../presentational/Schedule'; @@ -66,7 +65,28 @@ class View extends React.Component { const schedule = extractSchedule(schedules, user, week); if (schedule.state === 'NOT_REQUESTED') { - dispatch(fetchSchedule(user, week)); + fetch(`/get/${user}?week=${week}`).then( + // success + (r) => { + r.text().then((htmlStr) => { + dispatch({ + type: 'VIEW/FETCH_SCHEDULE_SUCCESS', + user, + week, + htmlStr, + }); + }); + }, + + // error + () => { + dispatch({ + type: 'VIEW/FETCH_SCHEDULE_FAILURE', + week, + user, + }); + }, + ); } } diff --git a/src/client/react/index.js b/src/client/react/index.js index 2ce7b6c..566c847 100644 --- a/src/client/react/index.js +++ b/src/client/react/index.js @@ -25,7 +25,6 @@ import moment from 'moment'; import { createStore, applyMiddleware, compose } from 'redux'; import { Provider } from 'react-redux'; import logger from 'redux-logger'; -import thunk from 'redux-thunk'; import { BrowserRouter as Router, @@ -44,7 +43,7 @@ moment.locale('nl'); const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const store = createStore( reducer, - composeEnhancers(applyMiddleware(thunk, logger)), + composeEnhancers(applyMiddleware(logger)), ); ReactDOM.render( diff --git a/src/client/react/reducers/search.test.js b/src/client/react/reducers/search.test.js index bb1ad8a..48ca05a 100644 --- a/src/client/react/reducers/search.test.js +++ b/src/client/react/reducers/search.test.js @@ -33,21 +33,16 @@ window.USERS = [ const deepFreeze = require('deep-freeze'); const search = require('./search').default; const { _test } = require('./search'); -const { - setUser, - inputChange, - changeSelectedResult, -} = require('../actions/search'); describe('reducers', () => { describe('search', () => { describe('SEARCH/SET_USER', () => { it('Resets to the default state if the user is null', () => { - expect(search({ foo: 'bar' }, setUser(null))).toEqual(_test.DEFAULT_STATE); + expect(search({ foo: 'bar' }, { type: 'SEARCH/SET_USER', user: null })).toEqual(_test.DEFAULT_STATE); }); it('Sets all the values of that user properly', () => { - expect(search(undefined, setUser('s/18561'))).toEqual({ + expect(search(undefined, { type: 'SEARCH/SET_USER', user: 's/18561' })).toEqual({ results: [], searchText: '18561', selectedResult: 's/18561', @@ -57,7 +52,7 @@ describe('reducers', () => { describe('SEARCH/INPUT_CHANGE', () => { it('Returns no results when nothing is typed in', () => { - expect(search(undefined, inputChange(''))).toEqual({ + expect(search(undefined, { type: 'SEARCH/INPUT_CHANGE', searchText: '' })).toEqual({ results: [], searchText: '', selectedResult: null, @@ -65,7 +60,7 @@ describe('reducers', () => { }); it('Returns no results when a space is typed in', () => { - expect(search(undefined, inputChange(' '))).toEqual({ + expect(search(undefined, { type: 'SEARCH/INPUT_CHANGE', searchText: ' ' })).toEqual({ results: [], searchText: ' ', selectedResult: null, @@ -73,7 +68,7 @@ describe('reducers', () => { }); it('Preforms a basic search, only returning four results', () => { - expect(search(undefined, inputChange('18'))).toEqual({ + expect(search(undefined, { type: 'SEARCH/INPUT_CHANGE', searchText: '18' })).toEqual({ results: [ 's/18561', 's/18562', @@ -94,8 +89,8 @@ describe('reducers', () => { selectedResult: null, }; - const actionPlus = changeSelectedResult(+1); - const actionMin = changeSelectedResult(-1); + const actionPlus = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 }; + const actionMin = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 }; deepFreeze([prevState, actionPlus, actionMin]); @@ -112,8 +107,8 @@ describe('reducers', () => { selectedResult: null, }; - const actionPlus = changeSelectedResult(+1); - const actionMin = changeSelectedResult(-1); + const actionPlus = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 }; + const actionMin = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 }; deepFreeze([prevState, actionPlus, actionMin]); @@ -137,8 +132,8 @@ describe('reducers', () => { selectedResult: 's/18562', }; - const actionPlus = changeSelectedResult(+1); - const actionMin = changeSelectedResult(-1); + const actionPlus = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 }; + const actionMin = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 }; deepFreeze([prevState, actionPlus, actionMin]); @@ -160,7 +155,7 @@ describe('reducers', () => { results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18563', - }, changeSelectedResult(+1))).toEqual({ + }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 })).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, @@ -170,7 +165,7 @@ describe('reducers', () => { results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - }, changeSelectedResult(+1))).toEqual({ + }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 })).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18561', @@ -182,7 +177,7 @@ describe('reducers', () => { results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18561', - }, changeSelectedResult(-1))).toEqual({ + }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 })).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, @@ -192,7 +187,7 @@ describe('reducers', () => { results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - }, changeSelectedResult(-1))).toEqual({ + }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 })).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18563', -- cgit v1.1