import React from 'react'; import AvatarUpload from './avatar-upload.jsx'; import CheckBox from './checkbox.jsx'; import PermissionsEditor from './permissions-editor.jsx'; import TagManager from './tag-manager.jsx'; /* Edit account parameters */ export default class EditAccountView extends React.Component { constructor(props) { super(props); let me = this.props.tinode.getMeTopic(); let defacs = me.getDefaultAccess(); let fnd = this.props.tinode.getFndTopic(); this.state = { fullName: me.public ? me.public.fn : undefined, avatar: makeImageUrl(me.public ? me.public.photo : null), auth: defacs ? defacs.auth : null, anon: defacs ? defacs.anon : null, tags: fnd.tags(), showPermissionEditorFor: undefined, previousOnTags: fnd.onTagsUpdated }; this.tnNewTags = this.tnNewTags.bind(this); this.handleFullNameUpdate = this.handleFullNameUpdate.bind(this); this.handlePasswordUpdate = this.handlePasswordUpdate.bind(this); this.handleImageChanged = this.handleImageChanged.bind(this); this.handleCheckboxClick = this.handleCheckboxClick.bind(this); this.handleLaunchPermissionsEditor = this.handleLaunchPermissionsEditor.bind(this); this.handleHidePermissionsEditor = this.handleHidePermissionsEditor.bind(this); this.handlePermissionsChanged = this.handlePermissionsChanged.bind(this); this.handleTagsUpdated = this.handleTagsUpdated.bind(this); } componentDidMount() { let fnd = this.props.tinode.getFndTopic(); fnd.onTagsUpdated = this.tnNewTags; if (!fnd.isSubscribed()) { fnd.subscribe(fnd.startMetaQuery().withLaterDesc().withTags().build()).catch((err) => { this.props.onError(err.message, 'err'); }); } } componentWillUnmount() { var fnd = this.props.tinode.getFndTopic(); fnd.onTagsUpdated = this.state.previousOnTags; } tnNewTags(tags) { this.setState({tags: tags}); } handleFullNameUpdate(fn) { this.setState({fullName: fn}); this.props.onUpdateAccount(undefined, vcard(fn, this.state.avatar)); } handlePasswordUpdate(pwd) { this.setState({password: pwd}); this.props.onUpdateAccount(pwd); } handleImageChanged(img) { this.setState({avatar: img}); this.props.onUpdateAccount(undefined, vcard(this.state.fullName, img)); } handleCheckboxClick(what, checked) { if (what == 'sound') { this.props.onToggleMessageSounds(checked); } else if (what == 'alert') { this.props.onTogglePushNotifications(checked); } } handleLaunchPermissionsEditor(which) { this.setState({ showPermissionEditorFor: which, editedPermissions: this.state[which] }); } handleHidePermissionsEditor() { this.setState({showPermissionEditorFor: undefined}); } handlePermissionsChanged(perm) { let defacs = {}; defacs[this.state.showPermissionEditorFor] = perm; this.props.onUpdateAccount(undefined, undefined, defacs); let newState = {showPermissionEditorFor: undefined}; newState[this.state.showPermissionEditorFor] = perm; this.setState(newState); } handleTagsUpdated(tags) { // Check if tags have actually changed. if (arrayEqual(this.state.tags.slice(0), tags.slice(0))) { return; } this.props.onUpdateTags(tags); } render() { var tags = []; this.state.tags.map(function(tag) { tags.push({tag}); }); if (tags.length == 0) { tags = No tags defined. Add some.; } return ( {this.state.showPermissionEditorFor ? :
{this.props.myUserId}
Auth: {this.state.auth}
Anon: {this.state.anon}