8000 fix: redesign schedule bumper to handle multiple hours of change at once by presleyp · Pull Request #4535 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: redesign schedule bumper to handle multiple hours of change at once #4535

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

Merged
merged 15 commits into from
Oct 14, 2022
Merged
Prev Previous commit
Next Next commit
Add stepper max
  • Loading branch information
presleyp committed Oct 12, 2022
commit 31567ce1a578f9f9cd9bce2d53d52dbe086e3ae8
4 changes: 4 additions & 0 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface WorkspaceProps {
onDeadlineMinus: (hours: number) => void
deadlinePlusEnabled: () => boolean
deadlineMinusEnabled: () => boolean
maxDeadlineIncrease: number
maxDeadlineDecrease: number
}
handleStart: () => void
handleStop: () => void
Expand Down Expand Up @@ -121,6 +123,8 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
onDeadlinePlus={scheduleProps.onDeadlinePlus}
deadlineMinusEnabled={scheduleProps.deadlineMinusEnabled}
deadlinePlusEnabled={scheduleProps.deadlinePlusEnabled}
maxDeadlineDecrease={scheduleProps.maxDeadlineDecrease}
maxDeadlineIncrease={scheduleProps.maxDeadlineIncrease}
canUpdateWorkspace={canUpdateWorkspace}
/>
<WorkspaceActions
Expand Down
5 changes: 3 additions & 2 deletions site/src/components/WorkspaceScheduleButton/EditHours.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { useTranslation } from "react-i18next"

interface EditHoursProps {
handleSubmit: (hours: number) => void
max: number
}

export const EditHours = ({ handleSubmit }: EditHoursProps): JSX.Element => {
export const EditHours = ({ handleSubmit, max }: EditHoursProps): JSX.Element => {
const { t } = useTranslation("workspacePage")
const [hours, setHours] = useState(1)
const styles = useStyles()
Expand All @@ -19,7 +20,7 @@ export const EditHours = ({ handleSubmit }: EditHoursProps): JSX.Element => {
<Stack direction="row" alignItems="baseline" spacing={1}>
<TextField
className={styles.inputField}
inputProps={{ min: 0, step: 1 }}
inputProps={{ min: 0, max, step: 1 }}
label={t("workspaceScheduleButton.hours")}
value={hours}
onChange={(e) => setHours(parseInt(e.target.value))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dayjs from "dayjs"
import utc from "dayjs/plugin/utc"
import * as TypesGen from "../../api/typesGenerated"
import * as Mocks from "../../testHelpers/entities"
import { shouldDisplayPlusMinus } from "./WorkspaceScheduleButton"
import { canEditDeadline } from "./WorkspaceScheduleButton"

dayjs.extend(utc)

Expand All @@ -13,15 +13,15 @@ describe("WorkspaceScheduleButton", () => {
const workspace: TypesGen.Workspace = Mocks.MockStoppedWorkspace

// Then: shouldDisplayPlusMinus should be false
expect(shouldDisplayPlusMinus(workspace)).toBeFalsy()
expect(canEditDeadline(workspace)).toBeFalsy()
})

it("should display if the workspace is running", () => {
// Given: a stopped workspace
const workspace: TypesGen.Workspace = Mocks.MockWorkspace

// Then: shouldDisplayPlusMinus should be false
expect(shouldDisplayPlusMinus(workspace)).toBeTruthy()
expect(canEditDeadline(workspace)).toBeTruthy()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export interface WorkspaceScheduleButtonProps {
onDeadlineMinus: (hours: number) => void
deadlineMinusEnabled: () => boolean
deadlinePlusEnabled: () => boolean
maxDeadlineIncrease: number
maxDeadlineDecrease: number
canUpdateWorkspace: boolean
}

Expand All @@ -63,6 +65,8 @@ export const WorkspaceScheduleButton: React.FC<
onDeadlineMinus,
deadlinePlusEnabled,
deadlineMinusEnabled,
maxDeadlineDecrease,
maxDeadlineIncrease,
canUpdateWorkspace,
}) => {
const { t } = useTranslation("workspacePage")
Expand Down Expand Up @@ -121,7 +125,7 @@ export const WorkspaceScheduleButton: React.FC<
</IconButton>
</span>
<Maybe condition={editMode !== "off"}>
<EditHours handleSubmit={handleSubmitHours} />
<EditHours handleSubmit={handleSubmitHours} max={editMode === "add" ? maxDeadlineIncrease : maxDeadlineDecrease} />
</Maybe>
</Maybe>
</Stack>
Expand Down
4 changes: 4 additions & 0 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dayjs from "dayjs"
import { useContext } from "react"
import { Helmet } from "react-helmet-async"
import { useTranslation } from "react-i18next"
import { getMaxDeadline, getMaxDeadlineChange, getMinDeadline } from "util/schedule"
//import { getMaxDeadlineDecrease, getMaxDeadlineIncrease } from "util/schedule"
import { selectFeatureVisibility } from "xServices/entitlements/entitlementsSelectors"
import { StateFrom } from "xstate"
import { DeleteDialog } from "../../components/Dialogs/DeleteDialog/DeleteDialog"
Expand Down Expand Up @@ -95,6 +97,8 @@ export const WorkspaceReadyPage = ({
},
deadlineMinusEnabled: () => !bannerState.matches("atMinDeadline"),
deadlinePlusEnabled: () => !bannerState.matches("atMaxDeadline"),
maxDeadlineDecrease: getMaxDeadlineChange(bannerState.context.workspace.deadline, getMinDeadline()),
maxDeadlineIncrease: getMaxDeadlineChange(bannerState.context.workspace.deadline, getMaxDeadline(workspace, bannerState.context.template))
}}
isUpdating={workspaceState.hasTag("updating")}
workspace={workspace}
Expand Down
17 changes: 17 additions & 0 deletions site/src/util/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
deadlineExtensionMin,
extractTimezone,
getMaxDeadline,
getMaxDeadlineChange,
getMinDeadline,
stripTimezone,
} from "./schedule"
Expand Down Expand Up @@ -124,3 +125,19 @@ describe("canReduceDeadline", () => {
expect(canReduceDeadline(dayjs().add(100, "years"))).toBeTruthy()
})
})

describe("getMaxDeadlineChange", () => {
it("should return the number of hours you can add before hitting the max deadline", () => {
const deadline = dayjs()
const maxDeadline = dayjs().add(1, "hour").add(40, "minutes")
// you can only add one hour even though the max is 1:40 away
expect(getMaxDeadlineChange(deadline, maxDeadline)).toEqual(1)
})

it("should return the number of hours you can subtract before hitting the min deadline", () => {
const deadline = dayjs().add(2, "hours").add(40, "minutes")
const minDeadline = dayjs()
// you can only subtract 2 hours even though the min is 2:40 less
expect(getMaxDeadlineChange(deadline, minDeadline)).toEqual(2)
})
})
11 changes: 11 additions & 0 deletions site/src/util/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,14 @@ export function canReduceDeadline(deadline: dayjs.Dayjs): boolean {

export const getDeadline = (workspace: Workspace): dayjs.Dayjs =>
dayjs(workspace.latest_build.deadline).utc()

/**
* Get number of hours you can add or subtract to the current deadline before hitting the max or min deadline.
* @param deadline
* @param workspace
* @param template
* @returns number, in hours
*/
export const getMaxDeadlineChange = (deadline: dayjs.Dayjs, extremeDeadline: dayjs.Dayjs): number => {
return Math.abs(deadline.diff(extremeDeadline, "hours"))
}
0