Simplify withSection
This commit is contained in:
parent
d98d08a7e0
commit
f99e6306c3
1 changed files with 11 additions and 23 deletions
|
@ -25,21 +25,10 @@ export const groupMessagesByDate = (
|
||||||
messages: Array<Message>
|
messages: Array<Message>
|
||||||
): Array<Section> => {
|
): Array<Section> => {
|
||||||
const referenceDateTime = moment.utc(timestamp);
|
const referenceDateTime = moment.utc(timestamp);
|
||||||
const today = moment(referenceDateTime).startOf('day');
|
|
||||||
const yesterday = moment(referenceDateTime)
|
|
||||||
.subtract(1, 'day')
|
|
||||||
.startOf('day');
|
|
||||||
const thisWeek = moment(referenceDateTime).startOf('isoWeek');
|
|
||||||
const thisMonth = moment(referenceDateTime).startOf('month');
|
|
||||||
|
|
||||||
const sortedMessages = sortBy(messages, message => -message.received_at);
|
const sortedMessages = sortBy(messages, message => -message.received_at);
|
||||||
const messagesWithSection = sortedMessages.map(
|
const messagesWithSection = sortedMessages.map(
|
||||||
withSection({
|
withSection(referenceDateTime)
|
||||||
today,
|
|
||||||
yesterday,
|
|
||||||
thisWeek,
|
|
||||||
thisMonth,
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
const groupedMessages = groupBy(messagesWithSection, 'type');
|
const groupedMessages = groupBy(messagesWithSection, 'type');
|
||||||
const yearMonthMessages = Object.values(
|
const yearMonthMessages = Object.values(
|
||||||
|
@ -110,17 +99,16 @@ type MessageWithSection =
|
||||||
| MessageWithStaticSection
|
| MessageWithStaticSection
|
||||||
| MessageWithYearMonthSection;
|
| MessageWithYearMonthSection;
|
||||||
|
|
||||||
const withSection = ({
|
const withSection = (referenceDateTime: moment.Moment) => (
|
||||||
today,
|
message: Message
|
||||||
yesterday,
|
): MessageWithSection => {
|
||||||
thisWeek,
|
const today = moment(referenceDateTime).startOf('day');
|
||||||
thisMonth,
|
const yesterday = moment(referenceDateTime)
|
||||||
}: {
|
.subtract(1, 'day')
|
||||||
today: moment.Moment;
|
.startOf('day');
|
||||||
yesterday: moment.Moment;
|
const thisWeek = moment(referenceDateTime).startOf('isoWeek');
|
||||||
thisWeek: moment.Moment;
|
const thisMonth = moment(referenceDateTime).startOf('month');
|
||||||
thisMonth: moment.Moment;
|
|
||||||
}) => (message: Message): MessageWithSection => {
|
|
||||||
const messageReceivedDate = moment.utc(message.received_at);
|
const messageReceivedDate = moment.utc(message.received_at);
|
||||||
if (messageReceivedDate.isAfter(today)) {
|
if (messageReceivedDate.isAfter(today)) {
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue