-
Notifications
You must be signed in to change notification settings - Fork 324
/
TaskFooter.tsx
164 lines (156 loc) · 4.97 KB
/
TaskFooter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import styled from '@emotion/styled'
import graphql from 'babel-plugin-relay/macro'
import {EditorState} from 'draft-js'
import React from 'react'
import {createFragmentContainer} from 'react-relay'
import {AreaEnum} from '~/__generated__/UpdateTaskMutation.graphql'
import CardButton from '../../../../components/CardButton'
import IconLabel from '../../../../components/IconLabel'
import useAtmosphere from '../../../../hooks/useAtmosphere'
import useMutationProps from '../../../../hooks/useMutationProps'
import {UseTaskChild} from '../../../../hooks/useTaskChildFocus'
import {Card} from '../../../../types/constEnums'
import {USER_DASH} from '../../../../utils/constants'
import removeContentTag from '../../../../utils/draftjs/removeContentTag'
import isTaskArchived from '../../../../utils/isTaskArchived'
import setLocalTaskError from '../../../../utils/relay/setLocalTaskError'
import {TaskFooter_task} from '../../../../__generated__/TaskFooter_task.graphql'
import OutcomeCardMessage from '../OutcomeCardMessage/OutcomeCardMessage'
import TaskFooterIntegrateToggle from './TaskFooterIntegrateToggle'
import TaskFooterTagMenuToggle from './TaskFooterTagMenuToggle'
import TaskFooterTeamAssignee from './TaskFooterTeamAssignee'
import TaskFooterUserAssignee from './TaskFooterUserAssignee'
const Footer = styled('div')({
display: 'flex',
justifyContent: 'space-between',
maxWidth: '100%',
padding: `12px ${Card.PADDING} ${Card.PADDING}`
})
const ButtonGroup = styled('div')<{cardIsActive: boolean}>(({cardIsActive}) => ({
display: 'flex',
justifyContent: 'flex-end',
opacity: cardIsActive ? 1 : 0
}))
// ButtonSpacer helps truncated names (…) be consistent
const ButtonSpacer = styled('div')({
display: 'inline-block',
height: 24,
verticalAlign: 'middle',
width: 24
})
const AvatarBlock = styled('div')({
flex: 1,
height: 24,
minWidth: 0
})
interface Props {
area: AreaEnum
cardIsActive: boolean
editorState: EditorState
isAgenda: boolean
task: TaskFooter_task
useTaskChild: UseTaskChild
dataCy: string
}
const TaskFooter = (props: Props) => {
const {area, cardIsActive, editorState, isAgenda, task, useTaskChild, dataCy} = props
const {onCompleted, onError, submitMutation, submitting} = useMutationProps()
const handleError = (err: Error) => {
onError(err)
if (err.message) {
setLocalTaskError(atmosphere, taskId, err.message)
}
}
const handleCompleted = (res, errors) => {
onCompleted(res, errors)
const payload = res?.[Object.keys(res)[0]]
const error = payload?.error?.message ?? errors?.[0]?.message ?? null
setLocalTaskError(atmosphere, taskId, error)
}
const mutationProps = {
onCompleted: handleCompleted,
submitMutation,
submitting,
onError: handleError
}
const atmosphere = useAtmosphere()
const showTeam = area === USER_DASH
const {content, id: taskId, error, integration, tags, userId} = task
const isArchived = isTaskArchived(tags)
const canAssignUser = !integration && !isArchived
const canAssignTeam = !isArchived
return (
<React.Fragment>
<Footer>
<AvatarBlock>
{showTeam ? (
<TaskFooterTeamAssignee
canAssign={canAssignTeam}
task={task}
useTaskChild={useTaskChild}
/>
) : (
<TaskFooterUserAssignee
area={area}
canAssign={canAssignUser}
cardIsActive={cardIsActive}
task={task}
useTaskChild={useTaskChild}
/>
)}
</AvatarBlock>
<ButtonGroup cardIsActive={cardIsActive}>
{integration || isArchived || !userId ? (
<ButtonSpacer />
) : (
<TaskFooterIntegrateToggle
dataCy={`${dataCy}-integration`}
mutationProps={mutationProps}
task={task}
useTaskChild={useTaskChild}
/>
)}
{isArchived ? (
<CardButton
onClick={() => removeContentTag('archived', atmosphere, taskId, content, area)}
>
<IconLabel icon='reply' />
</CardButton>
) : (
<TaskFooterTagMenuToggle
dataCy={`${dataCy}-tag`}
area={area}
editorState={editorState}
isAgenda={isAgenda}
task={task}
useTaskChild={useTaskChild}
mutationProps={mutationProps}
/>
)}
</ButtonGroup>
</Footer>
{error && <OutcomeCardMessage onClose={handleCompleted} message={error} />}
</React.Fragment>
)
}
export default createFragmentContainer(TaskFooter, {
task: graphql`
fragment TaskFooter_task on Task {
id
content
error
integration {
__typename
}
tags
team {
id
}
userId
...TaskFooterTeamAssignee_task
...TaskFooterUserAssignee_task
...TaskFooterTagMenu_task
...TaskFooterIntegrateMenuRoot_task
}
`
})