Skip to content

Commit

Permalink
hook for copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninarchive committed Dec 20, 2023
1 parent 9f850d7 commit e96b29d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
7 changes: 2 additions & 5 deletions frontend/src/actions/projectActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {TimesheetCheckAttachmentType} from '../models';
import {authService} from '../components/users/authService';
import {FullProjectMonthModel} from '../components/project/models/FullProjectMonthModel';

export function saveProject(project: IProjectModel, navigate?: any, after: 'to-list' | 'to-details' = 'to-list') {
export function saveProject(project: IProjectModel, navigate?: any, after: 'to-list' | 'to-details' = 'to-list', triggerCopy?: any) {
return (dispatch: Dispatch) => {
dispatch(busyToggle());
return request
Expand All @@ -30,10 +30,7 @@ export function saveProject(project: IProjectModel, navigate?: any, after: 'to-l
if (after === 'to-list') {
navigate('/projects');
} else {
// First navigate away?
// Workaround for EditProject not reloading the form
// when the url _id changes. Need a hook for this :)
navigate('/projects');
triggerCopy(response.body._id);
navigate(`/projects/${response.body._id}`);
}
}
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/project/CopyProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {Modal} from '../controls/Modal';
import {saveProject} from '../../actions/projectActions';
import {ArrayInput} from '../controls/form-controls/inputs/ArrayInput';
import {FullFormConfig} from '../../models';
import { ContractStatus } from '../client/models/ContractModels';
import {ContractStatus} from '../client/models/ContractModels';

type CopyProjectProps = {
projectToCopy: IProjectModel;
triggerCopy: any;
}


Expand All @@ -25,7 +26,7 @@ export const copyProjectProperties: FullFormConfig = [
];


export const CopyProject = ({projectToCopy}: CopyProjectProps) => {
export const CopyProject = ({projectToCopy, triggerCopy}: CopyProjectProps) => {
const [open, setOpen] = useState<boolean>(false);
const defaultProject = {
...projectToCopy,
Expand All @@ -45,7 +46,7 @@ export const CopyProject = ({projectToCopy}: CopyProjectProps) => {
<Modal
show
onClose={() => setOpen(false)}
onConfirm={() => dispatch(saveProject(project, history, 'to-details') as any)}
onConfirm={() => dispatch(saveProject(project, history, 'to-details', triggerCopy) as any)}
title={t('project.copy.modalTitle')}
>
<div className="container">
Expand Down
13 changes: 9 additions & 4 deletions frontend/src/components/project/EditProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,23 @@ export const EditProject = () => {
const navigate = useNavigate();
const params = useParams();
const model = useSelector((state: ConfacState) => state.projects.find(c => c._id === params.id));
console.log('retrieved model', model);
const consultants = useSelector((state: ConfacState) => state.consultants);
const [project, setProject] = useState<IProjectModel>(model || getNewProject());
const consultant = useSelector((state: ConfacState) => state.consultants.find(x => x._id === project.consultantId) || getNewConsultant());
const clients = useSelector((state: ConfacState) => state.clients);
const client = useSelector((state: ConfacState) => state.clients.find(x => x._id === project.client.clientId) || getNewClient());
const hasProjectMonths = useSelector((state: ConfacState) => state.projectsMonth.some(pm => pm.projectId === params.id));
const [needsSync, setNeedsSync] = useState<{consultant: boolean, client: boolean}>({consultant: false, client: false});
const [copyId, setCopyId] = useState(0);
const useCopy = (id: number) => {
setCopyId(id);
}

const docTitle = consultant._id ? 'projectEdit' : 'projectNew';
useDocumentTitle(docTitle, {consultant: consultant.firstName, client: client.name});

if (model && !project._id) {
if (model && (!project._id || copyId !== 0)) {
setCopyId(0); //needs to be reset, otherwise page will go into render loop
setProject(model);
}

Expand Down Expand Up @@ -131,7 +136,7 @@ export const EditProject = () => {
</Form>
<StickyFooter claim={Claim.ManageProjects}>
<ConfirmationButton
className="tst-confirm-delete-project"
className="tst-confirm-delete-project"
onClick={() => dispatch(deleteProject(project._id, navigate) as any)}
variant="danger"
title={t('project.deleteConfirm.title')}
Expand All @@ -140,7 +145,7 @@ export const EditProject = () => {
>
{t('project.deleteConfirm.content')}
</ConfirmationButton>
{project.endDate && project._id && <CopyProject projectToCopy={project} />}
{project.endDate && project._id && <CopyProject projectToCopy={project} triggerCopy={useCopy} />}
<BusyButton className="tst-save-project" onClick={() => dispatch(saveProject(project, navigate) as any)} disabled={isButtonDisabled}>
{t('save')}
</BusyButton>
Expand Down

0 comments on commit e96b29d

Please sign in to comment.