Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add more snooze options (#7344)
- Loading branch information
Showing
8 changed files
with
237 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
getUnixTime, | ||
format, | ||
add, | ||
startOfWeek, | ||
addWeeks, | ||
startOfMonth, | ||
isMonday, | ||
isToday, | ||
setHours, | ||
} from 'date-fns'; | ||
import wootConstants from 'dashboard/constants/globals'; | ||
|
||
const SNOOZE_OPTIONS = wootConstants.SNOOZE_OPTIONS; | ||
|
||
export const findStartOfNextWeek = currentDate => { | ||
const startOfNextWeek = startOfWeek(addWeeks(currentDate, 1)); | ||
return isMonday(startOfNextWeek) | ||
? startOfNextWeek | ||
: add(startOfNextWeek, { | ||
days: (8 - startOfNextWeek.getDay()) % 7, | ||
}); | ||
}; | ||
|
||
export const findStartOfNextMonth = currentDate => { | ||
const startOfNextMonth = startOfMonth(add(currentDate, { months: 1 })); | ||
return isMonday(startOfNextMonth) | ||
? startOfNextMonth | ||
: add(startOfNextMonth, { | ||
days: (8 - startOfNextMonth.getDay()) % 7, | ||
}); | ||
}; | ||
|
||
export const findNextDay = currentDate => { | ||
return add(currentDate, { days: 1 }); | ||
}; | ||
|
||
export const setHoursToNine = date => { | ||
return setHours(date, 9, 0, 0); | ||
}; | ||
|
||
export const findSnoozeTime = (snoozeType, currentDate = new Date()) => { | ||
let parsedDate = null; | ||
if (snoozeType === SNOOZE_OPTIONS.AN_HOUR_FROM_NOW) { | ||
parsedDate = add(currentDate, { hours: 1 }); | ||
} else if (snoozeType === SNOOZE_OPTIONS.UNTIL_TOMORROW) { | ||
parsedDate = setHoursToNine(findNextDay(currentDate)); | ||
} else if (snoozeType === SNOOZE_OPTIONS.UNTIL_NEXT_WEEK) { | ||
parsedDate = setHoursToNine(findStartOfNextWeek(currentDate)); | ||
} else if (snoozeType === SNOOZE_OPTIONS.UNTIL_NEXT_MONTH) { | ||
parsedDate = setHoursToNine(findStartOfNextMonth(currentDate)); | ||
} | ||
|
||
return parsedDate ? getUnixTime(parsedDate) : null; | ||
}; | ||
export const conversationReopenTime = snoozedUntil => { | ||
if (!snoozedUntil) { | ||
return null; | ||
} | ||
const date = new Date(snoozedUntil); | ||
|
||
if (isToday(date)) { | ||
return format(date, 'h.mmaaa'); | ||
} | ||
return snoozedUntil ? format(date, 'd MMM, h.mmaaa') : null; | ||
}; |
105 changes: 105 additions & 0 deletions
105
app/javascript/dashboard/helper/specs/snoozeHelpers.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
findSnoozeTime, | ||
conversationReopenTime, | ||
findStartOfNextWeek, | ||
findStartOfNextMonth, | ||
findNextDay, | ||
setHoursToNine, | ||
} from '../snoozeHelpers'; | ||
|
||
describe('#Snooze Helpers', () => { | ||
describe('findStartOfNextWeek', () => { | ||
it('should return first working day of next week if a date is passed', () => { | ||
const today = new Date('06/16/2023'); | ||
const startOfNextWeek = new Date('06/19/2023'); | ||
expect(findStartOfNextWeek(today)).toEqual(startOfNextWeek); | ||
}); | ||
it('should return first working day of next week if a date is passed', () => { | ||
const today = new Date('06/03/2023'); | ||
const startOfNextWeek = new Date('06/05/2023'); | ||
expect(findStartOfNextWeek(today)).toEqual(startOfNextWeek); | ||
}); | ||
}); | ||
|
||
describe('findStartOfNextMonth', () => { | ||
it('should return first working day of next month if a valid date is passed', () => { | ||
const today = new Date('06/21/2023'); | ||
const startOfNextMonth = new Date('07/03/2023'); | ||
expect(findStartOfNextMonth(today)).toEqual(startOfNextMonth); | ||
}); | ||
it('should return first working day of next month if a valid date is passed', () => { | ||
const today = new Date('02/28/2023'); | ||
const startOfNextMonth = new Date('03/06/2023'); | ||
expect(findStartOfNextMonth(today)).toEqual(startOfNextMonth); | ||
}); | ||
}); | ||
|
||
describe('setHoursToNine', () => { | ||
it('should return date with 9.00AM time', () => { | ||
const nextDay = new Date('06/17/2023'); | ||
nextDay.setHours(9, 0, 0, 0); | ||
expect(setHoursToNine(nextDay)).toEqual(nextDay); | ||
}); | ||
}); | ||
|
||
describe('findSnoozeTime', () => { | ||
it('should return nil if until_next_reply is passed', () => { | ||
expect(findSnoozeTime('until_next_reply')).toEqual(null); | ||
}); | ||
|
||
it('should return next hour time stamp if an_hour_from_now is passed', () => { | ||
const nextHour = new Date(); | ||
nextHour.setHours(nextHour.getHours() + 1); | ||
expect(findSnoozeTime('an_hour_from_now')).toBeCloseTo( | ||
Math.floor(nextHour.getTime() / 1000) | ||
); | ||
}); | ||
|
||
it('should return next day 9.00AM time stamp until_tomorrow is passed', () => { | ||
const today = new Date('06/16/2023'); | ||
const nextDay = new Date('06/17/2023'); | ||
nextDay.setHours(9, 0, 0, 0); | ||
expect(findSnoozeTime('until_tomorrow', today)).toBeCloseTo( | ||
nextDay.getTime() / 1000 | ||
); | ||
}); | ||
|
||
it('should return next week monday 9.00AM time stamp if until_next_week is passed', () => { | ||
const today = new Date('06/16/2023'); | ||
const startOfNextWeek = new Date('06/19/2023'); | ||
startOfNextWeek.setHours(9, 0, 0, 0); | ||
expect(findSnoozeTime('until_next_week', today)).toBeCloseTo( | ||
startOfNextWeek.getTime() / 1000 | ||
); | ||
}); | ||
|
||
it('should return next month 9.00AM time stamp if until_next_month is passed', () => { | ||
const today = new Date('06/21/2023'); | ||
const startOfNextMonth = new Date('07/03/2023'); | ||
startOfNextMonth.setHours(9, 0, 0, 0); | ||
expect(findSnoozeTime('until_next_month', today)).toBeCloseTo( | ||
startOfNextMonth.getTime() / 1000 | ||
); | ||
}); | ||
}); | ||
|
||
describe('conversationReopenTime', () => { | ||
it('should return nil if snoozedUntil is nil', () => { | ||
expect(conversationReopenTime(null)).toEqual(null); | ||
}); | ||
|
||
it('should return formatted date if snoozedUntil is not nil', () => { | ||
expect(conversationReopenTime('2023-06-07T09:00:00.000Z')).toEqual( | ||
'7 Jun, 9.00am' | ||
); | ||
}); | ||
}); | ||
|
||
describe('findNextDay', () => { | ||
it('should return next day', () => { | ||
const today = new Date('06/16/2023'); | ||
const nextDay = new Date('06/17/2023'); | ||
expect(findNextDay(today)).toEqual(nextDay); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
app/javascript/dashboard/routes/dashboard/commands/CommandBarIcons.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters