Skip to content

Commit 89047bb

Browse files
committed
Enable editing of out of office ranges
1 parent 59d1e0b commit 89047bb

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

frontend/src/employee-frontend/components/out-of-office/OutOfOfficeEditor.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { localDateRange } from 'lib-common/form/fields'
99
import { object, required } from 'lib-common/form/form'
1010
import { useForm, useFormFields } from 'lib-common/form/hooks'
1111
import { StateOf } from 'lib-common/form/types'
12+
import { OutOfOfficePeriod } from 'lib-common/generated/api-types/outofoffice'
1213
import LocalDate from 'lib-common/local-date'
1314
import { useMutationResult } from 'lib-common/query'
1415
import { AsyncButton } from 'lib-components/atoms/buttons/AsyncButton'
@@ -21,22 +22,29 @@ import { upsertOutOfOfficePeriodMutation } from './queries'
2122

2223
const outOfOfficeForm = object({ period: required(localDateRange()) })
2324

24-
function initialFormState(): StateOf<typeof outOfOfficeForm> {
25+
function initialFormState(
26+
editedPeriod: OutOfOfficePeriod | null
27+
): StateOf<typeof outOfOfficeForm> {
28+
const rangeConfig = { minDate: LocalDate.todayInSystemTz() }
2529
return {
26-
period: localDateRange.empty({ minDate: LocalDate.todayInSystemTz() })
30+
period: editedPeriod
31+
? localDateRange.fromRange(editedPeriod.period, rangeConfig)
32+
: localDateRange.empty(rangeConfig)
2733
}
2834
}
2935

3036
export interface OutOfOfficeEditorProps {
3137
onClose: () => void
38+
editedPeriod: OutOfOfficePeriod | null
3239
}
3340

3441
export default React.memo(function OutOfOfficeEditor({
35-
onClose
42+
onClose,
43+
editedPeriod
3644
}: OutOfOfficeEditorProps) {
3745
const { i18n, lang } = useTranslation()
3846

39-
const form = useForm(outOfOfficeForm, initialFormState, {
47+
const form = useForm(outOfOfficeForm, () => initialFormState(editedPeriod), {
4048
...i18n.validationErrors
4149
})
4250

@@ -49,7 +57,7 @@ export default React.memo(function OutOfOfficeEditor({
4957
const onSubmit = () =>
5058
upsertOutOfOffice({
5159
body: {
52-
id: null,
60+
id: editedPeriod?.id ?? null,
5361
period: period.value()
5462
}
5563
})

frontend/src/employee-frontend/components/out-of-office/OutOfOfficePage.tsx

+31-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
//
33
// SPDX-License-Identifier: LGPL-2.1-or-later
44

5-
import React, { Fragment, useState } from 'react'
5+
import React, { Fragment, useMemo, useState } from 'react'
66
import styled from 'styled-components'
77

88
import { useTranslation } from 'employee-frontend/state/i18n'
99
import { Result } from 'lib-common/api'
10+
import { OutOfOfficePeriod } from 'lib-common/generated/api-types/outofoffice'
1011
import { useQueryResult } from 'lib-common/query'
1112
import { AsyncButton } from 'lib-components/atoms/buttons/AsyncButton'
1213
import { Button } from 'lib-components/atoms/buttons/Button'
@@ -21,10 +22,28 @@ import { outOfOfficePeriodsQuery } from './queries'
2122
export default React.memo(function OutOfOfficePage() {
2223
const { i18n } = useTranslation()
2324
const [isEditing, setIsEditing] = useState(false)
25+
const [selectedPeriod, setSelectedPeriod] =
26+
useState<OutOfOfficePeriod | null>(null)
2427

2528
const getPeriodsResult = useQueryResult(outOfOfficePeriodsQuery())
2629
const periods = getPeriodsResult.getOrElse([])
2730

31+
const startEdit = useMemo(
32+
() => (period: OutOfOfficePeriod) => {
33+
setSelectedPeriod(period)
34+
setIsEditing(true)
35+
},
36+
[]
37+
)
38+
39+
const onCloseEditor = useMemo(
40+
() => () => {
41+
setSelectedPeriod(null)
42+
setIsEditing(false)
43+
},
44+
[]
45+
)
46+
2847
return (
2948
<Container>
3049
<ContentArea opaque>
@@ -33,7 +52,7 @@ export default React.memo(function OutOfOfficePage() {
3352
<Gap size="m" />
3453
<Label>{i18n.outOfOffice.header}</Label>
3554
<Gap size="s" />
36-
{periods.length > 0 ? (
55+
{!selectedPeriod && periods.length > 0 ? (
3756
<PeriodListContainer>
3857
{periods.map((period) => (
3958
<li key={period.id}>
@@ -43,6 +62,7 @@ export default React.memo(function OutOfOfficePage() {
4362
text={i18n.common.edit}
4463
appearance="inline"
4564
icon={faPen}
65+
onClick={() => startEdit(period)}
4666
/>
4767
<AsyncButton
4868
text={i18n.common.remove}
@@ -59,9 +79,14 @@ export default React.memo(function OutOfOfficePage() {
5979
</li>
6080
))}
6181
</PeriodListContainer>
62-
) : isEditing ? (
63-
<OutOfOfficeEditor onClose={() => setIsEditing(false)} />
64-
) : (
82+
) : null}
83+
{isEditing ? (
84+
<OutOfOfficeEditor
85+
onClose={onCloseEditor}
86+
editedPeriod={selectedPeriod}
87+
/>
88+
) : null}
89+
{periods.length === 0 && !isEditing ? (
6590
<Fragment>
6691
<div>{i18n.outOfOffice.noFutureOutOfOffice}</div>
6792
<Gap size="m" />
@@ -71,7 +96,7 @@ export default React.memo(function OutOfOfficePage() {
7196
onClick={() => setIsEditing(true)}
7297
/>
7398
</Fragment>
74-
)}
99+
) : null}
75100
</ContentArea>
76101
</Container>
77102
)

0 commit comments

Comments
 (0)