2
2
//
3
3
// SPDX-License-Identifier: LGPL-2.1-or-later
4
4
5
- import React , { Fragment , useState } from 'react'
5
+ import React , { Fragment , useMemo , useState } from 'react'
6
6
import styled from 'styled-components'
7
7
8
8
import { useTranslation } from 'employee-frontend/state/i18n'
9
9
import { Result } from 'lib-common/api'
10
+ import { OutOfOfficePeriod } from 'lib-common/generated/api-types/outofoffice'
10
11
import { useQueryResult } from 'lib-common/query'
11
12
import { AsyncButton } from 'lib-components/atoms/buttons/AsyncButton'
12
13
import { Button } from 'lib-components/atoms/buttons/Button'
@@ -21,10 +22,28 @@ import { outOfOfficePeriodsQuery } from './queries'
21
22
export default React . memo ( function OutOfOfficePage ( ) {
22
23
const { i18n } = useTranslation ( )
23
24
const [ isEditing , setIsEditing ] = useState ( false )
25
+ const [ selectedPeriod , setSelectedPeriod ] =
26
+ useState < OutOfOfficePeriod | null > ( null )
24
27
25
28
const getPeriodsResult = useQueryResult ( outOfOfficePeriodsQuery ( ) )
26
29
const periods = getPeriodsResult . getOrElse ( [ ] )
27
30
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
+
28
47
return (
29
48
< Container >
30
49
< ContentArea opaque >
@@ -33,7 +52,7 @@ export default React.memo(function OutOfOfficePage() {
33
52
< Gap size = "m" />
34
53
< Label > { i18n . outOfOffice . header } </ Label >
35
54
< Gap size = "s" />
36
- { periods . length > 0 ? (
55
+ { ! selectedPeriod && periods . length > 0 ? (
37
56
< PeriodListContainer >
38
57
{ periods . map ( ( period ) => (
39
58
< li key = { period . id } >
@@ -43,6 +62,7 @@ export default React.memo(function OutOfOfficePage() {
43
62
text = { i18n . common . edit }
44
63
appearance = "inline"
45
64
icon = { faPen }
65
+ onClick = { ( ) => startEdit ( period ) }
46
66
/>
47
67
< AsyncButton
48
68
text = { i18n . common . remove }
@@ -59,9 +79,14 @@ export default React.memo(function OutOfOfficePage() {
59
79
</ li >
60
80
) ) }
61
81
</ 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 ? (
65
90
< Fragment >
66
91
< div > { i18n . outOfOffice . noFutureOutOfOffice } </ div >
67
92
< Gap size = "m" />
@@ -71,7 +96,7 @@ export default React.memo(function OutOfOfficePage() {
71
96
onClick = { ( ) => setIsEditing ( true ) }
72
97
/>
73
98
</ Fragment >
74
- ) }
99
+ ) : null }
75
100
</ ContentArea >
76
101
</ Container >
77
102
)
0 commit comments