// @flow import * as React from 'react'; import compose from 'recompose/compose'; import { connect } from 'react-redux'; import { withRouter } from 'react-router'; import { Link } from 'react-router-dom'; import editChannelMutation from 'shared/graphql/mutations/channel/editChannel'; import type { EditChannelType } from 'shared/graphql/mutations/channel/editChannel'; import type { GetChannelType } from 'shared/graphql/queries/channel/getChannel'; import deleteChannelMutation from 'shared/graphql/mutations/channel/deleteChannel'; import { openModal } from 'src/actions/modals'; import Tooltip from 'src/components/tooltip'; import { addToastWithTimeout } from 'src/actions/toasts'; import { Notice } from 'src/components/listItems/style'; import { PrimaryOutlineButton } from 'src/components/button'; import Icon from 'src/components/icon'; import { NullCard } from 'src/components/upsell'; import { Input, UnderlineInput, TextArea, Error, } from 'src/components/formElements'; import { SectionCard, SectionTitle } from 'src/components/settingsViews/style'; import { whiteSpaceRegex, oddHyphenRegex, } from 'src/views/viewHelpers/textValidationHelper'; import { Form, TertiaryActionContainer, Description, Actions, GeneralNotice, Location, } from 'src/components/editForm/style'; import type { Dispatch } from 'redux'; type State = { name: string, nameError: boolean, slug: string, description: ?string, descriptionError: boolean, isPrivate: boolean, channelId: string, channelData: Object, isLoading: boolean, }; type Props = { editChannel: Function, dispatch: Dispatch, channel: GetChannelType, }; class ChannelWithData extends React.Component { constructor(props) { super(props); const { channel } = this.props; this.state = { name: channel.name, nameError: false, slug: channel.slug, description: channel.description, descriptionError: false, isPrivate: channel.isPrivate || false, channelId: channel.id, channelData: channel, isLoading: false, }; } handleChange = e => { const key = e.target.id; const value = e.target.value; const { isPrivate } = this.state; const newState = {}; // checkboxes should reverse the value if (key === 'isPrivate') { newState[key] = !isPrivate; } else { newState[key] = value; let hasInvalidChars = value.search(whiteSpaceRegex) >= 0; let hasOddHyphens = value.search(oddHyphenRegex) >= 0; this.updateStateOnError(newState, key, hasInvalidChars || hasOddHyphens); } this.setState(prevState => { return Object.assign({}, prevState, { ...newState, }); }); }; updateStateOnError(state, key, setError) { if (key === 'name') { state['nameError'] = setError; } else { state['descriptionError'] = setError; } } save = e => { e.preventDefault(); const { name, slug, description, isPrivate, channelId } = this.state; const input = { name, slug, description, isPrivate, channelId, }; this.setState({ isLoading: true, }); // if privacy changed in this edit if (this.props.channel.isPrivate !== isPrivate) { } this.props .editChannel(input) .then(({ data }: EditChannelType) => { const { editChannel: channel } = data; this.setState({ isLoading: false, }); // the mutation returns a channel object. if it exists, if (channel !== undefined) { this.props.dispatch(addToastWithTimeout('success', 'Channel saved!')); } return; }) .catch(err => { this.setState({ isLoading: false, }); this.props.dispatch(addToastWithTimeout('error', err.message)); }); }; triggerDeleteChannel = (e, channelId) => { e.preventDefault(); const { name, channelData } = this.state; const message = (

Are you sure you want to delete{' '} {channelData.community.name}/{name} ?

All conversations posted in this channel will be deleted.

All messages, reactions, and media shared in this channel will be deleted.

This cannot be undone.

); return this.props.dispatch( openModal('DELETE_DOUBLE_CHECK_MODAL', { id: channelId, entity: 'channel', message, redirect: `/${channelData.community.slug}`, }) ); }; render() { const { name, nameError, slug, description, descriptionError, isPrivate, isLoading, } = this.state; const { channel } = this.props; if (!channel) { return ( {/* TODO: wire up button */} Create ); } else { return ( View Channel Channel Settings
Name {nameError && ( Channel name can`t have invalid characters. )} {`URL: /${channel.community.slug}/`} {descriptionError && ( Oops, there may be some invalid characters - try fixing that up. )} {/* {slug !== 'general' && Private channel } */} {isPrivate ? ( Only channel members can see the threads, messages, and members in this channel. You can manually approve users who request to join this channel. ) : channel.community.isPrivate ? ( Members in your private community will be able to join this channel, post threads and messages, and will be able to see other members. ) : ( Anyone on Spectrum can join this channel, post threads and messages, and will be able to see other members. )} {// if the user is moving from private to public this.props.channel.isPrivate && !isPrivate && ( When a private channel is made public all pending users will be added as members of the channel. Blocked users will remain blocked from viewing all content in this channel but in the future any new person will be able to join. )} {isLoading ? 'Saving...' : 'Save'} {slug !== 'general' && ( this.triggerDeleteChannel(e, channel.id)} data-cy="delete-channel-button" /> )} {slug === 'general' && ( The General channel is the default channel for your community. It can’t be deleted or private, but you can still change the name and description. )}
); } } } const Channel = compose( deleteChannelMutation, editChannelMutation, withRouter )(ChannelWithData); export default connect()(Channel);