Skip to content

Commit

Permalink
Merge pull request #281 from itenium-be/project-extension
Browse files Browse the repository at this point in the history
Addition of date validations
  • Loading branch information
Laoujin authored Jul 5, 2024
2 parents 3486fde + 89e10ca commit bce5a17
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
3 changes: 2 additions & 1 deletion frontend/src/components/controls/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ModalProps = BaseModalProps & {
children: any,
/** For custom sizing of the Modal etc */
dialogClassName?: string,
disableSave?: boolean
}

export const Modal = (props: ModalProps) => {
Expand All @@ -54,7 +55,7 @@ export const Modal = (props: ModalProps) => {
<ReactModal.Footer>
<ReactButton onClick={props.onClose} variant="light">{t('close')}</ReactButton>
{props.onConfirm ? (
<ReactButton onClick={onConfirm} variant={props.confirmVariant || 'success'}>
<ReactButton onClick={onConfirm} variant={props.confirmVariant || 'success'} disabled={props.disableSave}>
{props.confirmText || t('save')}
</ReactButton>
) : null}
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/components/controls/other/ProjectValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Moment} from "moment";

export const isDateIntervalValid = (startDate: Moment, endDate: Moment | undefined):boolean => {
return startDate.diff(endDate) < 0;
};

export const isProjectValid = (startDate: Moment, endDate: Moment | undefined, previousProjectEndDate: Moment):boolean =>{
const intervalValid = isDateIntervalValid(startDate, endDate)
const projectValid = isDateIntervalValid(previousProjectEndDate, startDate)
return intervalValid && projectValid
}
74 changes: 74 additions & 0 deletions frontend/src/components/controls/spec/ProjectValidator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import moment from "moment"
import { isDateIntervalValid, isProjectValid } from "../other/ProjectValidator"

describe('ProjectValidator is date inteval valid', () => {
it('Should return true if start date is smaller then end date', () => {
const startDate = moment('2024-01-01')
const endDate = moment('2024-01-31')

const res = isDateIntervalValid(startDate, endDate)

expect(res).toBeTruthy()
})

it('Should return true if end date is undefined', () => {
const startDate = moment('2024-01-01')
const endDate = undefined

const res = isDateIntervalValid(startDate, endDate)

expect(res).toBeTruthy()
})

it('Should return false if start date is bigger then end date', () => {
const startDate = moment('2024-02-01')
const endDate = moment('2024-01-31')

const res = isDateIntervalValid(startDate, endDate)

expect(res).toBeFalsy()
})
})


describe('ProjectValidator is project valid', () => {
it('Should return true if previous end date is smaller then new start date and end date is undefined', () => {
const startDate = moment('2024-01-01')
const endDate = undefined
const previousProjectEndDate = moment('2023-12-31')

const res = isProjectValid(startDate, endDate, previousProjectEndDate)

expect(res).toBeTruthy()
})

it('Should return true if previous end date is smaller then new start date and new end date is bigger then new start date', () => {
const startDate = moment('2024-01-01')
const endDate = moment('2024-01-31')
const previousProjectEndDate = moment('2023-12-31')

const res = isProjectValid(startDate, endDate, previousProjectEndDate)

expect(res).toBeTruthy()
})

it('Should return false if previous end date is bigger then new start date and new end date is undefined', () => {
const startDate = moment('2024-12-31')
const endDate = undefined
const previousProjectEndDate = moment('2023-01-01')

const res = isProjectValid(startDate, endDate, previousProjectEndDate)

expect(res).toBeFalsy()
})

it('Should return false if new start date is bigger then new end date', () => {
const startDate = moment('2024-01-02')
const endDate = moment('2023-01-01')
const previousProjectEndDate = moment('2023-01-01')

const res = isProjectValid(startDate, endDate, previousProjectEndDate)

expect(res).toBeFalsy()
})
})
2 changes: 2 additions & 0 deletions frontend/src/components/project/CopyProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {saveProject} from '../../actions/projectActions';
import {ArrayInput} from '../controls/form-controls/inputs/ArrayInput';
import {FullFormConfig} from '../../models';
import {ContractStatus} from '../client/models/ContractModels';
import {isProjectValid} from '../controls/other/ProjectValidator';

type CopyProjectProps = {
projectToCopy: IProjectModel;
Expand Down Expand Up @@ -47,6 +48,7 @@ export const CopyProject = ({projectToCopy}: CopyProjectProps) => {
onClose={() => setOpen(false)}
onConfirm={() => dispatch(saveProject(project, history, 'to-details') as any)}
title={t('project.copy.modalTitle')}
disableSave={!isProjectValid(project.startDate, project.endDate, projectToCopy.endDate!)}
>
<div className="container">
<div className="row">
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/project/EditProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {Col} from 'react-bootstrap';
import { SingleContractIcon } from "../client/contract/SingleContractIcon";
import {EnhanceWithConfirmation} from '../enhancers/EnhanceWithConfirmation';
import {Button} from '../controls/form-controls/Button';
import {isDateIntervalValid} from '../controls/other/ProjectValidator';


const ConfirmationButton = EnhanceWithConfirmation(Button);
Expand Down Expand Up @@ -102,7 +103,7 @@ export const EditProject = () => {
const isButtonDisabled = !project.consultantId
|| !project.client || !project.client.clientId
|| (!project.client.defaultInvoiceLines.length && !project.client.defaultInvoiceLines[0].price)
|| !project.startDate;
|| !project.startDate || !isDateIntervalValid(project.startDate, project.endDate);

return (
<Container className="edit-container">
Expand Down

0 comments on commit bce5a17

Please sign in to comment.