diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-01-28 16:06:56 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-01-28 16:06:56 +0100 |
commit | 3b98d4c4f13424c89a10580065075998d37ae857 (patch) | |
tree | 2dc9917bd6c13770aaf07a1e1231cc11d97ec35a | |
parent | 8670ada517bc8beb69d152c82f282322b9ea8d64 (diff) |
Improve week logic
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/client/react/components/container/WeekSelector.js | 13 | ||||
-rw-r--r-- | src/client/react/components/page/User.js | 6 | ||||
-rw-r--r-- | src/client/react/lib/purifyWeek.js | 9 | ||||
-rw-r--r-- | yarn.lock | 8 |
5 files changed, 20 insertions, 17 deletions
diff --git a/package.json b/package.json index ae4310a..f98c220 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ "react": "^16.2.0", "react-dom": "^16.2.0", "react-icons": "^2.2.7", - "react-moment-proptypes": "^1.5.0", "react-redux": "^5.0.6", "react-router-dom": "^4.2.2", "redux": "^3.7.2", diff --git a/src/client/react/components/container/WeekSelector.js b/src/client/react/components/container/WeekSelector.js index c9174ca..eef8d8d 100644 --- a/src/client/react/components/container/WeekSelector.js +++ b/src/client/react/components/container/WeekSelector.js @@ -1,17 +1,18 @@ import React from 'react'; import PropTypes from 'prop-types'; import moment from 'moment'; -import momentPropTypes from 'react-moment-proptypes'; import queryString from 'query-string'; import { withRouter } from 'react-router-dom'; +import purifyWeek from '../../lib/purifyWeek'; + const WeekSelector = ({ urlWeek, location, history }) => { const updateWeek = (change) => { - const newWeek = moment().week(urlWeek.week() + change); - const isCurrentWeek = moment().week() === newWeek.week(); + const newWeek = purifyWeek(urlWeek + change); + const isCurrentWeek = moment().week() === newWeek; const query = queryString.stringify({ - week: isCurrentWeek ? undefined : newWeek.week(), + week: isCurrentWeek ? undefined : newWeek, }); history.push(`${location.pathname}?${query}`); }; @@ -19,14 +20,14 @@ const WeekSelector = ({ urlWeek, location, history }) => { return ( <div> <button onClick={() => updateWeek(-1)}>Prev</button> - Week {urlWeek.week()} + Week {urlWeek} <button onClick={() => updateWeek(+1)}>Next</button> </div> ); }; WeekSelector.propTypes = { - urlWeek: momentPropTypes.momentObj.isRequired, + urlWeek: PropTypes.number.isRequired, history: PropTypes.shape({ push: PropTypes.func.isRequired, }).isRequired, diff --git a/src/client/react/components/page/User.js b/src/client/react/components/page/User.js index 72e92c2..10d608d 100644 --- a/src/client/react/components/page/User.js +++ b/src/client/react/components/page/User.js @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'; import { Redirect } from 'react-router-dom'; import queryString from 'query-string'; import moment from 'moment'; +import purifyWeek from '../../lib/purifyWeek'; import Search from '../container/Search'; import View from '../container/View'; import users from '../../users'; @@ -11,7 +12,7 @@ import WeekSelector from '../container/WeekSelector'; const App = ({ match, location }) => { const user = `${match.params.type}/${match.params.value}`; const weekStr = queryString.parse(location.search).week; - const week = weekStr ? moment().week(weekStr) : moment(); + const week = purifyWeek(weekStr ? parseInt(weekStr, 10) : moment().week()); if (!users.allIds.includes(user)) { // Invalid user, redirect to index. @@ -26,8 +27,7 @@ const App = ({ match, location }) => { <WeekSelector urlWeek={week} /> </div> </div> - {/* The View object just wants the week number. */} - <View user={user} week={week.week()} /> + <View user={user} week={week} /> </div> ); }; diff --git a/src/client/react/lib/purifyWeek.js b/src/client/react/lib/purifyWeek.js new file mode 100644 index 0000000..939f5af --- /dev/null +++ b/src/client/react/lib/purifyWeek.js @@ -0,0 +1,9 @@ +import moment from 'moment'; + +export default function purifyWeek(week) { + // This ensures that week 0 will become week 52 and that week 53 will become + // week 1. This also accounts for leap years. Because date logic can be so + // complicated we off load it to moment.js so that we can be sure it's bug + // free. + return moment().week(week).week(); +} @@ -4754,7 +4754,7 @@ module-deps@^4.0.8: through2 "^2.0.0" xtend "^4.0.0" -moment@>=1.6.0, moment@^2.20.1: +moment@^2.20.1: version "2.20.1" resolved "https://registry.yarnpkg.com/moment/-/moment-2.20.1.tgz#d6eb1a46cbcc14a2b2f9434112c1ff8907f313fd" @@ -5808,12 +5808,6 @@ react-icons@^2.2.7: dependencies: react-icon-base "2.1.0" -react-moment-proptypes@^1.5.0: - version "1.5.0" - resolved "https://registry.yarnpkg.com/react-moment-proptypes/-/react-moment-proptypes-1.5.0.tgz#4a448cd6479efc5dd509283f361f3753c3abe60e" - dependencies: - moment ">=1.6.0" - react-redux@^5.0.6: version "5.0.6" resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-5.0.6.tgz#23ed3a4f986359d68b5212eaaa681e60d6574946" |