index.js 4.1 KB

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