From 9d5dcc0e4ad468439c99069d0fa36024a8ada84d Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Sat, 13 Mar 2021 07:53:19 +0400 Subject: [PATCH] Edit contact done --- src/components/popups/peer.ts | 2 +- .../sidebarRight/tabs/editContact.ts | 71 +++++++++++++------ src/lib/appManagers/appUsersManager.ts | 54 +++++++++----- src/lib/rootScope.ts | 2 +- src/scss/partials/_rightSidebar.scss | 4 ++ 5 files changed, 93 insertions(+), 40 deletions(-) diff --git a/src/components/popups/peer.ts b/src/components/popups/peer.ts index 3c580d23..9c0fb3d2 100644 --- a/src/components/popups/peer.ts +++ b/src/components/popups/peer.ts @@ -24,4 +24,4 @@ export default class PopupPeer extends PopupElement { this.container.insertBefore(p, this.header.nextElementSibling); } -} \ No newline at end of file +} diff --git a/src/components/sidebarRight/tabs/editContact.ts b/src/components/sidebarRight/tabs/editContact.ts index 95ebaeeb..5d9bbd2e 100644 --- a/src/components/sidebarRight/tabs/editContact.ts +++ b/src/components/sidebarRight/tabs/editContact.ts @@ -5,11 +5,15 @@ import { SettingSection } from "../../sidebarLeft"; import Row from "../../row"; import CheckboxField from "../../checkboxField"; import Button from "../../button"; -import appChatsManager from "../../../lib/appManagers/appChatsManager"; -import { attachClickEvent } from "../../../helpers/dom"; +import { attachClickEvent, toggleDisability } from "../../../helpers/dom"; import appUsersManager from "../../../lib/appManagers/appUsersManager"; import appNotificationsManager from "../../../lib/appManagers/appNotificationsManager"; import PeerTitle from "../../peerTitle"; +import appMessagesManager from "../../../lib/appManagers/appMessagesManager"; +import rootScope from "../../../lib/rootScope"; +import appPeersManager from "../../../lib/appManagers/appPeersManager"; +import PopupPeer from "../../popups/peer"; +import { addCancelButton } from "../../popups"; export default class AppEditContactTab extends SliderSuperTab { private nameInputField: InputField; @@ -64,11 +68,31 @@ export default class AppEditContactTab extends SliderSuperTab { text: 'Notifications' }); + notificationsCheckboxField.input.addEventListener('change', (e) => { + if(!e.isTrusted) { + return; + } + + appMessagesManager.mutePeer(this.peerId); + }); + + this.listenerSetter.add(rootScope, 'notify_settings', (update) => { + if(update.peer._ !== 'notifyPeer') return; + const peerId = appPeersManager.getPeerId(update.peer.peer); + if(this.peerId === peerId) { + const enabled = !appNotificationsManager.isMuted(update.notify_settings); + if(enabled !== notificationsCheckboxField.value) { + notificationsCheckboxField.value = enabled; + } + } + }); + const notificationsRow = new Row({ checkboxField: notificationsCheckboxField }); - notificationsCheckboxField.value = !appNotificationsManager.isPeerLocalMuted(this.peerId, false); + const enabled = !appNotificationsManager.isPeerLocalMuted(this.peerId, false); + notificationsCheckboxField.value = enabled; const profileNameDiv = document.createElement('div'); profileNameDiv.classList.add('profile-name'); @@ -87,25 +111,9 @@ export default class AppEditContactTab extends SliderSuperTab { attachClickEvent(this.editPeer.nextBtn, () => { this.editPeer.nextBtn.disabled = true; - - let promises: Promise[] = []; - const id = -this.peerId; - if(this.nameInputField.isValid()) { - promises.push(appChatsManager.editTitle(id, this.nameInputField.value)); - } - - if(this.lastNameInputField.isValid()) { - promises.push(appChatsManager.editAbout(id, this.lastNameInputField.value)); - } - - if(this.editPeer.uploadAvatar) { - promises.push(this.editPeer.uploadAvatar().then(inputFile => { - return appChatsManager.editPhoto(id, inputFile); - })); - } - - Promise.race(promises).finally(() => { + appUsersManager.addContact(this.peerId, this.nameInputField.value, this.lastNameInputField.value, appUsersManager.getUser(this.peerId).phone) + .finally(() => { this.editPeer.nextBtn.removeAttribute('disabled'); this.close(); }); @@ -119,6 +127,27 @@ export default class AppEditContactTab extends SliderSuperTab { const btnDelete = Button('btn-primary btn-transparent danger', {icon: 'delete', text: 'Delete Contact'}); + attachClickEvent(btnDelete, () => { + new PopupPeer('popup-delete-contact', { + peerId: this.peerId, + title: 'Delete Contact?', + description: `Are you sure you want to delete ${appPeersManager.getPeerTitle(this.peerId)} from your contact list?`, + buttons: addCancelButton([{ + text: 'DELETE', + callback: () => { + toggleDisability([btnDelete], true); + + appUsersManager.deleteContacts([this.peerId]).then(() => { + this.close(); + }, () => { + toggleDisability([btnDelete], false); + }); + }, + isDanger: true + }]) + }).show(); + }, {listenerSetter: this.listenerSetter}); + section.content.append(btnDelete); this.scrollable.append(section.container); diff --git a/src/lib/appManagers/appUsersManager.ts b/src/lib/appManagers/appUsersManager.ts index 4067de98..664d6452 100644 --- a/src/lib/appManagers/appUsersManager.ts +++ b/src/lib/appManagers/appUsersManager.ts @@ -723,25 +723,19 @@ export class AppUsersManager { }); } - /* public onContactUpdated(userId: number, isContact: boolean) { - userId = parseInt('' + userId); - - if(Array.isArray(this.contactsList)) { - var curPos = this.contactsList.indexOf(userId); - var curIsContact = curPos !== -1; - - if(isContact !== curIsContact) { - if(isContact) { - this.contactsList.push(userId) - searchIndexManager.indexObject(userId, this.getUserSearchText(userId), this.contactsIndex); - } else { - this.contactsList.splice(curPos, 1); - } - - rootScope.$broadcast('contacts_update', userId); + public onContactUpdated(userId: number, isContact: boolean) { + const curIsContact = this.contactsList.has(userId); + if(isContact !== curIsContact) { + if(isContact) { + this.contactsList.add(userId) + searchIndexManager.indexObject(userId, this.getUserSearchText(userId), this.contactsIndex); + } else { + this.contactsList.delete(userId); } + + rootScope.broadcast('contacts_update', userId); } - } */ + } public setUserStatus(userId: number, offline: boolean) { if(this.isBot(userId)) { @@ -763,6 +757,32 @@ export class AppUsersManager { rootScope.broadcast('user_update', userId); } } + + public addContact(userId: number, first_name: string, last_name: string, phone: string, showPhone?: true) { + return apiManager.invokeApi('contacts.addContact', { + id: this.getUserInput(userId), + first_name, + last_name, + phone, + add_phone_privacy_exception: showPhone + }).then((updates) => { + apiUpdatesManager.processUpdateMessage(updates); + + this.onContactUpdated(userId, true); + }); + } + + public deleteContacts(userIds: number[]) { + return apiManager.invokeApi('contacts.deleteContacts', { + id: userIds.map(userId => this.getUserInput(userId)) + }).then((updates) => { + apiUpdatesManager.processUpdateMessage(updates); + + userIds.forEach(userId => { + this.onContactUpdated(userId, false); + }); + }); + } } const appUsersManager = new AppUsersManager(); diff --git a/src/lib/rootScope.ts b/src/lib/rootScope.ts index ed3e1ca0..a76dc844 100644 --- a/src/lib/rootScope.ts +++ b/src/lib/rootScope.ts @@ -72,7 +72,7 @@ export type BroadcastEvents = { 'state_synchronized': number, 'state_synchronizing': number, - //'contacts_update': any, + 'contacts_update': number, 'avatar_update': number, 'chat_full_update': number, 'poll_update': {poll: Poll, results: PollResults}, diff --git a/src/scss/partials/_rightSidebar.scss b/src/scss/partials/_rightSidebar.scss index 3ecd2321..b7405b32 100644 --- a/src/scss/partials/_rightSidebar.scss +++ b/src/scss/partials/_rightSidebar.scss @@ -347,6 +347,10 @@ /* > div:not(:empty) + .content-empty { display: none; } */ + + > div:first-child { + transform: translateY(0); + } } /* .scrollable-y {