File size: 5,641 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React from 'react';
import Button from '@mui/material/Button';
import CircularProgress from '@mui/material/CircularProgress';
import CloudUploadIcon from '@mui/icons-material/CloudUpload';
import ErrorIcon from '@mui/icons-material/Error';
import type { ImageLoaded, ImageUploadProps, ImageUploadState } from './types';
import { defaultTranslations } from './defaultTranslations';
import type { TranslatorFunction } from '../../core/components/hooks';
import { useUiTranslator } from '../../core/components/hooks';

const NO_FILE_ERROR_CODE = 1;
const BAD_EXTENSION_ERROR_CODE = 2;
const TOO_BIG_ERROR_CODE = 3;
const UPLOADING_ERROR_CODE = 4;

/*
 * FIXME: rewrite to functional component
 */
class ImageUpload extends React.Component<
  ImageUploadProps & {
    t: TranslatorFunction;
  },
  ImageUploadState
> {
  static defaultProps = {
    icon: <CloudUploadIcon style={{ marginLeft: '8px' }} />,
    allowedExtensions: ['jpg', 'jpeg', 'png'],
    maxFileSize: 5242880,
    translations: defaultTranslations,
  };
  fileInput?: HTMLInputElement | null;

  state: ImageUploadState = {
    isUploading: false,
    hasError: false,
    errorText: '',
    progress: 0,
  };

  hasExtension = (fileName: string) => {
    const patternPart = this.props.allowedExtensions
      ? this.props.allowedExtensions.map((a) => a.toLowerCase()).join('|')
      : '';
    const pattern = '(' + patternPart.replace(/\./g, '\\.') + ')$';
    return new RegExp(pattern, 'i').test(fileName.toLowerCase());
  };

  handleError = (errorCode: number) => {
    let errorText: string | null;

    switch (errorCode) {
      case NO_FILE_ERROR_CODE:
        errorText = this.props.t(this.props.translations?.noFileError);
        break;
      case BAD_EXTENSION_ERROR_CODE:
        errorText = this.props.t(this.props.translations?.badExtensionError);
        break;
      case TOO_BIG_ERROR_CODE:
        errorText = this.props.t(this.props.translations?.tooBigError);
        break;
      case UPLOADING_ERROR_CODE:
        errorText = this.props.t(this.props.translations?.uploadingError);
        break;
      default:
        errorText = this.props.t(this.props.translations?.unknownError);
        break;
    }
    // Need to flick "isUploading" because otherwise the handler doesn't fire properly
    this.setState({ hasError: true, errorText, isUploading: true }, () =>
      this.setState({ isUploading: false })
    );
    setTimeout(() => this.setState({ hasError: false, errorText: '' }), 5000);
  };

  handleFileSelected: React.ChangeEventHandler<HTMLInputElement> = (e) => {
    if (!e.target.files || !e.target.files[0]) {
      this.handleError(NO_FILE_ERROR_CODE);
      return;
    }
    const file = e.target.files[0];
    if (!this.hasExtension(file.name)) {
      this.handleError(BAD_EXTENSION_ERROR_CODE);
      return;
    }
    if (this.props.maxFileSize && file.size > this.props.maxFileSize) {
      this.handleError(TOO_BIG_ERROR_CODE);
      return;
    }
    if (this.props.imageLoaded) {
      this.readFile(file).then((data) => this.props.imageLoaded?.(data));
    }
    if (this.props.imageUpload) {
      this.setState({ isUploading: true });
      this.props
        .imageUpload(file, this.handleReportProgress)
        .then((resp) => {
          this.setState({ progress: undefined, isUploading: false });
          this.props.imageUploaded && this.props.imageUploaded(resp);
        })
        .catch((error) => {
          this.setState({ isUploading: false });
          this.props.imageUploadError && this.props.imageUploadError(error);
        });
    }
  };

  readFile(file: File): Promise<ImageLoaded> {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();

      // Read the image via FileReader API and save image result in state.
      reader.onload = function (e: ProgressEvent) {
        // Add the file name to the data URL
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let dataUrl: string = (e.target as any).result;
        dataUrl = dataUrl.replace(';base64', `;name=${file.name};base64`);
        resolve({ file, dataUrl });
      };

      reader.readAsDataURL(file);
    });
  }

  handleFileUploadClick: React.MouseEventHandler<HTMLElement> = () =>
    this.fileInput?.click();

  handleReportProgress = (progress: number) => this.setState({ progress });

  renderChildren = () => {
    if (this.state.isUploading) {
      return <CircularProgress value={this.state.progress} size={19} />;
    }
    if (this.state.hasError) {
      return (
        <React.Fragment>
          {this.state.errorText}
          <ErrorIcon style={{ marginLeft: '8px' }} />
        </React.Fragment>
      );
    }
    return (
      <React.Fragment>
        {this.props.translations?.buttonContent}
        {this.props.icon}
      </React.Fragment>
    );
  };

  render() {
    return (
      <React.Fragment>
        <Button
          disabled={this.state.isUploading}
          variant="contained"
          color={this.state.hasError ? 'secondary' : 'primary'}
          onClick={this.handleFileUploadClick}
          style={this.props.style}
          size="small"
        >
          {this.renderChildren()}
        </Button>
        {!this.state.isUploading && (
          <input
            style={{ display: 'none' }}
            ref={(fileInput) => (this.fileInput = fileInput)}
            type="file"
            onChange={this.handleFileSelected}
          />
        )}
      </React.Fragment>
    );
  }
}

export default (props: ImageUploadProps) => {
  const { t } = useUiTranslator();
  return <ImageUpload {...props} t={t} />;
};