From f1aa8b7d3b14d2224b713a426f5183c309e4fec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=BA=86=E4=BC=9F?= Date: Fri, 25 Aug 2017 13:09:50 +0800 Subject: [PATCH 1/5] Fix issue #212 --- .../components/challenge-listing/index.jsx | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/shared/components/challenge-listing/index.jsx b/src/shared/components/challenge-listing/index.jsx index 3943de34d4..bce829d022 100755 --- a/src/shared/components/challenge-listing/index.jsx +++ b/src/shared/components/challenge-listing/index.jsx @@ -22,6 +22,29 @@ import './style.scss'; const CHALLENGE_PLACEHOLDER_COUNT = 8; export default function ChallengeListing(props) { + function CheckDateTime(str) { + const reg = /^(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{3})Z$/; + const r = str.match(reg); + if (r == null) return false; + r[2] -= 1; + const d = new Date(r[1], r[2], r[3], r[4], r[5], r[6]); + if (d.getFullYear() !== r[1]) return false; + if (d.getMonth() !== r[2]) return false; + if (d.getDate() !== r[3]) return false; + if (d.getHours() !== r[4]) return false; + if (d.getMinutes() !== r[5]) return false; + if (d.getSeconds() !== r[6]) return false; + return true; + } + const filterState = props.filterState; + if (filterState) { + if (!!filterState.startDate && CheckDateTime(filterState.startDate) === false) { + delete filterState.startDate; + } + if (!!filterState.endDate && CheckDateTime(filterState.endDate) === false) { + delete filterState.endDate; + } + } let challenges = props.challenges; if (props.communityFilter) { From dad8bfd57ff379fd76ec62ecab910594c667644d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=BA=86=E4=BC=9F?= Date: Mon, 28 Aug 2017 13:14:08 +0800 Subject: [PATCH 2/5] update issue 212's fix logic as suggested --- .../components/challenge-listing/index.jsx | 25 ++----------------- .../reducers/challenge-listing/index.js | 7 ++++++ src/shared/utils/url.js | 6 +++++ 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/shared/components/challenge-listing/index.jsx b/src/shared/components/challenge-listing/index.jsx index bce829d022..1989c3e551 100755 --- a/src/shared/components/challenge-listing/index.jsx +++ b/src/shared/components/challenge-listing/index.jsx @@ -16,35 +16,14 @@ import Listing from './Listing'; import ChallengeCardPlaceholder from './placeholders/ChallengeCard'; import SRMCard from './SRMCard'; +import { updateQuery } from 'utils/url'; import './style.scss'; // Number of challenge placeholder card to display const CHALLENGE_PLACEHOLDER_COUNT = 8; export default function ChallengeListing(props) { - function CheckDateTime(str) { - const reg = /^(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{3})Z$/; - const r = str.match(reg); - if (r == null) return false; - r[2] -= 1; - const d = new Date(r[1], r[2], r[3], r[4], r[5], r[6]); - if (d.getFullYear() !== r[1]) return false; - if (d.getMonth() !== r[2]) return false; - if (d.getDate() !== r[3]) return false; - if (d.getHours() !== r[4]) return false; - if (d.getMinutes() !== r[5]) return false; - if (d.getSeconds() !== r[6]) return false; - return true; - } - const filterState = props.filterState; - if (filterState) { - if (!!filterState.startDate && CheckDateTime(filterState.startDate) === false) { - delete filterState.startDate; - } - if (!!filterState.endDate && CheckDateTime(filterState.endDate) === false) { - delete filterState.endDate; - } - } + updateQuery({ }); let challenges = props.challenges; if (props.communityFilter) { diff --git a/src/shared/reducers/challenge-listing/index.js b/src/shared/reducers/challenge-listing/index.js index 18b762adb2..f67d048dc8 100644 --- a/src/shared/reducers/challenge-listing/index.js +++ b/src/shared/reducers/challenge-listing/index.js @@ -8,6 +8,7 @@ import logger from 'utils/logger'; import { handleActions } from 'redux-actions'; import { combine, resolveReducers } from 'utils/redux'; import { updateQuery } from 'utils/url'; +import moment from 'moment'; import filterPanel from '../challenge-listing/filter-panel'; import sidebar, { factory as sidebarFactory } from '../challenge-listing/sidebar'; @@ -270,6 +271,12 @@ export function factory(req) { if (req) { state.filter = req.query.filter; + if (!!state.filter.startDate && moment(state.filter.startDate).isValid() === false) { + delete state.filter.startDate; + } + if (!!state.filter.endDate && moment(state.filter.endDate).isValid() === false) { + delete state.filter.endDate; + } state.selectedCommunityId = req.query.communityId; } diff --git a/src/shared/utils/url.js b/src/shared/utils/url.js index c5717aca23..fc3512d691 100644 --- a/src/shared/utils/url.js +++ b/src/shared/utils/url.js @@ -6,6 +6,7 @@ import _ from 'lodash'; import qs from 'qs'; +import moment from 'moment'; /** * If executed client-side (determined in this case by the presence of global @@ -28,6 +29,11 @@ export function updateQuery(update) { if (_.isUndefined(value)) delete query[key]; else query[key] = value; }); + if (!!query.filter.startDate && moment(query.filter.startDate).isValid() === false) { + delete query.filter.startDate; console.log('delete start date'); + } else if (query.filter.endDate && moment(query.filter.endDate).isValid() === false) { + delete query.filter.endDate; console.log('delete end date'); + } query = `?${qs.stringify(query, { encode: false })}`; window.history.replaceState(window.history.state, '', query); From 336aa771ab33ed3677675cf6b6a0c33e1d5bafbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=BA=86=E4=BC=9F?= Date: Mon, 28 Aug 2017 13:23:03 +0800 Subject: [PATCH 3/5] remove useless log output --- src/shared/utils/url.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/utils/url.js b/src/shared/utils/url.js index fc3512d691..23cae1f94a 100644 --- a/src/shared/utils/url.js +++ b/src/shared/utils/url.js @@ -30,9 +30,9 @@ export function updateQuery(update) { else query[key] = value; }); if (!!query.filter.startDate && moment(query.filter.startDate).isValid() === false) { - delete query.filter.startDate; console.log('delete start date'); + delete query.filter.startDate; } else if (query.filter.endDate && moment(query.filter.endDate).isValid() === false) { - delete query.filter.endDate; console.log('delete end date'); + delete query.filter.endDate; } query = `?${qs.stringify(query, { encode: false })}`; From b50589ccea6fd324374a351f86b181b815188d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=BA=86=E4=BC=9F?= Date: Mon, 28 Aug 2017 14:28:05 +0800 Subject: [PATCH 4/5] Fix failed test scenario --- src/shared/components/challenge-listing/index.jsx | 2 -- src/shared/reducers/challenge-listing/index.js | 6 ++++-- src/shared/routes/Topcoder/ChallengeListing.jsx | 2 ++ src/shared/utils/url.js | 6 ++++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/shared/components/challenge-listing/index.jsx b/src/shared/components/challenge-listing/index.jsx index 1989c3e551..3943de34d4 100755 --- a/src/shared/components/challenge-listing/index.jsx +++ b/src/shared/components/challenge-listing/index.jsx @@ -16,14 +16,12 @@ import Listing from './Listing'; import ChallengeCardPlaceholder from './placeholders/ChallengeCard'; import SRMCard from './SRMCard'; -import { updateQuery } from 'utils/url'; import './style.scss'; // Number of challenge placeholder card to display const CHALLENGE_PLACEHOLDER_COUNT = 8; export default function ChallengeListing(props) { - updateQuery({ }); let challenges = props.challenges; if (props.communityFilter) { diff --git a/src/shared/reducers/challenge-listing/index.js b/src/shared/reducers/challenge-listing/index.js index f67d048dc8..2843ca70ff 100644 --- a/src/shared/reducers/challenge-listing/index.js +++ b/src/shared/reducers/challenge-listing/index.js @@ -271,10 +271,12 @@ export function factory(req) { if (req) { state.filter = req.query.filter; - if (!!state.filter.startDate && moment(state.filter.startDate).isValid() === false) { + if (!!state.filter && !!state.filter.startDate + && moment(state.filter.startDate).isValid() === false) { delete state.filter.startDate; } - if (!!state.filter.endDate && moment(state.filter.endDate).isValid() === false) { + if (!!state.filter && !!state.filter.endDate + && moment(state.filter.endDate).isValid() === false) { delete state.filter.endDate; } state.selectedCommunityId = req.query.communityId; diff --git a/src/shared/routes/Topcoder/ChallengeListing.jsx b/src/shared/routes/Topcoder/ChallengeListing.jsx index d44eb668fc..d792893192 100644 --- a/src/shared/routes/Topcoder/ChallengeListing.jsx +++ b/src/shared/routes/Topcoder/ChallengeListing.jsx @@ -8,6 +8,7 @@ import LoadingIndicator from 'components/LoadingIndicator'; import qs from 'qs'; import React from 'react'; import { SplitRoute } from 'utils/router'; +import { updateQuery } from 'utils/url'; export default function ChallengeListingRoute() { return ( @@ -19,6 +20,7 @@ export default function ChallengeListingRoute() { /* webpackChunkName: "challenge-listing" */ 'containers/challenge-listing/Listing', ).then(({ default: ChallengeListing }) => { + updateQuery({ }); const query = renderProps.location.search ? qs.parse(renderProps.location.search.slice(1)) : null; const currencyFromUrl = _.get(query, 'currency'); diff --git a/src/shared/utils/url.js b/src/shared/utils/url.js index 23cae1f94a..72aac6ae4f 100644 --- a/src/shared/utils/url.js +++ b/src/shared/utils/url.js @@ -29,9 +29,11 @@ export function updateQuery(update) { if (_.isUndefined(value)) delete query[key]; else query[key] = value; }); - if (!!query.filter.startDate && moment(query.filter.startDate).isValid() === false) { + if (!!query.filter && !!query.filter.startDate + && moment(query.filter.startDate).isValid() === false) { delete query.filter.startDate; - } else if (query.filter.endDate && moment(query.filter.endDate).isValid() === false) { + } else if (!!query.filter && !!query.filter.endDate + && moment(query.filter.endDate).isValid() === false) { delete query.filter.endDate; } From 2c292d5bb812bd245f21361a4fe9b0530bf8f4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=BA=86=E4=BC=9F?= Date: Wed, 30 Aug 2017 11:28:46 +0800 Subject: [PATCH 5/5] update the date time check logic as suggested --- src/shared/routes/Topcoder/ChallengeListing.jsx | 11 ++++++++++- src/shared/utils/url.js | 9 --------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/shared/routes/Topcoder/ChallengeListing.jsx b/src/shared/routes/Topcoder/ChallengeListing.jsx index d792893192..6ba908ee88 100644 --- a/src/shared/routes/Topcoder/ChallengeListing.jsx +++ b/src/shared/routes/Topcoder/ChallengeListing.jsx @@ -7,6 +7,7 @@ import _ from 'lodash'; import LoadingIndicator from 'components/LoadingIndicator'; import qs from 'qs'; import React from 'react'; +import moment from 'moment'; import { SplitRoute } from 'utils/router'; import { updateQuery } from 'utils/url'; @@ -20,9 +21,17 @@ export default function ChallengeListingRoute() { /* webpackChunkName: "challenge-listing" */ 'containers/challenge-listing/Listing', ).then(({ default: ChallengeListing }) => { - updateQuery({ }); const query = renderProps.location.search ? qs.parse(renderProps.location.search.slice(1)) : null; + if (query.filter && query.filter.startDate + && !moment(query.filter.startDate).isValid()) { + delete query.filter.startDate; + } + if (query.filter && query.filter.endDate + && !moment(query.filter.endDate).isValid()) { + delete query.filter.endDate; + } + updateQuery({ filter: query.filter }); const currencyFromUrl = _.get(query, 'currency'); const prizeMode = currencyFromUrl && `money-${currencyFromUrl}`; return ( diff --git a/src/shared/utils/url.js b/src/shared/utils/url.js index 72aac6ae4f..ca48322c08 100644 --- a/src/shared/utils/url.js +++ b/src/shared/utils/url.js @@ -6,7 +6,6 @@ import _ from 'lodash'; import qs from 'qs'; -import moment from 'moment'; /** * If executed client-side (determined in this case by the presence of global @@ -29,14 +28,6 @@ export function updateQuery(update) { if (_.isUndefined(value)) delete query[key]; else query[key] = value; }); - if (!!query.filter && !!query.filter.startDate - && moment(query.filter.startDate).isValid() === false) { - delete query.filter.startDate; - } else if (!!query.filter && !!query.filter.endDate - && moment(query.filter.endDate).isValid() === false) { - delete query.filter.endDate; - } - query = `?${qs.stringify(query, { encode: false })}`; window.history.replaceState(window.history.state, '', query); }