Created
April 4, 2021 09:14
-
-
Save BallerIndustries/dc1ef4c2ec2650593926d47b7f2806fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import {PlelariousTransport} from "../../../helpers/PlelariousTransport"; | |
import {StoryMedia} from "../Story"; | |
import {Media} from "../Media"; | |
interface Props { | |
token: string | |
storyId: string | |
transport: PlelariousTransport | |
} | |
interface State { | |
isLoading: boolean | |
files: Array<File> | |
storyMedia: Array<StoryMedia> | |
error: any | |
} | |
export class UploadMediaModal extends React.Component<Props, State> { | |
constructor(props: Readonly<Props>) { | |
super(props); | |
this.state = { files: [], storyMedia: [], isLoading: true, error: null} | |
} | |
async componentDidMount() { | |
await this.refresh(); | |
} | |
async refresh() { | |
const {transport, storyId, token} = this.props | |
try { | |
const storyMedia = await transport.getStoryMedia(storyId, token) | |
this.setState({storyMedia, isLoading: false}) | |
} | |
catch (error) { | |
this.setState({error, isLoading: false}) | |
} | |
} | |
render() { | |
return <div> | |
<h3>Upload Media</h3> | |
{this.renderBody()} | |
</div> | |
} | |
renderBody() { | |
const {isLoading, error, storyMedia} = this.state | |
const {storyId, token, transport} = this.props | |
if (isLoading) { | |
return <p>Loading...</p> | |
} | |
if (error !== null) { | |
const data = error?.response?.data | |
console.log('error =', error) | |
console.log('data =', data) | |
return <p>Failed to upload, see console for details</p> | |
} | |
const onUpload = async () => { | |
try { | |
this.setState({isLoading: true}) | |
await transport.uploadFiles(storyId, token, this.state.files) | |
await this.refresh() | |
} | |
catch (error) { | |
this.setState({isLoading: false, error}) | |
} | |
} | |
const onFileChange = (event: any) => { | |
const fileList = event.target.files | |
const files = [] | |
for (let i = 0; i < fileList.length; i++) { | |
files.push(fileList[i]) | |
} | |
this.setState({ files: files }); | |
}; | |
const mediaBody = storyMedia.map(it => <Media media={it}/>) | |
return <> | |
{mediaBody} | |
<br /> | |
<input onChange={onFileChange} type="file" accept="image/*, audio/mp3, video/mp4" multiple={true}/> | |
<button onClick={onUpload}>Upload</button> | |
</> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment