Skip to content
Snippets Groups Projects
Commit 9b2996f6 authored by Planoetscher Daniel (Student Com20)'s avatar Planoetscher Daniel (Student Com20)
Browse files

completed project edit

parent 8559a17c
No related branches found
No related tags found
No related merge requests found
import { Project, ProjectColors } from 'adapters/project'; import { Project, ProjectColors, Status } from 'adapters/project';
import { FormEvent, useCallback, useEffect, useState } from 'react'; import { FormEvent, useCallback, useEffect, useState } from 'react';
import Callout from 'components/ui/Callout'; import Callout from 'components/ui/Callout';
import Button from 'components/ui/Button'; import Button from 'components/ui/Button';
...@@ -8,7 +8,7 @@ import { getTeam, getTeams, Team } from 'adapters/team'; ...@@ -8,7 +8,7 @@ import { getTeam, getTeams, Team } from 'adapters/team';
interface Props { interface Props {
project?: Project project?: Project
onSubmit: (teams: string[], name: string, text: string, color: string, deadline?: Date) => void; onSubmit: (teams: string[], name: string, text: string, color: string, status?: Status, deadline?: Date) => void;
} }
function validateName(name: string): string | null { function validateName(name: string): string | null {
...@@ -35,10 +35,19 @@ function validateColor(color: string): string | null { ...@@ -35,10 +35,19 @@ function validateColor(color: string): string | null {
} }
} }
function validateTeams(teams: string[]): string | null {
if (teams.length > 0) {
return null;
} else {
return 'Please choose at least one team';
}
}
export default function ProjectForm({ project, onSubmit }: Props) { export default function ProjectForm({ project, onSubmit }: Props) {
const [name, setName] = useState(project?.name); const [name, setName] = useState(project?.name);
const [text, setText] = useState(project?.text); const [text, setText] = useState(project?.text);
const [status, setStatus] = useState(project?.status);
const [color, setColor] = useState(project?.color); const [color, setColor] = useState(project?.color);
const [deadline, setDeadline] = useState(project?.deadline); const [deadline, setDeadline] = useState(project?.deadline);
const [error, setError] = useState(''); const [error, setError] = useState('');
...@@ -66,15 +75,22 @@ export default function ProjectForm({ project, onSubmit }: Props) { ...@@ -66,15 +75,22 @@ export default function ProjectForm({ project, onSubmit }: Props) {
const colors = Object.values(ProjectColors); const colors = Object.values(ProjectColors);
const allStatus = Object.values(Status);
console.log(allStatus);
const handleSubmit = useCallback(async (e: FormEvent) => { const handleSubmit = useCallback(async (e: FormEvent) => {
e.preventDefault(); e.preventDefault();
if (validateName(name ?? '') === null && validateText(text ?? '') === null && validateColor(color ?? '') === null) { if (validateName(name ?? '') === null &&
onSubmit?.(teams, name ?? '', text ?? '', color ?? '', deadline); validateText(text ?? '') === null &&
validateColor(color ?? '') === null &&
validateTeams(teams) === null
) {
onSubmit?.(teams, name ?? '', text ?? '', color ?? '', status ?? Status.OPEN, deadline);
} else { } else {
setError('Please fill in the mandatory fields.'); setError('Please fill in the mandatory fields.');
} }
}, [onSubmit, setError, name, text, color, deadline, teams]); }, [onSubmit, setError, name, text, color, deadline, teams, status]);
return ( return (
<form onSubmit={handleSubmit} className="project-form"> <form onSubmit={handleSubmit} className="project-form">
...@@ -137,8 +153,23 @@ export default function ProjectForm({ project, onSubmit }: Props) { ...@@ -137,8 +153,23 @@ export default function ProjectForm({ project, onSubmit }: Props) {
} }
</div> </div>
{
status &&
<select onChange={(e: any) => {
let currentStatus = Object.values(Status).find(s => s === e.target.value) ?? Status.OPEN;
setStatus(currentStatus);
}
}>
{
allStatus.map((s) => (
<option selected={status === s} value={s} key={s}>{s}</option>
))
}
</select>
}
<Button type="submit"> <Button type="submit">
Create {project ? 'Update' : 'Create'}
</Button> </Button>
</form> </form>
) )
......
import { createProject } from "adapters/project"; import { createProject, Status } from "adapters/project";
import ProjectForm from "components/forms/ProjectForm"; import ProjectForm from "components/forms/ProjectForm";
import { useCallback, useState } from "react"; import { useCallback, useState } from "react";
import { useHistory } from "react-router"; import { useHistory } from "react-router";
...@@ -7,7 +7,7 @@ import Callout from 'components/ui/Callout'; ...@@ -7,7 +7,7 @@ import Callout from 'components/ui/Callout';
export default function ProjectCreate() { export default function ProjectCreate() {
const history = useHistory(); const history = useHistory();
const [error, setError] = useState(''); const [error, setError] = useState('');
const handleSubmit = useCallback(async (teams: string[], name: string, text: string, color: string, deadline?: Date) => { const handleSubmit = useCallback(async (teams: string[], name: string, text: string, color: string, status?: Status, deadline?: Date) => {
try { try {
if (await createProject({ teams, name, text, color, deadline })) { if (await createProject({ teams, name, text, color, deadline })) {
history.push('/projects'); history.push('/projects');
......
import { getProject, Project, updateProject } from 'adapters/project'; import { getProject, Project, Status, updateProject } from 'adapters/project';
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { useHistory, useParams } from 'react-router'; import { useHistory, useParams } from 'react-router';
import ProjectForm from 'components/forms/ProjectForm'; import ProjectForm from 'components/forms/ProjectForm';
...@@ -21,27 +21,34 @@ export default function ProjectEdit() { ...@@ -21,27 +21,34 @@ export default function ProjectEdit() {
history.goBack(); history.goBack();
}); });
}, []); }, []);
const handleSubmit = useCallback(async (teams: string[], name: string, text: string, color: string, deadline?: Date) => {
const handleSubmit = useCallback(async (teams: string[], name: string, text: string, color: string, status?: Status, deadline?: Date) => {
try { try {
/*if (await updateProject({ teams, name, text, color, deadline })) {
if (project) {
let removedTeams = project.teams, addedTeams = teams;
removedTeams.filter((teamId) => teams.indexOf(teamId) === -1);
addedTeams.filter((teamId) => project.teams.indexOf(teamId) === -1);
await updateProject(project.id, { remove_teams: removedTeams, add_teams: addedTeams, name, text, color, status, deadline: deadline?.toString() });
history.push('/projects'); history.push('/projects');
} else { }
setError('There was an error with your registration. Please try again!'); } catch (e) {
}*/ setError('There was an error with updating your project. Please try again!');
} catch (e) { } }
}, [history]); }, [history, project]);
return ( return (
<div className="project-create-page"> <div className="project-create-page">
{ {
project ? project ?
<div className="content-container"> <div className="content-container">
<h1>Edit the project {project?.name}</h1> <h1>Edit the project {project?.name}</h1>
{error && <Callout message={error} />} {error && <Callout message={error} />}
<ProjectForm onSubmit={handleSubmit} project={project} /> <ProjectForm onSubmit={handleSubmit} project={project} />
</div> : </div> :
<h2>Loading...</h2> <h2>Loading...</h2>
} }
</div> </div>
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment