|
@@ -34,9 +34,6 @@ function promiseReducer(state = {}, { type, name, status, payload, error }) {
|
|
|
return state
|
|
|
}
|
|
|
|
|
|
-const store = createStore(promiseReducer)
|
|
|
-store.subscribe(() => console.log('state of store:', store.getState()))
|
|
|
-
|
|
|
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 })
|
|
@@ -80,6 +77,98 @@ 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})
|
|
|
+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(`ЗАПРОС НА ЛОГИН`, {login ,password}))
|
|
|
+
|
|
|
+
|
|
|
+const actionFullLogin = (login, password) =>
|
|
|
+ async dispatch => {
|
|
|
+ let token = await dispatch(actionLogin(login, password))
|
|
|
+ if (token){
|
|
|
+ dispatch(actionAuthLogin(token))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+//const actionRegister //actionPromise
|
|
|
+//const actionFullRegister = (login, password) => //actionRegister + actionFullLogin
|
|
|
+//+ интерфейс к этому - форму логина, регистрации, может повесить это на #/login #/register
|
|
|
+//+ #/orders показывает ваши бывшие заказы:
|
|
|
+ //сделать actionMyOrders
|
|
|
+ //
|
|
|
+
|
|
|
+/********************************MY STUFFF END****************************************************** */
|
|
|
const actionRootCats = () =>
|
|
|
actionPromise('rootCats', gql(`query {
|
|
|
CategoryFind(query: "[{\\"parent\\":null}]"){
|
|
@@ -123,8 +212,7 @@ const actionGoodById = (_id) =>
|
|
|
|
|
|
|
|
|
store.subscribe(() => {
|
|
|
- //console.log(location)
|
|
|
- const { rootCats } = store.getState()
|
|
|
+ const { rootCats } = store.getState().promise
|
|
|
|
|
|
if (rootCats?.payload) {
|
|
|
aside.innerHTML = ''
|
|
@@ -144,7 +232,7 @@ window.onhashchange = () => {
|
|
|
category() {
|
|
|
store.dispatch(actionCatById(_id))
|
|
|
},
|
|
|
- good() { //задиспатчить actionGoodById
|
|
|
+ good() {
|
|
|
store.dispatch(actionGoodById(_id))
|
|
|
},
|
|
|
}
|
|
@@ -155,9 +243,7 @@ window.onhashchange()
|
|
|
|
|
|
|
|
|
store.subscribe(() => {
|
|
|
- //console.log(location)
|
|
|
- //console.log('drawing cat list')
|
|
|
- const { catById } = store.getState()
|
|
|
+ const { catById } = store.getState().promise
|
|
|
const [, route, _id] = location.hash.split('/')
|
|
|
|
|
|
if (catById?.payload && route === 'category') {
|
|
@@ -203,8 +289,7 @@ store.subscribe(() => {
|
|
|
})
|
|
|
|
|
|
store.subscribe(() => {
|
|
|
- //console.log('drawing page of good')
|
|
|
- const { goodById } = store.getState()
|
|
|
+ const { goodById } = store.getState().promise
|
|
|
const [, route, _id] = location.hash.split('/')
|
|
|
|
|
|
if (goodById?.payload && route === 'good') {
|
|
@@ -227,5 +312,5 @@ store.subscribe(() => {
|
|
|
})
|
|
|
|
|
|
window.onload = () => {
|
|
|
- location.href = location.href + "#/category/5dc49f4d5df9d670df48cc64"
|
|
|
+ location.href = "#/category/5dc49f4d5df9d670df48cc64"
|
|
|
}
|