forked from box/box-ui-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTaskButton.js
More file actions
106 lines (91 loc) · 3.36 KB
/
AddTaskButton.js
File metadata and controls
106 lines (91 loc) · 3.36 KB
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
// @flow
import * as React from 'react';
import { withRouter, type RouterHistory } from 'react-router-dom';
import AddTaskMenu from './AddTaskMenu';
import TaskModal from './TaskModal';
import { TASK_TYPE_APPROVAL } from '../../constants';
import type { TaskFormProps } from './activity-feed/task-form/TaskForm';
import type { TaskType } from '../../common/types/tasks';
import type { ElementsXhrError } from '../../common/types/api';
import type { InternalSidebarNavigation, InternalSidebarNavigationHandler } from '../common/types/SidebarNavigation';
type Props = {|
history: RouterHistory,
internalSidebarNavigation?: InternalSidebarNavigation,
internalSidebarNavigationHandler?: InternalSidebarNavigationHandler,
isDisabled: boolean,
onTaskModalClose: () => void,
routerDisabled?: boolean,
taskFormProps: TaskFormProps,
|};
type State = {
error: ?ElementsXhrError,
isTaskFormOpen: boolean,
taskType: TaskType,
};
class AddTaskButton extends React.Component<Props, State> {
buttonRef = React.createRef<HTMLButtonElement>();
state = {
error: null,
isTaskFormOpen: false,
taskType: TASK_TYPE_APPROVAL,
};
static defaultProps = {
isDisabled: false,
};
/*
1. Pushing the open state into history keeps the sidebar open upon resize and refresh
2. Preventing the sidebar from closing keeps the task modal open upon edit and resize
*/
handleClickMenuItem = (taskType: TaskType) => {
const { history, internalSidebarNavigation, internalSidebarNavigationHandler, routerDisabled } = this.props;
if (routerDisabled && internalSidebarNavigationHandler) {
internalSidebarNavigationHandler(
{
...internalSidebarNavigation,
open: true,
},
true,
);
} else {
history.replace({ state: { open: true } });
}
this.setState({ isTaskFormOpen: true, taskType });
};
handleModalClose = () => {
const { onTaskModalClose } = this.props;
this.setState({ isTaskFormOpen: false, error: null }, () => {
if (this.buttonRef.current) {
this.buttonRef.current.focus();
}
});
onTaskModalClose();
};
handleSubmitError = (e: ElementsXhrError) => this.setState({ error: e });
setAddTaskButtonRef = (element: HTMLButtonElement) => {
this.buttonRef.current = element;
};
render() {
const { isDisabled, taskFormProps } = this.props;
const { isTaskFormOpen, taskType, error } = this.state;
return (
<>
<AddTaskMenu
isDisabled={isDisabled}
onMenuItemClick={this.handleClickMenuItem}
setAddTaskButtonRef={this.setAddTaskButtonRef}
/>
<TaskModal
error={error}
onSubmitError={this.handleSubmitError}
onSubmitSuccess={this.handleModalClose}
onModalClose={this.handleModalClose}
isTaskFormOpen={isTaskFormOpen}
taskFormProps={taskFormProps}
taskType={taskType}
/>
</>
);
}
}
export { AddTaskButton as AddTaskButtonComponent };
export default withRouter(AddTaskButton);