|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import {Component, ReactElement} from "react"; |
| 21 | +import {InputEditorProps} from "./InputEditor"; |
| 22 | +import * as React from "react"; |
| 23 | +import {InputEditorUpdate} from "./updates/InputEditorUpdate"; |
| 24 | + |
| 25 | +const enum States { |
| 26 | + Showing, |
| 27 | + Loading, |
| 28 | +} |
| 29 | + |
| 30 | +interface ShowingState { |
| 31 | + type: States.Showing; |
| 32 | + error: string | null; |
| 33 | +} |
| 34 | + |
| 35 | +interface LoadingState { |
| 36 | + type: States.Loading; |
| 37 | +} |
| 38 | + |
| 39 | +type State = ShowingState | LoadingState; |
| 40 | + |
| 41 | +function toB64(file: File): Promise<string> { |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + const reader = new FileReader(); |
| 44 | + reader.onload = e => { |
| 45 | + const contents = e.target.result as ArrayBuffer; |
| 46 | + const b64 = btoa(String.fromCharCode.apply(null, new Uint8Array(contents))); |
| 47 | + resolve(b64); |
| 48 | + }; |
| 49 | + reader.onerror = e => { |
| 50 | + reject(e); |
| 51 | + }; |
| 52 | + reader.onabort = e => { |
| 53 | + reject(e); |
| 54 | + }; |
| 55 | + reader.readAsArrayBuffer(file); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +export class FileInputEditor extends Component<InputEditorProps, State> { |
| 60 | + |
| 61 | + public state: State = { |
| 62 | + type: States.Showing, |
| 63 | + error: null, |
| 64 | + }; |
| 65 | + |
| 66 | + private fileInput: React.RefObject<HTMLInputElement> = React.createRef(); |
| 67 | + |
| 68 | + public render(): ReactElement<any> { |
| 69 | + switch (this.state.type) { |
| 70 | + case States.Showing: |
| 71 | + return this.renderShowingState(); |
| 72 | + case States.Loading: |
| 73 | + return this.renderLoadingState(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private renderShowingState(): ReactElement<any> { |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <input type="file" |
| 81 | + ref={this.fileInput} |
| 82 | + onChange={e => this.onFileInputChanged(e)} |
| 83 | + required /> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + private renderLoadingState(): ReactElement<any> { |
| 89 | + return ( |
| 90 | + <div> |
| 91 | + <input type="file" |
| 92 | + ref={this.fileInput} |
| 93 | + onChange={e => this.onFileInputChanged(e)} |
| 94 | + required /> |
| 95 | + Loading... |
| 96 | + </div> |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + private onFileInputChanged(e: React.FormEvent): void { |
| 101 | + const file: File = this.fileInput.current.files[0]; |
| 102 | + const filename = file.name; |
| 103 | + |
| 104 | + this.setState({ |
| 105 | + type: States.Loading, |
| 106 | + }, () => { |
| 107 | + toB64(file) |
| 108 | + .then(b64Str => { |
| 109 | + const upd = InputEditorUpdate.value({ |
| 110 | + filename: file.name, |
| 111 | + data: b64Str, |
| 112 | + }); |
| 113 | + this.props.onJobInputUpdate(upd); |
| 114 | + this.setState({ |
| 115 | + type: States.Showing, |
| 116 | + error: null, |
| 117 | + }); |
| 118 | + }) |
| 119 | + .catch(err => { |
| 120 | + this.setState({ |
| 121 | + type: States.Showing, |
| 122 | + error: err, |
| 123 | + }); |
| 124 | + }) |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments