2018-01-12 22:58:21 +01:00
|
|
|
import Inferno from 'inferno';
|
2018-02-22 07:47:39 +01:00
|
|
|
import { Component } from 'inferno';
|
2018-01-12 22:58:21 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import marked from 'marked';
|
|
|
|
import store from 'store';
|
|
|
|
|
|
|
|
import { Modal } from '../../structure/Modal';
|
2018-01-13 19:57:14 +01:00
|
|
|
import { LoginForm } from './LoginForm';
|
2018-03-10 00:31:27 +01:00
|
|
|
// import { MyAccount } from './MyAccount';
|
2018-01-12 22:58:21 +01:00
|
|
|
|
2018-01-13 20:09:15 +01:00
|
|
|
import { request } from '../../../Helpers';
|
2018-01-12 22:58:21 +01:00
|
|
|
|
2018-03-10 00:31:27 +01:00
|
|
|
const defaultUserData = {
|
|
|
|
email: '',
|
|
|
|
username: '',
|
|
|
|
publicName: '',
|
|
|
|
allowEmails: true,
|
|
|
|
useIPAPronunciation: true,
|
|
|
|
};
|
|
|
|
|
2018-01-12 22:58:21 +01:00
|
|
|
export class AccountManager extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
PropTypes.checkPropTypes({
|
|
|
|
updater: PropTypes.object.isRequired,
|
|
|
|
}, props, 'prop', 'AccountManager');
|
|
|
|
|
2018-03-10 00:31:27 +01:00
|
|
|
const userData = store.get('LexicongaUserData');
|
|
|
|
|
2018-01-12 22:58:21 +01:00
|
|
|
this.state = {
|
|
|
|
isLoggedIn: false,
|
2018-03-10 00:31:27 +01:00
|
|
|
userData: {
|
|
|
|
email: userData ? userData.email : defaultUserData.email,
|
|
|
|
username: userData ? userData.username : defaultUserData.username,
|
|
|
|
publicName: userData ? userData.publicName : defaultUserData.publicName,
|
|
|
|
allowEmails: userData ? userData.allowEmails : defaultUserData.allowEmails,
|
|
|
|
useIPAPronunciation: userData ? userData.useIPAPronunciation : defaultUserData.useIPAPronunciation,
|
|
|
|
},
|
|
|
|
userDictionaries: [],
|
2018-01-12 22:58:21 +01:00
|
|
|
};
|
2018-03-10 00:31:27 +01:00
|
|
|
|
|
|
|
this.getDictionaryNames();
|
2018-01-12 22:58:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logIn (email, password) {
|
2018-01-21 22:22:31 +01:00
|
|
|
return request('login', { email, password }, this.handleResponse.bind(this));
|
2018-01-12 22:58:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logOut () {
|
|
|
|
store.remove('LexicongaToken');
|
2018-03-10 00:31:27 +01:00
|
|
|
store.remove('LexicongaUserData');
|
|
|
|
this.setState({
|
|
|
|
isLoggedIn: false,
|
|
|
|
userData: Object.assign({}, defaultUserData),
|
|
|
|
userDictionaries: [],
|
|
|
|
});
|
2018-01-12 22:58:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 22:22:31 +01:00
|
|
|
signUp (email, password, userData) {
|
|
|
|
request('create-account', {
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
userData,
|
|
|
|
}, this.handleResponse.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResponse (response) {
|
|
|
|
const { data, error } = response;
|
|
|
|
if (error) {
|
|
|
|
console.error(data);
|
|
|
|
} else {
|
2018-03-10 00:31:27 +01:00
|
|
|
store.set('LexicongaToken', data.token);
|
|
|
|
store.set('LexicongaUserData', data.user);
|
|
|
|
this.setState({
|
|
|
|
isLoggedIn: true,
|
|
|
|
userData: data.user,
|
|
|
|
}, () => {
|
|
|
|
this.getDictionaryNames();
|
2018-01-21 22:22:31 +01:00
|
|
|
this.props.updater.sync();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 00:31:27 +01:00
|
|
|
updateUserData (newUserData) {
|
|
|
|
const token = store.get('LexicongaToken');
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
store.set('LexicongaUserData', newUserData);
|
|
|
|
this.setState({ userData: newUserData }, () => {
|
|
|
|
request('set-user-data', { token, userData: newUserData }, (response) => {
|
|
|
|
const {data, error} = response;
|
|
|
|
if (error) {
|
|
|
|
console.error(data);
|
|
|
|
} else {
|
|
|
|
console.log(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getDictionaryNames () {
|
|
|
|
const token = store.get('LexicongaToken');
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
return request('get-all-dictionary-names', { token }, (response) => {
|
|
|
|
const {data, error} = response;
|
|
|
|
if (error) {
|
|
|
|
console.error(data);
|
|
|
|
} else {
|
|
|
|
this.setState({ userDictionaries: data });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 22:58:21 +01:00
|
|
|
render () {
|
|
|
|
const token = store.get('LexicongaToken');
|
|
|
|
|
|
|
|
if (token) {
|
2018-03-10 00:31:27 +01:00
|
|
|
const { userData } = this.state;
|
|
|
|
|
2018-01-12 22:58:21 +01:00
|
|
|
return (
|
|
|
|
<div>
|
2018-03-10 00:31:27 +01:00
|
|
|
<Modal buttonText='Account' title='My Account' onShow={ this.getDictionaryNames.bind(this) }>
|
2018-01-12 22:58:21 +01:00
|
|
|
<div className='content has-text-left'>
|
|
|
|
<p>Hello My Account!</p>
|
|
|
|
</div>
|
2018-03-10 00:31:27 +01:00
|
|
|
{/* <MyAccount
|
|
|
|
email={ userData.email }
|
|
|
|
username={ userData.username }
|
|
|
|
publicName={ userData.publicName }
|
|
|
|
allowEmails={ userData.allowEmails }
|
|
|
|
useIPAPronunciation={ userData.useIPAPronunciation }
|
|
|
|
updateUserData={ this.updateUserData.bind(this) }
|
|
|
|
changeDictionary={ () => {} } /> */}
|
2018-01-12 22:58:21 +01:00
|
|
|
</Modal>
|
|
|
|
<a className='button' onClick={this.logOut.bind(this)}>
|
|
|
|
Log Out
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Modal buttonText='Log In/Sign Up' title='Log In/Sign Up'>
|
2018-01-21 22:22:31 +01:00
|
|
|
<LoginForm logIn={this.logIn.bind(this)} signUp={this.signUp.bind(this)} />
|
2018-01-12 22:58:21 +01:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|