Skip to content

Commit e7b080e

Browse files
committed
the buttons now do things
1 parent 8e51888 commit e7b080e

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

site/src/api/api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import axios, { AxiosRequestHeaders } from "axios"
22
import * as Types from "./types"
33
import { WorkspaceBuildTransition } from "./types"
44
import * as TypesGen from "./typesGenerated"
5+
import dayjs from "dayjs"
56

67
const CONTENT_TYPE_JSON: AxiosRequestHeaders = {
78
"Content-Type": "application/json",
@@ -339,7 +340,7 @@ export const getWorkspaceBuildLogs = async (
339340

340341
export const putWorkspaceExtension = async (
341342
workspaceId: string,
342-
extendWorkspaceRequest: TypesGen.PutExtendWorkspaceRequest,
343+
newDeadline: dayjs.Dayjs,
343344
): Promise<void> => {
344-
await axios.put(`/api/v2/workspaces/${workspaceId}/extend`, extendWorkspaceRequest)
345+
await axios.put(`/api/v2/workspaces/${workspaceId}/extend`, { deadline: newDeadline })
345346
}

site/src/components/WorkspaceSchedule/WorkspaceSchedule.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ export const shouldDisplayPlusMins = (workspace: Workspace): boolean => {
9191
return deadline.year() > 1
9292
}
9393

94-
export const WorkspaceSchedule: FC<WorkspaceScheduleProps> = ({ workspace }) => {
94+
export const WorkspaceSchedule: FC<WorkspaceScheduleProps> = ({
95+
workspace,
96+
onDeadlineMinus,
97+
onDeadlinePlus,
98+
}) => {
9599
const styles = useStyles()
96100

97101
return (
@@ -115,10 +119,10 @@ export const WorkspaceSchedule: FC<WorkspaceScheduleProps> = ({ workspace }) =>
115119
</div>
116120
<div>
117121
<Stack direction="row">
118-
<Button className={styles.editDeadline}>
122+
<Button className={styles.editDeadline} onClick={onDeadlineMinus}>
119123
<span className={styles.scheduleLabel}>{Language.editDeadlineMinus}</span>
120124
</Button>
121-
<Button className={styles.editDeadline}>
125+
<Button className={styles.editDeadline} onClick={onDeadlinePlus}>
122126
<span className={styles.scheduleLabel}>{Language.editDeadlinePlus}</span>
123127
</Button>
124128
</Stack>

site/src/pages/WorkspacePage/WorkspacePage.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { useMachine, useSelector } from "@xstate/react"
2+
import dayjs from "dayjs"
3+
import minMax from "dayjs/plugin/minMax"
24
import React, { useContext, useEffect } from "react"
35
import { Helmet } from "react-helmet"
46
import { useParams } from "react-router-dom"
@@ -13,6 +15,8 @@ import { XServiceContext } from "../../xServices/StateContext"
1315
import { workspaceMachine } from "../../xServices/workspace/workspaceXService"
1416
import { workspaceScheduleBannerMachine } from "../../xServices/workspaceSchedule/workspaceScheduleBannerXService"
1517

18+
dayjs.extend(minMax)
19+
1620
export const WorkspacePage: React.FC = () => {
1721
const { username: usernameQueryParam, workspace: workspaceQueryParam } = useParams()
1822
const username = firstOrItem(usernameQueryParam, null)
@@ -56,15 +60,33 @@ export const WorkspacePage: React.FC = () => {
5660
bannerProps={{
5761
isLoading: bannerState.hasTag("loading"),
5862
onExtend: () => {
59-
bannerSend({ type: "EXTEND_DEADLINE_DEFAULT", workspaceId: workspace.id })
63+
bannerSend({
64+
type: "UPDATE_DEADLINE",
65+
workspaceId: workspace.id,
66+
newDeadline: dayjs().utc().add(4, "hours"),
67+
})
6068
},
6169
}}
6270
scheduleProps={{
6371
onDeadlineMinus: () => {
64-
console.log("not implemented")
72+
bannerSend({
73+
type: "UPDATE_DEADLINE",
74+
workspaceId: workspace.id,
75+
newDeadline: boundedDeadline(
76+
dayjs(workspace.latest_build.deadline).utc().add(-1, "hours"),
77+
dayjs(),
78+
),
79+
})
6580
},
6681
onDeadlinePlus: () => {
67-
console.log("not implemented")
82+
bannerSend({
83+
type: "UPDATE_DEADLINE",
84+
workspaceId: workspace.id,
85+
newDeadline: boundedDeadline(
86+
dayjs(workspace.latest_build.deadline).utc().add(1, "hours"),
87+
dayjs(),
88+
),
89+
})
6890
},
6991
}}
7092
workspace={workspace}
@@ -89,3 +111,9 @@ export const WorkspacePage: React.FC = () => {
89111
)
90112
}
91113
}
114+
115+
export const boundedDeadline = (newDeadline: dayjs.Dayjs, now: dayjs.Dayjs): dayjs.Dayjs => {
116+
const minDeadline = now.add(30, "minutes")
117+
const maxDeadline = now.add(24, "hours")
118+
return dayjs.min(dayjs.max(minDeadline, newDeadline), maxDeadline)
119+
}

site/src/xServices/workspaceSchedule/workspaceScheduleBannerXService.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* @fileoverview workspaceScheduleBanner is an xstate machine backing a form,
3-
* presented as an Alert/banner, for reactively extending a workspace schedule.
3+
* presented as an Alert/banner, for reactively updating a workspace schedule.
44
*/
55
import { createMachine } from "xstate"
66
import * as API from "../../api/api"
7+
import dayjs from "dayjs"
78
import { displayError, displaySuccess } from "../../components/GlobalSnackbar/utils"
8-
import { defaultWorkspaceExtension } from "../../util/workspace"
99

1010
export const Language = {
1111
errorExtension: "Failed to extend workspace deadline.",
1212
successExtension: "Successfully extended workspace deadline.",
1313
}
1414

15-
export type WorkspaceScheduleBannerEvent = { type: "EXTEND_DEADLINE_DEFAULT"; workspaceId: string }
15+
export type WorkspaceScheduleBannerEvent = { type: "UPDATE_DEADLINE"; workspaceId: string, newDeadline: dayjs.Dayjs }
1616

1717
export const workspaceScheduleBannerMachine = createMachine(
1818
{
@@ -25,13 +25,13 @@ export const workspaceScheduleBannerMachine = createMachine(
2525
states: {
2626
idle: {
2727
on: {
28-
EXTEND_DEADLINE_DEFAULT: "extendingDeadline",
28+
UPDATE_DEADLINE: "updatingDeadline",
2929
},
3030
},
31-
extendingDeadline: {
31+
updatingDeadline: {
3232
invoke: {
33-
src: "extendDeadlineDefault",
34-
id: "extendDeadlineDefault",
33+
src: "updateDeadline",
34+
id: "updateDeadline",
3535
onDone: {
3636
target: "idle",
3737
actions: "displaySuccessMessage",
@@ -56,8 +56,8 @@ export const workspaceScheduleBannerMachine = createMachine(
5656
},
5757

5858
services: {
59-
extendDeadlineDefault: async (_, event) => {
60-
await API.putWorkspaceExtension(event.workspaceId, defaultWorkspaceExtension())
59+
updateDeadline: async (_, event) => {
60+
await API.putWorkspaceExtension(event.workspaceId, event.newDeadline )
6161
},
6262
},
6363
},

0 commit comments

Comments
 (0)