main.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { compose, combineReducers, applyMiddleware } from 'redux'
  2. import { configureStore } from '@reduxjs/toolkit'
  3. import logger from 'redux-logger'
  4. import Uppy from '@uppy/core'
  5. import ReduxStore from '@uppy/store-redux'
  6. import * as uppyReduxStore from '@uppy/store-redux'
  7. import Dashboard from '@uppy/dashboard'
  8. import Tus from '@uppy/tus'
  9. import '@uppy/core/dist/style.css'
  10. import '@uppy/dashboard/dist/style.css'
  11. function counter (state = 0, action) {
  12. switch (action.type) {
  13. case 'INCREMENT':
  14. return state + 1
  15. case 'DECREMENT':
  16. return state - 1
  17. default:
  18. return state
  19. }
  20. }
  21. const reducer = combineReducers({
  22. counter,
  23. // You don't have to use the `uppy` key. But if you don't,
  24. // you need to provide a custom `selector` to the `uppyReduxStore` call below.
  25. uppy: uppyReduxStore.reducer,
  26. })
  27. let enhancer = applyMiddleware(
  28. uppyReduxStore.middleware(),
  29. logger,
  30. )
  31. if (typeof __REDUX_DEVTOOLS_EXTENSION__ !== 'undefined') {
  32. // eslint-disable-next-line no-undef
  33. enhancer = compose(enhancer, __REDUX_DEVTOOLS_EXTENSION__())
  34. }
  35. const store = configureStore({
  36. reducer,
  37. enhancers: [enhancer],
  38. middleware: (getDefaultMiddleware) => getDefaultMiddleware({
  39. serializableCheck: {
  40. ignoredActions: [uppyReduxStore.STATE_UPDATE],
  41. ignoreState: true,
  42. },
  43. }),
  44. })
  45. // Counter example from https://github.com/reactjs/redux/blob/master/examples/counter-vanilla/index.html
  46. const valueEl = document.querySelector('#value')
  47. function getCounter () { return store.getState().counter }
  48. function render () {
  49. valueEl.innerHTML = getCounter().toString()
  50. }
  51. render()
  52. store.subscribe(render)
  53. document.querySelector('#increment').onclick = () => {
  54. store.dispatch({ type: 'INCREMENT' })
  55. }
  56. document.querySelector('#decrement').onclick = () => {
  57. store.dispatch({ type: 'DECREMENT' })
  58. }
  59. document.querySelector('#incrementIfOdd').onclick = () => {
  60. if (getCounter() % 2 !== 0) {
  61. store.dispatch({ type: 'INCREMENT' })
  62. }
  63. }
  64. document.querySelector('#incrementAsync').onclick = () => {
  65. setTimeout(() => store.dispatch({ type: 'INCREMENT' }), 1000)
  66. }
  67. // Uppy using the same store
  68. const uppy = new Uppy({
  69. id: 'redux',
  70. store: new ReduxStore({ store }),
  71. // If we had placed our `reducer` elsewhere in Redux, eg. under an `uppy` key in the state for a profile page,
  72. // we'd do something like:
  73. //
  74. // store: new ReduxStore({
  75. // store: store,
  76. // id: 'avatar',
  77. // selector: state => state.pages.profile.uppy
  78. // }),
  79. debug: true,
  80. })
  81. uppy.use(Dashboard, {
  82. target: '#app',
  83. inline: true,
  84. width: 400,
  85. })
  86. uppy.use(Tus, { endpoint: 'https://tusd.tusdemo.net/files/' })