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

ui/rotations: convert util.js to ts #2479

Merged
merged 2 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions web/src/app/rotations/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { calcNewActiveIndex, handoffSummary, reorderList } from './util'
import {
calcNewActiveIndex,
handoffSummary,
HandoffSummaryInput,
reorderList,
} from './util'
import { Settings, Zone } from 'luxon'
import { CreateRotationInput } from '../../schema'

let oldZone: Zone
beforeAll(() => {
Expand Down Expand Up @@ -45,7 +49,7 @@ describe('calcNewActiveIndex', () => {
})

describe('handoffSummary', () => {
const check = (rotation: Partial<CreateRotationInput>, exp: string): void => {
const check = (rotation: HandoffSummaryInput, exp: string): void => {
expect(handoffSummary(rotation)).toBe(exp)
}

Expand Down
31 changes: 25 additions & 6 deletions web/src/app/rotations/util.js → web/src/app/rotations/util.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { DateTime } from 'luxon'
import { CreateRotationInput, RotationType } from '../../schema'

// calcNewActiveIndex returns the newActiveIndex for a swap operation
// -1 will be returned if there was no change
export function calcNewActiveIndex(oldActiveIndex, oldIndex, newIndex) {
export function calcNewActiveIndex(
oldActiveIndex: number,
oldIndex: number,
newIndex: number,
): number {
if (oldIndex === newIndex) {
return -1
}
Expand All @@ -21,7 +26,7 @@ export function calcNewActiveIndex(oldActiveIndex, oldIndex, newIndex) {
}

// formatTime returns the formatted time with the timezone (if different than local timezone)
export function formatTime(time, tz) {
export function formatTime(time: string, tz: string): string {
const schedTime = DateTime.fromISO(time, { zone: tz }).toLocaleString(
DateTime.TIME_SIMPLE,
)
Expand All @@ -36,7 +41,7 @@ export function formatTime(time, tz) {
}

// formatDay returns the day given a time and timezone
export function formatDay(time, tz) {
export function formatDay(time: string, tz: string): string {
const day = DateTime.fromISO(time, { zone: tz }).weekdayLong
const localDay = DateTime.fromISO(time).weekdayLong

Expand All @@ -49,7 +54,11 @@ export function formatDay(time, tz) {

// formatWeeklySummary returns the summary for a weekly rotation
// taking into consideration extra formatting needed if timezone does not match with local timezone
export function formatWeeklySummary(shiftLength, start, tz) {
export function formatWeeklySummary(
shiftLength: number,
start: string,
tz: string,
): string {
let details = ''
const day = DateTime.fromISO(start, { zone: tz }).weekdayLong
const schedTime = DateTime.fromISO(start, { zone: tz }).toLocaleString(
Expand All @@ -71,8 +80,14 @@ export function formatWeeklySummary(shiftLength, start, tz) {
return details
}

export interface HandoffSummaryInput extends Partial<CreateRotationInput> {
start: string
shiftLength: number
type: RotationType
}

// handoffSummary returns the summary description for the rotation
export function handoffSummary(rotation) {
export function handoffSummary(rotation: HandoffSummaryInput): string {
const tz = rotation.timeZone

if (!tz) return 'Loading handoff information...'
Expand Down Expand Up @@ -106,7 +121,11 @@ export function handoffSummary(rotation) {

// reorderList will move an item from the oldIndex to the newIndex, preserving order
// returning the result as a new array.
export function reorderList(_items, oldIndex, newIndex) {
export function reorderList(
_items: unknown[],
oldIndex: number,
newIndex: number,
): unknown[] {
const items = _items.slice()
items.splice(oldIndex, 1) // remove 1 element from oldIndex position
items.splice(newIndex, 0, _items[oldIndex]) // add dest to newIndex position
Expand Down