provider.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react'
  2. import {
  3. AsyncStorage,
  4. WebView } from 'react-native'
  5. import Instagram from '@uppy/instagram'
  6. import Url from './url'
  7. function getQueryParamValueFromUrl (name, url) {
  8. name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]')
  9. var regexS = '[\\?&]' + name + '=([^&#]*)'
  10. var regex = new RegExp(regexS)
  11. var results = regex.exec(url)
  12. return results == null ? null : results[1]
  13. }
  14. export default class UppyRNProvider extends React.Component {
  15. constructor () {
  16. super()
  17. this.state = {
  18. instagram: {
  19. user: 'bla@gmail.com',
  20. items: [
  21. { caption: Date.now(), url: 'http://lorempixel.com/200/200/cats/1' },
  22. { caption: Date.now(), url: 'http://lorempixel.com/200/200/cats/2' },
  23. { caption: Date.now(), url: 'http://lorempixel.com/200/200/cats/3' },
  24. { caption: Date.now(), url: 'http://lorempixel.com/200/200/cats/4' },
  25. { caption: Date.now(), url: 'http://lorempixel.com/200/200/cats/5' },
  26. { caption: Date.now(), url: 'http://lorempixel.com/200/200/' },
  27. { caption: Date.now(), url: 'http://lorempixel.com/200/200/' },
  28. { caption: Date.now(), url: 'http://lorempixel.com/200/200/' },
  29. { caption: Date.now(), url: 'http://lorempixel.com/200/200/' }
  30. ]
  31. }
  32. }
  33. }
  34. componentDidMount () {
  35. const uppy = this.props.uppy
  36. const options = Object.assign(
  37. { id: 'uppyRN:Instagram' },
  38. this.props,
  39. { storage: AsyncStorage }
  40. )
  41. delete options.uppy
  42. uppy.use(Instagram, options)
  43. this.plugin = uppy.getPlugin(options.id)
  44. this.setState({
  45. authUrl: this.plugin.provider.authUrl()
  46. })
  47. }
  48. componentWillUnmount () {
  49. const uppy = this.props.uppy
  50. uppy.removePlugin(this.plugin)
  51. }
  52. // renderGrid (items) {
  53. // return (
  54. // <View style={styles.container}>
  55. // <FlatList
  56. // data={items}
  57. // renderItem={({item}) => (
  58. // <View style={{ flex: 1, flexDirection: 'column', margin: 1 }}>
  59. // <Image style={styles.item} source={{uri: item.url}} />
  60. // </View>
  61. // )}
  62. // keyExtractor={(item, index) => index.toString()}
  63. // numColumns={3}
  64. // />
  65. // </View>
  66. // )
  67. // }
  68. renderInstagram () {
  69. console.log(this.state.authUrl)
  70. return <WebView
  71. source={{ uri: this.state.authUrl }}
  72. style={{ marginTop: 20 }}
  73. onNavigationStateChange={(ev) => {
  74. const url = ev.url
  75. const token = getQueryParamValueFromUrl('uppyAuthToken', url)
  76. console.log(token)
  77. this.plugin.provider.setAuthToken(token)
  78. console.log(this.plugin.provider.list('recent'))
  79. // return this.renderGrid(this.state.instagram.items)
  80. }}
  81. />
  82. }
  83. render () {
  84. if (this.props.providerID === 'Url') {
  85. return <Url {...this.props} />
  86. }
  87. return this.renderInstagram()
  88. }
  89. }
  90. // const styles = StyleSheet.create({
  91. // container: {
  92. // justifyContent: 'center',
  93. // flex: 1,
  94. // paddingTop: 30
  95. // },
  96. // item: {
  97. // justifyContent: 'center',
  98. // alignItems: 'center',
  99. // height: 100
  100. // }
  101. // })