update frontend to backend renames

This commit is contained in:
Christian Fehmer 2025-09-12 15:05:33 +02:00 committed by Christian Fehmer
parent ed7eaed764
commit 95e7e266e7
5 changed files with 29 additions and 27 deletions

View file

@ -1122,7 +1122,7 @@ function convertConnections(
getAuthenticatedUser()?.uid === connection.initiatorUid;
return [
isMyRequest ? connection.friendUid : connection.initiatorUid,
isMyRequest ? connection.receiverUid : connection.initiatorUid,
connection.status === "pending" && !isMyRequest
? "incoming"
: connection.status,

View file

@ -3,7 +3,7 @@ import { Connection } from "@monkeytype/schemas/connections";
import Ape from "../../ape";
import { format } from "date-fns/format";
import { isAuthenticated } from "../../firebase";
import { getFriendUid } from "../../pages/friends";
import { getReceiverUid } from "../../pages/friends";
import * as DB from "../../db";
import { updateFriendRequestsIndicator } from "../account-button";
@ -59,11 +59,11 @@ function refreshList(): void {
}
const content = blockedUsers.map(
(blocked) => `
<tr data-id="${blocked._id}" data-uid="${getFriendUid(blocked)}">
<tr data-id="${blocked._id}" data-uid="${getReceiverUid(blocked)}">
<td><a href="${location.origin}/profile/${
blocked.initiatorUid
}?isUid" router-link>${blocked.initiatorName}</a></td>
<td>${format(new Date(blocked.addedAt), "dd MMM yyyy HH:mm")}</td>
<td>${format(new Date(blocked.lastModified), "dd MMM yyyy HH:mm")}</td>
<td>
<button class="delete">
<i class="fas fa-fw fa-trash-alt"></i>

View file

@ -437,7 +437,7 @@ function isFriend(uid: string | undefined): boolean {
if (uid === undefined || uid === getAuthenticatedUser()?.uid) return false;
return Object.entries(DB.getSnapshot()?.connections ?? []).some(
([friendUid, status]) => friendUid === uid && status === "accepted"
([receiverUid, status]) => receiverUid === uid && status === "accepted"
);
}

View file

@ -35,28 +35,28 @@ let friendsTable: SortedTable<Friend> | undefined = undefined;
let pendingRequests: Connection[] | undefined;
let friendsList: Friend[] | undefined;
export function getFriendUid(
connection: Pick<Connection, "initiatorUid" | "friendUid">
export function getReceiverUid(
connection: Pick<Connection, "initiatorUid" | "receiverUid">
): string {
const me = getAuthenticatedUser();
if (me === null)
throw new Error("expected to be authenticated in getFriendUid");
throw new Error("expected to be authenticated in getReceiverUid");
if (me.uid === connection.initiatorUid) return connection.friendUid;
if (me.uid === connection.initiatorUid) return connection.receiverUid;
return connection.initiatorUid;
}
export async function addFriend(friendName: string): Promise<true | string> {
const result = await Ape.connections.create({ body: { friendName } });
export async function addFriend(receiverName: string): Promise<true | string> {
const result = await Ape.connections.create({ body: { receiverName } });
if (result.status !== 200) {
return `Friend request failed: ${result.body.message}`;
} else {
const snapshot = DB.getSnapshot();
if (snapshot !== undefined) {
const friendUid = getFriendUid(result.body.data);
const receiverUid = getReceiverUid(result.body.data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
snapshot.connections[friendUid] = result.body.data.status;
snapshot.connections[receiverUid] = result.body.data.status;
updatePendingConnections();
}
return true;
@ -89,8 +89,8 @@ const addFriendModal = new SimpleModal({
],
buttonText: "request",
onlineOnly: true,
execFn: async (_thisPopup, friendName) => {
const result = await addFriend(friendName);
execFn: async (_thisPopup, receiverName) => {
const result = await addFriend(receiverName);
if (result !== true) {
return {
@ -98,7 +98,7 @@ const addFriendModal = new SimpleModal({
message: result,
};
} else {
return { status: 1, message: `Request send to ${friendName}` };
return { status: 1, message: `Request send to ${receiverName}` };
}
},
});
@ -153,13 +153,13 @@ function updatePendingConnections(): void {
const html = pendingRequests
.map(
(item) => `<tr data-id="${item._id}" data-friend-uid="${getFriendUid(
item
)}">
(item) => `<tr data-id="${
item._id
}" data-receiver-uid="${getReceiverUid(item)}">
<td><a href="${location.origin}/profile/${
item.initiatorUid
}?isUid" router-link>${item.initiatorName}</a></td>
<td>${formatAge(item.addedAt)} ago</td>
<td>${formatAge(item.lastModified)} ago</td>
<td class="actions">
<button class="accepted" aria-label="accept" data-balloon-pos="up">
<i class="fas fa-check fa-fw"></i>
@ -255,7 +255,9 @@ function buildFriendRow(entry: Friend): HTMLTableRowElement {
</div>
</td>
<td>${
entry.addedAt !== undefined ? formatAge(entry.addedAt, "short") : "-"
entry.lastModified !== undefined
? formatAge(entry.lastModified, "short")
: "-"
}</td>
<td><span aria-label="total xp: ${
isSafeNumber(entry.xp) ? formatXp(entry.xp) : ""
@ -413,16 +415,16 @@ $(".pageFriends .pendingRequests table").on("click", async (e) => {
const snapshot = DB.getSnapshot();
if (snapshot) {
const friendUid = row.dataset["friendUid"];
if (friendUid === undefined) {
throw new Error("Cannot find friendUid of target.");
const receiverUid = row.dataset["receiverUid"];
if (receiverUid === undefined) {
throw new Error("Cannot find receiverUid of target.");
}
if (action === "rejected") {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete, @typescript-eslint/no-unsafe-member-access
delete snapshot.connections[friendUid];
delete snapshot.connections[receiverUid];
} else {
snapshot.connections[friendUid] = action;
snapshot.connections[receiverUid] = action;
}
DB.setSnapshot(snapshot);
}

View file

@ -1439,7 +1439,7 @@ function isFriend(uid: string): boolean {
if (uid === getAuthenticatedUser()?.uid) return false;
return Object.entries(DB.getSnapshot()?.connections ?? []).some(
([friendUid, status]) => friendUid === uid && status === "accepted"
([receiverUid, status]) => receiverUid === uid && status === "accepted"
);
}