update project
This commit is contained in:
@@ -26,9 +26,11 @@ app.use("/", indexRoute);
|
|||||||
// "/projects"
|
// "/projects"
|
||||||
const projectsIndexRoute = require('./routes/projects/index')(sequelize);
|
const projectsIndexRoute = require('./routes/projects/index')(sequelize);
|
||||||
const projectsNewRoute = require('./routes/projects/new')(sequelize);
|
const projectsNewRoute = require('./routes/projects/new')(sequelize);
|
||||||
|
const projectsEditRoute = require('./routes/projects/edit')(sequelize);
|
||||||
const projectsDeleteRoute = require('./routes/projects/delete')(sequelize);
|
const projectsDeleteRoute = require('./routes/projects/delete')(sequelize);
|
||||||
app.use('/projects', projectsIndexRoute);
|
app.use('/projects', projectsIndexRoute);
|
||||||
app.use('/projects', projectsNewRoute);
|
app.use('/projects', projectsNewRoute);
|
||||||
|
app.use('/projects', projectsEditRoute);
|
||||||
app.use('/projects', projectsDeleteRoute);
|
app.use('/projects', projectsDeleteRoute);
|
||||||
|
|
||||||
// "/folders"
|
// "/folders"
|
||||||
|
|||||||
@@ -9,11 +9,7 @@ module.exports = function (sequelize) {
|
|||||||
router.delete("/:projectId", async (req, res) => {
|
router.delete("/:projectId", async (req, res) => {
|
||||||
const projectId = req.params.projectId;
|
const projectId = req.params.projectId;
|
||||||
try {
|
try {
|
||||||
const project = await Project.findOne({
|
const project = await Project.findByPk(projectId);
|
||||||
where: {
|
|
||||||
id: projectId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (!project) {
|
if (!project) {
|
||||||
return res.status(404).send("Project not found");
|
return res.status(404).send("Project not found");
|
||||||
}
|
}
|
||||||
|
|||||||
29
backend/routes/projects/edit.js
Normal file
29
backend/routes/projects/edit.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
const defineProject = require("../../models/projects");
|
||||||
|
const { DataTypes } = require("sequelize");
|
||||||
|
|
||||||
|
module.exports = function (sequelize) {
|
||||||
|
const Project = defineProject(sequelize, DataTypes);
|
||||||
|
|
||||||
|
router.put("/:projectId", async (req, res) => {
|
||||||
|
const projectId = req.params.projectId;
|
||||||
|
const { name, detail } = req.body;
|
||||||
|
try {
|
||||||
|
const project = await Project.findByPk(projectId);
|
||||||
|
if (!project) {
|
||||||
|
return res.status(404).send("Project not found");
|
||||||
|
}
|
||||||
|
await project.update({
|
||||||
|
name,
|
||||||
|
detail,
|
||||||
|
});
|
||||||
|
res.json(project);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).send("Internal Server Error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return router;
|
||||||
|
};
|
||||||
@@ -45,10 +45,6 @@ async function fetchProjects(url: string) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create project
|
* Create project
|
||||||
*
|
|
||||||
* @async
|
|
||||||
* @function
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
*/
|
||||||
async function createProject(name: string, detail: string) {
|
async function createProject(name: string, detail: string) {
|
||||||
const newProjectData = {
|
const newProjectData = {
|
||||||
@@ -79,6 +75,41 @@ async function createProject(name: string, detail: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update project
|
||||||
|
*/
|
||||||
|
async function updateProject(projectId: number, name: string, detail: string) {
|
||||||
|
const updatedProjectData = {
|
||||||
|
name: name,
|
||||||
|
detail: detail,
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchOptions = {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(updatedProjectData),
|
||||||
|
};
|
||||||
|
|
||||||
|
const url = `${apiServer}/projects/${projectId}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, fetchOptions);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
}
|
||||||
|
const data = await response.json();
|
||||||
|
return data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating project:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete project
|
||||||
|
*/
|
||||||
async function deleteProject(projectId: number) {
|
async function deleteProject(projectId: number) {
|
||||||
const fetchOptions = {
|
const fetchOptions = {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
@@ -120,7 +151,9 @@ export default function ProjectsPage() {
|
|||||||
|
|
||||||
// dialog
|
// dialog
|
||||||
const [isProjectDialogOpen, setIsProjectDialogOpen] = useState(false);
|
const [isProjectDialogOpen, setIsProjectDialogOpen] = useState(false);
|
||||||
const [editingProject, setEditingProject] = useState(null);
|
const [editingProject, setEditingProject] = useState<ProjectType | null>(
|
||||||
|
null
|
||||||
|
);
|
||||||
const openDialogForCreate = () => {
|
const openDialogForCreate = () => {
|
||||||
setIsProjectDialogOpen(true);
|
setIsProjectDialogOpen(true);
|
||||||
setEditingProject(null);
|
setEditingProject(null);
|
||||||
@@ -132,11 +165,28 @@ export default function ProjectsPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onSubmit = async (name: string, detail: string) => {
|
const onSubmit = async (name: string, detail: string) => {
|
||||||
|
if (editingProject) {
|
||||||
|
const updatedProject = await updateProject(
|
||||||
|
editingProject.id,
|
||||||
|
name,
|
||||||
|
detail
|
||||||
|
);
|
||||||
|
const updatedProjects = projects.map((project) =>
|
||||||
|
project.id === updatedProject.id ? updatedProject : project
|
||||||
|
);
|
||||||
|
setProjects(updatedProjects);
|
||||||
|
} else {
|
||||||
const newProject = await createProject(name, detail);
|
const newProject = await createProject(name, detail);
|
||||||
setProjects([...projects, newProject]);
|
setProjects([...projects, newProject]);
|
||||||
|
}
|
||||||
closeDialog();
|
closeDialog();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onEditClicked = (project: ProjectType) => {
|
||||||
|
setEditingProject(project);
|
||||||
|
setIsProjectDialogOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
const onDeleteClicked = async (projectId: number) => {
|
const onDeleteClicked = async (projectId: number) => {
|
||||||
try {
|
try {
|
||||||
await deleteProject(projectId);
|
await deleteProject(projectId);
|
||||||
@@ -164,6 +214,7 @@ export default function ProjectsPage() {
|
|||||||
<ProjectCard
|
<ProjectCard
|
||||||
key={index}
|
key={index}
|
||||||
project={project}
|
project={project}
|
||||||
|
onEditClicked={onEditClicked}
|
||||||
onDeleteClicked={onDeleteClicked}
|
onDeleteClicked={onDeleteClicked}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@@ -171,7 +222,7 @@ export default function ProjectsPage() {
|
|||||||
|
|
||||||
<ProjectDialog
|
<ProjectDialog
|
||||||
isOpen={isProjectDialogOpen}
|
isOpen={isProjectDialogOpen}
|
||||||
project={editingProject}
|
editingProject={editingProject}
|
||||||
onCancel={closeDialog}
|
onCancel={closeDialog}
|
||||||
onSubmit={onSubmit}
|
onSubmit={onSubmit}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
} from "@nextui-org/react";
|
} from "@nextui-org/react";
|
||||||
import { DotsIcon } from "@/components/icons";
|
import { DotsIcon } from "@/components/icons";
|
||||||
|
|
||||||
export function ProjectCard({ project, onDeleteClicked }) {
|
export function ProjectCard({ project, onEditClicked, onDeleteClicked }) {
|
||||||
return (
|
return (
|
||||||
<Card className="w-[250px]">
|
<Card className="w-[250px]">
|
||||||
<CardHeader className="flex gap-3 h-[50px] justify-between text-ellipsis overflow-hidden">
|
<CardHeader className="flex gap-3 h-[50px] justify-between text-ellipsis overflow-hidden">
|
||||||
@@ -24,17 +24,14 @@ export function ProjectCard({ project, onDeleteClicked }) {
|
|||||||
</div>
|
</div>
|
||||||
<Dropdown>
|
<Dropdown>
|
||||||
<DropdownTrigger>
|
<DropdownTrigger>
|
||||||
<Button
|
<Button isIconOnly variant="light" size="sm">
|
||||||
isIconOnly
|
|
||||||
variant="light"
|
|
||||||
size="sm"
|
|
||||||
className="text-transparent hover:text-inherit"
|
|
||||||
>
|
|
||||||
<DotsIcon size={16} />
|
<DotsIcon size={16} />
|
||||||
</Button>
|
</Button>
|
||||||
</DropdownTrigger>
|
</DropdownTrigger>
|
||||||
<DropdownMenu aria-label="Static Actions">
|
<DropdownMenu aria-label="Static Actions">
|
||||||
<DropdownItem key="edit">Edit project</DropdownItem>
|
<DropdownItem key="edit" onClick={() => onEditClicked(project)}>
|
||||||
|
Edit project
|
||||||
|
</DropdownItem>
|
||||||
<DropdownItem
|
<DropdownItem
|
||||||
key="delete"
|
key="delete"
|
||||||
className="text-danger"
|
className="text-danger"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Input,
|
Input,
|
||||||
@@ -38,6 +38,30 @@ export function ProjectDialog({
|
|||||||
errorMessage: "",
|
errorMessage: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (editingProject) {
|
||||||
|
setProjectName({
|
||||||
|
...projectName,
|
||||||
|
text: editingProject.name,
|
||||||
|
});
|
||||||
|
|
||||||
|
setProjectDetail({
|
||||||
|
...projectDetail,
|
||||||
|
text: editingProject.detail ? editingProject.detail : "",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setProjectName({
|
||||||
|
...projectName,
|
||||||
|
text: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
setProjectDetail({
|
||||||
|
...projectDetail,
|
||||||
|
text: "",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [editingProject]);
|
||||||
|
|
||||||
const clear = () => {
|
const clear = () => {
|
||||||
setProjectName({
|
setProjectName({
|
||||||
isValid: false,
|
isValid: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user