// Edit account parameters. import React from 'react'; import { FormattedMessage } from 'react-intl'; import AvatarUpload from '../widgets/avatar-upload.jsx'; import CheckBox from '../widgets/checkbox.jsx'; import InPlaceEdit from '../widgets/in-place-edit.jsx'; import PermissionsEditor from '../widgets/permissions-editor.jsx'; import TagManager from '../widgets/tag-manager.jsx'; import { makeImageUrl } from '../lib/blob-helpers.js'; import { arrayEqual, vcard } from '../lib/utils.js'; export default class EditAccountView extends React.Component { constructor(props) { super(props); const me = this.props.tinode.getMeTopic(); const defacs = me ? me.getDefaultAccess() : null; const 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, fndSubscribed: false, 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() { const fnd = this.props.tinode.getFndTopic(); fnd.onTagsUpdated = this.tnNewTags; if (!fnd.isSubscribed()) { fnd.subscribe(fnd.startMetaQuery().withTags().build()) .then((ctrl) => { this.setState({fndSubscribed: true}); }) .catch((err) => { this.props.onError(err.message, 'err'); }); } } componentWillUnmount() { const fnd = this.props.tinode.getFndTopic(); fnd.onTagsUpdated = this.state.previousOnTags; if (fnd.isSubscribed() && this.state.fndSubscribed) { fnd.leave(); } } tnNewTags(tags) { this.setState({tags: tags}); } handleFullNameUpdate(fn) { this.setState({fullName: fn}); this.props.onUpdateAccount(undefined, vcard(fn, null)); } handlePasswordUpdate(pwd) { this.setState({password: pwd}); this.props.onUpdateAccount(pwd); } handleImageChanged(img) { this.setState({avatar: img}); this.props.onUpdateAccount(undefined, vcard(null, 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 = ; } return ( {this.state.showPermissionEditorFor ? : { (full_name_placeholder) => } { (password_unchanged) => } {this.props.myUserId} Auth: {this.state.auth} Anon: {this.state.anon} {this.props.desktopAlertsEnabled ? : } { (title_tag_manager) => } exit_to_app } ); } };