Added response history grouping by time (#1761)

This commit is contained in:
Nilay Majorwar 2019-11-08 03:40:08 +05:30 committed by Gregory Schier
parent 8073797816
commit 49804432a5
2 changed files with 39 additions and 2 deletions

View File

@ -1,6 +1,7 @@
// @flow // @flow
import * as React from 'react'; import * as React from 'react';
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import moment from 'moment';
import { Dropdown, DropdownButton, DropdownDivider, DropdownItem } from '../base/dropdown'; import { Dropdown, DropdownButton, DropdownDivider, DropdownItem } from '../base/dropdown';
import StatusTag from '../tags/status-tag'; import StatusTag from '../tags/status-tag';
import URLTag from '../tags/url-tag'; import URLTag from '../tags/url-tag';
@ -82,6 +83,41 @@ class ResponseHistoryDropdown extends React.PureComponent<Props> {
); );
} }
renderPastResponses(responses: Array<Response>) {
const now = moment();
// Four arrays for four time groups
const categories = [[], [], [], []];
responses.forEach(r => {
const resTime = moment(r.modified);
if (now.diff(resTime, 'minutes') < 5) {
// Five minutes ago
categories[0].push(r);
} else if (now.diff(resTime, 'hours') < 2) {
// Two hours ago
categories[1].push(r);
} else if (now.isSame(resTime, 'day')) {
// Today
categories[2].push(r);
} else if (now.isSame(resTime, 'week')) {
// This week
categories[3].push(r);
}
});
return (
<React.Fragment>
<DropdownDivider>5 Minutes Ago</DropdownDivider>
{categories[0].map(this.renderDropdownItem)}
<DropdownDivider>2 Hours Ago</DropdownDivider>
{categories[1].map(this.renderDropdownItem)}
<DropdownDivider>Today</DropdownDivider>
{categories[2].map(this.renderDropdownItem)}
<DropdownDivider>This Week</DropdownDivider>
{categories[3].map(this.renderDropdownItem)}
</React.Fragment>
);
}
render() { render() {
const { const {
activeResponse, // eslint-disable-line no-unused-vars activeResponse, // eslint-disable-line no-unused-vars
@ -117,8 +153,7 @@ class ResponseHistoryDropdown extends React.PureComponent<Props> {
<i className="fa fa-trash-o" /> <i className="fa fa-trash-o" />
Clear History Clear History
</DropdownItem> </DropdownItem>
<DropdownDivider>Past Responses</DropdownDivider> {this.renderPastResponses(responses)}
{responses.map(this.renderDropdownItem)}
</Dropdown> </Dropdown>
</KeydownBinder> </KeydownBinder>
); );

View File

@ -3,6 +3,8 @@
declare type moment = { declare type moment = {
fromNow: () => string, fromNow: () => string,
format: (fmt: string) => string, format: (fmt: string) => string,
diff: (date: any, fmt?: string, floating?: boolean) => number,
isSame: (date?: any, units?: ?string) => boolean,
}; };
declare module 'moment' { declare module 'moment' {