Skip to content

Commit

Permalink
feat: add user logger
Browse files Browse the repository at this point in the history
  • Loading branch information
elskow committed Dec 21, 2023
1 parent 9d3eb62 commit 8f98b15
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
8 changes: 0 additions & 8 deletions src/pages/api/addkegiatan/route.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { postRouter } from '@/server/api/routers/post'
import { userRouter } from '@/server/api/routers/user'
import { createTRPCRouter } from '@/server/api/trpc'

/**
Expand All @@ -8,6 +9,7 @@ import { createTRPCRouter } from '@/server/api/trpc'
*/
export const appRouter = createTRPCRouter({
post: postRouter,
user: userRouter,
})

// export type definition of API
Expand Down
27 changes: 27 additions & 0 deletions src/server/api/routers/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from 'zod'

import { createTRPCRouter, publicProcedure } from '@/server/api/trpc'
import { db } from '@/server/db'

export const userRouter = createTRPCRouter({
addUser: publicProcedure
.input(z.object({ email: z.string().nonempty("Email cannot be empty") }))
.query(async ({ input }) => {
let user = await db.user.findFirst({
where: { email: input.email.trim() },
})

if (!user) {
user = await db.user.create({
data: {
email: input.email.trim(),
role: 'User',
},
})
}

return {
user,
}
}),
})
6 changes: 5 additions & 1 deletion src/ui/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useSession } from 'next-auth/react'
import GoogleLoginBtn from '@/ui/components/GoogleLoginBtn'
import LoadingSpinner from '@/ui/components/LoadingSpinner'
import GoogleLogoutBtn from './components/GoogleLogoutBtn'
import { api } from '@/utils/api'

const navItems = [
{ label: 'Home', icon: <FaHome />, href: '/' },
Expand Down Expand Up @@ -64,6 +65,9 @@ const MainLayout = ({ children }: MainLayoutProps) => {
const [isOpen, setIsOpen] = useState(false)
const [isScrolling, setIsScrolling] = useState(false)

const email = session?.user?.email ?? ''
const _ = api.user.addUser.useQuery({ email })

useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 0) {
Expand Down Expand Up @@ -130,7 +134,7 @@ const MainLayout = ({ children }: MainLayoutProps) => {
)}
</ul>
</div>
{session ? (
{session?.user?.email ? (
<div className='flex flex-grow overflow-y-hidden'>
<aside className='hidden w-80 overflow-y-hidden border-r border-gray-200 bg-white p-4 pt-12 md:sticky md:top-0 md:block md:pt-6'>
<NavList />
Expand Down

0 comments on commit 8f98b15

Please sign in to comment.