main.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const { createStore, compose, combineReducers, applyMiddleware } = require('redux')
  2. const logger = require('redux-logger').default
  3. const Uppy = require('@uppy/core')
  4. const uppyReduxStore = require('@uppy/store-redux')
  5. const Dashboard = require('@uppy/dashboard')
  6. const Tus = require('@uppy/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,
  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 (typeof __REDUX_DEVTOOLS_EXTENSION__ !== 'undefined') {
  28. // eslint-disable-next-line no-undef
  29. enhancer = compose(enhancer, __REDUX_DEVTOOLS_EXTENSION__())
  30. }
  31. const store = createStore(reducer, enhancer)
  32. // Counter example from https://github.com/reactjs/redux/blob/master/examples/counter-vanilla/index.html
  33. const valueEl = document.querySelector('#value')
  34. function getCounter () { return store.getState().counter }
  35. function render () {
  36. valueEl.innerHTML = getCounter().toString()
  37. }
  38. render()
  39. store.subscribe(render)
  40. document.querySelector('#increment').onclick = () => {
  41. store.dispatch({ type: 'INCREMENT' })
  42. }
  43. document.querySelector('#decrement').onclick = () => {
  44. store.dispatch({ type: 'DECREMENT' })
  45. }
  46. document.querySelector('#incrementIfOdd').onclick = () => {
  47. if (getCounter() % 2 !== 0) {
  48. store.dispatch({ type: 'INCREMENT' })
  49. }
  50. }
  51. document.querySelector('#incrementAsync').onclick = () => {
  52. setTimeout(() => store.dispatch({ type: 'INCREMENT' }), 1000)
  53. }
  54. // Uppy using the same store
  55. const uppy = new Uppy({
  56. id: 'redux',
  57. store: uppyReduxStore({ 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://tusd.tusdemo.net/' })
  74. window.uppy = uppy