Fix upload(post) function to update user posts field too

This commit is contained in:
Alex Erdei 2025-05-06 13:01:33 +01:00
parent 36398127ec
commit 48e8eca904
1 changed files with 29 additions and 0 deletions

View File

@ -601,6 +601,35 @@ export async function upload(post) {
body: JSON.stringify(body), body: JSON.stringify(body),
}); // $fetch adds Authorization }); // $fetch adds Authorization
/* --------------------------------------------------------------
2 Update my users.posts array (DB + Redux)
-------------------------------------------------------------- */
try {
const state = store.getState();
const me = state.currentUser; // already normalised
const currentPosts = me?.posts ?? [];
const updatedPosts = [...currentPosts, post_id];
/* 2a. Persist to the server ---------------------------------- */
await $fetch(USERS_URL, {
method: "PUT",
body: JSON.stringify({
user_id,
posts: JSON.stringify(updatedPosts),
}),
});
} catch (err) {
console.warn("[upload] failed to patch users.posts:", err.message);
}
/* keep old contract: caller expects { id } */ /* keep old contract: caller expects { id } */
return { id: post_id }; return { id: post_id };