diff --git a/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.html b/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.html index 704b1b308..b19eee943 100644 --- a/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.html +++ b/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.html @@ -10,7 +10,13 @@
{{ "components.selectCollaborators.title" | translate }}
name="trash" slot="start" > - {{ threadsByUserId[userId].name || threadsByUserId[userId].email }} + {{ + userProfilesById.get(userId)?.name || + userProfilesById.get(userId)?.email || + threadsByUserId[userId]?.name || + threadsByUserId[userId]?.email || + "--" + }} diff --git a/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.ts b/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.ts index 51019ce81..08d714304 100644 --- a/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.ts +++ b/packages/frontend/src/app/components/select-collaborators/select-collaborators.component.ts @@ -1,4 +1,10 @@ -import { Component, EventEmitter, Input, Output } from "@angular/core"; +import { + Component, + EventEmitter, + Input, + Output, + type AfterViewInit, +} from "@angular/core"; import { TranslateService } from "@ngx-translate/core"; import { LoadingService } from "../../services/loading.service"; @@ -6,16 +12,19 @@ import { MessagingService } from "../../services/messaging.service"; import { ToastController, ModalController } from "@ionic/angular"; import { UtilService } from "../../services/util.service"; import { UserService } from "../../services/user.service"; +import { TRPCService } from "../../services/trpc.service"; +import type { UserPublic } from "@recipesage/prisma"; @Component({ selector: "select-collaborators", templateUrl: "select-collaborators.component.html", styleUrls: ["./select-collaborators.component.scss"], }) -export class SelectCollaboratorsComponent { +export class SelectCollaboratorsComponent implements AfterViewInit { @Input() selectedCollaboratorIds: string[] = []; @Output() selectedCollaboratorIdsChanged = new EventEmitter(); + userProfilesById: Map = new Map(); threadsByUserId: any = {}; existingThreads: any = []; pendingThread = ""; @@ -31,15 +40,35 @@ export class SelectCollaboratorsComponent { public toastCtrl: ToastController, public modalCtrl: ModalController, public userService: UserService, + public trpcService: TRPCService, public utilService: UtilService, public loadingService: LoadingService, public messagingService: MessagingService, public translate: TranslateService, - ) { + ) {} + + ngAfterViewInit() { this.loadThreads().then( () => {}, () => {}, ); + + this.loadUserProfiles(); + } + + async loadUserProfiles() { + if (!this.selectedCollaboratorIds.length) return; + + const userProfiles = await this.trpcService.handle( + this.trpcService.trpc.users.getUserProfilesById.query({ + ids: this.selectedCollaboratorIds, + }), + ); + + if (!userProfiles) return; + this.userProfilesById = new Map( + userProfiles.map((userProfile) => [userProfile.id, userProfile]), + ); } async loadThreads() { diff --git a/packages/trpc/src/index.ts b/packages/trpc/src/index.ts index d1136c320..4bffe2370 100644 --- a/packages/trpc/src/index.ts +++ b/packages/trpc/src/index.ts @@ -58,6 +58,7 @@ import { getShoppingLists } from "./procedures/shoppingLists/getShoppingLists"; import { updateShoppingList } from "./procedures/shoppingLists/updateShoppingList"; import { updateShoppingListItem } from "./procedures/shoppingLists/updateShoppingListItem"; import { updateShoppingListItems } from "./procedures/shoppingLists/updateShoppingListItems"; +import { getUserProfilesById } from "./procedures/users/getUserProfilesById"; const appRouter = router({ labelGroups: router({ @@ -121,6 +122,7 @@ const appRouter = router({ }), users: router({ getMe, + getUserProfilesById, updatePreferences, getPreferences, signInWithGoogle, diff --git a/packages/trpc/src/procedures/users/getUserProfilesById.ts b/packages/trpc/src/procedures/users/getUserProfilesById.ts new file mode 100644 index 000000000..750107363 --- /dev/null +++ b/packages/trpc/src/procedures/users/getUserProfilesById.ts @@ -0,0 +1,27 @@ +import { prisma } from "@recipesage/prisma"; +import { publicProcedure } from "../../trpc"; +import { userPublic } from "@recipesage/prisma"; +import { validateTrpcSession } from "@recipesage/util/server/general"; +import { z } from "zod"; + +export const getUserProfilesById = publicProcedure + .input( + z.object({ + ids: z.array(z.string().uuid()).min(1).max(100), + }), + ) + .query(async ({ ctx, input }) => { + const session = ctx.session; + validateTrpcSession(session); + + const profiles = await prisma.user.findMany({ + where: { + id: { + in: input.ids, + }, + }, + ...userPublic, + }); + + return profiles; + });