Skip to content

Commit

Permalink
feat: allow uploading videos
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaiodias committed May 24, 2019
1 parent 7df2cb1 commit 5e869e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/defaults/render-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default ({
countdownTime,
timeLimit,
isReplayingVideo,
useVideoInput,

onTurnOnCamera,
onTurnOffCamera,
Expand All @@ -55,7 +56,7 @@ export default ({
if (isReplayingVideo) {
return (
<Button onClick={onStopReplaying} data-qa='start-replaying'>
Record another video
Use another video
</Button>
)
}
Expand All @@ -70,6 +71,14 @@ export default ({
)
}

if (useVideoInput) {
return (
<Button onClick={onOpenVideoInput} data-qa='open-input'>
Upload a video
</Button>
)
}

return shouldUseVideoInput ? (
<Button onClick={onOpenVideoInput} data-qa='open-input'>
Record a video
Expand Down
42 changes: 35 additions & 7 deletions src/video-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export default class VideoRecorder extends Component {
countdownTime: 3000
}

videoInput = React.createRef()

constructor (props) {
super(props)

Expand Down Expand Up @@ -146,7 +148,12 @@ export default class VideoRecorder extends Component {
isVideoInputSupported
},
() => {
if (this.state.isInlineRecordingSupported && this.props.isOnInitially) {
if (this.props.useVideoInput && this.props.isOnInitially) {
this.handleOpenVideoInput()
} else if (
this.state.isInlineRecordingSupported &&
this.props.isOnInitially
) {
this.turnOnCamera()
} else if (
this.state.isVideoInputSupported &&
Expand Down Expand Up @@ -414,12 +421,20 @@ export default class VideoRecorder extends Component {
}

handleVideoSelected (e) {
if (this.state.isReplayingVideo) {
this.setState({
isReplayingVideo: false
})
}

const files = e.target.files || e.dataTransfer.files
if (files.length === 0) return

const startedAt = new Date().getTime()
const video = files[0]

e.target.value = null

const extension = video.type === 'video/quicktime' ? 'mov' : undefined

getVideoInfo(video)
Expand Down Expand Up @@ -450,14 +465,19 @@ export default class VideoRecorder extends Component {
this.props.onOpenVideoInput()
}

this.videoInput.click()
this.videoInput.current.value = null
this.videoInput.current.click()
}

handleStopReplaying () {
if (this.props.onStopReplaying) {
this.props.onStopReplaying()
}

if (this.props.useVideoInput && this.props.isOnInitially) {
return this.handleOpenVideoInput()
}

this.setState({
isReplayingVideo: false
})
Expand All @@ -475,7 +495,8 @@ export default class VideoRecorder extends Component {
renderVideoInputView,
renderUnsupportedView,
renderErrorView,
renderLoadingView
renderLoadingView,
useVideoInput
} = this.props

const {
Expand All @@ -489,14 +510,15 @@ export default class VideoRecorder extends Component {
} = this.state

const shouldUseVideoInput =
!isInlineRecordingSupported && isVideoInputSupported
useVideoInput || (!isInlineRecordingSupported && isVideoInputSupported)

const videoInput = shouldUseVideoInput ? (
<input
ref={el => (this.videoInput = el)}
ref={this.videoInput}
key='videoInput'
type='file'
accept='video/*'
capture='user'
capture={useVideoInput ? undefined : 'user'}
style={{ display: 'none' }}
onChange={this.handleVideoSelected}
/>
Expand Down Expand Up @@ -565,7 +587,12 @@ export default class VideoRecorder extends Component {
isReplayVideoMuted
} = this.state

const { countdownTime, timeLimit, renderActions } = this.props
const {
countdownTime,
timeLimit,
renderActions,
useVideoInput
} = this.props

return (
<Wrapper>
Expand All @@ -583,6 +610,7 @@ export default class VideoRecorder extends Component {
isReplayVideoMuted,
countdownTime,
timeLimit,
useVideoInput,

onTurnOnCamera: this.turnOnCamera,
onTurnOffCamera: this.turnOffCamera,
Expand Down
4 changes: 4 additions & 0 deletions src/video-recorder.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ stories.add('with isOnInitially=true', () => (
{...actionLoggers}
/>
))

stories.add('with useVideoInput=true isOnInitially=true', () => (
<VideoRecorder isOnInitially useVideoInput {...actionLoggers} />
))

0 comments on commit 5e869e3

Please sign in to comment.