index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: 'GoogleDrive', title: 'Google Drive' },
  26. { id: 'Instagram', title: 'Instagram' }
  27. ],
  28. openProvider: null
  29. }
  30. this.takePicture = this.takePicture.bind(this)
  31. this.selectImage = this.selectImage.bind(this)
  32. this.selectDocument = this.selectDocument.bind(this)
  33. }
  34. componentDidMount () {
  35. this.uppy = this.props.uppy
  36. }
  37. takePicture () {
  38. takePicture().then((file) => {
  39. this.uppy.addFile({
  40. source: 'React Native',
  41. name: `media_${Date.now()}.jpg`,
  42. type: file.type,
  43. data: file
  44. })
  45. this.props.onRequestClose()
  46. })
  47. }
  48. selectImage () {
  49. selectImage().then((file) => {
  50. this.uppy.addFile({
  51. source: 'React Native',
  52. name: `media_${Date.now()}.jpg`,
  53. type: file.type,
  54. data: file
  55. })
  56. this.uppy.upload()
  57. this.props.onRequestClose()
  58. })
  59. }
  60. selectDocument () {
  61. selectDocument().then((file) => {
  62. this.uppy.addFile({
  63. source: 'React Native',
  64. name: `media_${Date.now()}.jpg`,
  65. type: file.type,
  66. data: file
  67. })
  68. this.props.onRequestClose()
  69. })
  70. }
  71. openProvider (id) {
  72. console.log('OPEN PROVIDER:', id)
  73. this.setState({
  74. openProvider: id
  75. })
  76. }
  77. chooseProvider (id) {
  78. console.log('PROVIDER SELECTED:', id)
  79. switch (id) {
  80. case 'LocalImages':
  81. this.selectImage()
  82. return
  83. case 'LocalDocuments':
  84. this.selectDocument()
  85. return
  86. case 'LocalCamera':
  87. this.takePicture()
  88. return
  89. default:
  90. this.openProvider(id)
  91. }
  92. }
  93. renderSourceList () {
  94. return (
  95. <ScrollView
  96. contentContainerStyle={{
  97. flex: '1',
  98. marginTop: 22,
  99. justifyContent: 'center'
  100. }}>
  101. <TouchableOpacity
  102. style={{
  103. alignItems: 'center',
  104. backgroundColor: '#DDDDDD',
  105. marginBottom: 10,
  106. padding: 10
  107. }}
  108. onPress={ev => this.props.onRequestClose()}>
  109. <Text>Close</Text>
  110. </TouchableOpacity>
  111. {this.state.providers.map((item, index) => {
  112. return (
  113. <TouchableOpacity
  114. style={{
  115. alignItems: 'center',
  116. backgroundColor: '#DDDDDD',
  117. marginBottom: 10,
  118. padding: 10
  119. }}
  120. key={index}
  121. onPress={ev => this.chooseProvider(item.id)}>
  122. <Text>{item.title}</Text>
  123. </TouchableOpacity>
  124. )
  125. })}
  126. </ScrollView>
  127. )
  128. }
  129. render () {
  130. return (
  131. <Modal
  132. animationType="slide"
  133. transparent={false}
  134. visible={this.props.show}
  135. supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
  136. onRequestClose={() => {
  137. Expo.Alert.alert('Modal has been closed.')
  138. this.props.onRequestClose()
  139. }}>
  140. {this.state.openProvider ? <Provider id={this.state.openProvider} uppy={this.uppy} /> : this.renderSourceList()}
  141. </Modal>
  142. )
  143. }
  144. }