Skip to content

Commit

Permalink
dirty temp!
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszbachorski committed Jul 21, 2024
1 parent df4783d commit 7b14b9b
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
123 changes: 123 additions & 0 deletions app/(dashboard)/users/[id]/EditUserForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use client'
import { Role } from '@/modules/firebase/role'
import { Button } from '@/packages/design-system/src/components/Button'
import { Input } from '@/packages/design-system/src/components/Input'
import {

Check warning on line 5 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L2-L5

Added lines #L2 - L5 were not covered by tests
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/packages/design-system/src/components/Select'
import type { Data } from './page'
import { useUser } from '@/modules/firebase/UserProvider'

Check warning on line 13 in app/(dashboard)/users/[id]/EditUserForm.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/EditUserForm.tsx#L13

[import/order] `@/modules/firebase/UserProvider` import should occur before import of `@/packages/design-system/src/components/Button`
import { Field } from '@/packages/design-system/src/forms/Field'

Check warning on line 14 in app/(dashboard)/users/[id]/EditUserForm.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/EditUserForm.tsx#L14

[import/order] `@/packages/design-system/src/forms/Field` import should occur before type import of `./page`
import { useForm } from '@/packages/design-system/src/forms/useForm'

Check warning on line 15 in app/(dashboard)/users/[id]/EditUserForm.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/EditUserForm.tsx#L15

[import/order] `@/packages/design-system/src/forms/useForm` import should occur before type import of `./page`
import { editUserFormSchema } from './utils'

Check warning on line 16 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L13-L16

Added lines #L13 - L16 were not covered by tests

interface EditUserDialogFormProps {
data: Data
}

export const EditUserForm = ({ data }: EditUserDialogFormProps) => {
const authUser = useUser()
const form = useForm({

Check warning on line 24 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L22-L24

Added lines #L22 - L24 were not covered by tests
formSchema: editUserFormSchema,
defaultValues: {
email: data.authUser.email ?? '',
displayName: data.authUser.displayName ?? '',
role: data.role.role,
organizationId: data.user?.organization,
invitationCode: data.user?.invitationCode,
},
})

const handleSubmit = form.handleSubmit((data) => {
console.log(data)

Check warning on line 36 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L35-L36

Added lines #L35 - L36 were not covered by tests
})

return (

Check warning on line 39 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L39

Added line #L39 was not covered by tests
<form onSubmit={handleSubmit}>
<Field
control={form.control}
name="email"
label="Email"
render={({ field }) => <Input type="email" {...field} />}

Check warning on line 45 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L45

Added line #L45 was not covered by tests
/>
<Field
control={form.control}
name="displayName"
label="Display name"
render={({ field }) => <Input {...field} />}

Check warning on line 51 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L51

Added line #L51 was not covered by tests
/>
<Field
control={form.control}
name="invitationCode"
label="Invitation code"
render={({ field }) => <Input {...field} />}

Check warning on line 57 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L57

Added line #L57 was not covered by tests
/>
{authUser.role === Role.admin && (
<Field

Check warning on line 60 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L60

Added line #L60 was not covered by tests
control={form.control}
name="organizationId"
label="Organization"
render={({ field }) => (
<Select onValueChange={field.onChange} {...field}>

Check warning on line 65 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L65

Added line #L65 was not covered by tests
<SelectTrigger>
<SelectValue placeholder="Organization" />
</SelectTrigger>
<SelectContent>
{data.organizations.map((organization) => (
<SelectItem value={organization.id} key={organization.id}>

Check warning on line 71 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L71

Added line #L71 was not covered by tests
{organization.name}
</SelectItem>
))}
</SelectContent>
</Select>
)}
/>
)}
<Field
control={form.control}
name="role"
label="Role"
render={({ field }) => (
<Select

Check warning on line 85 in app/(dashboard)/users/[id]/EditUserForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/EditUserForm.tsx#L85

Added line #L85 was not covered by tests
onValueChange={field.onChange}
{...field}
disabled={authUser.uid === data.authUser.id}
>
<SelectTrigger>
<SelectValue placeholder="Roles" />
</SelectTrigger>
<SelectContent>
<SelectItem value={Role.clinician} itemText="Clinician">
<div className="flex flex-col">
<b>Clinician</b>
<p>Clinician can access their organization data </p>
</div>
</SelectItem>
<SelectItem value={Role.owner} itemText="Organization owner">
<div className="flex flex-col">
<b>Organization owner</b>
<p>
Organization owner can manage their organization users and
data
</p>
</div>
</SelectItem>
<SelectItem value={Role.admin} itemText="Admin">
<div className="flex flex-col">
<b>Admin</b>
<p>Admin can modify every organization and invite users</p>
</div>
</SelectItem>
</SelectContent>
</Select>
)}
/>

<Button type="submit">Save user</Button>
</form>
)
}
10 changes: 10 additions & 0 deletions app/(dashboard)/users/[id]/actions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use server'
import { revalidatePath } from 'next/cache'

Check failure on line 2 in app/(dashboard)/users/[id]/actions.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/actions.tsx#L2

[@typescript-eslint/no-unused-vars] 'revalidatePath' is defined but never used. Allowed unused vars must match /^_/u.
import { type z } from 'zod'
import { type editUserFormSchema } from '@/app/(dashboard)/users/[id]/utils'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'

Check warning on line 5 in app/(dashboard)/users/[id]/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/actions.tsx#L5

Added line #L5 was not covered by tests
import { routes } from '@/modules/routes'

Check failure on line 6 in app/(dashboard)/users/[id]/actions.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/actions.tsx#L6

[@typescript-eslint/no-unused-vars] 'routes' is defined but never used. Allowed unused vars must match /^_/u.

export const editUser = async (payload: z.infer<typeof editUserFormSchema>) => {

Check failure on line 8 in app/(dashboard)/users/[id]/actions.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/actions.tsx#L8

[@typescript-eslint/no-unused-vars] 'payload' is defined but never used. Allowed unused args must match /^_/u.
const { callables } = await getAuthenticatedOnlyApp()

Check failure on line 9 in app/(dashboard)/users/[id]/actions.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/actions.tsx#L9

[@typescript-eslint/no-unused-vars] 'callables' is assigned a value but never used. Allowed unused vars must match /^_/u.

Check warning on line 9 in app/(dashboard)/users/[id]/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/actions.tsx#L8-L9

Added lines #L8 - L9 were not covered by tests
}
67 changes: 67 additions & 0 deletions app/(dashboard)/users/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//
import { Users } from 'lucide-react'
import { notFound } from 'next/navigation'
import { EditUserForm } from './EditUserForm'

Check warning on line 10 in app/(dashboard)/users/[id]/page.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(dashboard)/users/[id]/page.tsx#L10

[import/order] `./EditUserForm` import should occur after import of `@/packages/design-system/src/molecules/DashboardLayout`
import {

Check warning on line 11 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L8-L11

Added lines #L8 - L11 were not covered by tests
allowRoles,
getAuthenticatedOnlyApp,
getUserRole,
} from '@/modules/firebase/guards'
import { Role } from '@/modules/firebase/role'
import { mapAuthData } from '@/modules/firebase/user'
import { getDocData, getDocsData } from '@/modules/firebase/utils'
import { getUserName } from '@/packages/design-system/src/modules/auth/user'
import { PageTitle } from '@/packages/design-system/src/molecules/DashboardLayout'
import { DashboardLayout } from '../../DashboardLayout'

Check warning on line 21 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L16-L21

Added lines #L16 - L21 were not covered by tests

const getData = async (userId: string) => {
const { refs, docRefs } = await getAuthenticatedOnlyApp()
const organizations = await getDocsData(refs.organizations())
const user = await getDocData(docRefs.user(userId))
const allAuthData = await mapAuthData([userId], (data, id) => ({

Check warning on line 27 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L23-L27

Added lines #L23 - L27 were not covered by tests
id,
...data,
}))
const authUser = allAuthData.at(0)

Check warning on line 31 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L31

Added line #L31 was not covered by tests

if (!authUser) {
notFound()

Check warning on line 34 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L34

Added line #L34 was not covered by tests
}
return {

Check warning on line 36 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L36

Added line #L36 was not covered by tests
user,
organizations,
authUser,
role: await getUserRole(userId),
}
}

export type Data = Awaited<ReturnType<typeof getData>>

const UserPage = async ({ params }: { params: { id: string } }) => {
await allowRoles([Role.admin, Role.owner])
const data = await getData(params.id)

Check warning on line 48 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L46-L48

Added lines #L46 - L48 were not covered by tests

return (

Check warning on line 50 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L50

Added line #L50 was not covered by tests
<DashboardLayout
title={
<PageTitle
title="Edit user"
subTitle={getUserName(data.authUser)}
icon={<Users />}
/>
}
>
<div className="mx-auto w-full max-w-[800px]">
<EditUserForm data={data} />
</div>
</DashboardLayout>
)
}

export default UserPage

Check warning on line 67 in app/(dashboard)/users/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/page.tsx#L67

Added line #L67 was not covered by tests
10 changes: 10 additions & 0 deletions app/(dashboard)/users/[id]/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod'
import { Role } from '@/modules/firebase/role'

Check warning on line 2 in app/(dashboard)/users/[id]/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/utils.ts#L1-L2

Added lines #L1 - L2 were not covered by tests

export const editUserFormSchema = z.object({

Check warning on line 4 in app/(dashboard)/users/[id]/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/users/[id]/utils.ts#L4

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

0 comments on commit 7b14b9b

Please sign in to comment.