import React, { Component } from 'react' import PropTypes from 'prop-types' import { Mutation } from 'react-apollo' import { withStyles } from '@material-ui/core/styles' import Button from '@material-ui/core/Button' import EditIcon from '@material-ui/icons/Edit' import TextField from '@material-ui/core/TextField' import Dialog from '@material-ui/core/Dialog' import DialogActions from '@material-ui/core/DialogActions' import DialogContent from '@material-ui/core/DialogContent' import DialogContentText from '@material-ui/core/DialogContentText' import DialogTitle from '@material-ui/core/DialogTitle' import Typography from '@material-ui/core/Typography' import { MessageCard, ImageUpload } from '@superalgos/web-components' import log from '../../../utils/log' import UPDATE_TEAM_PROFILE from '../../../graphql/teams/UpdateTeamProfileMutation' import GET_TEAMS_BY_OWNER from '../../../graphql/teams/GetTeamsByOwnerQuery' import { checkGraphQLError } from '../../../utils/graphql-errors' import GET_AZURE_SAS from '../../../graphql/teams/GetAzureSASMutation' const styles = theme => ({ dialogContainer: { display: 'block', margin: '3em', minWidth: 400 }, buttonRight: { justifyContent: 'flex-end' } }) export class ManageTeamEdit extends Component { constructor (props) { super(props) this.handleClickOpen = this.handleClickOpen.bind(this) this.handleClose = this.handleClose.bind(this) this.handleChange = this.handleChange.bind(this) this.handleSubmit = this.handleSubmit.bind(this) this.handleAvatar = this.handleAvatar.bind(this) this.handleBanner = this.handleBanner.bind(this) log.debug('ManageTeamEdit', props.team) const motto = props.team.profile.motto || '' const description = props.team.profile.description || '' const avatar = props.team.profile.avatar || '' const banner = props.team.profile.banner || '' this.authId = props.authId this.state = { open: false, motto: motto, description: description, avatar: avatar, banner: banner } } render () { log.debug('ManageTeamEdit ', this.props, this.props.slug, this.props.team) const { classes, team } = this.props return ( {(updateTeamProfile, { loading, error, data }) => { let errors let loader if (loading) { loader = ( Submitting team... ) } if (error) { errors = error.graphQLErrors.map(({ message }, i) => { const displayMessage = checkGraphQLError(message) log.debug('updateTeamProfile error:', displayMessage) return ( {message} ) }) } return (
Edit Team Details {(getAzureSAS, { loading, error, data }) => { log.debug('getAzureSAS: ', loading, error, data, team.profile) const AzureStorageUrl = process.env.AZURE_STORAGE_URL const containerName = team.slug let AzureSASURL if (!loading && data !== undefined) { AzureSASURL = data.getAzureSAS } else { getAzureSAS({ variables: { teamSlug: containerName } }) } let avatar = null if (team.profile !== null && team.profile.avatar !== undefined && team.profile.avatar !== null) avatar = team.profile.avatar if (this.state.avatar !== null) avatar = this.state.avatar if (this.state.avatar === null && team.profile !== null && team.profile.avatar === undefined) avatar = process.env.STORAGE_URL + '/module-teams/module-default/aa-avatar-default.png' let banner = null if (team.profile !== null && team.profile.banner !== undefined && team.profile.banner !== null) banner = team.profile.banner if (this.state.banner !== null) banner = this.state.banner if (this.state.banner === null && team.profile !== null && team.profile.banner === undefined) banner = process.env.STORAGE_URL + '/module-teams/module-default/aa-banner-default.png' log.debug('team images: ', avatar, banner) if (loading || data === undefined) { return () } else { return ( ) } }} Team description: {loader} {errors}
) }}
) } handleClickOpen () { this.setState({ open: true }) } handleClose () { this.setState({ open: false }) } handleChange (e) { switch (e.target.id) { case 'motto': this.setState({ motto: e.target.value }) break case 'description': this.setState({ description: e.target.value }) break default: } } handleAvatar (avatarUrl) { log.debug('handleAvatar: ', avatarUrl) this.setState({ avatar: `${avatarUrl}?${Math.random()}` }) } handleBanner (bannerUrl) { log.debug('handleBanner: ', bannerUrl) this.setState({ banner: `${bannerUrl}?${Math.random()}` }) } async handleSubmit (e, updateTeamProfile, slug) { log.debug('handleSubmit: ', this.state) e.preventDefault() await updateTeamProfile({ variables: { slug, owner: this.authId, description: this.state.description, motto: this.state.motto, avatar: this.state.avatar, banner: this.state.banner } }) this.setState({ description: '', motto: '', open: false }) } } ManageTeamEdit.propTypes = { classes: PropTypes.object.isRequired, slug: PropTypes.string.isRequired, team: PropTypes.object, authId: PropTypes.string.isRequired } export default withStyles(styles)(ManageTeamEdit)