This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/scenes/Settings/Profile.js
Tom Moor 8cbcb77486 Base model refactor (#810)
* Big upgrades

* WIP: Stash

* Stash, 30 flow errors left

* Downgrade mobx

* WIP

* When I understand the difference between class and instance methods

* 💚

* Fixes: File import
Model saving edge cases
pinning and starring docs
Collection editing
Upgrade mobx devtools

* Notification settings saving works

* Disabled settings

* Document mailer

* Working notifications

* Colletion created notification
Ensure not notified for own actions

* Tidy up

* Document updated event only for document creation
Add indexes
Notification setting on user creation

* Commentary

* Fixed: Notification setting on signup

* Fix document move / duplicate stale data
Add BaseModel.refresh method

* Fixes: Title in sidebar not updated after editing document

* 💚

* Improve / restore error handling
Better handle offline errors

* 👕
2018-12-04 22:24:30 -08:00

172 lines
4.1 KiB
JavaScript

// @flow
import * as React from 'react';
import { observable } from 'mobx';
import { observer, inject } from 'mobx-react';
import styled from 'styled-components';
import AuthStore from 'stores/AuthStore';
import UiStore from 'stores/UiStore';
import ImageUpload from './components/ImageUpload';
import Input, { LabelText } from 'components/Input';
import Button from 'components/Button';
import CenteredContent from 'components/CenteredContent';
import PageTitle from 'components/PageTitle';
import UserDelete from 'scenes/UserDelete';
import Flex from 'shared/components/Flex';
type Props = {
auth: AuthStore,
ui: UiStore,
};
@observer
class Profile extends React.Component<Props> {
timeout: TimeoutID;
form: ?HTMLFormElement;
@observable name: string;
@observable avatarUrl: ?string;
@observable showDeleteModal: boolean = false;
componentDidMount() {
if (this.props.auth.user) {
this.name = this.props.auth.user.name;
}
}
componentWillUnmount() {
clearTimeout(this.timeout);
}
handleSubmit = async (ev: SyntheticEvent<*>) => {
ev.preventDefault();
await this.props.auth.updateUser({
name: this.name,
avatarUrl: this.avatarUrl,
});
this.props.ui.showToast('Profile saved', 'success');
};
handleNameChange = (ev: SyntheticInputEvent<*>) => {
this.name = ev.target.value;
};
handleAvatarUpload = async (avatarUrl: string) => {
this.avatarUrl = avatarUrl;
await this.props.auth.updateUser({
avatarUrl: this.avatarUrl,
});
this.props.ui.showToast('Profile picture updated', 'success');
};
handleAvatarError = (error: ?string) => {
this.props.ui.showToast(error || 'Unable to upload new avatar');
};
toggleDeleteAccount = () => {
this.showDeleteModal = !this.showDeleteModal;
};
get isValid() {
return this.form && this.form.checkValidity();
}
render() {
const { user, isSaving } = this.props.auth;
if (!user) return null;
const avatarUrl = this.avatarUrl || user.avatarUrl;
return (
<CenteredContent>
<PageTitle title="Profile" />
<h1>Profile</h1>
<ProfilePicture column>
<LabelText>Picture</LabelText>
<AvatarContainer>
<ImageUpload
onSuccess={this.handleAvatarUpload}
onError={this.handleAvatarError}
>
<Avatar src={avatarUrl} />
<Flex auto align="center" justify="center">
Upload
</Flex>
</ImageUpload>
</AvatarContainer>
</ProfilePicture>
<form onSubmit={this.handleSubmit} ref={ref => (this.form = ref)}>
<Input
label="Name"
value={this.name}
onChange={this.handleNameChange}
required
short
/>
<Button type="submit" disabled={isSaving || !this.isValid}>
{isSaving ? 'Saving…' : 'Save'}
</Button>
</form>
<DangerZone>
<LabelText>Delete Account</LabelText>
<p>
You may delete your account at any time, note that this is
unrecoverable.{' '}
<a onClick={this.toggleDeleteAccount}>Delete account</a>.
</p>
</DangerZone>
{this.showDeleteModal && (
<UserDelete onRequestClose={this.toggleDeleteAccount} />
)}
</CenteredContent>
);
}
}
const DangerZone = styled.div`
background: #fff;
position: absolute;
bottom: 16px;
`;
const ProfilePicture = styled(Flex)`
margin-bottom: 24px;
`;
const avatarStyles = `
width: 80px;
height: 80px;
border-radius: 50%;
`;
const AvatarContainer = styled(Flex)`
${avatarStyles};
position: relative;
div div {
${avatarStyles};
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
cursor: pointer;
transition: all 250ms;
}
&:hover div {
opacity: 1;
background: rgba(0, 0, 0, 0.75);
color: ${props => props.theme.white};
}
`;
const Avatar = styled.img`
${avatarStyles};
`;
export default inject('auth', 'ui')(Profile);