index.js 4.5 KB

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