Fix profile settings update flow and improve editor UX

This commit is contained in:
Adolfo Reyna
2026-02-20 21:53:10 -05:00
parent 01aeedf950
commit ffb8f9bec5
3 changed files with 125 additions and 35 deletions

4
API.js
View File

@@ -304,6 +304,10 @@ const API = {
return getCall("/user/" + CurrentProfile); return getCall("/user/" + CurrentProfile);
}, },
updateMyProfile(profile, data) { updateMyProfile(profile, data) {
if (CurrentProfile) {
delete userNameCache[CurrentProfile];
delete failedProfileCache[CurrentProfile];
}
return postCall("/user/myProfile", {profile, data}); return postCall("/user/myProfile", {profile, data});
}, },
searchProfiles(query){ searchProfiles(query){

View File

@@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { View, ImageBackground, ScrollView, Picker } from "react-native"; import { View, ImageBackground, ScrollView, Platform } from "react-native";
import { Text, TextInput, Button, Divider, Checkbox, RadioButton } from "react-native-paper"; import { Text, TextInput, Button, Divider, Checkbox, Menu } from "react-native-paper";
import i18n from "../i18nMessages.js"; import i18n from "../i18nMessages.js";
import Moment from 'moment'; import Moment from 'moment';
import 'moment/min/locales'; import 'moment/min/locales';
@@ -25,6 +25,26 @@ let ProfileSettings = () => {
const [updateKey, setUpdateKey] = React.useState(0); const [updateKey, setUpdateKey] = React.useState(0);
const [description, setDescription] = React.useState(viewerProfile.description || ""); const [description, setDescription] = React.useState(viewerProfile.description || "");
const [uploading, setUploading] = React.useState(false); const [uploading, setUploading] = React.useState(false);
const [languageMenuVisible, setLanguageMenuVisible] = React.useState(false);
const previewProfile = {
...(GlobalState.me || {}),
profile: {
...(GlobalState.me?.profile || {}),
firstName: name,
lastName: lastName,
description: description,
language: language,
photo: photoUrl,
},
};
const languageOptions = [
{ value: "es", label: "Español" },
{ value: "en", label: "English" },
{ value: "da", label: "Danish" },
{ value: "fr", label: "French" },
];
const currentLanguageLabel = languageOptions.find((opt) => opt.value === language)?.label || "English";
const pickImage = async () => { const pickImage = async () => {
if (uploading) return; if (uploading) return;
@@ -44,8 +64,8 @@ let ProfileSettings = () => {
setphotoUrl(newPhotoURL); setphotoUrl(newPhotoURL);
if (!GlobalState.me.profile) GlobalState.me.profile = {}; if (!GlobalState.me.profile) GlobalState.me.profile = {};
GlobalState.me.profile.photo = newPhotoURL; GlobalState.me.profile.photo = newPhotoURL;
updateProfile() await updateProfile({ photo: newPhotoURL });
setUpdateKey(updateKey + 1); setUpdateKey((k) => k + 1);
} }
setPhoto(null); setPhoto(null);
setUploading(false); setUploading(false);
@@ -91,27 +111,51 @@ let ProfileSettings = () => {
return uploadedFile; return uploadedFile;
}; };
let updateProfile = async () => { let updateProfile = async (overrides = {}) => {
let currentProfile = await API.getUserProfile(viewer?._id); let currentProfile = {};
const currentData = currentProfile?.data || {}; let currentData = {};
currentProfile = currentProfile?.profile || {};
try { try {
//let currentProfile = JSON.parse(JSON.stringify(viewer.profile)); currentData = JSON.parse(JSON.stringify(viewer?.data || {}));
currentProfile.firstName = name; } catch (_) {
currentProfile.lastName = lastName; currentData = {};
currentProfile.description = description; }
currentProfile.language = language; try {
currentProfile.photo = photoUrl; currentProfile = {
firstName: name,
lastName: lastName,
description,
language,
photo: overrides.photo ?? photoUrl,
};
console.log("updating", currentProfile); console.log("updating", currentProfile);
} catch (error) { } catch (error) {
alert("Error updating profile, contact administrator."); alert("Error updating profile, contact administrator.");
return; return;
} }
let r = await API.updateMyProfile(currentProfile, currentData).catch((e) => { let r = await API.updateMyProfile(currentProfile, currentData).catch((e) => {
console.error("Profile update request failed", e);
alert("Error updating profile, contact administrator."); alert("Error updating profile, contact administrator.");
return null;
}); });
if (!r || r.status !== "ok") {
console.error("Profile update unexpected response", r);
return alert("Error updating profile, contact administrator.");
}
const refreshedProfile = await API.getUserProfile(viewer?._id, true).catch(() => null);
if (refreshedProfile && refreshedProfile._id) {
GlobalState.me = {
...GlobalState.me,
...refreshedProfile,
};
} else {
if (!GlobalState.me.profile) GlobalState.me.profile = {};
GlobalState.me.profile = {
...GlobalState.me.profile,
...currentProfile,
};
}
console.log(r); console.log(r);
setUpdateKey(updateKey + 1); setUpdateKey((k) => k + 1);
} }
return ( return (
@@ -122,7 +166,12 @@ let ProfileSettings = () => {
imageStyle={{ resizeMode: "contain", opacity: 0.05 }} imageStyle={{ resizeMode: "contain", opacity: 0.05 }}
style={{ paddingBottom: 50 }} style={{ paddingBottom: 50 }}
> >
<Text style={{ marginBottom: 10, fontSize: 20 }}>Profile Settings</Text> <View style={{ paddingTop: 10 }}>
<ProfileCardHorizontal profileObj={previewProfile} skipFollow={true} skiptOnPress={true} key={updateKey} />
</View>
<Text style={{ marginBottom: 10, marginTop: 10, fontSize: 20 }}>Profile Setting</Text>
<Button icon="photo" mode="outlined" onPress={pickImage}>{!uploading ? i18n.t("message.updatePhoto") : "uploading"}</Button>
<Divider />
<View style={{ flexDirection: "row", justifyContent: "space-between" }}> <View style={{ flexDirection: "row", justifyContent: "space-between" }}>
<TextInput <TextInput
label={i18n.t("message.name")} label={i18n.t("message.name")}
@@ -145,22 +194,40 @@ let ProfileSettings = () => {
value={description} value={description}
onChangeText={text => setDescription(text)} onChangeText={text => setDescription(text)}
/> />
<Text>Language:</Text> <Text style={{ marginBottom: 4 }}>Language:</Text>
<View style={{ flexDirection: "row" }}> <View style={{ marginBottom: 10 }}>
<RadioButton.Group value={language} onValueChange={setLanguage} style={{ flexDirection: "row" }}> <Menu
<RadioButton.Item value="es" label="Español" /> visible={languageMenuVisible}
<RadioButton.Item value="en" label="English" /> onDismiss={() => setLanguageMenuVisible(false)}
<RadioButton.Item value="da" label="Danish" /> anchor={
<RadioButton.Item value="fr" label="French" /> <Button mode="outlined" onPress={() => setLanguageMenuVisible(true)}>
</RadioButton.Group> {currentLanguageLabel}
</Button>
}
>
{languageOptions.map((option) => (
<Menu.Item
key={option.value}
title={option.label}
onPress={() => {
setLanguage(option.value);
setLanguageMenuVisible(false);
}}
/>
))}
</Menu>
</View> </View>
<Button icon="photo" mode="outlined" onPress={pickImage}>{!uploading ? i18n.t("message.updatePhoto") : "uploading"}</Button> <Button
<Divider /> mode="contained"
<View style={{ paddingTop: 10 }}> onPress={updateProfile}
<Text style={{ fontSize: 20, padding: 5, color: "#666" }}>Preview:</Text> buttonColor="#c44d56"
<ProfileCardHorizontal profileObj={GlobalState.me} skipFollow={true} skiptOnPress={true} key={updateKey} /> textColor="#ffffff"
</View> style={{ marginTop: 8 }}
<Button mode="outlined" onPress={updateProfile}>{i18n.t("message.update")}</Button> contentStyle={{ paddingVertical: 6 }}
>
{i18n.t("message.update")}
</Button>
{/*
<Divider /> <Divider />
<Text style={{ fontSize: 20, padding: 5, color: "#666" }}>{i18n.t("message.optional")}:</Text> <Text style={{ fontSize: 20, padding: 5, color: "#666" }}>{i18n.t("message.optional")}:</Text>
<View style={{ flexDirection: "row", justifyContent: "space-evenly" }}> <View style={{ flexDirection: "row", justifyContent: "space-evenly" }}>
@@ -201,6 +268,7 @@ let ProfileSettings = () => {
//onChangeText={text => setLastName(text)} //onChangeText={text => setLastName(text)}
/> />
<Button mode="outlined" onPress={updateProfile}>{i18n.t("message.update")}</Button> <Button mode="outlined" onPress={updateProfile}>{i18n.t("message.update")}</Button>
*/}
</ImageBackground> </ImageBackground>
</ScrollView> </ScrollView>
) )

View File

@@ -1,21 +1,39 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { View, Share } from 'react-native'; import { View, Share, TouchableOpacity } from 'react-native';
import { Avatar, Button, Card, Title, Paragraph } from 'react-native-paper'; import { Avatar, Button, Card, Title, Paragraph } from 'react-native-paper';
import UserName from './UserName'; import UserName from './UserName';
import FollowButton from './basics/FollowButton'; import FollowButton from './basics/FollowButton';
import { useNavigation } from '@react-navigation/native';
import { useSnapshot } from 'valtio';
import GlobalState from '../contexts/GlobalState.js';
const DefaultPhoto = "https://social.emmint.com/uploads/e6f9be6d665dc43417701bf16a90122c.png"; const DefaultPhoto = "https://social.emmint.com/uploads/e6f9be6d665dc43417701bf16a90122c.png";
const ProfileHeader = ({ profileObj }) => { const ProfileHeader = ({ profileObj }) => {
const navigation = useNavigation();
const viewer = useSnapshot(GlobalState).me || {};
const safeProfileObj = profileObj || {}; const safeProfileObj = profileObj || {};
const safeProfile = safeProfileObj.profile || {}; const safeProfile = safeProfileObj.profile || {};
let photoUrl = safeProfile.photo ? 'https://social.emmint.com/' + safeProfile.photo + '?width=1000&height=1000' : DefaultPhoto; let photoUrl = safeProfile.photo ? 'https://social.emmint.com/' + safeProfile.photo + '?width=1000&height=1000' : DefaultPhoto;
const canEditProfileImage = !!(
safeProfileObj?._id &&
!safeProfileObj?.isGroup &&
String(safeProfileObj._id) === String(viewer?._id || '')
);
const handlePressPhoto = () => {
if (!canEditProfileImage) return;
navigation.navigate("ProfileSettings");
};
return ( return (
<> <>
<Card elevation={3}> <Card elevation={3}>
<Card.Cover source={{ uri: photoUrl, cache: 'force-cache' }} style={{ <TouchableOpacity activeOpacity={canEditProfileImage ? 0.8 : 1} onPress={handlePressPhoto}>
height: 300 <Card.Cover source={{ uri: photoUrl, cache: 'force-cache' }} style={{
}} /> height: 300
}} />
</TouchableOpacity>
<Card.Content style={{}}> <Card.Content style={{}}>
<Title style={{ position: "absolute", top: -60, left: 10, backgroundColor: "rgba(255,255,255,0.4)", padding: 10 }}> <Title style={{ position: "absolute", top: -60, left: 10, backgroundColor: "rgba(255,255,255,0.4)", padding: 10 }}>
<UserName profileid={safeProfileObj._id} /> <UserName profileid={safeProfileObj._id} />