diff --git a/client/src/components/UserImages.js b/client/src/components/UserImages.js index ffbc132b2..99c81a490 100644 --- a/client/src/components/UserImages.js +++ b/client/src/components/UserImages.js @@ -1,6 +1,6 @@ import React from 'react'; import {graphql, compose} from 'react-apollo' -import {getRolesQuery, updateUserMutation, getUsersQuery} from '../queries/queries' +import {updateUserImagesMutation, getUsersQuery} from '../queries/queries' import Avatar from '@material-ui/core/Avatar'; import classNames from 'classnames'; @@ -11,7 +11,9 @@ import Portrait from '../img/portrait.jpg' import { withStyles } from '@material-ui/core/styles' import Button from '@material-ui/core/Button'; +import Paper from '@material-ui/core/Paper' import Grid from '@material-ui/core/Grid' +import Typography from '@material-ui/core/Typography' const options = { accept: ['image/jpeg', 'image/jpg', 'image/png'], @@ -40,23 +42,89 @@ const styles = theme => ({ width: 60, height: 60, }, + root: { + width: '50%', + flexGrow: 1, + padding: 10, + marginLeft: '25%', + marginTop: '2%' + }, + typography: { + width: '80%', + marginLeft: '10%', + marginTop: 40 + }, + button: { + margin: theme.spacing.unit, + marginTop: theme.spacing.unit * 3 + } }) class UserImages extends React.Component { constructor (props) { super(props) + + this.defaultValuesSet = false + this.state = { - avatarHandle: '' + avatarHandle: '', + avatarChangeDate: '' } } + submitForm (e) { + e.preventDefault() + this.props.updateUserImagesMutation({ + variables: { + id: this.state.id, + avatarHandle: this.state.avatarHandle, + avatarChangeDate: this.state.avatarChangeDate + }, + refetchQueries: [{ query: getUsersQuery}] // This allow us to re run whatever queries are necesary after the mutation. + }) + + /* Before we are done, we need to update the state of the local storage. */ + + let user = JSON.parse(localStorage.getItem('loggedInUser')) + + user.avatarHandle = this.state.avatarHandle + user.avatarChangeDate = this.state.avatarChangeDate + + localStorage.setItem('loggedInUser', JSON.stringify(user)) + } + + componentWillMount () { + if (this.defaultValuesSet === false) { + let userData = localStorage.getItem('loggedInUser') + + if (userData === 'undefined') { return } + + let user = JSON.parse(userData) + this.defaultValuesSet = true + + /* To avoid console warning, we need to take care of the fields that are null. */ + + if (user.avatarHandle === null) { user.avatarHandle = '' } + if (user.avatarChangeDate === null) { user.avatarChangeDate = '' } + + /* Now we are ready to set the initial state. */ + + this.setState({ + id: user.id, + avatarHandle: user.avatarHandle, + avatarChangeDate: user.avatarChangeDate + }) + } + } + onSuccess = (uploadResults) => { console.log('onSuccess') console.log(uploadResults) const handle = uploadResults.filesUploaded[0].handle - this.setState({avatarHandle: handle}) + const currentDate = (new Date()).toString(); + this.setState({avatarHandle: handle, avatarChangeDate: currentDate}) } onError = () => { @@ -66,36 +134,70 @@ class UserImages extends React.Component { render() { const { classes } = this.props return ( -