123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- {
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
- }
- {
- function createStore(reducer) {
- let state = reducer(undefined, {})
- let cbs = []
- const getState = () => state
- const subscribe = cb => (cbs.push(cb),
- () => cbs = cbs.filter(c => c !== cb))
- const dispatch = action => {
- if (typeof action === 'function') {
- return action(dispatch, getState)
- }
- const newState = reducer(state, action)
- if (newState !== state) {
- state = newState
- for (let cb of cbs) {
- cb()
- }
- }
- }
- return {
- getState,
- dispatch,
- subscribe
- }
- }
- }
- {
- function promiseReducer(state = {}, { type, status, payload, error, nameOfPromise }) {
- if (type === 'PROMISE') {
- return {
- ...state,
- [nameOfPromise]: { status, payload, error }
- }
- }
- return state
- }
-
-
- const actionPending = nameOfPromise => ({ nameOfPromise, type: 'PROMISE', status: 'PENDING' })
- const actionFulfilled = (nameOfPromise, payload) => ({ nameOfPromise, type: 'PROMISE', status: 'FULFILLED', payload })
- const actionRejected = (nameOfPromise, error) => ({ nameOfPromise, type: 'PROMISE', status: 'REJECTED', error })
- const actionPromise = (nameOfPromise, promise) =>
- async dispatch => {
- dispatch(actionPending(nameOfPromise))
- try {
- const payload = await promise
- dispatch(actionFulfilled(nameOfPromise, payload))
- return payload
- }
- catch (error) {
- dispatch(actionRejected(nameOfPromise, error))
- }
- }
-
- {
-
-
-
-
-
- }
- }
- {
- const jwtDecode = function (token) {
- try {
- let parseData = token.split('.')[1]
- return JSON.parse(atob(parseData))
- }
- catch (e) {
- return undefined
- }
- }
- function authReducer(state = {}, { type, token }) {
- if (type === 'AUTH_LOGIN') {
- let payload = jwtDecode(token)
- return state = {
- token,
- payload
- }
- }
- if (type === 'AUTH_LOGOUT') {
- return {}
- }
- return state
- }
-
- const actionAuthLogin = token => ({ type: 'AUTH_LOGIN', token })
- const actionAuthLogout = () => ({ type: 'AUTH_LOGOUT' })
- const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2Mzc3ZTEzM2I3NGUxZjVmMmVjMWMxMjUiLCJsb2dpbiI6InRlc3Q1IiwiYWNsIjpbIjYzNzdlMTMzYjc0ZTFmNWYyZWMxYzEyNSIsInVzZXIiXX0sImlhdCI6MTY2ODgxMjQ1OH0.t1eQlRwkcP7v9JxUPMo3dcGKprH-uy8ujukNI7xE3A0"
-
-
-
-
-
- }
- {
- function cartReducer(state = {}, { type, count, good }) {
- if (type === 'CART_ADD') {
- return {
- ...state,
- [good._id]: {
- good,
- count: (state[good._id] ? state[good._id].count + count : count)
- }
- }
- }
- if (type === 'CART_SUB') {
- if (state[good._id]) {
- let newCount = state[good._id].count - count
- if (newCount > 0) {
- return {
- ...state,
- [good._id]: {
- good,
- count: newCount
- }
- }
- } else {
- delete state[good._id]
- return { ...state }
- }
- } else {
- return undefined
- }
- }
- if (type === 'CART_DEL') {
- delete state[good._id]
- return { ...state }
- }
- if (type === 'CART_SET') {
- if (count > 0) {
- return {
- ...state,
- [good._id]: {
- good,
- count
- }
- }
- } else {
- delete state[good._id]
- return { ...state }
- }
- }
- if (type === 'CART_CLEAR') {
- return state = {}
- }
- return state
- }
-
-
- const actionCartAdd = (good, count = 1) => ({ type: 'CART_ADD', count, good })
-
- const actionCartSub = (good, count = 1) => ({ type: 'CART_SUB', count, good })
-
- const actionCartDel = (good) => ({ type: 'CART_DEL', good })
-
- const actionCartSet = (good, count = 1) => ({ type: 'CART_SET', count, good })
-
- const actionCartClear = () => ({ type: 'CART_CLEAR' })
-
- const store = createStore(cartReducer)
- store.subscribe(() => console.log(store.getState()))
- console.log(store.getState())
- store.dispatch(actionCartAdd({ _id: 'пиво', price: 50 }))
-
- store.dispatch(actionCartAdd({ _id: 'чипсы', price: 75 }))
-
-
-
-
- store.dispatch(actionCartAdd({ _id: 'пиво', price: 50 }, 5))
-
-
-
-
- store.dispatch(actionCartSet({ _id: 'чипсы', price: 75 }, 2))
-
-
-
-
- store.dispatch(actionCartSub({ _id: 'пиво', price: 50 }, 4))
-
-
-
-
- store.dispatch(actionCartDel({ _id: 'чипсы', price: 75 }))
-
-
-
- store.dispatch(actionCartClear())
- }
|