Skip to content

Commit

Permalink
Merge branch 'profile' into profileFront
Browse files Browse the repository at this point in the history
  • Loading branch information
MustaphaJAM committed Jun 7, 2024
2 parents 2a08686 + f3ac0ec commit 69bd70f
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 72 deletions.
2 changes: 2 additions & 0 deletions app/Payment/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Payments from '../components/profile/Payment';
export default Payments;
2 changes: 2 additions & 0 deletions app/PersonalInfo/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import UserProfileCard from '../components/profile/PersonalInfo';
export default UserProfileCard;
2 changes: 2 additions & 0 deletions app/Preferences/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Preferences from '../components/profile/Preferences';
export default Preferences;
2 changes: 2 additions & 0 deletions app/Security/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Security from '../components/profile/security';
export default Security;
43 changes: 43 additions & 0 deletions app/api/profile/perfernces/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NextResponse } from 'next/server';
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "@/app/actions/getCurrentUser";

// Handler for GET request to retrieve preferences
export async function GET() {
const currentUser = await getCurrentUser();

if (!currentUser) {
return NextResponse.error();
}

// Retrieve user preferences from the database
const user = await prisma.user.findUnique({
where: { id: currentUser.id },
select: {
currency: true,
language: true,
},
});

return NextResponse.json(user);
}

// Handler for POST request to save preferences
export async function POST(request: Request) {
const currentUser = await getCurrentUser();

if (!currentUser) {
return NextResponse.error();
}

const body = await request.json();
const { currency, language } = body;

// Save preferences to the database
await prisma.user.update({
where: { id: currentUser.id },
data: { currency, language },
});

return NextResponse.json({ message: 'Preferences saved successfully' });
}
65 changes: 65 additions & 0 deletions app/api/profile/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { NextResponse } from "next/server";
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "@/app/actions/getCurrentUser";

// Handler for the PUT
export async function PUT(request: Request) {
// Get the current user
const currentUser = await getCurrentUser();

// If there is no current user, return an error response
if (!currentUser) {
return NextResponse.error();
}

// Parse the request body as JSON
const body = await request.json();

// Destructure the required properties from the request body
const {
name,
surname,
username,
dateOfBirth,
email,
telephone,
gender,
image
} = body;

// Update the user's favoriteIds in the database
const user = await prisma.user.update({
where:{id: currentUser.id},
data: {
name,
surname,
username,
dateOfBirth,
email,
telephone,
gender,
image
}
});

// Return the created listing as a JSON response
return NextResponse.json(user);
}
// Handler for the GET request
export async function GET(request: Request) {
try {
// Get the current user
const currentUser = await getCurrentUser();

// If there is no current user, return an error response
if (!currentUser) {
return NextResponse.error();
}

// Return the user data as a JSON response
return NextResponse.json(currentUser);
} catch (error) {
console.error(error);
return NextResponse.error();
}
}
59 changes: 59 additions & 0 deletions app/api/profile/security/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { NextResponse } from "next/server";
import prisma from "@/app/libs/prismadb";
import bcrypt from "bcrypt";
import getCurrentUser from "@/app/actions/getCurrentUser";


export async function PUT(request: Request) {
try {
const body = await request.json();

const { password , recoveryEmail } = body;

// Get the current user
const currentUser = await getCurrentUser();

// If no user is found, return an error response
if (!currentUser) {
return NextResponse.error();
}
const hashedPassword = await bcrypt.hash(password, 12);

const user = await prisma.user.update({
where: {
id: currentUser.id
},
data: { hashedPassword, recoveryEmail },
});

return NextResponse.json(user);
} catch (error) {
console.error(error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}
//Delete user account endpoint
export async function DELETE(request: Request) {
try {
// Get the current user
const currentUser = await getCurrentUser();

// If no user is found, return an error response
if (!currentUser) {
return NextResponse.error();
}

// Delete the user from the database
await prisma.user.delete({
where: {
id: currentUser.id
}
});

// Return a success response
return NextResponse.json({ message: "User account deleted successfully" });
} catch (error) {
console.error(error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}
3 changes: 3 additions & 0 deletions app/components/profile/Payment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, { useState } from 'react';
import ProfileFrameElement from './profileFrame';
import ProfilePage from '@/app/profile/layout';

const Payments = () => {
const [showForm, setShowForm] = useState(false);
Expand All @@ -24,6 +25,7 @@ const Payments = () => {
};

return (
<ProfilePage>
<ProfileFrameElement text="Payments">
<div>
<div className="space-y-4 p-4 ">
Expand Down Expand Up @@ -72,6 +74,7 @@ const Payments = () => {
</div>
</div>
</ProfileFrameElement>
</ProfilePage>
);
};

Expand Down
25 changes: 14 additions & 11 deletions app/components/profile/PersonalInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import ProfileFrameElement from "./profileFrame";
import ProfilePage from "@/app/profile/layout";

interface UserProfileCardProps {
user: {
name: string | null;
email: string | null;
}
}
// interface UserProfileCardProps {
// user: {
// name: string | null;
// email: string | null;
// }
// }

const UserProfileCard: React.FC<UserProfileCardProps> = ({ user }) => {
const PersonalInfo = () => {
return (
<ProfilePage>
<ProfileFrameElement text="Personal information">
<form className="space-y-4 m-4 px-8">
<div className="flex flex-col">
Expand All @@ -19,7 +21,7 @@ const UserProfileCard: React.FC<UserProfileCardProps> = ({ user }) => {
<div className="grid grid-cols-2">
<div className="flex flex-col mr-2">
<label className="mb-1 font-medium">Name</label>
<input type="text" value={user.name || "Not Available"} className="p-2 border rounded focus:border-pink-500 focus:outline-none" />
<input type="text" placeholder="fatima ezzahra abi" className="p-2 border rounded focus:border-pink-500 focus:outline-none" />
</div>
<div className="flex flex-col ml-2">
<label className="mb-1 font-medium">Surname</label>
Expand All @@ -29,7 +31,7 @@ const UserProfileCard: React.FC<UserProfileCardProps> = ({ user }) => {

<div className="flex flex-col">
<label className="mb-1 font-medium">Email</label>
<input type="email" value={user.email || "Not Available"} className="p-2 border rounded focus:border-pink-500 focus:outline-none" />
<input type="email" placeholder="fatimazzahraabi@gmail.com" className="p-2 border rounded focus:border-pink-500 focus:outline-none" />
</div>
<div className="flex flex-col">
<label className="mb-1 font-medium">Contact Number</label>
Expand All @@ -38,7 +40,7 @@ const UserProfileCard: React.FC<UserProfileCardProps> = ({ user }) => {
<div className="grid grid-cols-2 mr-2">
<div className="flex flex-col">
<label className="mb-1 font-medium">Date of Birth</label>
<input type="date" value={user.email || "Not Available"} className="p-2 border rounded focus:border-pink-500 focus:outline-none" />
<input type="date" className="p-2 border rounded focus:border-pink-500 focus:outline-none" />
</div>
<div className="flex flex-col ml-2">
<label className="mb-1 font-medium">Gender</label>
Expand All @@ -56,7 +58,8 @@ const UserProfileCard: React.FC<UserProfileCardProps> = ({ user }) => {
</div>
</form>
</ProfileFrameElement>
</ProfilePage>
);
}

export default UserProfileCard;
export default PersonalInfo;
4 changes: 3 additions & 1 deletion app/components/profile/Preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { useState } from 'react';
import ProfileFrameElement from './profileFrame';

import ProfilePage from '@/app/profile/layout';

const Preferences = () => {
const [currency, setCurrency] = useState('dollar');
Expand Down Expand Up @@ -43,6 +43,7 @@ const Preferences = () => {
};

return (
<ProfilePage>
<ProfileFrameElement text="Preferences">
<div className="flex flex-col items-center space-y-48 p-4 h-full">
<div className="flex flex-row space-x-12 w-full justify-around">
Expand Down Expand Up @@ -111,6 +112,7 @@ const Preferences = () => {
</div>
</div>
</ProfileFrameElement>
</ProfilePage>
);
}

Expand Down
Loading

0 comments on commit 69bd70f

Please sign in to comment.