import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import ImmutablePropTypes from 'react-immutable-proptypes' import ImmutablePureComponent from 'react-immutable-pure-component' import { injectIntl, defineMessages } from 'react-intl' import isObject from 'lodash.isobject' import { setupListEditor, resetListEditor, removeFromListEditor, addToListEditor, fetchListSuggestions, clearListSuggestions, changeListSuggestions, } from '../actions/lists' import { openModal } from '../actions/modal' import { MODAL_LIST_EDITOR, MODAL_LIST_DELETE, } from '../constants' import Account from '../components/account' import Button from '../components/button' import Divider from '../components/divider' import Input from '../components/input' import TabBar from '../components/tab_bar' import Text from '../components/text' class ListEdit extends ImmutablePureComponent { state = { activeTab: this.props.tab || 'members' } componentDidMount() { const { onInitialize, listId } = this.props if (listId) { onInitialize(listId) } } componentDidUpdate(prevProps) { if (this.props.listId !== prevProps.listId) { this.props.onInitialize(this.props.listId) } } componentWillUnmount() { this.props.onReset() } handleChangeTab = (tab) => { this.setState({ activeTab: tab }) } onClickClose = () => { this.props.onClose(MODAL_LIST_EDITOR) } handleOnDeleteList = () => { this.props.onDeleteList(this.props.list) } handleAddOrRemoveFromList = (accountId) => { if (this.props.accountIds.includes(accountId)) { this.props.onRemoveAccountFromList(accountId) } else { this.props.onAddAccountToList(accountId) } } handleSearchSuggestionsChange = (value) => { this.props.onChangeSuggestions(value) } handleSearchSuggestionsKeyUp = (e) => { if (e.keyCode === 13) { this.props.onSubmitSearchSuggestions(this.props.searchSuggestionsValue) } } handleSearchSuggestionsSubmit = () => { this.props.onSubmitSearchSuggestions(this.props.searchSuggestionsValue) } render() { const { title, accountIds, searchAccountIds, intl, searchSuggestionsValue, } = this.props const { activeTab } = this.state // : todo : save new list title return (
this.handleChangeTab('members'), active: activeTab === 'members', }, { title: 'Add new', onClick: () => this.handleChangeTab('add-new'), active: activeTab === 'add-new', }, { title: 'Settings', onClick: () => this.handleChangeTab('settings'), active: activeTab === 'settings', }, ]} />
{ activeTab === 'members' &&
Total members ({accountIds.size}) { accountIds && accountIds.map((accountId) => ( this.handleAddOrRemoveFromList(accountId)} actionIcon='subtract' /> )) }
} { activeTab === 'settings' &&
Once you delete a list you cannot retrieve it.
} { activeTab === 'add-new' &&
Search results ({searchAccountIds.size}) { searchAccountIds && searchAccountIds.map((accountId) => { if (accountIds.includes(accountId)) return null return ( this.handleAddOrRemoveFromList(accountId)} actionIcon='add' /> ) }) }
}
) } } const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, save: { id: 'lists.new.save_title', defaultMessage: 'Save Title' }, changeTitle: { id: 'lists.edit.submit', defaultMessage: 'Change title' }, addToList: { id: 'lists.account.add', defaultMessage: 'Add to list' }, removeFromList: { id: 'lists.account.remove', defaultMessage: 'Remove from list' }, editList: { id: 'lists.edit', defaultMessage: 'Edit list' }, editListTitle: { id: 'lists.new.edit_title_placeholder', defaultMessage: 'Edit list title' }, remove: { id: 'lists.account.remove', defaultMessage: 'Remove from list' }, add: { id: 'lists.account.add', defaultMessage: 'Add to list' }, search: { id: 'lists.search', defaultMessage: 'Search people...' }, searchMembers: { id: 'lists.search_members', defaultMessage: 'Search members...' }, searchTitle: { id: 'tabs_bar.search', defaultMessage: 'Search' }, }) const mapStateToProps = (state, { params, id }) => { const listId = isObject(params) ? params['id'] : id return { listId, list: state.getIn(['lists', listId]), title: state.getIn(['listEditor', 'title']), disabled: !state.getIn(['listEditor', 'isChanged']), accountIds: state.getIn(['listEditor', 'accounts', 'items']), searchAccountIds: state.getIn(['listEditor', 'suggestions', 'items']), searchSuggestionsValue: state.getIn(['listEditor', 'suggestions', 'value']), } } const mapDispatchToProps = (dispatch) => ({ onDeleteList(list) { dispatch(openModal(MODAL_LIST_DELETE, { list })) }, onChangeTitle(value) { dispatch(changeListEditorTitle(value)) }, onUpdateList() { dispatch(submitListEditor(false)) }, onInitialize(listId) { dispatch(setupListEditor(listId)) }, onReset() { dispatch(resetListEditor()) }, onRemoveAccountFromList(accountId) { dispatch(removeFromListEditor(accountId)) }, onAddAccountToList(accountId) { dispatch(addToListEditor(accountId)) }, onSubmitSearchSuggestions(value) { dispatch(fetchListSuggestions(value)) }, onClearSearchSuggestions() { dispatch(clearListSuggestions()) }, onChangeSuggestions(value) { dispatch(changeListSuggestions(value)) }, }) ListEdit.propTypes = { list: ImmutablePropTypes.map, title: PropTypes.string, listId: PropTypes.string.isRequired, onClose: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, onInitialize: PropTypes.func.isRequired, onReset: PropTypes.func.isRequired, searchSuggestionsValue: PropTypes.string.isRequired, accountIds: ImmutablePropTypes.list.isRequired, searchAccountIds: ImmutablePropTypes.list.isRequired, onRemoveAccountFromList: PropTypes.func.isRequired, onAddAccountToList: PropTypes.func.isRequired, onChangeSuggestions: PropTypes.func.isRequired, onClearSearchSuggestions: PropTypes.func.isRequired, onSubmitSearchSuggestions: PropTypes.func.isRequired, onDeleteList: PropTypes.func.isRequired, tab: PropTypes.string, } export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(ListEdit))