* feature edit profile screen * minor refactoring * Apply suggestions from code review Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * ts fixes * revert floatingTextInput label This reverts commit a778e1f76191aea7c1a18d60a23ffbd6d3dec0eb. * code clean up * Apply suggestions from code review Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * code fix * code fix * Adding preventDoubleTap * rename id to fieldKey * Update edit_profile.tsx * wip * navigating through fields; partly done * navigating through fields - partly done * navigating through fields; partly done * completed field navigation * added theme into dependency array * code clean up * revert conditions for disabling fields * Added colorFilters prop to Loading component * Completed loading feature on Edit Profile screen * code clean up * Add profile_error * renamed valid_mime_types to valid_image_mime_types * added props isDisabled to email field * refactored next field logic * fix * fix * code clean up * code clean up * Updated ESLINT hook rules to warning instead of disabled * code fix * code fix * new line within your_profile component * added memo for Field component * added canSave to dependency array * update loading component - color filter * Update app/screens/edit_profile/edit_profile.tsx Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * dependency fix * fix to fetch my latest status * fix to remove unnecessary user id for local action updateLocalUser * prevents bouncing for iOS * code revert * Adding textInputStyle and animatedTextStyle to FloatingTextInput component * correction after dev session * adding changes as per new ux * Update edit_profile.tsx * corrections after ux review * ux review * ux review * code clean up * Adding userProfileFields into useMemo * Add enableSaveButton to dependency of submitUser * Revert fetching status on userProfile * EditProfile enable extraScrollHeight on iOS only Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
144 lines
5.1 KiB
TypeScript
144 lines
5.1 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {SYSTEM_IDENTIFIERS} from '@constants/database';
|
|
import General from '@constants/general';
|
|
import DatabaseManager from '@database/manager';
|
|
import {queryRecentCustomStatuses} from '@queries/servers/system';
|
|
import {queryCurrentUser} from '@queries/servers/user';
|
|
|
|
import {addRecentReaction} from './reactions';
|
|
|
|
import type Model from '@nozbe/watermelondb/Model';
|
|
import type UserModel from '@typings/database/models/servers/user';
|
|
|
|
export const setCurrentUserStatusOffline = async (serverUrl: string) => {
|
|
const serverDatabase = DatabaseManager.serverDatabases[serverUrl];
|
|
if (!serverDatabase) {
|
|
return {error: `No database present for ${serverUrl}`};
|
|
}
|
|
|
|
const {database, operator} = serverDatabase;
|
|
|
|
const user = await queryCurrentUser(database);
|
|
if (!user) {
|
|
return {error: `No current user for ${serverUrl}`};
|
|
}
|
|
|
|
user.prepareStatus(General.OFFLINE);
|
|
|
|
try {
|
|
await operator.batchRecords([user]);
|
|
} catch {
|
|
// eslint-disable-next-line no-console
|
|
console.log('FAILED TO BATCH CHANGES FOR SET CURRENT USER STATUS OFFLINE');
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export const updateLocalCustomStatus = async (serverUrl: string, user: UserModel, customStatus?: UserCustomStatus) => {
|
|
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
|
if (!operator) {
|
|
return {error: `${serverUrl} database not found`};
|
|
}
|
|
|
|
const models: Model[] = [];
|
|
const currentProps = {...user.props, customStatus: customStatus || {}};
|
|
const userModel = user.prepareUpdate((u: UserModel) => {
|
|
u.props = currentProps;
|
|
});
|
|
|
|
models.push(userModel);
|
|
if (customStatus) {
|
|
const recent = await updateRecentCustomStatuses(serverUrl, customStatus, true);
|
|
if (Array.isArray(recent)) {
|
|
models.push(...recent);
|
|
}
|
|
|
|
if (customStatus.emoji) {
|
|
const recentEmojis = await addRecentReaction(serverUrl, customStatus.emoji, true);
|
|
if (Array.isArray(recentEmojis)) {
|
|
models.push(...recentEmojis);
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
await operator.batchRecords(models);
|
|
} catch {
|
|
// eslint-disable-next-line no-console
|
|
console.log('FAILED TO BATCH CHANGES FOR UPDATING CUSTOM STATUS');
|
|
}
|
|
|
|
return {};
|
|
};
|
|
|
|
export const updateRecentCustomStatuses = async (serverUrl: string, customStatus: UserCustomStatus, prepareRecordsOnly = false, remove = false) => {
|
|
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
|
if (!operator) {
|
|
return {error: `${serverUrl} database not found`};
|
|
}
|
|
|
|
const recent = await queryRecentCustomStatuses(operator.database);
|
|
const recentStatuses = (recent ? recent.value : []) as UserCustomStatus[];
|
|
const index = recentStatuses.findIndex((cs) => (
|
|
cs.emoji === customStatus.emoji &&
|
|
cs.text === customStatus.text &&
|
|
cs.duration === customStatus.duration
|
|
));
|
|
|
|
if (index !== -1) {
|
|
recentStatuses.splice(index, 1);
|
|
}
|
|
|
|
if (!remove) {
|
|
recentStatuses.unshift(customStatus);
|
|
}
|
|
|
|
return operator.handleSystem({
|
|
systems: [{
|
|
id: SYSTEM_IDENTIFIERS.RECENT_CUSTOM_STATUS,
|
|
value: JSON.stringify(recentStatuses),
|
|
}],
|
|
prepareRecordsOnly,
|
|
});
|
|
};
|
|
|
|
export const updateLocalUser = async (
|
|
serverUrl: string,
|
|
userDetails: Partial<UserProfile> & { status?: string},
|
|
) => {
|
|
const database = DatabaseManager.serverDatabases[serverUrl]?.database;
|
|
if (!database) {
|
|
return {error: `${serverUrl} database not found`};
|
|
}
|
|
|
|
try {
|
|
const user = await queryCurrentUser(database);
|
|
if (user) {
|
|
await database.write(async () => {
|
|
await user.update((userRecord: UserModel) => {
|
|
userRecord.authService = userDetails.auth_service ?? user.authService;
|
|
userRecord.email = userDetails.email ?? user.email;
|
|
userRecord.firstName = userDetails.first_name ?? user.firstName;
|
|
userRecord.lastName = userDetails.last_name ?? user.lastName;
|
|
userRecord.lastPictureUpdate = userDetails.last_picture_update ?? user.lastPictureUpdate;
|
|
userRecord.locale = userDetails.locale ?? user.locale;
|
|
userRecord.nickname = userDetails.nickname ?? user.nickname;
|
|
userRecord.notifyProps = userDetails.notify_props ?? user.notifyProps;
|
|
userRecord.position = userDetails?.position ?? user.position;
|
|
userRecord.props = userDetails.props ?? user.props;
|
|
userRecord.roles = userDetails.roles ?? user.roles;
|
|
userRecord.status = userDetails?.status ?? user.status;
|
|
userRecord.timezone = userDetails.timezone ?? user.timezone;
|
|
userRecord.username = userDetails.username ?? user.username;
|
|
});
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return {error};
|
|
}
|
|
|
|
return {};
|
|
};
|