Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(egh): add instructor profile edit page #327

Merged
merged 27 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
980e11d
fix: add tailwindcss-radix to egghead deps and turbo dev
zacjones93 Nov 5, 2024
65a71cb
feat: add profile edit
zacjones93 Nov 5, 2024
5f460c7
fix: import styles before other directives
zacjones93 Nov 5, 2024
b4b172f
fix: move login styles to layout
zacjones93 Nov 6, 2024
d78976f
Merge branch 'main' of github.com:joelhooks/course-builder into zac/e…
zacjones93 Nov 13, 2024
b4f7102
Merge branch 'main' of github.com:joelhooks/course-builder into zac/e…
zacjones93 Nov 19, 2024
ae1ce6a
feat: add profile table
zacjones93 Nov 19, 2024
db896cd
feat: add profiles to course builder schema
zacjones93 Nov 19, 2024
28023ac
fix: reorder exported schema
zacjones93 Nov 19, 2024
bfc09d3
add profile to rest of course builder products
zacjones93 Nov 19, 2024
98191b7
add rails instructor sync back
zacjones93 Nov 19, 2024
b5eb1e0
add profile picture with note on gravatar
zacjones93 Nov 19, 2024
321f5f2
chore: refactor profile update logic to users lib
zacjones93 Nov 19, 2024
494dcb9
reset form on submit
zacjones93 Nov 19, 2024
df9dcfd
only show instructor section if can create
zacjones93 Nov 19, 2024
c7e1ce6
fix: properly type next page
zacjones93 Nov 19, 2024
d7eb05d
fix: reset to not using turbo
zacjones93 Nov 19, 2024
e11d5d4
fix: @import goes first
zacjones93 Nov 19, 2024
bacddd8
fix: adjust user shape for getAbilityRules
zacjones93 Nov 19, 2024
464ea7d
fix: appease prettier
zacjones93 Nov 19, 2024
36376bb
Merge branch 'main' into zac/egg-371-add-instructor-profile-edit-page
zacjones93 Nov 20, 2024
a54e4a5
fix: remove UserProfile table
zacjones93 Nov 21, 2024
a34d515
fix: split form into account and profile pages
zacjones93 Nov 21, 2024
6268cae
copy affiliate code to clipboard
zacjones93 Nov 21, 2024
11f6969
Merge branch 'main' of github.com:joelhooks/course-builder into zac/e…
zacjones93 Nov 21, 2024
be35fe3
sync account update to egghead
zacjones93 Nov 21, 2024
b7a7f0b
Merge branch 'main' into zac/egg-371-add-instructor-profile-edit-page
zacjones93 Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/ai-hero/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getCourseBuilderSchema } from '@coursebuilder/adapter-drizzle/mysql'
export const {
accounts,
accountsRelations,
profiles,
profilesRelations,
permissions,
permissionsRelations,
rolePermissions,
Expand Down
2 changes: 2 additions & 0 deletions apps/astro-party/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getCourseBuilderSchema } from '@coursebuilder/adapter-drizzle/mysql'
export const {
accounts,
accountsRelations,
profiles,
profilesRelations,
permissions,
permissionsRelations,
rolePermissions,
Expand Down
2 changes: 2 additions & 0 deletions apps/course-builder-video-blog/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getCourseBuilderSchema } from '@coursebuilder/adapter-drizzle/mysql'
export const {
accounts,
accountsRelations,
profiles,
profilesRelations,
permissions,
permissionsRelations,
rolePermissions,
Expand Down
2 changes: 2 additions & 0 deletions apps/course-builder-web/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getCourseBuilderSchema } from '@coursebuilder/adapter-drizzle/mysql'
export const {
accounts,
accountsRelations,
profiles,
profilesRelations,
permissions,
permissionsRelations,
rolePermissions,
Expand Down
6 changes: 3 additions & 3 deletions apps/egghead/src/ability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ type GetAbilityOptions = {
name: string
description: string | null
active: boolean
createdAt: Date | null
updatedAt: Date | null
deletedAt: Date | null
createdAt?: Date | null
updatedAt?: Date | null
deletedAt?: Date | null
}[]
}
}
Expand Down
37 changes: 37 additions & 0 deletions apps/egghead/src/app/(user)/profile/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { redirect } from 'next/navigation'
import { Layout } from '@/components/app/layout'
import { db } from '@/db'
import { getServerAuthSession } from '@/server/auth'

import EditProfileForm from '../_components/edit-profile-form'

type Props = {
params: Promise<{ id: string }>
}

export default async function ProfilePage(props: Props) {
const { session, ability } = await getServerAuthSession()
const params = await props.params

const fullUser = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.id, params.id),
with: {
profiles: true,
},
})

if (
ability.can('manage', 'all') ||
ability.can('read', 'User', session.user?.id)
) {
return (
<Layout>
<main className="mx-auto w-full max-w-screen-sm">
<EditProfileForm user={fullUser} />
</main>
</Layout>
)
}

redirect('/')
}
201 changes: 201 additions & 0 deletions apps/egghead/src/app/(user)/profile/_components/edit-account-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
'use client'

import React from 'react'
import { createAppAbility } from '@/ability'
import { api } from '@/trpc/react'
import { zodResolver } from '@hookform/resolvers/zod'
import { Clipboard } from 'lucide-react'
import { useForm } from 'react-hook-form'
import { z } from 'zod'

import {
Button,
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
Input,
useToast,
} from '@coursebuilder/ui'

const formSchema = z.object({
name: z.string(),
email: z.string().email(),
affiliateCode: z.string(),
slackChannelId: z.string(),
slackId: z.string(),
})

const EditAccountForm: React.FC<{
user: any
}> = ({ user }) => {
const { data: abilityRules } = api.ability.getCurrentAbilityRules.useQuery()
const ability = createAppAbility(abilityRules)

const isAdmin = ability.can('manage', 'all')
const isContributor = ability.can('create', 'Content')

const { mutateAsync: updateInstructorAccount } =
api.users.updateInstructorAccount.useMutation({
onSuccess: async (data) => {
form.reset()
form.setValue('slackChannelId', data.slackChannelId as string)
form.setValue('slackId', data.slackId as string)
toast({ title: 'Profile updated successfully!' })
},
})
const { toast } = useToast()
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
name: user?.name || '',
email: user?.email || '',
affiliateCode: user?.fields?.affiliateCode || '',
slackChannelId: user?.fields?.slackChannelId || '',
slackId: user?.fields?.slackId || '',
},
values: {
name: user?.name || '',
email: user?.email || '',
affiliateCode: user?.fields?.affiliateCode || '',
slackChannelId: user?.fields?.slackChannelId || '',
slackId: user?.fields?.slackId || '',
},
reValidateMode: 'onBlur',
})

const onSubmit = async (values: z.infer<typeof formSchema>) => {
return await updateInstructorAccount(
{ ...values, userId: user.id },
{
onSuccess: async (data) => {
form.reset()
form.setValue('slackChannelId', data.slackChannelId as string)
form.setValue('slackId', data.slackId as string)
toast({ title: 'Account updated successfully!' })
},
},
)
}

return (
<div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<fieldset className="space-y-5">
<h1 className="text-xl font-semibold">Account Information</h1>
<FormField
name="email"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="email">Email</FormLabel>
<FormControl>
<Input
readOnly
disabled
id="email"
{...field}
onChange={field.onChange}
/>
</FormControl>
<FormDescription>
Contact egghead staff to update your email.
</FormDescription>
</FormItem>
)}
/>
{user?.fields?.affiliateCode && isContributor && (
<FormField
name="affiliateCode"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="affiliateCode">
<div className="flex items-center gap-2">
<p>Affiliate Code</p>
<button
className="rounded-md p-1 hover:bg-gray-100"
type="button"
onClick={() => {
navigator.clipboard.writeText(
`?af=${user?.fields?.affiliateCode}`,
)
toast({
title: 'Affiliate Code Copied to Clipboard',
})
}}
>
<Clipboard className="h-4 w-4" />
</button>
</div>
</FormLabel>
<FormControl>
<Input
readOnly
id="affiliateCode"
{...field}
onChange={field.onChange}
/>
</FormControl>
<FormDescription>
Append your code to the end of egghead URLs to track your
referrals. e.g. ?af={user?.fields?.affiliateCode}
</FormDescription>
</FormItem>
)}
/>
)}
{isAdmin && (
<>
<h3 className="text-lg font-semibold">Admin</h3>
<FormField
name="slackId"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="slackId">Slack ID</FormLabel>
<FormControl>
<Input
id="slackId"
{...field}
onChange={field.onChange}
/>
</FormControl>
<FormDescription></FormDescription>
</FormItem>
)}
/>
<FormField
name="slackChannelId"
render={({ field }) => (
<FormItem>
<FormLabel htmlFor="slackChannelId">
Slack Channel ID
</FormLabel>
<FormControl>
<Input
id="slackChannelId"
{...field}
onChange={field.onChange}
/>
</FormControl>
<FormDescription></FormDescription>
</FormItem>
)}
/>
</>
)}
</fieldset>
{(Object.keys(form.formState.dirtyFields).length > 0 ||
form.formState.isSubmitting) && (
<Button type="submit" disabled={form.formState.isSubmitting}>
Update profile
</Button>
)}
</form>
</Form>
</div>
)
}

export default EditAccountForm
Loading
Loading