-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(WebexActivity): move activity header into its own file
- Loading branch information
1 parent
c5f10be
commit fcb30a7
Showing
9 changed files
with
199 additions
and
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {format, isToday, isSameWeek, isYesterday} from 'date-fns'; | ||
|
||
import WebexAvatar from '../WebexAvatar/WebexAvatar'; | ||
import {usePerson} from '../hooks'; | ||
|
||
import './ActivityHeader.scss'; | ||
|
||
/** | ||
* Returns a formatted timestamp based on the given date's offset from the current time. | ||
* | ||
* @param {Date} timestamp Date instance to format | ||
* @returns {string} formattedDate | ||
*/ | ||
export function formatMessageDate(timestamp) { | ||
let formattedDate; | ||
|
||
if (isToday(timestamp)) { | ||
// 12:00 PM | ||
formattedDate = format(timestamp, 'p'); | ||
} else if (isYesterday(timestamp)) { | ||
// Yesterday, 12:00 PM | ||
formattedDate = `Yesterday, ${format(timestamp, 'p')}`; | ||
} else if (isSameWeek(timestamp, new Date())) { | ||
// Monday, 12:00 PM | ||
formattedDate = format(timestamp, 'iiii, p'); | ||
} else { | ||
// 1/1/2020, 12:00 PM | ||
formattedDate = format(timestamp, 'P, p'); | ||
} | ||
|
||
return formattedDate; | ||
} | ||
|
||
/** | ||
* ActivityHeader component displays the header content of an activity. | ||
* Header includes the avatar of the activity author as well as the | ||
* activity timestamp. | ||
* | ||
* @param {object} props | ||
* @returns {object} JSX of the component | ||
*/ | ||
export default function ActivityHeader({personID, timestamp}) { | ||
const {displayName} = usePerson(personID); | ||
|
||
return ( | ||
<div className="activity-header"> | ||
<WebexAvatar personID={personID} /> | ||
<div className="activity-author"> | ||
<span>{displayName}</span> | ||
<span className="activity-timestamp">{formatMessageDate(new Date(timestamp))}</span> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
ActivityHeader.propTypes = { | ||
personID: PropTypes.string.isRequired, | ||
timestamp: PropTypes.string.isRequired, | ||
}; |
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,28 @@ | ||
$content-left-margin: 3.5rem; //avatar width + margin | ||
|
||
.activity-header { | ||
display: flex; | ||
flex-direction: column; | ||
position: relative; | ||
|
||
.activity-author { | ||
@include noselect(); | ||
display: flex; | ||
min-width: 18.75rem !important; | ||
margin-left: $content-left-margin; | ||
font-size: 0.875rem; | ||
line-height: 1.375rem; | ||
text-align: left; | ||
color: $md-gray-70; | ||
word-wrap: break-word; | ||
} | ||
|
||
.md-avatar { | ||
position: absolute; | ||
margin: 0 0.5rem; | ||
} | ||
|
||
.activity-timestamp { | ||
margin-left: 0.5rem; | ||
} | ||
} |
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,56 @@ | ||
import React from 'react'; | ||
import {addDays, addHours, startOfToday, startOfWeek, startOfYesterday, subDays} from 'date-fns'; | ||
|
||
import ActivityHeader, {formatMessageDate} from './ActivityHeader'; | ||
|
||
jest.mock('../hooks/usePerson'); | ||
|
||
describe('Activity Header', () => { | ||
let today, sunday; | ||
|
||
beforeEach(() => { | ||
today = startOfToday(); // Mock date is set to Thu, Aug 1 2019 00:00 | ||
sunday = startOfWeek(today); // Start of week is Sun, July 28 2019 00:00 | ||
}); | ||
|
||
describe('component snapshot', () => { | ||
test('matches header at 3:00 PM', () => { | ||
const headerComponent = <ActivityHeader personID="default" timestamp={addHours(today, 15).toString()} />; | ||
|
||
expect(shallow(headerComponent)).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('formatMessageDate() returns', () => { | ||
test('AM/PM time from today date', () => { | ||
const nineAM = addHours(today, 9); | ||
|
||
expect(formatMessageDate(nineAM)).toEqual('9:00 AM'); | ||
}); | ||
|
||
test('"Yesterday" and AM/PM time from yesterday date', () => { | ||
const yesterdayOnePM = addHours(startOfYesterday(), 13); | ||
|
||
expect(formatMessageDate(yesterdayOnePM)).toEqual('Yesterday, 1:00 PM'); | ||
}); | ||
|
||
test('day name and AM/PM time from a date of this week', () => { | ||
const monday = addDays(sunday, 1); | ||
const mondayTenAM = addHours(monday, 10); | ||
|
||
expect(formatMessageDate(mondayTenAM)).toEqual('Monday, 10:00 AM'); | ||
}); | ||
|
||
test('formatted date for dates older than a week', () => { | ||
const lastFriday = subDays(sunday, 2); | ||
const lastFridayNoon = addHours(lastFriday, 12); | ||
|
||
expect(formatMessageDate(lastFridayNoon)).toEqual('07/26/2019, 12:00 PM'); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
today = null; | ||
sunday = null; | ||
}); | ||
}); |
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
23 changes: 23 additions & 0 deletions
23
src/components/WebexActivity/__snapshots__/ActivityHeader.test.js.snap
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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Activity Header component snapshot matches header at 3:00 PM 1`] = ` | ||
<div | ||
className="activity-header" | ||
> | ||
<WebexAvatar | ||
personID="default" | ||
/> | ||
<div | ||
className="activity-author" | ||
> | ||
<span> | ||
Webex Component User | ||
</span> | ||
<span | ||
className="activity-timestamp" | ||
> | ||
3:00 PM | ||
</span> | ||
</div> | ||
</div> | ||
`; |
Oops, something went wrong.