Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Duration to Sampling Period UI Table #1414

Merged
merged 14 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { DATE_FORMAT } from 'constants/dateTimeFormats';
import dayjs from 'dayjs';
import { useCodesContext } from 'hooks/useContext';
import { formatTimeDifference } from 'utils/datetime';
import { getCodesName } from 'utils/Utils';

export interface ISamplingSitePeriodRowData {
Expand Down Expand Up @@ -82,6 +83,15 @@
field: 'end_time',
headerName: 'End time',
flex: 1
},
{
field: 'duration',
headerName: 'Duration',
flex: 1,
renderCell: (params) => {
const { start_date, start_time, end_date, end_time } = params.row;
return formatTimeDifference(start_date, start_time, end_date, end_time);

Check warning on line 93 in app/src/features/surveys/sampling-information/periods/table/SamplingPeriodTable.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/features/surveys/sampling-information/periods/table/SamplingPeriodTable.tsx#L92-L93

Added lines #L92 - L93 were not covered by tests
}
}
];

Expand Down
49 changes: 49 additions & 0 deletions app/src/utils/datetime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
import { pluralize } from './Utils';

dayjs.extend(duration);

Check warning on line 5 in app/src/utils/datetime.ts

View check run for this annotation

Codecov / codecov/patch

app/src/utils/datetime.ts#L5

Added line #L5 was not covered by tests
NickPhura marked this conversation as resolved.
Show resolved Hide resolved

/**
* Combine date and time and return ISO string.
*
Expand All @@ -11,3 +17,46 @@
}
return new Date(`${date}T00:00:00`).toISOString();
};

/**
* Formats the time difference between two timestamps into a human-readable string.
*
* @param {string} startDate
* @param {string | null} startTime
* @param {string} endDate
* @param {string | null} endTime
* @returns {string | null} A formatted string indicating an amount of time
*/
export const formatTimeDifference = (

Check warning on line 30 in app/src/utils/datetime.ts

View check run for this annotation

Codecov / codecov/patch

app/src/utils/datetime.ts#L30

Added line #L30 was not covered by tests
startDate: string,
startTime: string | null,
endDate: string,
endTime: string | null
) => {
const startDateTime = startTime ? dayjs(`${startDate} ${startTime}`) : dayjs(startDate);
const endDateTime = endTime ? dayjs(`${endDate} ${endTime}`) : dayjs(endDate);

// Calculate the difference in milliseconds
const diff = endDateTime.diff(startDateTime);

Check warning on line 40 in app/src/utils/datetime.ts

View check run for this annotation

Codecov / codecov/patch

app/src/utils/datetime.ts#L40

Added line #L40 was not covered by tests

// Calculate years, days, hours, minutes, and seconds
const years = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
mauberti-bc marked this conversation as resolved.
Show resolved Hide resolved
const days = Math.floor((diff / (1000 * 60 * 60 * 24)) % 365);
const hours = Math.floor((diff / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((diff / (1000 * 60)) % 60);
const seconds = Math.floor((diff / 1000) % 60);

Check warning on line 47 in app/src/utils/datetime.ts

View check run for this annotation

Codecov / codecov/patch

app/src/utils/datetime.ts#L43-L47

Added lines #L43 - L47 were not covered by tests

// Construct the time difference string
const formatParts = [];

Check warning on line 50 in app/src/utils/datetime.ts

View check run for this annotation

Codecov / codecov/patch

app/src/utils/datetime.ts#L50

Added line #L50 was not covered by tests

if (years > 0) formatParts.push(`${years} ${pluralize(years, 'year')}`);
if (days > 0) formatParts.push(`${days} ${pluralize(days, 'day')}`);
if (hours > 0) formatParts.push(`${hours} ${pluralize(hours, 'hour')}`);
if (minutes > 0) formatParts.push(`${minutes} ${pluralize(minutes, 'minute')}`);
if (seconds > 0) formatParts.push(`${seconds} ${pluralize(seconds, 'second')}`);

// Return formatted string.
// Slice to omit unnecessary detail with the assumption that if the duration is > 1 hour, seconds do not matter, if > 1 year, minutes do not matter, etc.
// If duration is 0, return null
return formatParts.length > 0 ? formatParts.slice(0, 2).join(' and ') : null;
};
Loading