Skip to content

Commit

Permalink
Restructure everything after overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszbachorski committed Jul 30, 2024
1 parent a0d4be2 commit af44259
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 348 deletions.
13 changes: 5 additions & 8 deletions app/(dashboard)/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
//
import Link from 'next/link'
import { LogoType } from '@/components/icons/LogoType'
import {
getAuthenticatedOnlyApp,
getCurrentUserRole,
} from '@/modules/firebase/guards'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'

Check warning on line 10 in app/(dashboard)/DashboardLayout.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/DashboardLayout.tsx#L10

Added line #L10 was not covered by tests
import { getUserInfo } from '@stanfordbdhg/design-system/modules/auth/user'
import {
DashboardLayout as DashboardLayoutBase,
Expand All @@ -23,8 +20,8 @@ interface DashboardLayoutProps
extends Omit<DashboardLayoutPropsBase, 'aside' | 'mobile'> {}

export const DashboardLayout = async (props: DashboardLayoutProps) => {
const { currentUser } = await getAuthenticatedOnlyApp()
const { role: userRole } = await getCurrentUserRole()
const { currentUser, user: userDoc } = await getAuthenticatedOnlyApp()
const role = userDoc.type

Check warning on line 24 in app/(dashboard)/DashboardLayout.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/DashboardLayout.tsx#L23-L24

Added lines #L23 - L24 were not covered by tests
const user = <User user={getUserInfo(currentUser)} />

return (
Expand All @@ -35,15 +32,15 @@ export const DashboardLayout = async (props: DashboardLayoutProps) => {
<LogoType className="!h-auto !w-full px-2 xl:px-8" />
</Link>
<nav className="mt-9 flex flex-col gap-1 xl:w-full">
<MenuLinks role={userRole} />
<MenuLinks userType={role} />
</nav>
{user}
</>
}
mobile={
<>
<nav className="mt-9 flex flex-col gap-1 px-4">
<MenuLinks role={userRole} />
<MenuLinks userType={role} />
</nav>
{user}
</>
Expand Down
8 changes: 4 additions & 4 deletions app/(dashboard)/MenuLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
'use client'
import { Home, Users, Contact } from 'lucide-react'
import { usePathname } from 'next/navigation'
import { Role } from '@/modules/firebase/role'
import { UserType } from '@/modules/firebase/utils'
import { routes } from '@/modules/routes'

Check warning on line 12 in app/(dashboard)/MenuLinks.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/MenuLinks.tsx#L11-L12

Added lines #L11 - L12 were not covered by tests
import { MenuItem } from '@stanfordbdhg/design-system/molecules/DashboardLayout'

interface MenuLinksProps {
role: Role
userType: UserType
}

export const MenuLinks = ({ role }: MenuLinksProps) => {
export const MenuLinks = ({ userType }: MenuLinksProps) => {

Check warning on line 19 in app/(dashboard)/MenuLinks.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/MenuLinks.tsx#L19

Added line #L19 was not covered by tests
const pathname = usePathname()

const hrefProps = (href: string) => ({
Expand All @@ -27,7 +27,7 @@ export const MenuLinks = ({ role }: MenuLinksProps) => {
return (
<>
<MenuItem {...hrefProps('/')} label="Home" icon={<Home />} />
{[Role.admin, Role.owner].includes(role) && (
{[UserType.admin, UserType.owner].includes(userType) && (
<MenuItem

Check warning on line 31 in app/(dashboard)/MenuLinks.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/MenuLinks.tsx#L31

Added line #L31 was not covered by tests
{...hrefProps(routes.users.index)}
label="Users"
Expand Down
15 changes: 5 additions & 10 deletions app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
// SPDX-License-Identifier: MIT
//
import { type ReactNode } from 'react'
import {
getAuthenticatedOnlyApp,
getCurrentUserRole,
} from '@/modules/firebase/guards'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'
import { UserContextProvider } from '@/modules/firebase/UserProvider'
import { getDocData } from '@/modules/firebase/utils'
import { getDocDataOrThrow } from '@/modules/firebase/utils'
import { getUserInfo } from '@/packages/design-system/src/modules/auth/user'

Check warning on line 12 in app/(dashboard)/layout.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/layout.tsx#L9-L12

Added lines #L9 - L12 were not covered by tests

interface DashboardLayoutProps {
Expand All @@ -22,14 +19,12 @@ export const dynamic = 'force-dynamic'

const DashboardLayout = async ({ children }: DashboardLayoutProps) => {
const { currentUser, docRefs } = await getAuthenticatedOnlyApp()
const { role } = await getCurrentUserRole()
const user = await getDocData(docRefs.user(currentUser.uid))
const user = await getDocDataOrThrow(docRefs.user(currentUser.uid))
return (

Check warning on line 23 in app/(dashboard)/layout.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/layout.tsx#L20-L23

Added lines #L20 - L23 were not covered by tests
<UserContextProvider
user={{
...getUserInfo(currentUser),
organization: user?.organization,
role,
auth: getUserInfo(currentUser),
user,
}}
>
{children}
Expand Down
46 changes: 10 additions & 36 deletions app/(dashboard)/patients/PatientForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
//
'use client'
import { z } from 'zod'

Check warning on line 9 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L9

Added line #L9 was not covered by tests
import { Role } from '@/modules/firebase/role'
import { useUser } from '@/modules/firebase/UserProvider'
import { type Organization, type User } from '@/modules/firebase/utils'
import { type User } from '@/modules/firebase/utils'
import { Button } from '@/packages/design-system/src/components/Button'
import { Input } from '@/packages/design-system/src/components/Input'
import {

Check warning on line 13 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L11-L13

Added lines #L11 - L13 were not covered by tests
Expand All @@ -21,48 +19,45 @@ import {
} from '@/packages/design-system/src/components/Select'
import { Field } from '@/packages/design-system/src/forms/Field'
import { useForm } from '@/packages/design-system/src/forms/useForm'
import { type UserInfo } from '@/packages/design-system/src/modules/auth/user'
import {

Check warning on line 22 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L20-L22

Added lines #L20 - L22 were not covered by tests
getUserName,
type UserInfo,
} from '@/packages/design-system/src/modules/auth/user'

export const patientFormSchema = z.object({

Check warning on line 27 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L27

Added line #L27 was not covered by tests
email: z.string().min(1, 'Email is required'),
displayName: z.string(),
invitationCode: z.string(),
clinician: z.string(),
organizationId: z.string().optional(),
})

export type PatientFormSchema = z.infer<typeof patientFormSchema>

interface PatientFormProps {
organizations: Array<Pick<Organization, 'name' | 'id'>>
clinicians: Array<{
id: string
name: string
displayName: string | null
email: string | null
}>
userInfo?: Pick<UserInfo, 'email' | 'displayName' | 'uid'>
user?: Pick<User, 'organization' | 'invitationCode'>
user?: Pick<User, 'organization' | 'invitationCode' | 'clinician'>
onSubmit: (data: PatientFormSchema) => Promise<void>
}

export const PatientForm = ({

Check warning on line 47 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L47

Added line #L47 was not covered by tests
organizations,
user,
clinicians,
userInfo,
onSubmit,
}: PatientFormProps) => {
const isEdit = !!user
const authUser = useUser()
const form = useForm({

Check warning on line 54 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L52-L54

Added lines #L52 - L54 were not covered by tests
formSchema: patientFormSchema,
defaultValues: {
email: userInfo?.email ?? '',
displayName: userInfo?.displayName ?? '',
organizationId:
authUser.role === Role.owner ?
authUser.organization
: user?.organization,
invitationCode: user?.invitationCode ?? '',
clinician: user?.clinician ?? '',
},
})

Expand Down Expand Up @@ -92,27 +87,6 @@ export const PatientForm = ({
render={({ field }) => <Input {...field} />}

Check warning on line 87 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L87

Added line #L87 was not covered by tests
/>
)}
{authUser.role === Role.admin && (
<Field
control={form.control}
name="organizationId"
label="Organization"
render={({ field }) => (
<Select onValueChange={field.onChange} {...field}>
<SelectTrigger>
<SelectValue placeholder="Organization" />
</SelectTrigger>
<SelectContent>
{organizations.map((organization) => (
<SelectItem value={organization.id} key={organization.id}>
{organization.name}
</SelectItem>
))}
</SelectContent>
</Select>
)}
/>
)}
<Field
control={form.control}
name="clinician"
Expand All @@ -125,7 +99,7 @@ export const PatientForm = ({
<SelectContent>
{clinicians.map((clinician) => (
<SelectItem value={clinician.id} key={clinician.id}>

Check warning on line 101 in app/(dashboard)/patients/PatientForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/PatientForm.tsx#L101

Added line #L101 was not covered by tests
{clinician.name}
{getUserName(clinician)}
</SelectItem>
))}
</SelectContent>
Expand Down
22 changes: 9 additions & 13 deletions app/(dashboard)/patients/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: MIT
//
import { deleteField } from '@firebase/firestore'
import { deleteField, updateDoc } from '@firebase/firestore'
import { Contact } from 'lucide-react'
import { revalidatePath } from 'next/cache'
import { notFound } from 'next/navigation'
Expand All @@ -14,14 +14,13 @@ import {
type PatientFormSchema,
} from '@/app/(dashboard)/patients/PatientForm'
import { getFormProps } from '@/app/(dashboard)/patients/utils'
import { getAuthenticatedOnlyApp, getUserRole } from '@/modules/firebase/guards'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'
import { mapAuthData } from '@/modules/firebase/user'
import { getDocDataOrThrow, updateDocData } from '@/modules/firebase/utils'
import { getDocDataOrThrow, UserType } from '@/modules/firebase/utils'
import { routes } from '@/modules/routes'
import { getUserName } from '@/packages/design-system/src/modules/auth/user'
import { PageTitle } from '@/packages/design-system/src/molecules/DashboardLayout'
import { DashboardLayout } from '../../DashboardLayout'
import { Role } from '@/modules/firebase/role'

interface PatientPageProps {
params: { id: string }
Expand All @@ -32,15 +31,14 @@ const PatientPage = async ({ params }: PatientPageProps) => {
const userId = params.id
const allAuthData = await mapAuthData({ userIds: [userId] }, (data, id) => ({
uid: id,
...data,
email: data.auth.email,
displayName: data.auth.displayName,
}))
const authUser = allAuthData.at(0)?.auth
const userRole = await getUserRole(userId)
if (!authUser || userRole.role !== Role.user) {
const authUser = allAuthData.at(0)
const user = await getDocDataOrThrow(docRefs.user(userId))
if (!authUser || user.type !== UserType.patient) {
notFound()
}
const user = await getDocDataOrThrow(docRefs.user(userId))
const patient = await getDocDataOrThrow(docRefs.patient(userId))

const updatePatient = async (form: PatientFormSchema) => {
'use server'
Expand All @@ -51,13 +49,11 @@ const PatientPage = async ({ params }: PatientPageProps) => {
auth: {
displayName: form.displayName,
email: form.email,
phoneNumber: null,
photoURL: null,
},
},
})
const userRef = docRefs.user(userId)
await updateDocData(userRef, {
await updateDoc(userRef, {
invitationCode: form.invitationCode,
organization: form.organizationId ?? deleteField(),

Check failure on line 58 in app/(dashboard)/patients/[id]/page.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/patients/[id]/page.tsx#L58

[@typescript-eslint/no-unsafe-assignment] Unsafe assignment of an `any` value.
})
Expand Down
1 change: 0 additions & 1 deletion app/(dashboard)/patients/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const deletePatient = async (payload: { userId: string }) => {

export const deleteInvitation = async (payload: { invitationId: string }) => {
const { docRefs } = await getAuthenticatedOnlyApp()
// TODO: https://github.com/StanfordBDHG/ENGAGE-HF-Firebase/issues/38
await deleteDoc(docRefs.invitation(payload.invitationId))
revalidatePath(routes.patients.index)
return 'success'

Check warning on line 25 in app/(dashboard)/patients/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/actions.tsx#L21-L25

Added lines #L21 - L25 were not covered by tests
Expand Down
13 changes: 6 additions & 7 deletions app/(dashboard)/patients/invite/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Contact } from 'lucide-react'
import { redirect } from 'next/navigation'
import { getFormProps } from '@/app/(dashboard)/patients/utils'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'
import { getDocDataOrThrow, UserType } from '@/modules/firebase/utils'
import { routes } from '@/modules/routes'
import { PageTitle } from '@/packages/design-system/src/molecules/DashboardLayout'
import { DashboardLayout } from '../../DashboardLayout'
Expand All @@ -17,20 +18,18 @@ import { PatientForm, type PatientFormSchema } from '../PatientForm'
const InvitePatientPage = async () => {
const invitePatient = async (form: PatientFormSchema) => {

Check warning on line 19 in app/(dashboard)/patients/invite/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/invite/page.tsx#L18-L19

Added lines #L18 - L19 were not covered by tests
'use server'
const { callables } = await getAuthenticatedOnlyApp()
const { callables, docRefs } = await getAuthenticatedOnlyApp()
const clinician = await getDocDataOrThrow(docRefs.user(form.clinician))
await callables.createInvitation({

Check warning on line 23 in app/(dashboard)/patients/invite/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/invite/page.tsx#L21-L23

Added lines #L21 - L23 were not covered by tests
auth: {
displayName: form.displayName,
email: form.email,
phoneNumber: null,
photoURL: null,
},
// TODO
patient: {
user: {
type: UserType.patient,
clinician: form.clinician,
dateOfBirth: form.dateOfBirth,
organization: clinician.organization,
},
user: { organization: form.organizationId },
})
redirect(routes.patients.index)

Check warning on line 34 in app/(dashboard)/patients/invite/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/invite/page.tsx#L34

Added line #L34 was not covered by tests
// TODO: Confirmation message
Expand Down
Loading

0 comments on commit af44259

Please sign in to comment.