123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- import React, { useState } from "react";
- import "./App.css";
- import thunk from "redux-thunk";
- import { createStore, combineReducers, applyMiddleware } from "redux";
- import { Provider, connect } from "react-redux";
- 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 combineReducers(reducers) {
- return (state={}, action) => {
- const newState = {}
- // перебрать все редьюсеры
- if (reducers) {
- for (const [reducerName, reducer] of Object.entries(reducers)) {
- const newSubState = reducer(state[reducerName], action)
- if (newSubState !== state[reducerName]) {
- newState[reducerName] = newSubState
- }
- }
- // если newState не пустой, то вернуть стейт в
- if (Object.keys(newState).length !== 0) {
- return {...state, ...newState}
- } else {
- return state
- }
- }
- }
- }
- const combinedReducer = combineReducers({promise: promiseReducer, auth: authReducer, cart: cartReducer})
- const store = createStore(combinedReducer)
- store.subscribe(() => console.log(store.getState()))
- function jwtDecode(token){
- try {
- return JSON.parse(atob(token.split('.')[1]))
- }
- catch(e){
- }
- }
- function authReducer(state, {type, token}) {
- if (!state) {
- if (localStorage.authToken) {
- token = localStorage.authToken
- type = 'AUTH_LOGIN'
- } else {
- return {}
- }
- }
- if (type === 'AUTH_LOGIN') {
- let payload = jwtDecode(token)
- if (typeof payload === 'object') {
- localStorage.authToken = token
- return {
- ...state,
- token,
- payload
- }
- } else {
- return state
- }
- }
- if (type === 'AUTH_LOGOUT') {
- delete localStorage.authToken
- location.reload()
- return {}
- }
- return state
- }
- const actionAuthLogin = (token) => ({type: 'AUTH_LOGIN', token})
- const actionAuthLogout = () => ({type: 'AUTH_LOGOUT'})
- function cartReducer (state={}, {type, good={}, count=1}) {
- if (Object.keys(state).length === 0 && localStorage.cart) {
- let currCart = JSON.parse(localStorage.cart)
- if (currCart && Object.keys(currCart).length !== 0) {
- state = currCart
- }
- }
- const {_id} = good
- const types = {
- CART_ADD() {
- count = +count
- if (!count) {
- return state
- }
- let newState = {
- ...state,
- [_id]: {good, count: (count + (state[_id]?.count || 0)) < 1 ? 1 : count + (state[_id]?.count || 0)}
- }
- localStorage.cart = JSON.stringify(newState)
- return newState
- },
- CART_CHANGE() {
- count = +count
- if (!count) {
- return state
- }
- let newState = {
- ...state,
- [_id]: {good, count: count < 0 ? 0 : count}
- }
- localStorage.cart = JSON.stringify(newState)
- return newState
- },
- CART_REMOVE() {
- let { [_id]: removed, ...newState } = state
- localStorage.cart = JSON.stringify(newState)
- return newState
- },
- CART_CLEAR() {
- localStorage.cart = JSON.stringify({})
- return {}
- },
- }
- if (type in types) {
- return types[type]()
- }
- return state
- }
- const actionCartAdd = (good, count) => ({type: 'CART_ADD', good, count})
- const actionCartChange = (good, count) => ({type: 'CART_CHANGE', good, count})
- const actionCartRemove = (good) => ({type: 'CART_REMOVE', good})
- const actionCartClear = () => ({type: 'CART_CLEAR'})
- function promiseReducer(state={}, {type, status, payload, error, name}) {
- if (!state) {
- return {}
- }
- if (type === 'PROMISE') {
- return {
- ...state,
- [name]: {
- status: status,
- payload : payload,
- error: 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))
- try {
- let data = await promise
- dispatch(actionResolved(name, data))
- return data
- }
- catch(error){
- dispatch(actionRejected(name, error))
- }
- }
- )
- const getGQL = url => (
- async (query, variables={}) => {
- let obj = await fetch(url, {
- method: 'POST',
- headers: {
- "Content-Type": "application/json",
- ...(localStorage.authToken ? {Authorization: "Bearer " + localStorage.authToken} : {})
- },
- body: JSON.stringify({ query, variables })
- })
- let a = await obj.json()
- if (!a.data && a.errors) {
- throw new Error(JSON.stringify(a.errors))
- } else {
- return a.data[Object.keys(a.data)[0]]
- }
- }
- )
- const backURL = 'http://shop-roles.node.ed.asmer.org.ua/'
- const gql = getGQL(backURL + 'graphql');
- const actionOrder = () => (
- async (dispatch, getState) => {
- let {cart} = getState()
- const orderGoods = Object.entries(cart)
- .map(([_id, {good, count}]) => ({good: {_id}, count}))
- let result = await dispatch(actionPromise('order', gql(`
- mutation newOrder($order:OrderInput){
- OrderUpsert(order:$order)
- { _id total}
- }
- `, {order: {orderGoods}})))
- if (result?._id) {
- dispatch(actionCartClear())
- }
- })
- 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))
- location.hash = '#/category'
- } else {
- showErrorMessage('please, enter correct login and password', main)
- }
- }
- )
- const actionRegister = (login, password) => (
- actionPromise('register', gql(`mutation reg($user:UserInput) {
- UserUpsert(user:$user) {
- _id
- }
- }
- `, {user: {login, password}})
- )
- )
- const actionFullRegister = (login, password) => (
- async (dispatch) => {
- let registerId = await dispatch(actionRegister(login, password))
- if (registerId) {
- dispatch(actionFullLogin(login, password))
- }
- }
- )
- 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 goods {
- _id name price images {
- url
- }
- }
- subCategories {
- _id name
- }
- }
- }`, {q: JSON.stringify([{_id}])}))
- )
- const actionGoodById = (_id) => (
- actionPromise('goodById', gql(`query goodById($q: String) {
- GoodFindOne(query: $q) {
- _id name price description images {
- url
- }
- }
- }`, {q: JSON.stringify([{_id}])}))
- )
- const actionGoodsByUser = (_id) => (
- actionPromise('goodByUser', gql(`query oUser($query: String) {
- OrderFind(query:$query){
- _id orderGoods{
- price count total good{
- _id name categories{
- name
- }
- images {
- url
- }
- }
- }
- owner {
- _id login
- }
- }
- }`,
- {query: JSON.stringify([{___owner: _id}])}))
- )
- store.subscribe(() => {
- const {promise, auth} = store.getState()
- const {rootCats} = promise
- if (rootCats?.status === 'PENDING') {
- aside.innerHTML = `<img src="Loading_icon.gif">`
- } else {
- if (rootCats?.payload) {
- aside.innerHTML = ''
- authBox.innerHTML = ''
- const regBtn = document.createElement('a')
- regBtn.href = '#/register'
- regBtn.innerText = 'Register'
- const loginBtn = document.createElement('a')
- loginBtn.className = 'loginBtn'
- loginBtn.href = `#/login`
- loginBtn.innerText = 'Login'
- const logoutBtn = document.createElement('a')
- logoutBtn.innerText = 'Logout'
- auth.token ? authBox.append(logoutBtn) : authBox.append(regBtn, loginBtn)
- logoutBtn.onclick = () => {
- store.dispatch(actionAuthLogout())
- }
- for (const {_id, name} of rootCats?.payload) {
- const link = document.createElement('a')
- link.href = `#/category/${_id}`
- link.innerText = name
- aside.append(link)
- }
- }
- }
- })
- store.dispatch(actionRootCats())
- function createForm(parent, type, callback) {
- let {auth} = store.getState()
- let res = `<label for="login${type}">Nick</label>
- <input id="login${type}" type="text"/>
- <label for="pass${type}">Password</label>
- <input id="pass${type}" type="password"/>
-
- <button id="btn${type}">${type}</button>
- </div>`
- parent.innerHTML = res
- return () => window[`btn${type}`].onclick = () => {
- store.dispatch(callback(window[`login${type}`].value, window[`pass${type}`].value))
- }
- }
- let message = document.createElement('p')
- function showErrorMessage(text, parent) {
- message.innerHTML = text
- parent.append(message)
- }
- const createCartPage = (parent) => {
- parent.innerHTML = ''
- const {cart} = store.getState()
- const clearBtn = document.createElement('button')
- clearBtn.innerText = "clear all"
- if(Object.keys(cart).length !== 0) {
- parent.append(clearBtn)
- }
- clearBtn.onclick = () => {
- store.dispatch(actionCartClear())
- }
- const cartPage = document.createElement('div')
- if(Object.keys(cart).length === 0) {
- showErrorMessage('Hmm... Let`s add something into the cart!', cartPage)
- }
- main.append(cartPage)
- let cartCounter = 0
- for(const item in cart) {
- const {good} = cart[item]
- const {count, good: {_id: id, name: name, price: price, images: [{url}]}} = cart[item]
- cartCounter += count*price
- const card = document.createElement('div')
- card.innerHTML = `
- <h4>${name}</h4>
- </div>
- <img src="${backURL}/${url}" />
- <p>amount: </p>
- `
- const inputGr = document.createElement('div')
- card.lastElementChild.append(inputGr)
- const minusBtn = document.createElement('button')
- minusBtn.innerText = '-'
- inputGr.append(minusBtn)
- minusBtn.onclick = () => {
- store.dispatch(actionCartAdd(good, -1))
- }
- const changeCount = document.createElement('input')
- changeCount.type = 'number'
- changeCount.value = count
- changeCount.setAttribute('min', '1')
- inputGr.append(changeCount)
- changeCount.oninput = () => {
- store.dispatch(actionCartChange(good, changeCount.value))
- }
- const plusBtn = document.createElement('button')
- plusBtn.innerText = '+'
- inputGr.append(plusBtn)
- plusBtn.onclick = () => {
- store.dispatch(actionCartAdd(good))
- }
- const deleteGood = document.createElement('button')
- deleteGood.innerText = 'remove item'
- deleteGood.style.display = 'block'
- card.lastElementChild.append(deleteGood)
- deleteGood.onclick = () => {
- store.dispatch(actionCartRemove(good))
- }
- cartPage.append(card)
- }
- const total = document.createElement('h5')
- total.innerText = `Total: ${cartCounter} UAH`
- const sendOrder = document.createElement('button')
- sendOrder.innerText = 'Make an order'
- if(Object.keys(cart).length !== 0) {
- parent.append(total)
- parent.append(sendOrder)
- }
- const {auth} = store.getState()
- sendOrder.disabled = !auth.token;
- sendOrder.onclick = () => {
- store.dispatch(actionOrder())
- }
- }
- // location.hash
- window.onhashchange = () => {
- const [,route, _id] = location.hash.split('/')
- const routes = {
- category(){
- store.dispatch(actionCatById(_id))
- },
- good(){
- store.dispatch(actionGoodById(_id))
- },
- register(){
- const registerFunc = createForm(main, 'Register', actionFullRegister)
- registerFunc()
- },
- login(){
- const loginFunc = createForm(main, 'Login', actionFullLogin)
- loginFunc()
- },
- orders(){
- store.dispatch(actionGoodsByUser(_id))
- },
- cart(){
- createCartPage(main)
- }
- }
- if (route in routes) {
- routes[route]()
- }
- }
- store.subscribe(() => {
- const [,route] = location.hash.split('/')
- if (route === 'cart') {
- createCartPage(main)
- }
- })
- window.onhashchange()
- store.subscribe(() => {
- const {promise} = store.getState()
- const {catById} = promise
- const [,route, _id] = location.hash.split('/')
- if (catById?.status === 'PENDING') {
- main.innerHTML = `<img src="Loading_icon.gif">`
- } else {
- if (catById?.payload && route === 'category'){
- main.innerHTML = ''
- const catBody = document.createElement('div')
- main.append(catBody)
- const {name} = catById.payload;
- catBody.innerHTML = `<h1>${name}</h1>`
- if (catById.payload.subCategories) {
- const linkList = document.createElement('div')
- catBody.append(linkList)
- for(const {_id, name} of catById.payload.subCategories) {
- const link = document.createElement('a')
- link.href = `#/category/${_id}`
- link.innerText = name
- link.className = 'cat'
- catBody.append(link)
- }
- if(location.hash === '#/category/') {
- for(const {_id, name} of catById.payload) {
- const link = document.createElement('a')
- link.href = `#/category/${_id}`
- link.innerText = name
- link.className = 'cat'
- catBody.append(link)
- }
- }
- }
- if (catById.payload.goods) {
- const cardBody = document.createElement('div')
- main.append(cardBody)
- for (const good of catById.payload.goods){
- const {_id, name, price, images} = good
- const card = document.createElement('div')
- card.className = 'card'
- card.innerHTML = `
- <img src="${backURL}/${images[0].url}" />
- <div>
- <h4>${name}</h4>
- <h5>${price} UAH</h5>
- <a href="#/good/${_id}" class="showMore">
- Show more
- </a>
- </div>
- `
- const btnCart = document.createElement('button')
- btnCart.innerText = 'To cart'
- btnCart.onclick = () => {
- store.dispatch(actionCartAdd(good))
- }
- card.lastElementChild.append(btnCart)
- cardBody.append(card)
- }
- }
- }
- }
- })
- store.subscribe(() => {
- const {promise} = store.getState()
- const {goodById} = promise
- const [,route, _id] = location.hash.split('/');
- if (goodById?.status === 'PENDING') {
- main.innerHTML = `<img src="Loading_icon.gif">`
- } else {
- if (goodById?.payload && route === 'good') {
- main.innerHTML = ''
- const good = goodById.payload
- const {_id, name, images, price, description} = good
- const card = document.createElement('div')
- card.innerHTML = `<h2>${name}</h2>
- <img src="${backURL}/${images[0].url}" />
- <div>
- <h6>${description}</h6>
- <strong>Цена - ${price} грн</strong>
- </div>
- `
- const btnCart = document.createElement('button')
- btnCart.innerText = 'Add to cart'
- btnCart.onclick = () => {
- store.dispatch(actionCartAdd(good))
- }
- card.append(btnCart)
- main.append(card);
- }
- }
- }
- )
- store.subscribe(() => {
- const {auth} = store.getState()
- const name = document.createElement('div')
- name.innerText = `Hello, stranger`
- const {payload} = auth
- if (payload?.sub) {
- userBox.innerHTML = ''
- const {id, login} = payload.sub
- name.innerText = `Hello, ${login}`
- const myOrders = document.createElement('a')
- myOrders.innerText = 'My orders'
- myOrders.href = `#/orders/${id}`
- userBox.append(myOrders)
- } else {
- userBox.innerHTML = ''
- }
- userBox.append(name)
- })
- store.subscribe(() => {
- const {promise} = store.getState()
- const {goodByUser} = promise
- const [,route] = location.hash.split('/')
- if (goodByUser?.status === 'PENDING') {
- main.innerHTML = `<img src="Loading_icon.gif">`
- } else {
- if (goodByUser?.payload && route === 'orders'){
- main.innerHTML = ''
- const cardBody = document.createElement('div')
- main.append(cardBody)
- if (goodByUser.payload) {
- let totalMoney = 0
- for (const order of goodByUser.payload) {
- if (order.orderGoods) {
- for (const {price, count, total, good} of order.orderGoods) {
- if (price !== null && count !== null && total !== null && good !== null) {
- totalMoney += total
- const {_id, name, images} = good
- const card = document.createElement('div')
- card.innerHTML = `
- <img src="${backURL}/${images[0].url}" />
- <div>
- <h4>${name}</h4>
- // <h6>
- // bought: ${count}, ${price} UAH
- // </h6>
- <h6>
- Total: ${total} UAH
- </h6>
- <a href="#/good/${_id}">
- show more
- </a>
- </div>
- `
- cardBody.append(card)
- }
- }
- }
- }
- const totalBlock = document.createElement('h3')
- totalBlock.innerText = 'Total: ' + totalMoney + ' UAH'
- main.append(totalBlock)
- }
- }
- }
- })
- store.subscribe(() => {
- const {cart} = store.getState()
- let counter = 0;
- for (const key in cart) {
- counter += cart[key].count
- }
- cartCounter.innerText = counter
- })
|