bigbuffet-rw/app/soapbox/features/list_adder/index.js

106 lines
3 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
2020-03-27 13:59:38 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
2020-03-27 13:59:38 -07:00
import { createSelector } from 'reselect';
import { setupListAdder, resetListAdder } from 'soapbox/actions/lists';
import { CardHeader, CardTitle, Modal } from 'soapbox/components/ui';
2020-03-27 13:59:38 -07:00
import NewListForm from '../lists/components/new_list_form';
2022-01-10 14:01:24 -08:00
import Account from './components/account';
import List from './components/list';
2020-03-27 13:59:38 -07:00
// hack
2020-03-27 13:59:38 -07:00
const getOrderedLists = createSelector([state => state.get('lists')], lists => {
if (!lists) {
return lists;
}
return lists.toList().filter(item => !!item).sort((a, b) => a.get('title').localeCompare(b.get('title')));
});
2020-04-14 11:44:40 -07:00
const mapStateToProps = (state, { accountId }) => ({
2020-03-27 13:59:38 -07:00
listIds: getOrderedLists(state).map(list=>list.get('id')),
account: state.getIn(['accounts', accountId]),
});
const mapDispatchToProps = dispatch => ({
onInitialize: accountId => dispatch(setupListAdder(accountId)),
onReset: () => dispatch(resetListAdder()),
});
const messages = defineMessages({
close: { id: 'lightbox.close', defaultMessage: 'Close' },
subheading: { id: 'lists.subheading', defaultMessage: 'Your lists' },
add: { id: 'lists.new.create', defaultMessage: 'Add List' },
});
export default @connect(mapStateToProps, mapDispatchToProps)
@injectIntl
class ListAdder extends ImmutablePureComponent {
static propTypes = {
accountId: PropTypes.string.isRequired,
onClose: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
onInitialize: PropTypes.func.isRequired,
onReset: PropTypes.func.isRequired,
listIds: ImmutablePropTypes.list.isRequired,
2022-03-23 10:14:42 -07:00
account: ImmutablePropTypes.record,
2020-03-27 13:59:38 -07:00
};
componentDidMount() {
2020-03-27 13:59:38 -07:00
const { onInitialize, accountId } = this.props;
onInitialize(accountId);
}
componentWillUnmount() {
2020-03-27 13:59:38 -07:00
const { onReset } = this.props;
onReset();
}
onClickClose = () => {
this.props.onClose('LIST_ADDER');
};
render() {
2020-04-14 13:45:38 -07:00
const { accountId, listIds, intl } = this.props;
2020-03-27 13:59:38 -07:00
return (
<Modal
title={<FormattedMessage id='list_adder.header_title' defaultMessage='Add or Remove from Lists' />}
onClose={this.onClickClose}
>
2020-03-27 13:59:38 -07:00
<div className='compose-modal__content'>
<div className='list-adder'>
<div className='list-adder__account'>
<Account accountId={accountId} />
</div>
2020-04-14 11:44:40 -07:00
<br />
2020-03-27 13:59:38 -07:00
<CardHeader>
<CardTitle title={intl.formatMessage(messages.add)} />
</CardHeader>
2020-03-27 13:59:38 -07:00
<NewListForm />
2020-04-14 11:44:40 -07:00
<br />
2020-03-27 13:59:38 -07:00
<CardHeader>
<CardTitle title={intl.formatMessage(messages.subheading)} />
</CardHeader>
2020-03-27 13:59:38 -07:00
<div className='list-adder__lists'>
{listIds.map(ListId => <List key={ListId} listId={ListId} />)}
</div>
</div>
</div>
</Modal>
2020-03-27 13:59:38 -07:00
);
}
}