gab-social/app/javascript/gabsocial/containers/notification_container.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2020-04-03 02:05:49 +01:00
import { List as ImmutableList } from 'immutable'
2020-04-24 04:17:27 +01:00
import { makeGetNotification } from '../selectors'
2020-04-09 20:18:14 +01:00
import Notification from '../components/notification'
2019-07-02 08:10:25 +01:00
2020-04-03 02:05:49 +01:00
const getAccountFromState = (state, accountId) => {
return state.getIn(['accounts', accountId])
}
2019-07-02 08:10:25 +01:00
const makeMapStateToProps = () => {
2020-03-04 03:45:16 +00:00
const getNotification = makeGetNotification()
2019-07-02 08:10:25 +01:00
const mapStateToProps = (state, props) => {
2020-04-03 02:05:49 +01:00
const isFollows = !!props.notification.get('follow')
const isLikes = !!props.notification.get('like')
const isReposts = !!props.notification.get('repost')
const isGrouped = isFollows || isLikes || isReposts
const lastReadId = state.getIn(['notifications', 'lastReadId'])
2020-03-04 03:45:16 +00:00
2020-04-03 02:05:49 +01:00
if (isFollows) {
let lastUpdated
let isUnread
2020-04-03 02:05:49 +01:00
const list = props.notification.get('follow')
let accounts = ImmutableList()
list.forEach((item) => {
const account = getAccountFromState(state, item.get('account'))
accounts = accounts.set(accounts.size, account)
if (!lastUpdated) {
isUnread = parseInt(item.get('id')) > parseInt(lastReadId)
lastUpdated = item.get('created_at')
}
2020-04-03 02:05:49 +01:00
})
return {
type: 'follow',
accounts: accounts,
createdAt: lastUpdated,
isUnread: isUnread,
2020-04-03 02:05:49 +01:00
statusId: undefined,
}
} else if (isLikes || isReposts) {
const theType = isLikes ? 'like' : 'repost'
const list = props.notification.get(theType)
let lastUpdated = list.get('lastUpdated')
let isUnread = list.get('isUnread')
2020-03-04 03:45:16 +00:00
2020-04-03 02:05:49 +01:00
let accounts = ImmutableList()
const accountIdArr = list.get('accounts')
for (let i = 0; i < accountIdArr.length; i++) {
const accountId = accountIdArr[i];
const account = getAccountFromState(state, accountId)
accounts = accounts.set(accounts.size, account)
}
return {
type: theType,
accounts: accounts,
createdAt: lastUpdated,
isUnread: isUnread,
2020-04-03 02:05:49 +01:00
statusId: list.get('status'),
}
} else if (!isGrouped) {
const notification = getNotification(state, props.notification, props.notification.get('account'))
const account = notification.get('account')
const statusId = notification.get('status')
return {
type: notification.get('type'),
accounts: !!account ? ImmutableList([account]) : ImmutableList(),
2020-04-03 02:05:49 +01:00
createdAt: notification.get('created_at'),
isUnread: lastReadId < notification.get('id'),
2020-04-03 02:05:49 +01:00
statusId: statusId || undefined,
}
2020-03-04 03:45:16 +00:00
}
}
2019-07-02 08:10:25 +01:00
2020-03-04 03:45:16 +00:00
return mapStateToProps
}
2019-07-02 08:10:25 +01:00
export default connect(makeMapStateToProps)(Notification)