App.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // import * as Expo from 'expo'
  2. import React from 'react'
  3. import {
  4. Text,
  5. View,
  6. AsyncStorage,
  7. Image
  8. } from 'react-native'
  9. import Uppy from '@uppy/core'
  10. import Tus from '@uppy/tus'
  11. import UppyFilePicker from '@uppy/react-native'
  12. import FileList from './FileList'
  13. import PauseResumeButton from './PauseResumeButton'
  14. import ProgressBar from './ProgressBar'
  15. import SelectFiles from './SelectFilesButton'
  16. export default class App extends React.Component {
  17. constructor () {
  18. super()
  19. this.state = {
  20. progress: 0,
  21. total: 0,
  22. file: null,
  23. uploadURL: null,
  24. isFilePickerVisible: false,
  25. isPaused: false,
  26. uploadStarted: false,
  27. uploadComplete: false,
  28. info: null,
  29. totalProgress: 0
  30. }
  31. this.isReactNative = (typeof navigator !== 'undefined' &&
  32. typeof navigator.product === 'string' &&
  33. navigator.product.toLowerCase() === 'reactnative')
  34. this.showFilePicker = this.showFilePicker.bind(this)
  35. this.hideFilePicker = this.hideFilePicker.bind(this)
  36. this.togglePauseResume = this.togglePauseResume.bind(this)
  37. console.log('Is this React Native?', this.isReactNative)
  38. this.uppy = Uppy({ autoProceed: true, debug: true })
  39. this.uppy.use(Tus, {
  40. endpoint: 'https://master.tus.io/files/',
  41. urlStorage: AsyncStorage
  42. // fingerprint: customFingerprint
  43. })
  44. this.uppy.on('upload-progress', (file, progress) => {
  45. this.setState({
  46. progress: progress.bytesUploaded,
  47. total: progress.bytesTotal,
  48. totalProgress: this.uppy.state.totalProgress,
  49. uploadStarted: true
  50. })
  51. })
  52. this.uppy.on('upload-success', (file, response) => {
  53. // console.log(file.name, response)
  54. })
  55. this.uppy.on('complete', (result) => {
  56. this.setState({
  57. status: 'Upload complete ✅',
  58. uploadURL: result.successful[0] ? result.successful[0].uploadURL : null,
  59. uploadComplete: true,
  60. uploadStarted: false
  61. })
  62. console.log('Upload complete:', result)
  63. })
  64. this.uppy.on('info-visible', () => {
  65. const info = this.uppy.getState().info
  66. this.setState({
  67. info: info
  68. })
  69. console.log('uppy-info:', info)
  70. })
  71. this.uppy.on('info-hidden', () => {
  72. this.setState({
  73. info: null
  74. })
  75. })
  76. }
  77. showFilePicker () {
  78. this.setState({
  79. isFilePickerVisible: true,
  80. uploadStarted: false,
  81. uploadComplete: false
  82. })
  83. }
  84. hideFilePicker () {
  85. this.setState({
  86. isFilePickerVisible: false
  87. })
  88. }
  89. togglePauseResume () {
  90. if (this.state.isPaused) {
  91. this.uppy.resumeAll()
  92. this.setState({
  93. isPaused: false
  94. })
  95. } else {
  96. this.uppy.pauseAll()
  97. this.setState({
  98. isPaused: true
  99. })
  100. }
  101. }
  102. render () {
  103. return (
  104. <View style={{
  105. paddingTop: 100,
  106. paddingLeft: 50,
  107. paddingRight: 50,
  108. flex: 1
  109. }}>
  110. <Text style={{
  111. fontSize: 25,
  112. marginBottom: 20,
  113. textAlign: 'center'
  114. }}>Uppy in React Native</Text>
  115. <View style={{alignItems: 'center'}}>
  116. <Image
  117. style={{width: 80, height: 78, marginBottom: 50}}
  118. source={require('./assets/uppy-logo.png')}
  119. />
  120. </View>
  121. <SelectFiles showFilePicker={this.showFilePicker} />
  122. {this.state.info
  123. ? <Text style={{
  124. marginBottom: 10,
  125. marginTop: 10,
  126. color: '#b8006b'}}>{this.state.info.message}</Text>
  127. : null
  128. }
  129. <ProgressBar progress={this.state.totalProgress} />
  130. <PauseResumeButton
  131. isPaused={this.state.isPaused}
  132. onPress={this.togglePauseResume}
  133. uploadStarted={this.state.uploadStarted}
  134. uploadComplete={this.state.uploadComplete} />
  135. <UppyFilePicker
  136. show={this.state.isFilePickerVisible}
  137. uppy={this.uppy}
  138. onRequestClose={this.hideFilePicker}
  139. serverUrl="http://localhost:3020" />
  140. <FileList uppy={this.uppy} />
  141. {/* <Text>{this.state.status ? 'Status: ' + this.state.status : null}</Text>
  142. <Text>{this.state.progress} of {this.state.total}</Text> */}
  143. </View>
  144. )
  145. }
  146. }