// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React, {PureComponent} from 'react'; import PropTypes from 'prop-types'; import { Platform, TouchableWithoutFeedback, View, Text, findNodeHandle, } from 'react-native'; import {KeyboardAwareScrollView} from 'react-native-keyboard-aware-scroll-view'; import ErrorText from 'app/components/error_text'; import FormattedText from 'app/components/formatted_text'; import Loading from 'app/components/loading'; import StatusBar from 'app/components/status_bar'; import TextInputWithLocalizedPlaceholder from 'app/components/text_input_with_localized_placeholder'; import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme'; import {General} from 'mattermost-redux/constants'; import {getShortenedURL} from 'app/utils/url'; import {t} from 'app/utils/i18n'; export default class EditChannelInfo extends PureComponent { static propTypes = { navigator: PropTypes.object.isRequired, theme: PropTypes.object.isRequired, deviceWidth: PropTypes.number.isRequired, deviceHeight: PropTypes.number.isRequired, channelType: PropTypes.string, enableRightButton: PropTypes.func, saving: PropTypes.bool.isRequired, editing: PropTypes.bool, error: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), displayName: PropTypes.string, currentTeamUrl: PropTypes.string, channelURL: PropTypes.string, purpose: PropTypes.string, header: PropTypes.string, onDisplayNameChange: PropTypes.func, onChannelURLChange: PropTypes.func, onPurposeChange: PropTypes.func, onHeaderChange: PropTypes.func, oldDisplayName: PropTypes.string, oldChannelURL: PropTypes.string, oldHeader: PropTypes.string, oldPurpose: PropTypes.string, }; static defaultProps = { editing: false, }; blur = () => { if (this.nameInput) { this.nameInput.blur(); } // TODO: uncomment below once the channel URL field is added // if (this.urlInput) { // this.urlInput.blur(); // } if (this.purposeInput) { this.purposeInput.blur(); } if (this.headerInput) { this.headerInput.blur(); } if (this.scroll) { this.scroll.scrollToPosition(0, 0, true); } }; channelNameRef = (ref) => { this.nameInput = ref; }; channelURLRef = (ref) => { this.urlInput = ref; }; channelPurposeRef = (ref) => { this.purposeInput = ref; }; channelHeaderRef = (ref) => { this.headerInput = ref; }; close = (goBack = false) => { if (goBack) { this.props.navigator.pop({animated: true}); } else { this.props.navigator.dismissModal({ animationType: 'slide-down', }); } }; lastTextRef = (ref) => { this.lastText = ref; }; canUpdate = (displayName, channelURL, purpose, header) => { const { oldDisplayName, oldChannelURL, oldPurpose, oldHeader, } = this.props; return displayName !== oldDisplayName || channelURL !== oldChannelURL || purpose !== oldPurpose || header !== oldHeader; }; enableRightButton = (enable = false) => { this.props.enableRightButton(enable); }; onDisplayNameChangeText = (displayName) => { const {editing, onDisplayNameChange} = this.props; onDisplayNameChange(displayName); if (editing) { const {channelURL, purpose, header} = this.props; const canUpdate = this.canUpdate(displayName, channelURL, purpose, header); this.enableRightButton(canUpdate); return; } const displayNameExists = displayName && displayName.length >= 2; this.props.enableRightButton(displayNameExists); }; onDisplayURLChangeText = (channelURL) => { const {editing, onChannelURLChange} = this.props; onChannelURLChange(channelURL); if (editing) { const {displayName, purpose, header} = this.props; const canUpdate = this.canUpdate(displayName, channelURL, purpose, header); this.enableRightButton(canUpdate); } }; onPurposeChangeText = (purpose) => { const {editing, onPurposeChange} = this.props; onPurposeChange(purpose); if (editing) { const {displayName, channelURL, header} = this.props; const canUpdate = this.canUpdate(displayName, channelURL, purpose, header); this.enableRightButton(canUpdate); } }; onHeaderChangeText = (header) => { const {editing, onHeaderChange} = this.props; onHeaderChange(header); if (editing) { const {displayName, channelURL, purpose} = this.props; const canUpdate = this.canUpdate(displayName, channelURL, purpose, header); this.enableRightButton(canUpdate); } }; scrollRef = (ref) => { this.scroll = ref; }; scrollToEnd = () => { if (this.scroll && this.lastText) { this.scroll.scrollToFocusedInput(findNodeHandle(this.lastText)); } }; render() { const { theme, editing, channelType, currentTeamUrl, deviceWidth, deviceHeight, displayName, channelURL, header, purpose, } = this.props; const {error, saving} = this.props; const fullUrl = currentTeamUrl + '/channels'; const shortUrl = getShortenedURL(fullUrl, 35); const style = getStyleSheet(theme); const displayHeaderOnly = channelType === General.DM_CHANNEL || channelType === General.GM_CHANNEL; if (saving) { return ( ); } let displayError; if (error) { displayError = ( ); } return ( {displayError} {!displayHeaderOnly && ( )} {/*TODO: Hide channel url field until it's added to CreateChannel */} {false && editing && !displayHeaderOnly && ( {shortUrl} )} {!displayHeaderOnly && ( )} ); } } const getStyleSheet = makeStyleSheetFromTheme((theme) => { return { container: { flex: 1, backgroundColor: theme.centerChannelBg, }, scrollView: { flex: 1, backgroundColor: changeOpacity(theme.centerChannelColor, 0.03), paddingTop: 10, }, errorContainer: { backgroundColor: changeOpacity(theme.centerChannelColor, 0.03), }, errorWrapper: { justifyContent: 'center', alignItems: 'center', }, inputContainer: { marginTop: 10, backgroundColor: '#fff', }, input: { color: '#333', fontSize: 14, height: 40, paddingHorizontal: 15, }, titleContainer30: { flexDirection: 'row', marginTop: 30, }, titleContainer15: { flexDirection: 'row', marginTop: 15, }, title: { fontSize: 14, color: theme.centerChannelColor, marginLeft: 15, }, optional: { color: changeOpacity(theme.centerChannelColor, 0.5), fontSize: 14, marginLeft: 5, }, helpText: { fontSize: 14, color: changeOpacity(theme.centerChannelColor, 0.5), marginTop: 10, marginHorizontal: 15, }, }; });