main.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const { createStore, compose, combineReducers, applyMiddleware } = require('redux')
  2. const logger = require('redux-logger').default
  3. const Uppy = require('uppy/lib/core')
  4. const uppyReduxStore = require('uppy/lib/store/ReduxStore')
  5. const Dashboard = require('uppy/lib/plugins/Dashboard')
  6. const Tus = require('uppy/lib/plugins/Tus')
  7. function counter (state = 0, action) {
  8. switch (action.type) {
  9. case 'INCREMENT':
  10. return state + 1
  11. case 'DECREMENT':
  12. return state - 1
  13. default:
  14. return state
  15. }
  16. }
  17. const reducer = combineReducers({
  18. counter: counter,
  19. // You don't have to use the `uppy` key. But if you don't,
  20. // you need to provide a custom `selector` to the `uppyReduxStore` call below.
  21. uppy: uppyReduxStore.reducer
  22. })
  23. let enhancer = applyMiddleware(
  24. uppyReduxStore.middleware(),
  25. logger
  26. )
  27. if (window.__REDUX_DEVTOOLS_EXTENSION__) {
  28. enhancer = compose(enhancer, window.__REDUX_DEVTOOLS_EXTENSION__())
  29. }
  30. const store = createStore(reducer, enhancer)
  31. // Counter example from https://github.com/reactjs/redux/blob/master/examples/counter-vanilla/index.html
  32. const valueEl = document.querySelector('#value')
  33. function getCounter () { return store.getState().counter }
  34. function render () {
  35. valueEl.innerHTML = getCounter().toString()
  36. }
  37. render()
  38. store.subscribe(render)
  39. document.querySelector('#increment').onclick = () => {
  40. store.dispatch({ type: 'INCREMENT' })
  41. }
  42. document.querySelector('#decrement').onclick = () => {
  43. store.dispatch({ type: 'DECREMENT' })
  44. }
  45. document.querySelector('#incrementIfOdd').onclick = () => {
  46. if (getCounter() % 2 !== 0) {
  47. store.dispatch({ type: 'INCREMENT' })
  48. }
  49. }
  50. document.querySelector('#incrementAsync').onclick = () => {
  51. setTimeout(() => store.dispatch({ type: 'INCREMENT' }), 1000)
  52. }
  53. // Uppy using the same store
  54. const uppy = Uppy({
  55. autoProceed: false,
  56. id: 'redux',
  57. store: uppyReduxStore({ store: store }),
  58. // If we had placed our `reducer` elsewhere in Redux, eg. under an `uppy` key in the state for a profile page,
  59. // we'd do something like:
  60. //
  61. // store: uppyReduxStore({
  62. // store: store,
  63. // id: 'avatar',
  64. // selector: state => state.pages.profile.uppy
  65. // }),
  66. debug: true
  67. })
  68. uppy.use(Dashboard, {
  69. target: '#app',
  70. inline: true,
  71. width: 400
  72. })
  73. uppy.use(Tus, { endpoint: 'https://master.tus.io/' })
  74. uppy.run()
  75. window.uppy = uppy