// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import React from 'react'; import * as AsyncClient from 'utils/async_client.jsx'; import EmojiStore from 'stores/emoji_store.jsx'; import BackstageHeader from 'components/backstage/components/backstage_header.jsx'; import {FormattedMessage} from 'react-intl'; import FormError from 'components/form_error.jsx'; import {Link} from 'react-router'; import SpinnerButton from 'components/spinner_button.jsx'; export default class AddEmoji extends React.Component { static propTypes = { team: React.PropTypes.object, user: React.PropTypes.object }; static contextTypes = { router: React.PropTypes.object.isRequired }; constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); this.updateName = this.updateName.bind(this); this.updateImage = this.updateImage.bind(this); this.state = { name: '', image: null, imageUrl: '', saving: false, error: null }; } handleSubmit(e) { e.preventDefault(); if (this.state.saving) { return; } this.setState({ saving: true, error: null }); const emoji = { creator_id: this.props.user.id, name: this.state.name.trim().toLowerCase() }; // trim surrounding colons if the user accidentally included them in the name if (emoji.name.startsWith(':') && emoji.name.endsWith(':')) { emoji.name = emoji.name.substring(1, emoji.name.length - 1); } if (!emoji.name) { this.setState({ saving: false, error: ( ) }); return; } else if (/[^a-z0-9_-]/.test(emoji.name)) { this.setState({ saving: false, error: ( ) }); return; } else if (EmojiStore.hasSystemEmoji(emoji.name)) { this.setState({ saving: false, error: ( ) }); return; } if (!this.state.image) { this.setState({ saving: false, error: ( ) }); return; } AsyncClient.addEmoji( emoji, this.state.image, () => { // for some reason, browserHistory.push doesn't trigger a state change even though the url changes this.context.router.push('/' + this.props.team.name + '/emoji'); }, (err) => { this.setState({ saving: false, error: err.message }); } ); } updateName(e) { this.setState({ name: e.target.value }); } updateImage(e) { if (e.target.files.length === 0) { this.setState({ image: null, imageUrl: '' }); return; } const image = e.target.files[0]; const reader = new FileReader(); reader.onload = () => { this.setState({ image, imageUrl: reader.result }); }; reader.readAsDataURL(image); } render() { let filename = null; if (this.state.image) { filename = ( {this.state.image.name} ); } let preview = null; if (this.state.imageUrl) { preview = (
) }} />
); } return (
{filename}
{preview}
); } }