diff --git a/lab4/ClientApp/src/reports/DailyReport.tsx b/lab4/ClientApp/src/reports/DailyReport.tsx index 34cfe46..ab74218 100644 --- a/lab4/ClientApp/src/reports/DailyReport.tsx +++ b/lab4/ClientApp/src/reports/DailyReport.tsx @@ -1,17 +1,17 @@ import React, {useContext, useEffect, useState} from 'react'; import {LastDateContext} from "../App"; import DailyReportModel from "../models/DailyReportModel"; -import {Button, ButtonToolbar, Form, Table} from "react-bootstrap"; +import { Table } from "react-bootstrap"; import ProjectModel from "../models/ProjectModel"; import ReportEntryCreationRequest from "../models/ReportEntryCreationRequest"; import DailySummary from "../reports/DailySummary"; import '../custom.css'; import ReportEntryUpdateRequest from "../models/ReportEntryUpdateRequest"; -import getSpecificSetter from "../getSpecificSetter"; import ReportEntryModel from "../models/ReportEntryModel"; import ApiConnector from "../ApiConnector"; import ReportEntry from "./ReportEntry"; import EditableReportEntry from "./EditableReportEntry"; +import ReportEntryCreator from "./ReportEntryCreator"; export default function DailyReport() { const lastDateState = useContext(LastDateContext); @@ -26,32 +26,10 @@ export default function DailyReport() { const [projectList, setProjectList] = useState(null); useEffect(() => { ApiConnector.getProjects() - .then(data => { - setProjectList(data); - if (data.length > 0) - setAddedEntryProject(data[0].code); - }); + .then(data => setProjectList(data)) + .catch(error => alert(error)); }, []); - const [addedEntry, setAddedEntry] = useState({ - projectCode: '', - categoryCode: '', - time: 1, - description: '' - }); - const setAddedEntryProject = (projectCode: string) => setAddedEntry(prevState => ({...prevState, projectCode: projectCode, categoryCode: '' })); - const setAddedEntryCategory = getSpecificSetter(addedEntry, setAddedEntry, 'categoryCode') as ((value: typeof addedEntry.categoryCode) => void); - const setAddedEntryTime = (time: number) => setAddedEntry(prevValue => ({ ...prevValue, time: Math.max(time, 1) })); - const setAddedEntryDescription = getSpecificSetter(addedEntry, setAddedEntry, 'description') as ((value: typeof addedEntry.description) => void); - const clearAddedEntry = () => { - setAddedEntry({ - projectCode: projectList?.at(0)?.code ?? '', - categoryCode: '', - time: 1, - description: '' - }); - } - const [modifiedEntryId, setModifiedEntryId] = useState(null); const updateEntry = (updateRequest: ReportEntryUpdateRequest) => { @@ -65,11 +43,11 @@ export default function DailyReport() { .catch(error => alert(error)); }; - const appendEntry = () => { - ApiConnector.addReportEntry(lastDateState.state.lastDate, addedEntry) + const appendEntry = (addedEntry: ReportEntryCreationRequest) => { + return ApiConnector.addReportEntry(lastDateState.state.lastDate, addedEntry) .then(() => { - clearAddedEntry(); refreshDailyReport(); + return Promise.resolve(); }) .catch(error => alert(error)); }; @@ -127,34 +105,7 @@ export default function DailyReport() { Dodaj wpis... - - - setAddedEntryProject(evt.target.value)}> - {projectList?.map(project => ( - - ))} - - - - setAddedEntryCategory(evt.target.value)}> - {projectList?.filter(e => e.code === addedEntry.projectCode)[0]?.categories.map(category => ( - - ))} - - - - setAddedEntryTime(Number(evt.target.value))} /> - - - setAddedEntryDescription(evt.target.value)} /> - - - - - - - - + appendEntry(created)} /> {dailyReport !== null && projectList !== null ? (<> diff --git a/lab4/ClientApp/src/reports/ReportEntryCreator.tsx b/lab4/ClientApp/src/reports/ReportEntryCreator.tsx new file mode 100644 index 0000000..03d8239 --- /dev/null +++ b/lab4/ClientApp/src/reports/ReportEntryCreator.tsx @@ -0,0 +1,75 @@ +import {Button, ButtonToolbar, Form} from "react-bootstrap"; +import React, { useEffect, useState } from "react"; +import '../custom.css'; +import ReportEntryCreationRequest from "../models/ReportEntryCreationRequest"; +import getSpecificSetter from "../getSpecificSetter"; +import ProjectModel from "../models/ProjectModel"; + +type ReportEntryCreatorProps = { + projects: ProjectModel[] | null; + onConfirmClick: (created: ReportEntryCreationRequest) => Promise; +}; + +export default function ReportEntryCreator(props: ReportEntryCreatorProps) { + const [addedEntry, setAddedEntry] = useState({ + projectCode: '', + categoryCode: '', + time: 1, + description: '' + }); + const setAddedEntryProject = (projectCode: string) => setAddedEntry(prevState => ({...prevState, projectCode: projectCode, categoryCode: '' })); + const setAddedEntryCategory = getSpecificSetter(addedEntry, setAddedEntry, 'categoryCode') as ((value: typeof addedEntry.categoryCode) => void); + const setAddedEntryTime = (time: number) => setAddedEntry(prevValue => ({ ...prevValue, time: Math.max(time, 1) })); + const setAddedEntryDescription = getSpecificSetter(addedEntry, setAddedEntry, 'description') as ((value: typeof addedEntry.description) => void); + const clearAddedEntry = () => { + setAddedEntry({ + projectCode: props.projects?.at(0)?.code ?? '', + categoryCode: '', + time: 1, + description: '' + }); + } + + useEffect(() => { + if (props.projects && props.projects.length > 0) + setAddedEntryProject(props.projects[0].code); + else + setAddedEntryProject(''); + }, [props.projects]); + + const addEntry = () => { + props.onConfirmClick(addedEntry) + .then(clearAddedEntry); + } + + return ( + + setAddedEntryProject(evt.target.value)}> + {props.projects?.map(project => ( + + ))} + + + + setAddedEntryCategory(evt.target.value)}> + {props.projects?.filter(e => e.code === addedEntry.projectCode)[0]?.categories.map(category => ( + + ))} + + + + setAddedEntryTime(Number(evt.target.value))} /> + + + setAddedEntryDescription(evt.target.value)} /> + + + + + + + + ); +}