@@ -6,47 +6,6 @@ import SendIcon from './SendIcon';
6
6
import PaperclipIcon from './PaperclipIcon' ;
7
7
import { Attachment , Message } from '../helpers/types' ;
8
8
9
- const UploadFileButton = ( { isDisabled, accountId, baseUrl, onSuccess} : any ) => {
10
- const props = {
11
- action : `${ baseUrl } /api/upload` ,
12
- data : { account_id : accountId } ,
13
- multiple : true ,
14
- headers : {
15
- 'X-Requested-With' : null ,
16
- } ,
17
- onStart : ( file : File ) => {
18
- console . log ( 'onStart' , file . name ) ;
19
- } ,
20
- onSuccess ( { data} : { data : Attachment } ) {
21
- console . log ( 'onSuccess' , data ) ;
22
- onSuccess ( data ) ;
23
- } ,
24
- onError ( err : any ) {
25
- console . log ( 'onError' , err ) ;
26
- } ,
27
- } ;
28
-
29
- return (
30
- < Upload { ...props } >
31
- < Button
32
- variant = "link"
33
- disabled = { isDisabled }
34
- sx = { {
35
- display : 'flex' ,
36
- justifyContent : 'center' ,
37
- alignItems : 'center' ,
38
- borderRadius : '50%' ,
39
- height : '36px' ,
40
- width : '36px' ,
41
- padding : 0 ,
42
- } }
43
- >
44
- < PaperclipIcon width = { 16 } height = { 16 } />
45
- </ Button >
46
- </ Upload >
47
- ) ;
48
- } ;
49
-
50
9
const ChatFooter = ( {
51
10
placeholder,
52
11
emailInputPlaceholder,
@@ -66,41 +25,49 @@ const ChatFooter = ({
66
25
} ) => {
67
26
const [ message , setMessage ] = React . useState ( '' ) ;
68
27
const [ email , setEmail ] = React . useState ( '' ) ;
28
+ const [ isUploading , setIsUploading ] = React . useState ( false ) ;
29
+ const [ error , setError ] = React . useState < any > ( null ) ;
69
30
const messageInput = React . useRef ( null ) ;
70
31
71
32
const hasValidEmail = email && email . length > 5 && email . indexOf ( '@' ) !== - 1 ;
33
+ const isDisabled = isUploading || isSending ;
72
34
73
- const handleMessageChange = ( e : any ) => {
35
+ const handleMessageChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) =>
74
36
setMessage ( e . target . value ) ;
75
- } ;
76
37
77
- const handleEmailChange = ( e : any ) => {
38
+ const handleEmailChange = ( e : React . ChangeEvent < HTMLInputElement > ) =>
78
39
setEmail ( e . target . value ) ;
79
- } ;
80
40
81
- const handleSetEmail = ( e ?: any ) => {
41
+ const handleSetEmail = ( e ?: React . FormEvent < HTMLFormElement > ) => {
82
42
e && e . preventDefault ( ) ;
83
43
84
44
if ( messageInput . current ) {
85
45
messageInput . current . focus ( ) ;
86
46
}
87
47
} ;
88
48
89
- const handleSendMessage = ( e ?: any ) => {
49
+ const handleSendMessage = ( e ?: React . FormEvent < HTMLFormElement > ) => {
90
50
e && e . preventDefault ( ) ;
91
51
92
52
onSendMessage ( { body : message } , email ) ;
93
53
setMessage ( '' ) ;
94
54
setEmail ( '' ) ;
95
55
} ;
96
56
97
- const handleSendFile = ( file : Attachment ) => {
98
- if ( file && file . id ) {
99
- console . log ( 'Sending file!' , file ) ;
57
+ const handleUploadStarted = ( ) => setIsUploading ( true ) ;
58
+
59
+ const handleUploadError = ( err : any ) => {
60
+ setError ( err ) ;
61
+ setIsUploading ( false ) ;
62
+ } ;
100
63
64
+ const handleUploadSuccess = ( { data : file } : { data : Attachment } ) => {
65
+ if ( file && file . id ) {
101
66
onSendMessage ( { body : message , file_ids : [ file . id ] } , email ) ;
102
67
setMessage ( '' ) ;
103
68
setEmail ( '' ) ;
69
+ setIsUploading ( false ) ;
70
+ setError ( null ) ;
104
71
}
105
72
} ;
106
73
@@ -143,23 +110,46 @@ const ChatFooter = ({
143
110
maxRows = { 4 }
144
111
autoFocus
145
112
value = { message }
146
- disabled = { shouldRequireEmail && ! hasValidEmail }
113
+ disabled = { isDisabled || ( shouldRequireEmail && ! hasValidEmail ) }
147
114
onKeyDown = { handleKeyDown }
148
115
onChange = { handleMessageChange }
149
116
/>
150
117
</ Box >
151
118
152
119
< Flex pl = { 3 } >
153
- < UploadFileButton
154
- accountId = { accountId }
155
- baseUrl = { baseUrl }
156
- onSuccess = { handleSendFile }
157
- />
120
+ < Upload
121
+ action = { `${ baseUrl } /api/upload` }
122
+ data = { { account_id : accountId } }
123
+ headers = { { 'X-Requested-With' : null } }
124
+ onStart = { handleUploadStarted }
125
+ onSuccess = { handleUploadSuccess }
126
+ onError = { handleUploadError }
127
+ >
128
+ < Button
129
+ variant = "link"
130
+ disabled = { isDisabled }
131
+ sx = { {
132
+ display : 'flex' ,
133
+ justifyContent : 'center' ,
134
+ alignItems : 'center' ,
135
+ borderRadius : '50%' ,
136
+ height : '36px' ,
137
+ width : '36px' ,
138
+ padding : 0 ,
139
+ } }
140
+ >
141
+ < PaperclipIcon
142
+ width = { 16 }
143
+ height = { 16 }
144
+ fill = { error ? 'red' : 'gray' }
145
+ />
146
+ </ Button >
147
+ </ Upload >
158
148
159
149
< Button
160
150
variant = "primary"
161
151
type = "submit"
162
- disabled = { isSending }
152
+ disabled = { isDisabled }
163
153
sx = { {
164
154
display : 'flex' ,
165
155
justifyContent : 'center' ,
0 commit comments