function createStore(reducer) { let state = reducer(undefined, {}) //стартовая инициализация состояния, запуск редьюсера со state === undefined let cbs = [] //массив подписчиков const getState = () => state //функция, возвращающая переменную из замыкания const subscribe = cb => (cbs.push(cb), //запоминаем подписчиков в массиве () => cbs = cbs.filter(c => c !== cb)) //возвращаем функцию unsubscribe, которая удаляет подписчика из списка const dispatch = action => { if (typeof action === 'function') { //если action - не объект, а функция return action(dispatch, getState) //запускаем эту функцию и даем ей dispatch и getState для работы } const newState = reducer(state, action) //пробуем запустить редьюсер if (newState !== state) { //проверяем, смог ли редьюсер обработать action state = newState //если смог, то обновляем state for (let cb of cbs) cb() //и запускаем подписчиков } } return { getState, //добавление функции getState в результирующий объект dispatch, subscribe //добавление subscribe в объект } } function promiseReducer(state = {}, { type, name, status, payload, error }) { if (type === 'PROMISE') { return { ...state, [name]: { status, payload, error } } } return state } const actionPending = name => ({ type: 'PROMISE', status: 'PENDING', name }) const actionResolved = (name, payload) => ({ type: 'PROMISE', status: 'RESOLVED', name, payload }) const actionRejected = (name, error) => ({ type: 'PROMISE', status: 'REJECTED', name, error }) const actionPromise = (name, promise) => async dispatch => { dispatch(actionPending(name)) // 1. {delay1000: {status: 'PENDING'}} try { let payload = await promise dispatch(actionResolved(name, payload)) return payload } catch (error) { dispatch(actionRejected(name, error)) } } const getGQL = url => (query, variables = {}) => fetch(url, { //метод method: 'POST', headers: { //заголовок content-type "Content-Type": "application/json", ...(localStorage.authToken ? { "Authorization": "Bearer " + localStorage.authToken } : {}) }, //body с ключами query и variables body: JSON.stringify({ query, variables }) }) .then(res => res.json()) .then(data => { if (data.errors && !data.data) throw new Error(JSON.stringify(data.errors)) return data.data[Object.keys(data.data)[0]] }) const backURL = 'http://shop-roles.asmer.fs.a-level.com.ua' const gql = getGQL(`${backURL}/graphql`) const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms)) /********************************MY STUFFF START****************************************************** */ function jwtDecode(token) { try { let decoded = token.split('.') decoded = decoded[1] decoded = atob(decoded) decoded = JSON.parse(decoded) return decoded } catch (e) { return; } } function authReducer(state, { type, token }) { if (!state) { if (!localStorage.authToken) { console.log('NO-TOKEN') return {} } else { type = 'AUTH_LOGIN' token = localStorage.authToken } } if (type === 'AUTH_LOGIN') { console.log('AUTH-LOGIN') let decoded = jwtDecode(token) if (decoded) { localStorage.authToken = token return { token, payload: decoded } } } if (type === 'AUTH_LOGOUT') { console.log('AUTH-LOGOUT') localStorage.removeItem('authToken') return {} } return state } function combineReducers(reducers) { return (state = {}, action) => { const newState = {} for (const [reducerName, reducer] of Object.entries(reducers)) { let newSubState = reducer(state[reducerName], action) if (newSubState !== state[reducerName]) { newState[reducerName] = newSubState } } if (Object.keys(newState).length !== 0) { return { ...state, ...newState } } return state } } const combinedReducer = combineReducers({ promise: promiseReducer, auth: authReducer, cart: cartReducer }) const store = createStore(combinedReducer) const actionAuthLogin = token => ({ type: 'AUTH_LOGIN', token }) const actionAuthLogout = () => ({ type: 'AUTH_LOGOUT' }) //const store = createStore(authReducer) console.log(store.getState()) //стартовое состояние может быть с токеном store.subscribe(() => console.log(store.getState())) //ПЕРЕДЕЛАТЬ ОТОБРАЖЕНИЕ с поправкой на то, что теперь промисы не в корне state а в state.promise const actionLogin = (login, password) => actionPromise('login', gql(` query log($login:String, $password:String) { login(login: $login, password: $password) }`, { login, password })) const actionFullLogin = (login, password) => async dispatch => { let token = await dispatch(actionLogin(login, password)) if (token) { dispatch(actionAuthLogin(token)) } } const actionGetOrders = () => actionPromise('orders', gql(` query ord { OrderFind(query:"[{}]"){ _id orderGoods { price count total good{ name _id images { url } } } } } `)) //const actionRegister //actionPromise //const actionFullRegister = (login, password) => //actionRegister + actionFullLogin //+ интерфейс к этому - форму логина, регистрации, может повесить это на #/login #/register //+ #/orders показывает ваши бывшие заказы: //сделать actionMyOrders // let logBtn = document.getElementById('logBtn') /********************************MY STUFFF END****************************************************** */ /********************************MY STUFFF CART START****************************************************** */ function cartReducer(state = {}, { type, good = {}, count = 1 }) { //{ // _id1: {good, count} // _id2: {good, count} //} let { _id } = good const types = { CART_ADD() { //как CHANGE, только если ключ раньше был, то достать из count и добавить //к count из action. Если не было, достать 0 и добавить к count из action return { ...state, [_id]: { good, count: (state[_id]?.count || 0) + count } } }, CART_REMOVE() { //смочь скопировать объект и выкинуть ключ. как вариант через //деструктуризацию let { _id, ...newState } = state return newState }, CART_CHANGE() { return { ...state, //по аналогии с promiseReducer дописать [_id]: { good, count } } }, CART_CLEAR() { return {} }, } if (type in types) return types[type]() return state } //понаписывать action //прикрутить к товару кнопку которая делает store.dispatch(actionCartAdd(good)) const actionCartAdd = (good, count = 1) => ({ type: 'CART_ADD', good, count }) const actionCartDel = (good, count = 1) => ({ type: 'CART_REMOVE', good, count }) /********************************MY STUFFF CART END****************************************************** */ const actionRootCats = () => actionPromise('rootCats', gql(`query { CategoryFind(query: "[{\\"parent\\":null}]"){ _id name } }`)) const actionCatById = (_id) => //добавить подкатегории actionPromise('catById', gql(`query catById($q: String){ CategoryFindOne(query: $q){ _id name subCategories { _id name } goods { _id name price images { url } } } }`, { q: JSON.stringify([{ _id }]) })) store.dispatch(actionRootCats()) const actionGoodById = (_id) => actionPromise('goodById', gql(` query goodById ($good:String) { GoodFindOne(query: $good) { name description price categories { name } images { url } } }`, { good: JSON.stringify([{ _id }]) })) store.subscribe(() => { const { rootCats } = store.getState().promise if (rootCats?.payload) { aside.innerHTML = '' for (const { _id, name } of rootCats?.payload) { const link = document.createElement('a') link.href = `#/category/${_id}` link.innerText = name aside.append(link) } } }) window.onhashchange = () => { const [, route, _id] = location.hash.split('/') const routes = { category() { store.dispatch(actionCatById(_id)) }, good() { store.dispatch(actionGoodById(_id)) }, login() { let navBar = document.getElementById('navBar') navBar.style.visibility = 'hidden' let pass, log console.log('login') aside.innerHTML = '' main.innerHTML = `
${description}