index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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({ exif: true }).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.props.onRequestClose()
  62. })
  63. }
  64. selectDocument () {
  65. selectDocument().then((file) => {
  66. this.uppy.addFile({
  67. source: 'React Native',
  68. name: `media_${Date.now()}.jpg`,
  69. type: file.type,
  70. data: file
  71. })
  72. this.props.onRequestClose()
  73. })
  74. }
  75. openProvider (id) {
  76. console.log('OPEN PROVIDER:', id)
  77. this.setState({
  78. openProvider: id
  79. })
  80. }
  81. chooseProvider (id) {
  82. console.log('PROVIDER SELECTED:', id)
  83. switch (id) {
  84. case 'LocalImages':
  85. this.selectImage()
  86. return
  87. case 'LocalDocuments':
  88. this.selectDocument()
  89. return
  90. case 'LocalCamera':
  91. this.takePicture()
  92. return
  93. default:
  94. this.openProvider(id)
  95. }
  96. }
  97. renderSourceList () {
  98. return (
  99. <ScrollView
  100. contentContainerStyle={{
  101. flex: '1',
  102. marginTop: 22,
  103. justifyContent: 'center'
  104. }}>
  105. <TouchableOpacity
  106. style={{
  107. alignItems: 'center',
  108. backgroundColor: '#c6e1f3',
  109. marginBottom: 10,
  110. padding: 10
  111. }}
  112. onPress={ev => this.props.onRequestClose()}>
  113. <Text>Close</Text>
  114. </TouchableOpacity>
  115. {this.state.providers.map((item, index) => {
  116. return (
  117. <TouchableOpacity
  118. style={{
  119. alignItems: 'center',
  120. backgroundColor: '#c6e1f3',
  121. marginBottom: 10,
  122. padding: 10
  123. }}
  124. key={index}
  125. onPress={ev => this.chooseProvider(item.id)}>
  126. <Text>{item.title}</Text>
  127. </TouchableOpacity>
  128. )
  129. })}
  130. </ScrollView>
  131. )
  132. }
  133. render () {
  134. return (
  135. <Modal
  136. animationType="slide"
  137. transparent={false}
  138. visible={this.props.show}
  139. supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
  140. onRequestClose={() => {
  141. Expo.Alert.alert('Modal has been closed.')
  142. this.props.onRequestClose()
  143. }}>
  144. {this.state.openProvider
  145. ? <Provider
  146. providerID={this.state.openProvider}
  147. uppy={this.uppy}
  148. onSuccess={() => {
  149. this.setState({
  150. openProvider: null
  151. })
  152. this.props.onRequestClose()
  153. }}
  154. {...this.props} />
  155. : this.renderSourceList()
  156. }
  157. </Modal>
  158. )
  159. }
  160. }