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 RotationForm to ts #2474

Merged
merged 6 commits into from
Jul 19, 2022
Merged
Changes from 1 commit
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
@@ -1,5 +1,4 @@
import React, { useState } from 'react'
import p from 'prop-types'
import {
TextField,
Grid,
Expand All @@ -18,6 +17,24 @@ import { TimeZoneSelect } from '../selection'
import { ISODateTimePicker } from '../util/ISOPickers'
import NumberField from '../util/NumberField'
import Spinner from '../loading/components/Spinner'
import { FieldError } from '../util/errutil'

interface RotationFormProps {
value: Value
errors: FieldError[]
onChange: (value: Value) => void
disabled?: boolean
}

interface Value {
name: string
description: string
timeZone: string
type: string
shiftLength: number
start: string
favorite: boolean
}

const query = gql`
query calcRotationHandoffTimes($input: CalcRotationHandoffTimesInput) {
Expand Down Expand Up @@ -48,16 +65,16 @@ const useStyles = makeStyles({

// getHours converts a count and one of ['hourly', 'daily', 'weekly']
// into length in hours e.g. (2, daily) => 48
function getHours(count, unit) {
const lookup = {
function getHours(count: number, unit: string): number {
const lookup: { [unit: string]: number } = {
hourly: 1,
daily: 24,
weekly: 24 * 7,
}
return lookup[unit] * count
}

export default function RotationForm(props) {
export default function RotationForm(props: RotationFormProps): JSX.Element {
const { value } = props
const classes = useStyles()
const localZone = DateTime.local().zone.name
Expand All @@ -81,7 +98,7 @@ export default function RotationForm(props) {
const isHandoffValid = DateTime.fromISO(value.start).isValid
const nextHandoffs = isCalculating
? []
: data.calcRotationHandoffTimes.map((iso) =>
: data.calcRotationHandoffTimes.map((iso: string) =>
DateTime.fromISO(iso)
.setZone(configZone)
.toLocaleString(DateTime.DATETIME_FULL),
Expand Down Expand Up @@ -182,7 +199,7 @@ export default function RotationForm(props) {
</Typography>
{isHandoffValid ? (
<ol className={classes.handoffsList}>
{nextHandoffs.map((text, i) => (
{nextHandoffs.map((text: string, i: number) => (
<Typography
key={i}
component='li'
Expand Down Expand Up @@ -210,31 +227,3 @@ export default function RotationForm(props) {
</FormContainer>
)
}

RotationForm.propTypes = {
value: p.shape({
name: p.string.isRequired,
description: p.string.isRequired,
timeZone: p.string.isRequired,
type: p.oneOf(rotationTypes).isRequired,
shiftLength: p.number.isRequired,
start: p.string.isRequired,
}).isRequired,

errors: p.arrayOf(
p.shape({
field: p.oneOf([
'name',
'description',
'timeZone',
'type',
'start',
'shiftLength',
]).isRequired,
message: p.string.isRequired,
}),
),

onChange: p.func.isRequired,
disabled: p.bool,
}