import { DateTime } from 'luxon'; import { inject, observer } from 'mobx-react'; import React from 'react'; import { EmbeddedYoutube } from '../../components'; import { IVideoArchive, Permissions } from '../../model'; import { ArchiveService } from '../../services/archive.service'; import { AppStore } from '../../stores'; import './video-archive.scss'; interface IProps { appStore: AppStore; } interface IState { archives: IVideoArchive[]; url: string; error?: string; } @inject('appStore') @observer export class VideoArchive extends React.Component { constructor(props: IProps) { super(props); this.state = { archives: [], url: '', }; } componentDidMount() { this.loadArchives(); } async loadArchives() { const archives = await ArchiveService.getVideoArchive(); if (archives) { this.setState({ archives, }); } } onSubmit = async (e: React.FormEvent) => { e.preventDefault(); const { url } = this.state; this.setState({ error: undefined }); try { await ArchiveService.postVideoArchive({ url }); } catch (e) { this.setState({ error: 'Invalid URL' }); return; } this.setState({ url: '' }); this.loadArchives(); }; renderForm() { const { error, url } = this.state; return (
this.setState({ url: e.target.value })} /> {error && ( {error} )}
); } renderArchives() { return this.state.archives.map((v, k) => (

{v.title}

{v.uploaded_by} -{' '} {DateTime.fromISO(v.created_at).toLocaleString()}
)); } render() { const { claims } = this.props.appStore; return (
{claims && claims.permissions > Permissions.User && this.renderForm()}
{this.renderArchives()}
); } }