|
@@ -1,75 +1,29 @@
|
|
|
import {createStore, applyMiddleware, combineReducers} from 'redux';
|
|
|
-//import thunk from 'redux-thunk';
|
|
|
-import createSagaMiddleware from 'redux-saga';
|
|
|
-import { all, takeLatest, takeLeading, takeEvery, put } from 'redux-saga/effects';
|
|
|
-import {actionSearchResult} from '../actions';
|
|
|
-import { GraphQLClient } from 'graphql-request';
|
|
|
+import thunk from 'redux-thunk';
|
|
|
|
|
|
-const gql = new GraphQLClient('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
|
|
|
|
|
|
-const sagaMiddleware = createSagaMiddleware()
|
|
|
-
|
|
|
-
|
|
|
-let store = createStore((state={}, {type, ...params}) => { //единственный редьюсер данного хранилища
|
|
|
- if (type === 'SEARCH_RESULT'){
|
|
|
- return {searchResult: {...params}}
|
|
|
- }
|
|
|
- return state//, в таком случае вызываются все редьюсеры, но далеко не всегда action.type будет относится к этому редьюсеру. Тогда редьюсер должен вернуть state как есть.
|
|
|
-}, applyMiddleware(sagaMiddleware))
|
|
|
-
|
|
|
-const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
|
|
|
-
|
|
|
-function* aWorker({timeout, message}){
|
|
|
- yield put({type: 'LOG', text: `aWorker started with ${timeout}`})
|
|
|
- yield delay(timeout)
|
|
|
- console.log(message)
|
|
|
-}
|
|
|
-
|
|
|
-function* actionCheck(){
|
|
|
- yield takeLeading('ACTION_A', aWorker)
|
|
|
-}
|
|
|
-
|
|
|
-function* searchWorker({text}){
|
|
|
- yield put(actionSearchResult({payload: null}))
|
|
|
- yield delay(2000)
|
|
|
-// let payload = yield delay(2000) //типа fetch
|
|
|
- let payload = yield gql.request(`
|
|
|
- query gf($query: String){
|
|
|
- GoodFind(query: $query){
|
|
|
- _id, name, description, price, images{
|
|
|
- _id, url
|
|
|
- }
|
|
|
+const reducers = {
|
|
|
+ promise(state={}, action){
|
|
|
+ if (['LOGOUT', 'LOGIN'].includes(action.type)) return {}
|
|
|
+ if (action.type === 'PROMISE'){
|
|
|
+ const { name="default", status, payload, error} = action
|
|
|
+ if (status){
|
|
|
+ return {
|
|
|
+ ...state, [name]: {status, payload: (status === 'PENDING' && state[name] && state[name].payload) || payload, error}
|
|
|
}
|
|
|
- }`, {query: JSON.stringify([
|
|
|
- {
|
|
|
- $or: [{name: `/${text}/`}, {description: `/${text}/`}] //регулярки пишутся в строках
|
|
|
- },
|
|
|
- {
|
|
|
- sort: [{name: 1}]} //сортируем по title алфавитно
|
|
|
- ])
|
|
|
- })
|
|
|
- yield put(actionSearchResult({payload}))
|
|
|
- console.log('search end' , text)
|
|
|
-}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return state;
|
|
|
+ },
|
|
|
+ //auth(){ //....
|
|
|
|
|
|
-function* searchCheck(){
|
|
|
- yield takeLatest('SEARCH', searchWorker)
|
|
|
+ //}
|
|
|
}
|
|
|
|
|
|
+let store = createStore(combineReducers(reducers), applyMiddleware(thunk))
|
|
|
|
|
|
-function* rootSaga(){
|
|
|
- yield all([
|
|
|
- actionCheck(),
|
|
|
- searchCheck()
|
|
|
- ])
|
|
|
-}
|
|
|
-
|
|
|
-sagaMiddleware.run(rootSaga)
|
|
|
-
|
|
|
-//store.subscribe(()=> console.log(store.getState())) // подписка на обновления store
|
|
|
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
|
|
|
|
|
|
-store.dispatch({type: 'ACTION_A', timeout: 5000, message: 'HELLO WORLD 5sec'})
|
|
|
-store.dispatch({type: 'ACTION_A', timeout: 2000, message: 'WORLD HELLO 2sec'})
|
|
|
|
|
|
export default store
|
|
|
|