Refresh profile from server and return after update

This commit is contained in:
Adolfo Reyna
2026-02-20 22:10:44 -05:00
parent ffb8f9bec5
commit cc9ca1d075
2 changed files with 26 additions and 10 deletions

View File

@@ -256,16 +256,25 @@ let Profile = ({ navigation, route }) => {
tag, tag,
}); });
setLoading(true); setLoading(true);
API.getPosts(route.params.profileid).then((data) => { Promise.all([
API.getUserProfile(route.params.profileid, true),
API.getPosts(route.params.profileid),
]).then(([profileObj, data]) => {
const nextProfile = profileObj && profileObj._id ? profileObj : {};
const profileData = nextProfile.profile || {};
const safePosts = Array.isArray(data) ? data : []; const safePosts = Array.isArray(data) ? data : [];
setProfile(nextProfile);
navigation.setOptions({ title: (profileData.firstName || "") + " " + (profileData.lastName || "") });
setPosts(safePosts); setPosts(safePosts);
storeProfilePosts(route.params.profileid, safePosts); storeProfilePosts(route.params.profileid, safePosts);
setLoading(false); setLoading(false);
logProfile('refresh:end', { logProfile('refresh:end', {
profileid: route.params.profileid, profileid: route.params.profileid,
count: safePosts.length, count: safePosts.length,
cached: true, cached: false,
}); });
}).catch(() => {
setLoading(false);
}); });
}} }}
/> : /> :

View File

@@ -10,10 +10,12 @@ import { useSnapshot } from 'valtio';
import GlobalState from '../contexts/GlobalState.js'; import GlobalState from '../contexts/GlobalState.js';
import API from "../API.js"; import API from "../API.js";
import * as ImagePicker from 'expo-image-picker'; import * as ImagePicker from 'expo-image-picker';
import { useNavigation } from '@react-navigation/native';
let ProfileSettings = () => { let ProfileSettings = () => {
const navigation = useNavigation();
const gState = useSnapshot(GlobalState); const gState = useSnapshot(GlobalState);
const viewer = gState.me; const viewer = gState.me;
const viewerProfile = viewer?.profile || {}; const viewerProfile = viewer?.profile || {};
@@ -64,8 +66,10 @@ 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;
await updateProfile({ photo: newPhotoURL }); await updateProfile({ photo: newPhotoURL }, false);
setUpdateKey((k) => k + 1); setUpdateKey((k) => k + 1);
} else {
alert("Could not upload photo. Please try another image.");
} }
setPhoto(null); setPhoto(null);
setUploading(false); setUploading(false);
@@ -74,17 +78,17 @@ let ProfileSettings = () => {
const handleUploadPhoto = async (photo) => { const handleUploadPhoto = async (photo) => {
console.log(photo) console.log(photo)
if (!photo) return; if (!photo) return '';
const uri = const uri =
Platform.OS === "android" Platform.OS === "android"
? photo.uri ? photo.uri
: photo.uri.replace("file://", ""); : photo.uri.replace("file://", "");
console.log(uri); console.log(uri);
const filename = photo.uri.split("/").pop(); const filename = photo.uri.split("/").pop() || "image.jpg";
const match = /\.(\w+)$/.exec(filename); const match = /\.(\w+)$/.exec(filename);
const ext = match?.[1]; const ext = (match?.[1] || "jpg").toLowerCase();
const type = match ? `image/${match[1]}` : `image`; const type = ext === "heic" || ext === "heif" ? "image/heic" : `image/${ext}`;
const formData = new FormData(); const formData = new FormData();
formData.append("banner", { formData.append("banner", {
uri, uri,
@@ -101,7 +105,7 @@ let ProfileSettings = () => {
.then((res) => res.json()) .then((res) => res.json())
.then((data) => { .then((data) => {
console.log(data); console.log(data);
return data.fileName; return data?.fileName || '';
}); });
console.log(uploadedFile); console.log(uploadedFile);
} catch (err) { } catch (err) {
@@ -111,7 +115,7 @@ let ProfileSettings = () => {
return uploadedFile; return uploadedFile;
}; };
let updateProfile = async (overrides = {}) => { let updateProfile = async (overrides = {}, goBackAfter = true) => {
let currentProfile = {}; let currentProfile = {};
let currentData = {}; let currentData = {};
try { try {
@@ -156,6 +160,9 @@ let ProfileSettings = () => {
} }
console.log(r); console.log(r);
setUpdateKey((k) => k + 1); setUpdateKey((k) => k + 1);
if (goBackAfter) {
navigation.goBack();
}
} }
return ( return (
@@ -219,7 +226,7 @@ let ProfileSettings = () => {
</View> </View>
<Button <Button
mode="contained" mode="contained"
onPress={updateProfile} onPress={() => updateProfile({}, true)}
buttonColor="#c44d56" buttonColor="#c44d56"
textColor="#ffffff" textColor="#ffffff"
style={{ marginTop: 8 }} style={{ marginTop: 8 }}