guesstimate/client/src/routes/project/index.tsx

70 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-07-09 13:00:42 +02:00
import React, { FunctionComponent, useEffect } from "react";
2020-04-22 22:07:52 +02:00
import style from "./style.module.css";
import { newProject, Project } from "../../models/project";
import { useProjectReducer, updateProjectLabel, patchProject } from "../../hooks/use-project-reducer";
2020-04-20 14:07:26 +02:00
import { getProjectStorageKey } from "../../util/storage";
import { useLocalStorage } from "../../hooks/use-local-storage";
2020-04-21 09:24:39 +02:00
import EditableText from "../../components/editable-text";
2020-04-21 14:10:50 +02:00
import Tabs from "../../components/tabs";
import EstimationTab from "./estimation-tab";
2020-04-21 20:45:47 +02:00
import ParamsTab from "./params-tab";
2020-04-23 08:45:06 +02:00
import ExportTab from "./export-tab";
import { useServerSync } from "../../hooks/use-server-sync";
2020-07-09 13:00:42 +02:00
import { RouteChildrenProps, RouteProps, useParams } from "react-router";
2020-04-20 11:14:46 +02:00
2020-04-20 14:07:26 +02:00
export interface ProjectProps {
2020-07-09 13:00:42 +02:00
projectId: string
2020-04-20 14:07:26 +02:00
}
2020-07-09 13:00:42 +02:00
const Project: FunctionComponent<ProjectProps> = () => {
const { projectId } = useParams();
const projectStorageKey = getProjectStorageKey(projectId);
const [ storedProject, storeProject ] = useLocalStorage(projectStorageKey, newProject(projectId));
const [ project, dispatch ] = useProjectReducer(storedProject);
useServerSync(project, (project: Project) => {
dispatch(patchProject(project));
});
const onProjectLabelChange = (projectLabel: string) => {
dispatch(updateProjectLabel(projectLabel));
};
// Save project in local storage on change
useEffect(()=> {
storeProject(project);
}, [project]);
return (
<div className={`container ${style.estimation}`}>
<EditableText
editIconClass="is-size-4"
render={(value) => (<h2 className="is-size-3">{value}</h2>)}
onChange={onProjectLabelChange}
value={project.label ? project.label : "Projet sans nom"}
/>
<div className={style.tabContainer}>
<Tabs items={[
{
label: 'Estimation',
icon: '📋',
render: () => <EstimationTab project={project} dispatch={dispatch} />
},
{
label: 'Options avancées',
icon: '⚙️',
render: () => <ParamsTab project={project} dispatch={dispatch} />
},
{
label: 'Exporter',
icon: '↗️',
render: () => <ExportTab project={project} />
}
]} />
</div>
</div>
);
2020-04-20 11:14:46 +02:00
};
2020-07-09 13:00:42 +02:00
2020-04-20 11:14:46 +02:00
export default Project;