Skip to content

Commit

Permalink
Resolve #27: Fix bug where user could not specify PB Happened On date…
Browse files Browse the repository at this point in the history
… in certain browsers
  • Loading branch information
big213 committed Oct 14, 2021
1 parent a6b8361 commit 155a093
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
handleError,
collapseObject,
capitalizeString,
serializeNestedProperty,
} from '~/services/base'
import CircularLoader from '~/components/common/circularLoader.vue'
Expand Down
52 changes: 45 additions & 7 deletions frontend/models/personalBest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const PersonalBest = <RecordInfo<'personalBest'>>{
text: 'Date Happened',
optional: true,
hint:
'Leave this blank to set to current date. To specify the exact date and time, use format: YYYY-MM-DD 1:23 PM',
'Leave this blank to set to current date. To specify the exact date and time, use format: YYYY-MM-DD 1:23:45 PM',
inputType: 'datepicker',
// default to today.
/* default: () => {
Expand All @@ -193,20 +193,58 @@ export const PersonalBest = <RecordInfo<'personalBest'>>{
serialize: (val: number) =>
val && new Date(val * 1000).toISOString().substring(0, 10),
// YYYY-MM-DD to unix timestamp
parseValue: (val: string) => {
parseValue: (val: string): number => {
// if falsey, default to current unix timestamp
if (!val) return new Date().getTime() / 1000

let dateString = val

// if only date specified, automatically append current time
if (dateString.match(/^\d{4}-\d{2}-\d{2}$/)) {
const currentDate = new Date()
let year, month, day, hours, minutes, seconds

if (
!dateString.match(
/^\d{4}-\d{2}-\d{2}(\s\d{1,2}:\d{2}(:\d{2})?(\s(AM|PM))?)?$/
)
) {
throw new Error('Invalid date format for Date Happened')
}

const dateParts = dateString.split(/-|:|\s/)

// required
year = Number(dateParts[0])
month = Number(dateParts[1]) - 1
day = Number(dateParts[2])

dateString += ` ${currentDate.getHours()}:${currentDate.getMinutes()}:${currentDate.getSeconds()}`
// optional
hours = Number(dateParts[3]) || null
minutes = Number(dateParts[4]) || null
seconds = Number(dateParts[5]) || null

// if PM, add 12 to hours
if (dateParts[6] === 'PM') hours += 12

if (hours > 23) throw new Error('Hours cannot be more than 23')

// if hours missing, automatically append current HH/MM/SS
if (hours === undefined) {
const currentDate = new Date()
hours = currentDate.getHours()
minutes = currentDate.getMinutes()
seconds = currentDate.getSeconds()
}

const msTimestamp = new Date(dateString).getTime()
const msTimestamp = new Date(
year,
month,
day,
hours,
minutes,
seconds
).getTime()

console.log(year, month, day, hours, minutes, seconds)

// date cannot be to far in the future
if (msTimestamp > new Date().getTime() + 1000 * 60 * 60 * 24) {
throw new Error(`Date Happened cannot be in the future`)
Expand Down

0 comments on commit 155a093

Please sign in to comment.