Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

It works a bit better now and shows on profile page (#262)

Sean Boult64e61f504b09
diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx
index 4a61cd80..0b8224e1 100644
--- a/src/app/settings/page.tsx
+++ b/src/app/settings/page.tsx
@@ -23,18 +23,20 @@ export default async function SettingsPage() {
},
});
- // make the user links have 4 at all times
+ // NOTE: make the user links have 4 at all times
const userLinks = (profileData.userLinks = [
...profileData.userLinks,
...Array(4 - profileData.userLinks.length).fill({
id: null,
url: '',
}),
- ]);
+ ])
+ // NOTE: sort the user links so empty strings are at the bottom
+ .sort((a, b) => b.url.localeCompare(a.url));
return (
<Settings
- data={{
+ profileData={{
bio: profileData.bio,
userLinks,
}}
diff --git a/src/components/settings/index.tsx b/src/components/settings/index.tsx
index a8a6842d..65386cb5 100644
--- a/src/components/settings/index.tsx
+++ b/src/components/settings/index.tsx
@@ -10,7 +10,6 @@ import { RichMarkdownEditor } from '../ui/rich-markdown-editor';
import { Github, Linkedin, Twitter, Youtube, Link as LinkIcon } from 'lucide-react';
import { toast } from '../ui/use-toast';
import { ToastAction } from '../ui/toast';
-
import { ErrorMessage } from '@hookform/error-message';
export interface UserLinkType {
@@ -26,18 +25,16 @@ interface FormValues {
export type FormSchema = z.infer<typeof formSchema>;
const formSchema = z.object({
- // TODO: make this have a good typesafe exp
- // userLinks: z.array(
- // z.object({
- // id: z.union([z.string(), z.null()]),
- // url: z.string().url(),
- // }),
- // ),
- userLinks: z.any(),
+ userLinks: z.array(
+ z.object({
+ id: z.union([z.string(), z.null()]),
+ url: z.union([z.string().url(), z.literal('')]),
+ }),
+ ),
bio: z.string().optional(),
});
-export const Settings = ({ data }: { data: FormSchema }) => {
+export const Settings = ({ profileData }: { profileData: FormSchema }) => {
const {
formState: { isValid, errors },
control,
@@ -46,7 +43,7 @@ export const Settings = ({ data }: { data: FormSchema }) => {
} = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
- ...data,
+ ...profileData,
},
});
@@ -65,19 +62,8 @@ export const Settings = ({ data }: { data: FormSchema }) => {
return;
}
- const userLinks = data.userLinks.map((link: UserLinkType, i: number) => ({
- ...link,
- // @ts-ignore
- url: values.userLinks[i].value,
- }));
-
- const updateProfileData = {
- bio: values.bio,
- userLinks,
- };
-
// call the server action
- await updateProfile(updateProfileData);
+ await updateProfile(values);
toast({
variant: 'success',
@@ -101,28 +87,26 @@ export const Settings = ({ data }: { data: FormSchema }) => {
/>
</div>
- <ErrorMessage
- errors={errors}
- name="userLinks"
- render={({ message }) => <p>{message}</p>}
- />
+ {/* TODO: get errors working on each field */}
+ <ErrorMessage errors={errors} name="userLinks" render={({ message }) => <p>{message}</p>} />
<div className="mt-8 flex flex-col items-start space-y-3">
<h4 className="text-xl font-bold">Social accounts</h4>
- {fields.map((val: UserLinkType, i: number) => {
- return (
- <div key={`url-${i}`} className="flex items-center gap-2">
- <MagikcIcon url={val?.url ?? ''} />
- <Input
- defaultValue={val?.url}
- placeholder="Link to social profile"
- className="w-64"
- // @ts-ignore
- {...register(`userLinks.${i}.value`)}
- />
- </div>
- );
- })}
+ {fields
+ .map((val, i) => {
+ return (
+ <div key={`url-${i}`} className="flex items-center gap-2">
+ <MagikcIcon url={val?.url ?? ''} />
+ <Input
+ defaultValue={val?.url}
+ placeholder="Link to social profile"
+ className="w-64"
+ // @ts-ignore
+ {...register(`userLinks.${i}.url`)}
+ />
+ </div>
+ );
+ })}
</div>
<Button type="submit" className="mt-6">
diff --git a/src/components/settings/settings.action.ts b/src/components/settings/settings.action.ts
index cb6cc936..6665a23a 100644
--- a/src/components/settings/settings.action.ts
+++ b/src/components/settings/settings.action.ts
@@ -39,16 +39,6 @@ export async function updateProfile(profileData: { bio: string; userLinks: UserL
),
);
- // // 4. Filter for all links that are empty string and delete from db
- // const emptyLinks = profileData.userLinks.filter((link) => link.url === '');
- // await prisma.userLink.deleteMany({
- // where: {
- // id: {
- // in: emptyLinks.map((link) => link.id ?? '-1'),
- // },
- // },
- // });
-
// do this after we do the shit
revalidatePath('/settings');
}
diff --git a/src/components/user/dashboard/index.tsx b/src/components/user/dashboard/index.tsx
index 3c063864..4d9f60ee 100644
--- a/src/components/user/dashboard/index.tsx
+++ b/src/components/user/dashboard/index.tsx
@@ -11,9 +11,11 @@ import { Button } from '~/components/ui/button';
import Link from 'next/link';
import { getServerSession } from 'next-auth';
import { authOptions } from '~/server/auth';
+import { ReactMarkdown } from 'react-markdown/lib/react-markdown';
interface Props {
- user: User;
+ // TODO: how do do this union type with just letting prisma halp
+ user: User & { userLinks: { id: string | null; url: string }[] };
}
export type UserData = NonNullable<Awaited<ReturnType<typeof getUserdata>>>;
@@ -54,12 +56,6 @@ export default async function Dashboard({ user }: Props) {
return (
<div className="container">
- {session?.user.id === user.id && (
- <Link href="/settings">
- <Button>Edit Profile</Button>
- </Link>
- )}
-
<div className="flex-1 space-y-4 pt-6">
<div className="flex items-center gap-4">
<div className="flex items-center space-x-2">
@@ -80,7 +76,31 @@ export default async function Dashboard({ user }: Props) {
Joined {getRelativeTime(user.createdAt)}
</p>
</div>
+ {session?.user.id === user.id && (
+ <Link href="/settings">
+ <Button>Edit Profile</Button>
+ </Link>
+ )}
+ </div>
+
+ <div className="max-w-md">
+ <ReactMarkdown>{user.bio}</ReactMarkdown>
</div>
+
+ {user.userLinks.length > 0 && (
+ <ol>
+ {user.userLinks
+ .filter((item) => item.url !== '')
+ .map((link) => (
+ <li key={link.id}>
+ <a href={link.url} target="_blank" rel="noopener noreferrer">
+ {link.url}
+ </a>
+ </li>
+ ))}
+ </ol>
+ )}
+
<Tabs defaultValue="in-progress" className="space-y-4">
<TabsList className="rounded-full border border-border bg-background">
<TabsTrigger
diff --git a/src/components/user/profile.tsx b/src/components/user/profile.tsx
index 53010023..1764ead6 100644
--- a/src/components/user/profile.tsx
+++ b/src/components/user/profile.tsx
@@ -20,6 +20,9 @@ export async function Profile({ username: usernameFromQuery }: Props) {
equals: username,
},
},
+ include: {
+ userLinks: true,
+ }
});
if (!user || !username) {