Last active
October 22, 2016 15:03
-
-
Save bnhansn/0e29f15c55e62ce84aff31653be895f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// @flow | |
import React, { Component } from 'react'; | |
import moment from 'moment'; | |
import groupBy from 'lodash/groupBy'; | |
import mapKeys from 'lodash/mapKeys'; | |
import { css, StyleSheet } from 'aphrodite'; | |
import Message from '../Message'; | |
const styles = StyleSheet.create({ | |
container: { | |
flex: '1', | |
padding: '10px 10px 0 10px', | |
background: '#fff', | |
overflowY: 'auto', | |
}, | |
dayDivider: { | |
position: 'relative', | |
margin: '1rem 0', | |
textAlign: 'center', | |
'::after': { | |
position: 'absolute', | |
top: '50%', | |
right: '0', | |
left: '0', | |
height: '1px', | |
background: 'rgb(240,240,240)', | |
content: '""', | |
}, | |
}, | |
dayText: { | |
zIndex: '1', | |
position: 'relative', | |
background: '#fff', | |
padding: '0 12px', | |
}, | |
}); | |
type MessageType = { | |
id: number, | |
inserted_at: string, | |
} | |
type Props = { | |
messages: Array<MessageType>, | |
} | |
class MessageList extends Component { | |
props: Props | |
renderMessages = messages => | |
messages.map(message => <Message key={message.id} message={message} />); | |
renderDays() { | |
const { messages } = this.props; | |
messages.map(message => message.day = moment(message.inserted_at).format('MMMM Do')); // eslint-disable-line | |
const dayGroups = groupBy(messages, 'day'); | |
const days = []; | |
mapKeys(dayGroups, (value, key) => { | |
days.push({ date: key, messages: value }); | |
}); | |
const today = moment().format('MMMM Do'); | |
const yesterday = moment().subtract(1, 'days').format('MMMM Do'); | |
return days.map(day => | |
<div key={day.date}> | |
<div className={css(styles.dayDivider)}> | |
<span className={css(styles.dayText)}> | |
{day.date === today && 'Today'} | |
{day.date === yesterday && 'Yesterday'} | |
{![today, yesterday].includes(day.date) && day.date} | |
</span> | |
</div> | |
{this.renderMessages(day.messages)} | |
</div> | |
); | |
} | |
render() { | |
return ( | |
<div className={css(styles.container)}> | |
{this.renderDays()} | |
</div> | |
); | |
} | |
} | |
export default MessageList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment