Project name input dialog
This commit is contained in:
@@ -2,7 +2,16 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { title } from "@/components/primitives";
|
import { title } from "@/components/primitives";
|
||||||
import { ProjectCard } from "./project-card";
|
import { ProjectCard } from "./project-card";
|
||||||
import { Button } from "@nextui-org/react";
|
import {
|
||||||
|
Button,
|
||||||
|
Input,
|
||||||
|
Textarea,
|
||||||
|
Modal,
|
||||||
|
ModalContent,
|
||||||
|
ModalHeader,
|
||||||
|
ModalBody,
|
||||||
|
ModalFooter,
|
||||||
|
} from "@nextui-org/react";
|
||||||
|
|
||||||
import Config from "@/config/config";
|
import Config from "@/config/config";
|
||||||
const apiServer = Config.apiServer;
|
const apiServer = Config.apiServer;
|
||||||
@@ -41,10 +50,10 @@ async function fetchProjects(url) {
|
|||||||
* @function
|
* @function
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
async function createProject() {
|
async function createProject(name, detail) {
|
||||||
const newProjectData = {
|
const newProjectData = {
|
||||||
name: "新しいプロジェクト",
|
name: name,
|
||||||
detail: "新しいプロジェクトの詳細説明がここにくるよ",
|
detail: detail,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchOptions = {
|
const fetchOptions = {
|
||||||
@@ -56,22 +65,22 @@ async function createProject() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const url = `${apiServer}/projects`;
|
const url = `${apiServer}/projects`;
|
||||||
fetch(url, fetchOptions)
|
|
||||||
.then((response) => {
|
try {
|
||||||
|
const response = await fetch(url, fetchOptions);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
}
|
}
|
||||||
return response.json();
|
const data = await response.json();
|
||||||
})
|
return data;
|
||||||
.then((data) => {
|
} catch (error) {
|
||||||
console.log("New project created:", data);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error creating new project:", error);
|
console.error("Error creating new project:", error);
|
||||||
});
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ProjectsPage() {
|
export default function ProjectsPage() {
|
||||||
|
// projects
|
||||||
const [projects, setProjects] = useState([]);
|
const [projects, setProjects] = useState([]);
|
||||||
const url = `${apiServer}/projects`;
|
const url = `${apiServer}/projects`;
|
||||||
|
|
||||||
@@ -81,22 +90,120 @@ export default function ProjectsPage() {
|
|||||||
const data = await fetchProjects(url);
|
const data = await fetchProjects(url);
|
||||||
setProjects(data);
|
setProjects(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in effect:', error.message);
|
console.error("Error in effect:", error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchDataEffect();
|
fetchDataEffect();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// new project data
|
||||||
|
const [projectName, setProjectName] = useState({
|
||||||
|
text: "",
|
||||||
|
isValid: false,
|
||||||
|
errorMessage: "",
|
||||||
|
});
|
||||||
|
const [projectDetail, setProjectDetail] = useState({
|
||||||
|
text: "",
|
||||||
|
isValid: false,
|
||||||
|
errorMessage: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
// modal
|
||||||
|
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
|
||||||
|
const openModal = () => {
|
||||||
|
setIsCreateDialogOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const closeModal = () => {
|
||||||
|
setProjectName({ text: "", isValid: false, errorMessage: "" });
|
||||||
|
setProjectDetail({ text: "", isValid: false, errorMessage: "" });
|
||||||
|
setIsCreateDialogOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onCreateClicked = async () => {
|
||||||
|
let isValid = true;
|
||||||
|
|
||||||
|
// validate projectName
|
||||||
|
if (!projectName.text) {
|
||||||
|
setProjectName({
|
||||||
|
text: "",
|
||||||
|
isValid: false,
|
||||||
|
errorMessage: "Please enter project name",
|
||||||
|
});
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate projectDetail
|
||||||
|
if (!projectDetail.text) {
|
||||||
|
setProjectDetail({
|
||||||
|
text: "",
|
||||||
|
isValid: true,
|
||||||
|
errorMessage: "Please enter project detail",
|
||||||
|
});
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isValid) {
|
||||||
|
const newProject = await createProject(
|
||||||
|
projectName.text,
|
||||||
|
projectDetail.text
|
||||||
|
);
|
||||||
|
setProjects([...projects, newProject])
|
||||||
|
closeModal();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex h-full items-center">
|
<div className="flex h-full items-center">
|
||||||
<h1 className={title()}>Projects</h1>
|
<h1 className={title()}>Projects</h1>
|
||||||
<Button variant="bordered" onClick={createProject} className="ms-5 mt-3">
|
<Button color="primary" onClick={openModal} className="ms-5 mt-3">
|
||||||
Create
|
Create
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<Modal isOpen={isCreateDialogOpen}>
|
||||||
|
<ModalContent>
|
||||||
|
<ModalHeader className="flex flex-col gap-1">Modal Title</ModalHeader>
|
||||||
|
<ModalBody>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
label="Project Name"
|
||||||
|
value={projectName.text}
|
||||||
|
isInvalid={projectName.isValid}
|
||||||
|
errorMessage={projectName.errorMessage}
|
||||||
|
onChange={(e) => {
|
||||||
|
setProjectName({
|
||||||
|
...projectName,
|
||||||
|
text: e.target.value,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Textarea
|
||||||
|
label="Project Detail"
|
||||||
|
value={projectDetail.text}
|
||||||
|
isInvalid={projectDetail.isValid}
|
||||||
|
errorMessage={projectDetail.errorMessage}
|
||||||
|
onChange={(e) => {
|
||||||
|
setProjectDetail({
|
||||||
|
...projectDetail,
|
||||||
|
text: e.target.value,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</ModalBody>
|
||||||
|
<ModalFooter>
|
||||||
|
<Button color="danger" variant="light" onPress={closeModal}>
|
||||||
|
Close
|
||||||
|
</Button>
|
||||||
|
<Button color="primary" onPress={onCreateClicked}>
|
||||||
|
Create
|
||||||
|
</Button>
|
||||||
|
</ModalFooter>
|
||||||
|
</ModalContent>
|
||||||
|
</Modal>
|
||||||
|
|
||||||
<div className="flex flex-wrap gap-4 mt-5">
|
<div className="flex flex-wrap gap-4 mt-5">
|
||||||
{projects.map((project, index) => (
|
{projects.map((project, index) => (
|
||||||
<ProjectCard
|
<ProjectCard
|
||||||
|
|||||||
Reference in New Issue
Block a user