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/schedules: Temp sched no coverage on page checkbox #1891

Merged
merged 16 commits into from
Sep 21, 2021
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
80 changes: 66 additions & 14 deletions web/src/app/schedules/temp-sched/TempSchedAddShiftsStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
Grid,
Typography,
makeStyles,
FormControlLabel,
Checkbox,
FormHelperText,
} from '@material-ui/core'
import ArrowRightAltIcon from '@material-ui/icons/ArrowRightAlt'
import { contentText, Shift, StepContainer } from './sharedUtils'
Expand All @@ -14,14 +17,23 @@ import TempSchedShiftsList from './TempSchedShiftsList'
import TempSchedAddShiftForm from './TempSchedAddShiftForm'
import { DateTime, Interval } from 'luxon'
import { FieldError } from '../../util/errutil'
import { isISOAfter } from '../../util/shifts'
import { isISOAfter, parseInterval } from '../../util/shifts'
import { Alert, AlertTitle } from '@material-ui/lab'
import { useScheduleTZ } from './hooks'
import { getCoverageGapItems } from './shiftsListUtil'

const useStyles = makeStyles((theme) => ({
contentText,
avatar: {
backgroundColor: theme.palette.primary.main,
},
shiftsListContainer: {
height: '100%',
display: 'flex',
flexDirection: 'column',
},
listOuterContainer: {
height: '100%',
position: 'relative',
overflowY: 'auto',
},
Expand All @@ -36,6 +48,10 @@ const useStyles = makeStyles((theme) => ({
maxHeight: '100%',
paddingRight: '2rem',
},
noCoverageError: {
marginTop: '.5rem',
marginBottom: '.5rem',
},
}))

type AddShiftsStepProps = {
Expand All @@ -46,6 +62,10 @@ type AddShiftsStepProps = {

scheduleID: string
edit?: boolean

coverageGapsAllowed?: boolean
setCoverageGapsAllowed: (isAllowed: boolean) => void
isShowingCoverageGapsWarning: boolean
}

type DTShift = {
Expand Down Expand Up @@ -102,10 +122,14 @@ export default function TempSchedAddShiftsStep({
end,
value,
edit,
coverageGapsAllowed,
setCoverageGapsAllowed,
isShowingCoverageGapsWarning,
}: AddShiftsStepProps): JSX.Element {
const classes = useStyles()
const [shift, setShift] = useState(null as Shift | null)
const [submitted, setSubmitted] = useState(false)
const { zone } = useScheduleTZ(scheduleID)

// set start equal to the temporary schedule's start
// can't this do on mount since the step renderer puts everyone on the DOM at once
Expand Down Expand Up @@ -159,6 +183,10 @@ export default function TempSchedAddShiftsStep({
})
setSubmitted(false)
}
const schedInterval = parseInterval({ start: start, end: end })

const hasCoverageGaps =
getCoverageGapItems(schedInterval, value, zone).length > 0

return (
<StepContainer data-cy='add-shifts-step'>
Expand Down Expand Up @@ -211,20 +239,44 @@ export default function TempSchedAddShiftsStep({
</Grid>

{/* shifts list container */}
<Grid item xs={6} className={classes.listOuterContainer}>
<div className={classes.listInnerContainer}>
<TempSchedShiftsList
scheduleID={scheduleID}
value={value}
start={start}
end={end}
onRemove={(shift: Shift) => {
setShift(shift)
onChange(value.filter((s) => !shiftEquals(shift, s)))
}}
edit={edit}
/>
<Grid item xs={6} className={classes.shiftsListContainer}>
<div className={classes.listOuterContainer}>
<div className={classes.listInnerContainer}>
<TempSchedShiftsList
scheduleID={scheduleID}
value={value}
start={start}
end={end}
onRemove={(shift: Shift) => {
setShift(shift)
onChange(value.filter((s) => !shiftEquals(shift, s)))
}}
edit={edit}
/>
</div>
</div>
{isShowingCoverageGapsWarning && hasCoverageGaps && (
<Alert severity='error' className={classes.noCoverageError}>
<AlertTitle>Gaps in coverage</AlertTitle>
<FormHelperText>
There are gaps in coverage. During these gaps, nobody on the
schedule will receive alerts. If you still want to proceed,
check the box and retry.
</FormHelperText>
<FormControlLabel
label='Allow gaps in coverage'
labelPlacement='end'
control={
<Checkbox
data-cy='no-coverage-checkbox'
checked={coverageGapsAllowed}
onChange={(e) => setCoverageGapsAllowed(e.target.checked)}
name='allowCoverageGaps'
/>
}
/>
</Alert>
)}
</Grid>
</Grid>
</StepContainer>
Expand Down
34 changes: 32 additions & 2 deletions web/src/app/schedules/temp-sched/TempSchedDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { parseInterval } from '../../util/shifts'
import { DateTime } from 'luxon'
import { getNextWeekday } from '../../util/luxon-helpers'
import { useScheduleTZ } from './hooks'
import { getCoverageGapItems } from './shiftsListUtil'
// allows changing the index programatically
const VirtualizeAnimatedViews = virtualize(SwipeableViews)

Expand Down Expand Up @@ -82,6 +83,25 @@ export default function TempSchedDialog({
},
})

const [shouldAllowNoCoverage, setShouldAllowNoCoverage] = useState(false)
const [isShowingCoverageGapsWarning, setIsShowingCoverageGapsWarning] =
useState(false)

const hasCoverageGaps =
getCoverageGapItems(schedInterval, value.shifts, zone).length > 0

const handleSubmit = (): void => {
if (hasCoverageGaps && !shouldAllowNoCoverage) {
setIsShowingCoverageGapsWarning(true)
return
}
if (isShowingCoverageGapsWarning && shouldAllowNoCoverage) {
setIsShowingCoverageGapsWarning(false)
}

submit()
}

type SlideRenderer = {
index: number
key: number
Expand All @@ -108,6 +128,9 @@ export default function TempSchedDialog({
start={value.start}
end={value.end}
edit={edit}
coverageGapsAllowed={shouldAllowNoCoverage}
setCoverageGapsAllowed={setShouldAllowNoCoverage}
isShowingCoverageGapsWarning={isShowingCoverageGapsWarning}
/>
)
}
Expand All @@ -116,13 +139,20 @@ export default function TempSchedDialog({
return <div />
}

const noCoverageErrs =
hasCoverageGaps && isShowingCoverageGapsWarning
? [new Error('This temporary schedule has gaps in coverage.')]
: []
const nonFieldErrs = nonFieldErrors(error).map((e) => ({
message: e.message,
}))
const fieldErrs = fieldErrors(error).map((e) => ({
message: `${e.field}: ${e.message}`,
}))
const errs = nonFieldErrs.concat(fieldErrs).concat(shiftErrors)
const errs = nonFieldErrs
.concat(fieldErrs)
.concat(shiftErrors)
.concat(noCoverageErrs)

return (
<FormDialog
Expand Down Expand Up @@ -171,7 +201,7 @@ export default function TempSchedDialog({
/>
</FormContainer>
}
onSubmit={() => submit()}
onSubmit={handleSubmit}
onNext={step === 1 ? null : () => setStep(step + 1)}
onBack={(edit ? step === 1 : step === 0) ? null : () => setStep(step - 1)}
/>
Expand Down
9 changes: 7 additions & 2 deletions web/src/cypress/integration/temporarySchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ function testTemporarySchedule(screen: string): void {
)
cy.get('[data-cy="loading-button"]').contains('Next').click()
cy.get(addShiftsSelector).should('be.visible.and.contain', 'STEP 2 OF 2')
cy.get('[data-cy="no-coverage-checkbox"]').should('not.exist')
cy.dialogForm({ userID: manualAddUser.name }, addShiftsSelector)
cy.get('[data-cy="shifts-list"]').should('not.contain', manualAddUser.name)
cy.get('button[data-cy="add-shift"]').click()
cy.get('[data-cy="shifts-list"]').should('contain', manualAddUser.name)
cy.dialogFinish('Submit')
cy.get('[data-cy="loading-button"]').contains('Submit').click()
cy.get('[data-cy="no-coverage-checkbox"]').should('be.visible').click()
cy.dialogFinish('Retry')
cy.visit('/schedules/' + schedule.id + '?start=' + start.toISO())
cy.get('div').contains('Temporary Schedule').click()
cy.get('div[data-cy="shift-tooltip"]').should('be.visible')
Expand Down Expand Up @@ -155,7 +158,9 @@ function testTemporarySchedule(screen: string): void {
)
cy.get('button[data-cy="add-shift"]').click()
cy.get('[data-cy="shifts-list"]').should('contain', manualAddUser.name)
cy.dialogFinish('Submit')
cy.get('[data-cy="loading-button"]').contains('Submit').click()
cy.get('[data-cy="no-coverage-checkbox"]').should('be.visible').click()
cy.dialogFinish('Retry')
cy.reload() // ensure calendar update
cy.get('div').contains(manualAddUser.name).click()
cy.get('div[data-cy="shift-tooltip"]').should('be.visible')
Expand Down