Skip to content

Commit

Permalink
fixed issues with update project, now new users assigned to the proje…
Browse files Browse the repository at this point in the history
…ct we popolate projects array
  • Loading branch information
Singh committed Nov 2, 2023
1 parent 234fca9 commit 97c0770
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 100 deletions.
33 changes: 28 additions & 5 deletions app/api/projects/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function PUT(
if (session?.user.role !== 'admin') {
return new Response(JSON.stringify({ error: 'User not admin' }), {
headers: { 'Content-Type': 'application/json' },
status: 400, // Internal Server Error
status: 403, // Forbidden
});
}
const body = await request.json();
Expand Down Expand Up @@ -46,18 +46,41 @@ export async function PUT(
})
);

// Identify users who have been added to the project
const addedUsers = updatedProject.assignedUsers.filter(
(userId: string) => !currentProject.assignedUsers.includes(userId)
);

// Update the assignedProjects array for each added user
await Promise.all(
addedUsers.map(async (userId: string) => {
await User.findByIdAndUpdate(
userId,
{
$push: { assignedProjects: updatedProject._id },
},
{ new: true }
);
})
);

return new Response(JSON.stringify({ project: updatedProject }), {
headers: { 'Content-Type': 'application/json' },
status: 200, // OK
});
} catch (error) {
return new Response(JSON.stringify({ error: 'Failed to update project' }), {
headers: { 'Content-Type': 'application/json' },
status: 500, // Internal Server Error
});
console.error(error); // Log the actual error to the server logs
return new Response(
JSON.stringify({ error: `Failed to update project: ${error.message}` }),
{
headers: { 'Content-Type': 'application/json' },
status: 500, // Internal Server Error
}
);
}
}


export async function DELETE(
request: NextRequest, // Assuming NextApiRequest is imported from 'next'
{ params }: { params: { id: string } }
Expand Down
147 changes: 74 additions & 73 deletions app/create-project/CreateProjectComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,81 +64,82 @@ const CreateProjectComponent: React.FC<CreateProjectProps> = ({
setShowAllUsers(!showAllUsers);
};

return (
<div className="container mx-auto p-4">
<h1 className="text-2xl sm:text-3xl mb-4">Create New Project</h1>
<form onSubmit={handleSubmit}>
<div className="mb-4">
<label
htmlFor="name"
className="block text-white-700 text-sm font-bold mb-2"
>
Project Name:
</label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
</div>
<div className="mb-4">
<label
htmlFor="description"
className="block text-white-700 text-sm font-bold mb-2"
>
Description:
</label>
<textarea
id="description"
value={description}
onChange={(e) => setDescription(e.target.value)}
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
</div>
<div className="mb-4">
<label className="block text-white-700 text-sm font-bold mb-2">
Assign Users:
</label>
<div className="overflow-hidden">
{users
.slice(0, showAllUsers ? users.length : 5)
.map((user, index) => (
<div key={index} className="mb-2">
<input
type="checkbox"
id={`user-${index}`}
checked={selectedUsers.includes(user._id)}
value={user._id}
onChange={handleCheckboxChange}
/>
<label htmlFor={`user-${index}`} className="ml-2">
{user.fullName || user.email}
</label>
</div>
))}
return (
<div className="container mx-auto p-4">
<h1 className="text-2xl sm:text-3xl mb-4">
{project ? 'Update' : 'Create New'} Project
</h1>
<form onSubmit={handleSubmit}>
<div className="mb-4">
<label
htmlFor="name"
className="block text-white-700 text-sm font-bold mb-2"
>
Project Name:
</label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
</div>
{users.length > 5 && (
<button
type="button"
onClick={toggleShowAllUsers}
className="text-blue-500 hover:underline"
<div className="mb-4">
<label
htmlFor="description"
className="block text-white-700 text-sm font-bold mb-2"
>
{showAllUsers ? 'Show Less' : 'Show More'}
</button>
)}
</div>
<button
type="submit"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
Create Project
</button>
</form>
</div>
);

Description:
</label>
<textarea
id="description"
value={description}
onChange={(e) => setDescription(e.target.value)}
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
</div>
<div className="mb-4">
<label className="block text-white-700 text-sm font-bold mb-2">
Assign Users:
</label>
<div className="overflow-hidden">
{users
.slice(0, showAllUsers ? users.length : 5)
.map((user, index) => (
<div key={index} className="mb-2">
<input
type="checkbox"
id={`user-${index}`}
checked={selectedUsers.includes(user._id)}
value={user._id}
onChange={handleCheckboxChange}
/>
<label htmlFor={`user-${index}`} className="ml-2">
{user.fullName || user.email}
</label>
</div>
))}
</div>
{users.length > 5 && (
<button
type="button"
onClick={toggleShowAllUsers}
className="text-blue-500 hover:underline"
>
{showAllUsers ? 'Show Less' : 'Show More'}
</button>
)}
</div>
<button
type="submit"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
{project ? 'Update' : 'Create'} Project
</button>
</form>
</div>
);
};

export default CreateProjectComponent;
42 changes: 20 additions & 22 deletions app/create-project/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,32 @@ const CreateProject = async ({
params: { slug: string };
searchParams?: { [key: string]: string | string[] | undefined };
}) => {

const users = await User.find({}).lean();
const project: ProjectDocument | null = searchParams?.id
? await Project.findById(searchParams?.id).lean()
: null;
return (
<div className="min-h-screen flex flex-col bg-gray-100 dark:bg-gray-800">
<div className="flex-grow p-4 sm:p-6 md:p-8 lg:p-10">
<div className="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-lg">
<h1 className="text-2xl sm:text-3xl font-semibold mb-4">
Create Project
</h1>
<CreateProjectComponent
users={toPlainObject(users)}
project={
project
? {
...project,
_id: project?._id?.toString(),
}
: null
}
/>
return (
<div className="min-h-screen flex flex-col bg-gray-100 dark:bg-gray-800">
<div className="flex-grow p-4 sm:p-6 md:p-8 lg:p-10">
<div className="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-lg">
<h1 className="text-2xl sm:text-3xl font-semibold mb-4">
{project ? 'Update' : 'Create'} Project
</h1>
<CreateProjectComponent
users={toPlainObject(users)}
project={
project
? {
...project,
_id: project?._id?.toString(),
}
: null
}
/>
</div>
</div>
</div>
</div>
);

);
};

export default CreateProject;

0 comments on commit 97c0770

Please sign in to comment.