This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
373 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
import prisma from "@/lib/db"; | ||
|
||
export async function GET(req, {params}) { | ||
const { id } = params; | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
return new NextResponse("User not found", { status: 404 }); | ||
} | ||
|
||
return NextResponse.json(user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use client"; | ||
|
||
import { UserContextProvider } from "@/lib/hooks/useUser"; | ||
|
||
function UserProvider({ session, children }) { | ||
return ( | ||
<UserContextProvider session={session}>{children}</UserContextProvider> | ||
); | ||
} | ||
|
||
export default UserProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import * as React from "react" | ||
import { Slot } from "@radix-ui/react-slot" | ||
import { Controller, FormProvider, useFormContext } from "react-hook-form"; | ||
|
||
import { cn } from "@/lib/utils" | ||
import { Label } from "@/components/ui/label" | ||
|
||
const Form = FormProvider | ||
|
||
const FormFieldContext = React.createContext({}) | ||
|
||
const FormField = ( | ||
{ | ||
...props | ||
} | ||
) => { | ||
return ( | ||
(<FormFieldContext.Provider value={{ name: props.name }}> | ||
<Controller {...props} /> | ||
</FormFieldContext.Provider>) | ||
); | ||
} | ||
|
||
const useFormField = () => { | ||
const fieldContext = React.useContext(FormFieldContext) | ||
const itemContext = React.useContext(FormItemContext) | ||
const { getFieldState, formState } = useFormContext() | ||
|
||
const fieldState = getFieldState(fieldContext.name, formState) | ||
|
||
if (!fieldContext) { | ||
throw new Error("useFormField should be used within <FormField>") | ||
} | ||
|
||
const { id } = itemContext | ||
|
||
return { | ||
id, | ||
name: fieldContext.name, | ||
formItemId: `${id}-form-item`, | ||
formDescriptionId: `${id}-form-item-description`, | ||
formMessageId: `${id}-form-item-message`, | ||
...fieldState, | ||
} | ||
} | ||
|
||
const FormItemContext = React.createContext({}) | ||
|
||
const FormItem = React.forwardRef(({ className, ...props }, ref) => { | ||
const id = React.useId() | ||
|
||
return ( | ||
(<FormItemContext.Provider value={{ id }}> | ||
<div ref={ref} className={cn("space-y-2", className)} {...props} /> | ||
</FormItemContext.Provider>) | ||
); | ||
}) | ||
FormItem.displayName = "FormItem" | ||
|
||
const FormLabel = React.forwardRef(({ className, ...props }, ref) => { | ||
const { error, formItemId } = useFormField() | ||
|
||
return ( | ||
(<Label | ||
ref={ref} | ||
className={cn(error && "text-destructive", className)} | ||
htmlFor={formItemId} | ||
{...props} />) | ||
); | ||
}) | ||
FormLabel.displayName = "FormLabel" | ||
|
||
const FormControl = React.forwardRef(({ ...props }, ref) => { | ||
const { error, formItemId, formDescriptionId, formMessageId } = useFormField() | ||
|
||
return ( | ||
(<Slot | ||
ref={ref} | ||
id={formItemId} | ||
aria-describedby={ | ||
!error | ||
? `${formDescriptionId}` | ||
: `${formDescriptionId} ${formMessageId}` | ||
} | ||
aria-invalid={!!error} | ||
{...props} />) | ||
); | ||
}) | ||
FormControl.displayName = "FormControl" | ||
|
||
const FormDescription = React.forwardRef(({ className, ...props }, ref) => { | ||
const { formDescriptionId } = useFormField() | ||
|
||
return ( | ||
(<p | ||
ref={ref} | ||
id={formDescriptionId} | ||
className={cn("text-[0.8rem] text-muted-foreground", className)} | ||
{...props} />) | ||
); | ||
}) | ||
FormDescription.displayName = "FormDescription" | ||
|
||
const FormMessage = React.forwardRef(({ className, children, ...props }, ref) => { | ||
const { error, formMessageId } = useFormField() | ||
const body = error ? String(error?.message) : children | ||
|
||
if (!body) { | ||
return null | ||
} | ||
|
||
return ( | ||
(<p | ||
ref={ref} | ||
id={formMessageId} | ||
className={cn("text-[0.8rem] font-medium text-destructive", className)} | ||
{...props}> | ||
{body} | ||
</p>) | ||
); | ||
}) | ||
FormMessage.displayName = "FormMessage" | ||
|
||
export { | ||
useFormField, | ||
Form, | ||
FormItem, | ||
FormLabel, | ||
FormControl, | ||
FormDescription, | ||
FormMessage, | ||
FormField, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use server"; | ||
|
||
export async function editMetrics(values, id) { | ||
const user = await prisma.user.update({ | ||
where: { | ||
id, | ||
}, | ||
data: { | ||
...values, | ||
}, | ||
}); | ||
|
||
return user; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useState, useContext, createContext, useEffect, useCallback } from "react"; | ||
|
||
export const UserContext = createContext(null); | ||
|
||
export const UserContextProvider = ({ session, children }) => { | ||
const [user, setUser] = useState(null); | ||
const id = session?.user?.id; | ||
|
||
const revalidateUser = useCallback(() => { | ||
fetch(`api/users/${id}`) | ||
.then((res) => res.json()) | ||
.then((user) => setUser(user)); | ||
}, [id]); | ||
|
||
useEffect(() => { | ||
if (session) { | ||
revalidateUser(); | ||
} | ||
}, [session, revalidateUser]) | ||
|
||
return ( | ||
<UserContext.Provider value={{ user, revalidateUser }}> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
export const useUser = () => { | ||
const context = useContext(UserContext); | ||
if (context === undefined) { | ||
throw new Error("useUser must be used within a UserContextProvider"); | ||
} | ||
return context; | ||
}; |
Oops, something went wrong.