Skip to content

Commit ca746f3

Browse files
committed
refactor: move addFriend to db.ts
1 parent 1cbeb68 commit ca746f3

4 files changed

Lines changed: 32 additions & 32 deletions

File tree

frontend/src/ts/components/pages/profile/UserDetails.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ import { formatDistanceToNowStrict } from "date-fns/formatDistanceToNowStrict";
1515
import { createEffect, createSignal, For, JSXElement, Show } from "solid-js";
1616

1717
import { Snapshot } from "../../../constants/default-snapshot";
18-
import { isFriend } from "../../../db";
18+
import { addFriend, isFriend } from "../../../db";
1919
import * as EditProfileModal from "../../../modals/edit-profile";
2020
import * as UserReportModal from "../../../modals/user-report";
21-
import { addFriend } from "../../../pages/friends";
2221
import { bp } from "../../../states/breakpoints";
2322
import { getUserId, isLoggedIn } from "../../../states/core";
2423
import {

frontend/src/ts/db.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,34 @@ function convertConnections(
11451145
);
11461146
}
11471147

1148+
export function getReceiverUid(
1149+
connection: Pick<Connection, "initiatorUid" | "receiverUid">,
1150+
): string {
1151+
const me = getAuthenticatedUser();
1152+
if (me === null) {
1153+
throw new Error("expected to be authenticated in getReceiverUid");
1154+
}
1155+
1156+
if (me.uid === connection.initiatorUid) return connection.receiverUid;
1157+
return connection.initiatorUid;
1158+
}
1159+
1160+
export async function addFriend(receiverName: string): Promise<true | string> {
1161+
const result = await Ape.connections.create({ body: { receiverName } });
1162+
1163+
if (result.status !== 200) {
1164+
return `Friend request failed: ${result.body.message}`;
1165+
} else {
1166+
const snapshot = getSnapshot();
1167+
if (snapshot !== undefined) {
1168+
const receiverUid = getReceiverUid(result.body.data);
1169+
// oxlint-disable-next-line no-unsafe-member-access
1170+
snapshot.connections[receiverUid] = result.body.data.status;
1171+
}
1172+
return true;
1173+
}
1174+
}
1175+
11481176
export function isFriend(uid: string | undefined): boolean {
11491177
if (uid === undefined || uid === getAuthenticatedUser()?.uid) return false;
11501178

frontend/src/ts/elements/account-settings/blocked-user-table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Connection } from "@monkeytype/schemas/connections";
33
import Ape from "../../ape";
44
import { format } from "date-fns/format";
55
import { isAuthenticated } from "../../firebase";
6-
import { getReceiverUid } from "../../pages/friends";
76
import * as DB from "../../db";
7+
import { getReceiverUid } from "../../db";
88
import { qsr } from "../../utils/dom";
99

1010
let blockedUsers: Connection[] = [];

frontend/src/ts/pages/friends.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getAvatarElement } from "../utils/discord-avatar";
2626
import { formatTypingStatsRatio } from "../utils/misc";
2727
import { getLanguageDisplayString } from "../utils/strings";
2828
import * as DB from "../db";
29+
import { addFriend, getReceiverUid } from "../db";
2930
import { getAuthenticatedUser } from "../firebase";
3031
import * as ServerConfiguration from "../ape/server-configuration";
3132
import { authEvent } from "../events/auth";
@@ -42,35 +43,6 @@ let friendsTable: SortedTable<Friend> | undefined = undefined;
4243
let pendingRequests: Connection[] | undefined;
4344
let friendsList: Friend[] | undefined;
4445

45-
export function getReceiverUid(
46-
connection: Pick<Connection, "initiatorUid" | "receiverUid">,
47-
): string {
48-
const me = getAuthenticatedUser();
49-
if (me === null) {
50-
throw new Error("expected to be authenticated in getReceiverUid");
51-
}
52-
53-
if (me.uid === connection.initiatorUid) return connection.receiverUid;
54-
return connection.initiatorUid;
55-
}
56-
57-
export async function addFriend(receiverName: string): Promise<true | string> {
58-
const result = await Ape.connections.create({ body: { receiverName } });
59-
60-
if (result.status !== 200) {
61-
return `Friend request failed: ${result.body.message}`;
62-
} else {
63-
const snapshot = DB.getSnapshot();
64-
if (snapshot !== undefined) {
65-
const receiverUid = getReceiverUid(result.body.data);
66-
// oxlint-disable-next-line no-unsafe-member-access
67-
snapshot.connections[receiverUid] = result.body.data.status;
68-
updatePendingConnections();
69-
}
70-
return true;
71-
}
72-
}
73-
7446
const addFriendModal = new SimpleModal({
7547
id: "addFriend",
7648
title: "Add a friend",
@@ -94,6 +66,7 @@ const addFriendModal = new SimpleModal({
9466
const result = await addFriend(receiverName);
9567

9668
if (result === true) {
69+
updatePendingConnections();
9770
return { status: "success", message: `Request sent to ${receiverName}` };
9871
}
9972

0 commit comments

Comments
 (0)