diff --git a/modules/user/queries.tsx b/modules/user/queries.tsx index bd30ab5b..f8c14461 100644 --- a/modules/user/queries.tsx +++ b/modules/user/queries.tsx @@ -16,6 +16,7 @@ import { getDocData, getDocDataOrThrow, getDocsData, + type ResourceType, type UserAuthenticationInformation, } from '@/modules/firebase/utils' import { queryClient } from '@/modules/query/queryClient' @@ -109,16 +110,25 @@ const getUserInvitationData = async (userId: string) => { } } +const invitationPrefix = 'invitation-' + /** * Gets user or invitation data * @param userId Starts with `invitation-` if user is an invitation. * It's necessary to prefix invitation id, because user id and invitation id are not guaranteed to be distinct * */ -export const getUserData = async (userId: string) => { - const prefix = 'invitation-' - if (userId.startsWith(prefix)) { - const id = userId.slice(prefix.length) - return getUserInvitationData(id) - } - return getUserAuthData(userId) -} +export const parseUserId = (userId: string) => + userId.startsWith(invitationPrefix) ? + { + userId: userId.slice(invitationPrefix.length), + resourceType: 'invitation' as const, + } + : { userId, resourceType: 'user' as const } + +export const getUserData = async ( + userId: string, + resourceType: ResourceType, +) => + resourceType === 'invitation' ? + getUserInvitationData(userId) + : getUserAuthData(userId) diff --git a/routes/~_dashboard/~patients/~$id/~index.tsx b/routes/~_dashboard/~patients/~$id/~index.tsx index 7ece3275..bf765995 100644 --- a/routes/~_dashboard/~patients/~$id/~index.tsx +++ b/routes/~_dashboard/~patients/~$id/~index.tsx @@ -21,7 +21,7 @@ import { getDocsData, type ResourceType, } from '@/modules/firebase/utils' -import { getUserData } from '@/modules/user/queries' +import { getUserData, parseUserId } from '@/modules/user/queries' import { Tabs, TabsContent, @@ -84,9 +84,9 @@ export enum PatientPageTab { const PatientPage = () => { const router = useRouter() - const { id: userId } = Route.useParams() const { tab } = Route.useSearch() const { + userId, medications, formProps, userMedications, @@ -246,13 +246,14 @@ export const Route = createFileRoute('/_dashboard/patients/$id/')({ tab: z.nativeEnum(PatientPageTab).optional().catch(undefined), }), loader: async ({ params }) => { - const userId = params.id - const userData = await getUserData(userId) - const { resourceType, user, authUser } = userData + const { userId, resourceType } = parseUserId(params.id) + const userData = await getUserData(userId, resourceType) + const { user, authUser } = userData if (user.type !== UserType.patient) throw notFound() return { user, + userId, authUser, resourceType, medications: await getMedicationsData(), diff --git a/routes/~_dashboard/~users/~$id.tsx b/routes/~_dashboard/~users/~$id.tsx index b53adb48..8942f47d 100644 --- a/routes/~_dashboard/~users/~$id.tsx +++ b/routes/~_dashboard/~users/~$id.tsx @@ -15,6 +15,7 @@ import { getDocDataOrThrow } from '@/modules/firebase/utils' import { queryClient } from '@/modules/query/queryClient' import { getUserData, + parseUserId, userOrganizationQueryOptions, } from '@/modules/user/queries' import { getUserName } from '@/packages/design-system/src/modules/auth/user' @@ -27,8 +28,8 @@ import { DashboardLayout } from '../DashboardLayout' const UserPage = () => { const router = useRouter() - const { id: userId } = Route.useParams() - const { authUser, user, resourceType, organizations } = Route.useLoaderData() + const { authUser, user, resourceType, organizations, userId } = + Route.useLoaderData() const updateUser = async (form: UserFormSchema) => { const authData = { @@ -88,12 +89,13 @@ export const Route = createFileRoute('/_dashboard/users/$id')({ component: UserPage, beforeLoad: () => ensureType([UserType.admin, UserType.owner]), loader: async ({ params }) => { - const userId = params.id - const { resourceType, user, authUser } = await getUserData(userId) + const { resourceType, userId } = parseUserId(params.id) + const { user, authUser } = await getUserData(userId, resourceType) if (user.type === UserType.patient) throw notFound() return { user, + userId, authUser, resourceType, organizations: await queryClient.ensureQueryData(