Skip to content

Commit

Permalink
Merge pull request #52 from syukoGit/51-remove-duration-in-hour-for-t…
Browse files Browse the repository at this point in the history
…he-create-activity-form

51 remove duration in hour for the create activity form
  • Loading branch information
syukoGit authored Dec 4, 2023
2 parents d4049b6 + 22d64f5 commit 01e859c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const NewActivityForm = ({ defaultStartDateTime = new Date(), defaultEndDateTime
const [newActivity, setNewActivity] = useState<NewActivity>({
title: '',
rate: 0,
durationInHours: 0,
startDateTime: defaultStartDateTime,
endDateTime: defaultEndDateTime,
unitAmount: 7.92,
Expand All @@ -37,10 +36,12 @@ const NewActivityForm = ({ defaultStartDateTime = new Date(), defaultEndDateTime
unitAmountError: undefined,
});

const amount =
isNaN(newActivity.durationInHours) || isNaN(newActivity.rate)
const durationInHours =
isNaN(newActivity.startDateTime.getTime()) || isNaN(newActivity.endDateTime.getTime())
? 0
: Number((newActivity.durationInHours * newActivity.unitAmount * (newActivity.rate / 100)).toFixed(2));
: (newActivity.endDateTime.getTime() - newActivity.startDateTime.getTime()) / 3600000;

const amount = isNaN(newActivity.rate) ? 0 : Number((durationInHours * newActivity.unitAmount * (newActivity.rate / 100)).toFixed(2));

const onSaveClick = () => {
let hasError = false;
Expand All @@ -62,11 +63,6 @@ const NewActivityForm = ({ defaultStartDateTime = new Date(), defaultEndDateTime
hasError = true;
}

if (isNaN(newActivity.durationInHours)) {
durationInHoursError = 'Duration is required';
hasError = true;
}

if (isNaN(newActivity.startDateTime.getTime())) {
startDateError = 'Start Date is required';
hasError = true;
Expand Down Expand Up @@ -155,14 +151,6 @@ const NewActivityForm = ({ defaultStartDateTime = new Date(), defaultEndDateTime
id='new-activity-form-end-date'
errorMessages={errors.endDateError ? [errors.endDateError] : undefined}
/>
<NumberInput
label='Duration in hour'
value={newActivity.durationInHours}
onChange={(e) => setNewActivity({ ...newActivity, durationInHours: e ?? NaN })}
required
id='new-activity-form-duration-in-hour'
errorMessages={errors.durationInHoursError ? [errors.durationInHoursError] : undefined}
/>
<NumberInput
label='Rate'
value={newActivity.rate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

&__inputs {
display: grid;
grid-template-columns: repeat(6, 0.5fr);
grid-template-columns: repeat(4, 0.5fr);
grid-row: repeat(4, auto);
gap: 20px;
width: 100%;

> :nth-child(1) {
grid-area: 1 / 1 / 2 / 7;
grid-area: 1 / 1 / 2 / 5;
}

> :nth-child(2) {
Expand All @@ -25,15 +25,11 @@
}

> :nth-child(4) {
grid-area: 2 / 5 / 3 / 7;
grid-area: 3 / 1 / 4 / 3;
}

> :nth-child(5) {
grid-area: 3 / 1 / 4 / 4;
}

> :nth-child(6) {
grid-area: 3 / 4 / 4 / 7;
grid-area: 3 / 3 / 4 / 5;
}
}
}
}
9 changes: 6 additions & 3 deletions FirefighterStats/Client/src/types/Activity.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
type Activity = {
amount: number;
durationInHours: number;
endDateTime: Date;
durationInHours: number;
id: string;
rate: number;
startDateTime: Date;
title: string;
unitAmount: number;
};

export type NewActivity = Omit<Activity, 'amount' | 'id'>;
export type NewActivity = Omit<Activity, 'amount' | 'id' | 'durationInHours'>;

export function isActivity(data: any): data is Activity {
return (
Expand All @@ -25,9 +25,12 @@ export function isActivity(data: any): data is Activity {
}

export function getNewActivityPreview(newActivity: NewActivity): Activity {
const durationInHours = Math.abs(newActivity.endDateTime.getTime() - newActivity.startDateTime.getTime()) / 36e5;

return {
...newActivity,
amount: Number((newActivity.durationInHours * newActivity.unitAmount * (newActivity.rate / 100)).toFixed(2)),
durationInHours,
amount: Number((durationInHours * newActivity.unitAmount * (newActivity.rate / 100)).toFixed(2)),
id: `new-activity-${newActivity.title}-${newActivity.startDateTime}-${newActivity.endDateTime}-preview`,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace FirefighterStats.Server.Entities.FirefighterActivities;
[UsedImplicitly]
public class Activity
{
public double Amount => Math.Round(DurationInHours * Rate * UnitAmount, 2);
public double Amount => Math.Round(DurationInHours * Rate * UnitAmount, 2, MidpointRounding.AwayFromZero);

public double DurationInHours => (EndDateTime - StartDateTime).TotalHours;

Expand All @@ -33,4 +33,4 @@ public class Activity
public required string Title { get; set; }

public required double UnitAmount { get; set; }
}
}

0 comments on commit 01e859c

Please sign in to comment.