Skip to content

Commit

Permalink
Merge pull request #33 from rapidosaas/develop
Browse files Browse the repository at this point in the history
added username and profile
  • Loading branch information
nazimboudeffa authored Dec 30, 2024
2 parents 69c8632 + 53f05b8 commit 6597a29
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/app/api/dashboard/avatar/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export async function GET() {
userId: session.user.id // Utilisez directement l'ID de la session
});

console.log('Profile:', profile.image);
if (!profile) {
return NextResponse.json({ error: 'Profile not found' }, { status: 404 });
}

console.log('Avatar:', profile.image);

return NextResponse.json({ avatar : profile.image }, { status: 200 });

Expand Down
18 changes: 18 additions & 0 deletions src/app/api/username/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { connectDB } from "@/lib/db";
import Profile from "@/lib/models/Profile";
import { NextResponse, NextRequest } from "next/server";

export async function POST(request: NextRequest) {
try {
await connectDB();
const body = await request.json();
console.log(body);
const profile = await Profile.findOne({ ...body });
console.log(profile);
return NextResponse.json({ profile }, { status: 200 });

} catch (error) {
console.log(error);
return NextResponse.json({ message: "Error", error }, { status: 500 });
}
}
27 changes: 25 additions & 2 deletions src/app/dashboard/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ export default function ProfileForm() {
.then((response) => response.json())
.then((data) => {
console.log('Data:', data);
setDefaultValues(data.profile);
reset(data.profile);
const profileData = {
...data.profile
};
setDefaultValues(profileData);
reset(profileData);
});
} else {
redirect("/");
Expand Down Expand Up @@ -110,6 +113,26 @@ export default function ProfileForm() {
noValidate
onSubmit={handleSubmit(onSubmit)}
>
<FormField
control={control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input
{...field}
value={field.value ?? ''}
onChange={(e) => {
const value = e.target.value.replace(/[^a-zA-Z0-9]/g, '');
field.onChange(value);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={control}
name="name"
Expand Down
16 changes: 16 additions & 0 deletions src/app/u/[username]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ProfilePage from '@/components/ProfilePage';

export default async function Page({
params,
}: Readonly<{
params: Promise<{ username: string }>
}>) {

const username = (await params).username;

return (
<main className="m-auto my-10 flex max-w-5xl flex-col items-center gap-5 px-3 md:flex-row md:items-start">
<ProfilePage username={username} />
</main>
);
}
12 changes: 10 additions & 2 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function Navbar() {

console.log('Session NavBar:', session);

const [avatar, setAvatar] = useState<string | null>(null);
const [avatar, setAvatar] = useState<string | null>(avatar1.src);

useEffect(() => {
if (session) {
Expand All @@ -36,9 +36,17 @@ export default function Navbar() {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((response) => response.json())
.then((response) => {
if (response.status === 404) {
// Profile not found, set a default avatar
setAvatar(avatar1.src);
return null;
}
return response.json();
})
.then((data) => {
console.log('Data:', data);
if (!data) return;
setAvatar(data.avatar);
})
.catch((error) => {
Expand Down
59 changes: 59 additions & 0 deletions src/components/ProfilePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use client";

import { formatMoney } from "@/lib/utils";
import { Banknote } from "lucide-react";
import { useEffect, useState } from "react";
import Badge from "./Badge";
import Profile from "@/lib/types/profile";

interface JobPageProps {
readonly username: string;
}

export default function JobPage({
username,
}: JobPageProps) {

const [profile, setProfile] = useState<Profile | undefined>();

useEffect(() => {
fetch("/api/username", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username }),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
setProfile(data.profile);
})
.catch((err) => console.log(err));
}, [username]);

return (
<section className="w-full grow space-y-5">
<div className="flex items-center gap-3">
<div>
<div>
<h1 className="text-xl font-bold">{profile?.username}</h1>
<p className="font-semibold">
<span>{profile?.name}</span>
</p>
</div>
<div className="text-muted-foreground">
<p className="flex items-center gap-1.5">
<Banknote size={16} className="shrink-0" />
{formatMoney(profile?.salary ?? 0)}
</p>
<p className="flex items-center gap-1">
{profile?.skills.map((skill: string) => (
<Badge key={skill}>{skill}</Badge>
))}
</p>
</div>
</div>
</div>
<div>{profile?.bio}</div>
</section>
);
}
4 changes: 3 additions & 1 deletion src/components/SkillsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export default forwardRef<HTMLInputElement, LocationInputProps>(
const [selectedSkills, setSelectedSkills] = useState<string[]>([]);

useEffect(() => {
setSelectedSkills(knownskills);
if (knownskills.length > 0) {
setSelectedSkills(knownskills);
}
}, [knownskills]);

const cities = useMemo(() => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/models/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const profileSchema = new Schema(
ref: "User",
required: true,
},
username: {
type: String,
required: true,
},
name: {
type: String,
required: true,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types/profile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
interface Profile {
_id?: string;
userId: string;
username: string;
name: string;
bio: string;
skills: string[];
salary: number;
Expand Down
1 change: 1 addition & 0 deletions src/lib/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const singInSchema = z.object({
});

export const profileSchema = z.object({
username: requiredString.max(100),
name: requiredString.max(100),
bio: z.string().max(250),
skills: z.array(z.string().max(100)).max(3),
Expand Down

0 comments on commit 6597a29

Please sign in to comment.