Skip to content

Commit 71e3d1d

Browse files
committed
Clean up file upload code a bit
1 parent fbdbbcf commit 71e3d1d

File tree

2 files changed

+48
-57
lines changed

2 files changed

+48
-57
lines changed

components/ChatFooter.tsx

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,6 @@ import SendIcon from './SendIcon';
66
import PaperclipIcon from './PaperclipIcon';
77
import {Attachment, Message} from '../helpers/types';
88

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-
509
const ChatFooter = ({
5110
placeholder,
5211
emailInputPlaceholder,
@@ -66,41 +25,49 @@ const ChatFooter = ({
6625
}) => {
6726
const [message, setMessage] = React.useState('');
6827
const [email, setEmail] = React.useState('');
28+
const [isUploading, setIsUploading] = React.useState(false);
29+
const [error, setError] = React.useState<any>(null);
6930
const messageInput = React.useRef(null);
7031

7132
const hasValidEmail = email && email.length > 5 && email.indexOf('@') !== -1;
33+
const isDisabled = isUploading || isSending;
7234

73-
const handleMessageChange = (e: any) => {
35+
const handleMessageChange = (e: React.ChangeEvent<HTMLTextAreaElement>) =>
7436
setMessage(e.target.value);
75-
};
7637

77-
const handleEmailChange = (e: any) => {
38+
const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) =>
7839
setEmail(e.target.value);
79-
};
8040

81-
const handleSetEmail = (e?: any) => {
41+
const handleSetEmail = (e?: React.FormEvent<HTMLFormElement>) => {
8242
e && e.preventDefault();
8343

8444
if (messageInput.current) {
8545
messageInput.current.focus();
8646
}
8747
};
8848

89-
const handleSendMessage = (e?: any) => {
49+
const handleSendMessage = (e?: React.FormEvent<HTMLFormElement>) => {
9050
e && e.preventDefault();
9151

9252
onSendMessage({body: message}, email);
9353
setMessage('');
9454
setEmail('');
9555
};
9656

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+
};
10063

64+
const handleUploadSuccess = ({data: file}: {data: Attachment}) => {
65+
if (file && file.id) {
10166
onSendMessage({body: message, file_ids: [file.id]}, email);
10267
setMessage('');
10368
setEmail('');
69+
setIsUploading(false);
70+
setError(null);
10471
}
10572
};
10673

@@ -143,23 +110,46 @@ const ChatFooter = ({
143110
maxRows={4}
144111
autoFocus
145112
value={message}
146-
disabled={shouldRequireEmail && !hasValidEmail}
113+
disabled={isDisabled || (shouldRequireEmail && !hasValidEmail)}
147114
onKeyDown={handleKeyDown}
148115
onChange={handleMessageChange}
149116
/>
150117
</Box>
151118

152119
<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>
158148

159149
<Button
160150
variant="primary"
161151
type="submit"
162-
disabled={isSending}
152+
disabled={isDisabled}
163153
sx={{
164154
display: 'flex',
165155
justifyContent: 'center',

helpers/theme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const getThemeConfig = (settings: ThemeSettings) => {
3939
lighter: overrides.light,
4040
secondary: '#722ed1',
4141
green: '#52c41a',
42+
red: '#ff4d4f',
4243
muted: '#f0f0f0',
4344
gray: 'rgba(0, 0, 0, 0.45)',
4445
// TODO: come up with better names!

0 commit comments

Comments
 (0)