index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React from 'react'
  2. import Expo from 'expo'
  3. import {
  4. // StyleSheet,
  5. Modal,
  6. Text,
  7. // View,
  8. ScrollView,
  9. // Button,
  10. // Image
  11. TouchableOpacity } from 'react-native'
  12. import takePicture from './takePicture'
  13. import selectImage from './selectImage'
  14. import selectDocument from './selectDocument'
  15. import Provider from './provider'
  16. // import ProviderGrid from './provider-grid'
  17. export default class UppyReactNativeFilePicker extends React.Component {
  18. constructor () {
  19. super()
  20. this.state = {
  21. providers: [
  22. { id: 'LocalImages', title: 'Pick Local Images/Videos' },
  23. { id: 'LocalDocuments', title: 'Pick Documents' },
  24. { id: 'LocalCamera', title: 'Take a Picture' },
  25. { id: 'Url', title: 'Url' },
  26. { id: 'GoogleDrive', title: 'Google Drive' },
  27. { id: 'Instagram', title: 'Instagram' }
  28. ],
  29. openProvider: null
  30. }
  31. this.takePicture = this.takePicture.bind(this)
  32. this.selectImage = this.selectImage.bind(this)
  33. this.selectDocument = this.selectDocument.bind(this)
  34. }
  35. componentDidMount () {
  36. this.uppy = this.props.uppy
  37. this.uppy.on('info-visible', () => {
  38. const info = this.uppy.getState().info
  39. console.log('uppy-info', info)
  40. })
  41. }
  42. takePicture () {
  43. takePicture().then((file) => {
  44. this.uppy.addFile({
  45. source: 'React Native',
  46. name: `media_${Date.now()}.jpg`,
  47. type: file.type,
  48. data: file
  49. })
  50. this.props.onRequestClose()
  51. })
  52. }
  53. selectImage () {
  54. selectImage().then((file) => {
  55. this.uppy.addFile({
  56. source: 'React Native',
  57. name: `media_${Date.now()}.jpg`,
  58. type: file.type,
  59. data: file
  60. })
  61. this.uppy.upload()
  62. this.props.onRequestClose()
  63. })
  64. }
  65. selectDocument () {
  66. selectDocument().then((file) => {
  67. this.uppy.addFile({
  68. source: 'React Native',
  69. name: `media_${Date.now()}.jpg`,
  70. type: file.type,
  71. data: file
  72. })
  73. this.props.onRequestClose()
  74. })
  75. }
  76. openProvider (id) {
  77. console.log('OPEN PROVIDER:', id)
  78. this.setState({
  79. openProvider: id
  80. })
  81. }
  82. chooseProvider (id) {
  83. console.log('PROVIDER SELECTED:', id)
  84. switch (id) {
  85. case 'LocalImages':
  86. this.selectImage()
  87. return
  88. case 'LocalDocuments':
  89. this.selectDocument()
  90. return
  91. case 'LocalCamera':
  92. this.takePicture()
  93. return
  94. default:
  95. this.openProvider(id)
  96. }
  97. }
  98. renderSourceList () {
  99. return (
  100. <ScrollView
  101. contentContainerStyle={{
  102. flex: '1',
  103. marginTop: 22,
  104. justifyContent: 'center'
  105. }}>
  106. <TouchableOpacity
  107. style={{
  108. alignItems: 'center',
  109. backgroundColor: '#c6e1f3',
  110. marginBottom: 10,
  111. padding: 10
  112. }}
  113. onPress={ev => this.props.onRequestClose()}>
  114. <Text>Close</Text>
  115. </TouchableOpacity>
  116. {this.state.providers.map((item, index) => {
  117. return (
  118. <TouchableOpacity
  119. style={{
  120. alignItems: 'center',
  121. backgroundColor: '#c6e1f3',
  122. marginBottom: 10,
  123. padding: 10
  124. }}
  125. key={index}
  126. onPress={ev => this.chooseProvider(item.id)}>
  127. <Text>{item.title}</Text>
  128. </TouchableOpacity>
  129. )
  130. })}
  131. </ScrollView>
  132. )
  133. }
  134. render () {
  135. return (
  136. <Modal
  137. animationType="slide"
  138. transparent={false}
  139. visible={this.props.show}
  140. supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
  141. onRequestClose={() => {
  142. Expo.Alert.alert('Modal has been closed.')
  143. this.props.onRequestClose()
  144. }}>
  145. {this.state.openProvider
  146. ? <Provider
  147. providerID={this.state.openProvider}
  148. uppy={this.uppy}
  149. onSuccess={() => {
  150. this.setState({
  151. openProvider: null
  152. })
  153. this.props.onRequestClose()
  154. }}
  155. {...this.props} />
  156. : this.renderSourceList()
  157. }
  158. </Modal>
  159. )
  160. }
  161. }