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

121 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-04-03 02:05:49 +01:00
import { List as ImmutableList } from 'immutable'
2020-04-24 04:17:27 +01:00
import { openModal } from '../actions/modal'
import { mentionCompose } from '../actions/compose'
2019-07-02 08:10:25 +01:00
import {
reblog,
2020-03-04 22:26:01 +00:00
favorite,
2019-07-02 08:10:25 +01:00
unreblog,
2020-03-04 22:26:01 +00:00
unfavorite,
2020-04-24 04:17:27 +01:00
} from '../actions/interactions'
2019-07-02 08:10:25 +01:00
import {
hideStatus,
revealStatus,
2020-04-24 04:17:27 +01:00
} from '../actions/statuses'
import { boostModal } from '../initial_state'
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
2020-03-04 03:45:16 +00:00
2020-04-03 02:05:49 +01:00
if (isFollows) {
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)
})
return {
type: 'follow',
accounts: accounts,
createdAt: undefined,
statusId: undefined,
}
} else if (isLikes || isReposts) {
const theType = isLikes ? 'like' : 'repost'
const list = props.notification.get(theType)
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: undefined,
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 {
accounts: !!account ? ImmutableList([account]) : ImmutableList(),
type: notification.get('type'),
createdAt: notification.get('created_at'),
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
2020-04-11 23:29:19 +01:00
const mapDispatchToProps = (dispatch) => ({
2019-07-02 08:10:25 +01:00
onMention: (account, router) => {
2020-03-04 03:45:16 +00:00
dispatch(mentionCompose(account, router))
2019-07-02 08:10:25 +01:00
},
2020-03-04 22:26:01 +00:00
onModalRepost (status) {
dispatch(repost(status))
2019-07-02 08:10:25 +01:00
},
2020-03-04 22:26:01 +00:00
onRepost (status, e) {
2019-07-02 08:10:25 +01:00
if (status.get('reblogged')) {
2020-03-04 22:26:01 +00:00
dispatch(unrepost(status))
2019-07-02 08:10:25 +01:00
} else {
if (e.shiftKey || !boostModal) {
2020-03-04 22:26:01 +00:00
this.onModalRepost(status)
2019-07-02 08:10:25 +01:00
} else {
2020-03-04 22:26:01 +00:00
dispatch(openModal('BOOST', { status, onRepost: this.onModalRepost }))
2019-07-02 08:10:25 +01:00
}
}
},
2020-03-04 22:26:01 +00:00
onFavorite (status) {
2020-04-02 17:57:04 +01:00
if (status.get('favourited')) {
2020-03-04 22:26:01 +00:00
dispatch(unfavorite(status))
2019-07-02 08:10:25 +01:00
} else {
2020-03-04 22:26:01 +00:00
dispatch(favorite(status))
2019-07-02 08:10:25 +01:00
}
},
onToggleHidden (status) {
if (status.get('hidden')) {
2020-03-04 03:45:16 +00:00
dispatch(revealStatus(status.get('id')))
2019-07-02 08:10:25 +01:00
} else {
2020-03-04 03:45:16 +00:00
dispatch(hideStatus(status.get('id')))
2019-07-02 08:10:25 +01:00
}
},
2020-03-04 03:45:16 +00:00
})
2019-07-02 08:10:25 +01:00
2020-03-04 03:45:16 +00:00
export default connect(makeMapStateToProps, mapDispatchToProps)(Notification)