gab-social/app/javascript/gabsocial/features/bookmark_collections.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-12-16 00:31:30 +00:00
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
2020-12-16 07:39:07 +00:00
import {
MODAL_BOOKMARK_COLLECTION_CREATE,
} from '../constants'
import {
meUsername,
} from '../initial_state'
2020-12-16 00:31:30 +00:00
import { fetchBookmarkCollections } from '../actions/bookmarks'
2020-12-16 07:39:07 +00:00
import { openModal } from '../actions/modal'
2020-12-16 00:31:30 +00:00
import ColumnIndicator from '../components/column_indicator'
2020-12-16 07:39:07 +00:00
import Block from '../components/block'
import Button from '../components/button'
import Text from '../components/text'
2020-12-16 00:31:30 +00:00
import List from '../components/list'
class BookmarkCollections extends ImmutablePureComponent {
componentDidMount() {
this.props.onFetchBookmarkCollections()
}
2020-12-16 07:39:07 +00:00
handleOpenModal = () => {
this.props.onOpenModal()
}
2020-12-16 00:31:30 +00:00
render() {
const {
isMyAccount,
2020-12-16 00:31:30 +00:00
isLoading,
isError,
bookmarkCollections,
} = this.props
if (!isMyAccount) {
return <ColumnIndicator type='missing' />
}
2020-12-16 00:31:30 +00:00
if (isError) {
return <ColumnIndicator type='error' message='Error fetching bookmark collections' />
}
console.log("bookmarkCollections:", bookmarkCollections)
let listItems = !!bookmarkCollections ? bookmarkCollections.map((b) => ({
to: `/${meUsername}/bookmark_collections/${b.get('id')}`,
title: b.get('title'),
})) : []
listItems = listItems.unshift({
to: `/${meUsername}/bookmark_collections/saved`,
title: 'Bookmarks',
})
console.log("listItems:", listItems)
2020-12-16 00:31:30 +00:00
return (
2020-12-16 07:39:07 +00:00
<Block>
<div className={[_s.d, _s.px15, _s.py10].join(' ')}>
<div className={[_s.d, _s.flexRow, _s.aiCenter].join(' ')}>
<Text size='extraLarge' weight='bold'>Bookmark Collections</Text>
<Button
className={[_s.px10, _s.mlAuto].join(' ')}
onClick={this.handleOpenModal}
backgroundColor='tertiary'
color='tertiary'
icon='add'
/>
</div>
</div>
<List
scrollKey='bookmark-collections'
emptyMessage='You have no bookmark collections'
items={listItems}
showLoading={isLoading}
/>
</Block>
2020-12-16 00:31:30 +00:00
)
}
}
const mapStateToProps = (state, { params: { username } }) => ({
isMyAccount: (username.toLowerCase() === meUsername.toLowerCase()),
2020-12-16 00:31:30 +00:00
isError: state.getIn(['bookmark_collections', 'isError']),
isLoading: state.getIn(['bookmark_collections', 'isLoading']),
bookmarkCollections: state.getIn(['bookmark_collections', 'items']),
2020-12-16 00:31:30 +00:00
})
const mapDispatchToProps = (dispatch) => ({
2020-12-16 07:39:07 +00:00
onOpenModal() {
dispatch(openModal(MODAL_BOOKMARK_COLLECTION_CREATE))
},
2020-12-16 00:31:30 +00:00
onFetchBookmarkCollections() {
dispatch(fetchBookmarkCollections())
},
})
BookmarkCollections.propTypes = {
isLoading: PropTypes.bool.isRequired,
isError: PropTypes.bool.isRequired,
onFetchBookmarkCollections: PropTypes.func.isRequired,
2020-12-16 07:39:07 +00:00
onOpenModal: PropTypes.func.isRequired,
2020-12-16 00:31:30 +00:00
bookmarkCollections: ImmutablePropTypes.list,
}
export default connect(mapStateToProps, mapDispatchToProps)(BookmarkCollections)