|
@@ -106,27 +106,43 @@ const getGQL = url =>
|
|
|
|
|
|
const gql = getGQL(backURL + 'graphql');
|
|
|
|
|
|
-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
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }`, {q: JSON.stringify([{_id}])}))
|
|
|
+
|
|
|
+ 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}])}))
|
|
|
|
|
|
//actionGoodById по аналогии
|
|
|
|
|
|
+const actionGoodById = (_id) =>
|
|
|
+ actionPromise('goodById', gql(`query goodById($q: String) {
|
|
|
+ GoodFindOne(query: $q) {
|
|
|
+ _id name price description images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`, {q: JSON.stringify([{_id}])}))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
store.dispatch(actionRootCats())
|
|
|
+
|
|
|
store.subscribe(() => {
|
|
|
const {rootCats} = store.getState()
|
|
|
if (rootCats?.payload) {
|
|
@@ -140,54 +156,96 @@ store.subscribe(() => {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+
|
|
|
// location.hash - адресная строка после решетки
|
|
|
window.onhashchange = () => {
|
|
|
- const [, route, _id] = location.hash.split('/')
|
|
|
+ const [,route, _id] = location.hash.split('/')
|
|
|
+
|
|
|
const routes = {
|
|
|
- category() {
|
|
|
+ category(){
|
|
|
store.dispatch(actionCatById(_id))
|
|
|
+ console.log('страница категорий')
|
|
|
},
|
|
|
- good() {
|
|
|
- //задиспатчить actionGoodById
|
|
|
+ good(){ //задиспатчить actionGoodById
|
|
|
+ store.dispatch(actionGoodById(_id))
|
|
|
+ console.log('страница товара')
|
|
|
},
|
|
|
}
|
|
|
- if (route in routes) {
|
|
|
+ if (route in routes)
|
|
|
routes[route]()
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-// запускает обработчик при загрузке страницы
|
|
|
window.onhashchange()
|
|
|
|
|
|
-
|
|
|
store.subscribe(() => {
|
|
|
const {catById} = store.getState()
|
|
|
const [,route, _id] = location.hash.split('/')
|
|
|
if (catById?.payload && route === 'category'){
|
|
|
- const {name} = catById.payload
|
|
|
- main.innerHTML = `<h1>${name}</h1> ТУТ ДОЛЖНЫ БЫТЬ ПОДКАТЕГОРИИ`
|
|
|
- for (const {_id, name, price, images} of catById.payload.goods){
|
|
|
- const card = document.createElement('div')
|
|
|
- card.innerHTML = `<h2>${name}</h2>
|
|
|
- <img src="${backURL}/${images[0].url}" />
|
|
|
- <strong>${price}</strong>
|
|
|
- ТУТ ДОЛЖНА БЫТЬ ССЫЛКА НА СТРАНИЦУ ТОВАРА
|
|
|
- ВИДА #/good/АЙДИ
|
|
|
- `
|
|
|
- main.append(card)
|
|
|
+ const {name} = catById.payload;
|
|
|
+ main.innerHTML = `<h1>${name}</h1>`
|
|
|
+
|
|
|
+ if (catById.payload.subCategories) {
|
|
|
+ for(const {_id, name} of catById.payload.subCategories) {
|
|
|
+ const link = document.createElement('a');
|
|
|
+ link.href = `#/category/${_id}`;
|
|
|
+ link.innerText = name;
|
|
|
+ main.append(link);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (catById.payload.goods) {
|
|
|
+ for (const {_id, name, price, images} of catById.payload.goods){
|
|
|
+ const card = document.createElement('div')
|
|
|
+ card.innerHTML = `<h2>${name}</h2>
|
|
|
+ <img src="${backURL}/${images[0].url}" />
|
|
|
+ <strong>${price}</strong>
|
|
|
+ <br>
|
|
|
+ <a href="#/good/${_id}">Перейти на страницу товара</a>
|
|
|
+ `
|
|
|
+ main.append(card)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
|
|
|
-
|
|
|
store.subscribe(() => {
|
|
|
+ const {goodById} = store.getState();
|
|
|
+ const [,route, _id] = location.hash.split('/');
|
|
|
+ if (goodById?.payload && route === 'good') {
|
|
|
+ main.innerHTML = '';
|
|
|
+ const {_id, name, images, price, description} = goodById.payload;
|
|
|
+ const card = document.createElement('div');
|
|
|
+ card.innerHTML = `<h2>${name}</h2>
|
|
|
+ <img src="${backURL}/${images[0].url}" />
|
|
|
+ <strong>${price}</strong>
|
|
|
+ <h2>${description}</h2>
|
|
|
+ `;
|
|
|
+ main.append(card);
|
|
|
+ }
|
|
|
+ }
|
|
|
//ТУТ ДОЛЖНА БЫТЬ ПРОВЕРКА НА НАЛИЧИЕ goodById в редакс
|
|
|
//и проверка на то, что сейчас в адресной строке адрес ВИДА #/good/АЙДИ
|
|
|
//в таком случае очищаем main и рисуем информацию про товар с подробностями
|
|
|
-})
|
|
|
+)
|
|
|
|
|
|
// store.dispatch(actionPromise('delay1000', delay(1000)))
|
|
|
// store.dispatch(actionPromise('delay2000', delay(2000)))
|
|
|
// store.dispatch(actionPromise('luke', fetch('https://swapi.dev/api/people/1/').then(res => res.json())))
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|