Alyona Brytvina vor 2 Jahren
Ursprung
Commit
7fdae26eb0
1 geänderte Dateien mit 61 neuen und 74 gelöschten Zeilen
  1. 61 74
      HW17/main.js

+ 61 - 74
HW17/main.js

@@ -94,39 +94,35 @@ const actionRootCats = () =>
 
 const actionCatById = (_id) =>  //добавить подкатегории
     actionPromise('catById', gql(`query catById($q: String){
-      CategoryFindOne(query: $q){
-            _id name goods {
-                _id name price description images {
+        CategoryFindOne(query: $q){
+            _id 
+            name
+            subCategories {
+                _id name
+            } 
+            goods {
+                _id name price images {
                     url
                 }
-            } subCategories{
-      _id name image{
-        url _id text
-      }
-    }
+            }
         }
     }`, {q: JSON.stringify([{_id}])}));
-// console.log(store.dispatch(actionCatById()))
-
-
-const actionGoodById = (_id) =>  //добавить подкатегории
-    actionPromise('goodById', gql(`query goodById($q: String){
-     CategoryFind(query: $q){
-            _id name image{
-              url text _id
-            } subCategories{
-              _id name image{
-                url _id text
-              } subCategories{
-                _id name image{
-                  url text _id
+
+const actionGoodById = (_id) =>
+    actionPromise('goodById', gql(`
+        query goodById ($good:String) {
+            GoodFindOne(query: $good) {
+                name 
+                description 
+                price
+                categories {
+                    name
+                } 
+                images {
+                    url
                 }
-              }
             }
-        }
-    } `, {q: JSON.stringify([{_id}])}));
-
-// store.dispatch(actionGoodById());
+        }`, {good: JSON.stringify([{_id}])}));
 
 store.dispatch(actionRootCats());
 
@@ -153,7 +149,7 @@ window.onhashchange = () => {
 
         category() {
             store.dispatch(actionCatById(_id));
-            console.log('ltjjj');
+            console.log('КАТЕГОРИЕСТРАНИЦА');
         },
         good() { //задиспатчить actionGoodById
             store.dispatch(actionGoodById(_id));
@@ -170,75 +166,66 @@ window.onhashchange = () => {
 window.onhashchange();
 
 store.subscribe(() => {
-    const {catById} = store.getState();
-    const [, route, _id] = location.hash.split('/');
+        const {catById} = store.getState();
+        const [, route, _id] = location.hash.split('/');
 
-    if (catById?.payload && route === 'category') {
+        if (catById?.payload && route === 'category') {
 
-        const {name} = catById.payload;
-        main.innerHTML = `<h1>${name}</h1> `;
+            const {name} = catById.payload;
+            main.innerHTML = `<h1>${name}</h1> `;
 
-        if (catById.payload.subCategories) {
-            for (let good of catById.payload.subCategories) {
-                const magic = `#/good/${good._id}`;
-                console.log(magic)
-                main.innerHTML += `<h2>${good.name}</h2>`;
-                main.innerHTML += `<a href="${magic}">${magic}</a>`;
+            if (catById.payload.subCategories) {
+                console.log('тут подкатегории');
+                for (let good of catById.payload.subCategories) {
+                    const magic = `#/category/${good._id}`;
+                    main.innerHTML += `<h2>${good.name}</h2>`;
+                    main.innerHTML += `<a href="${magic}">${magic}</a>`;
 
+                }
             }
-        } else {
 
-            for (const {_id, name, price, images} of catById.payload.goods) {
-                const card = document.createElement('div');
+            if (catById.payload.goods) {
+                for (const {_id, name, price, images} of catById.payload.goods) {
+                    const card = document.createElement('div');
 
-                const link = document.createElement('a');
-                const magic = `#/good/${_id}`;
+                    const link = document.createElement('a');
 
-                card.innerHTML = `<h2>${name}</h2>
+                    card.innerHTML = `<h2>${name}</h2>
                               <img src="${backURL}/${images[0].url}" />
                               <strong>${price}</strong>
-                              <a href="${magic}">${magic}</a>
+                              <a href="#/good/${_id}">Подробнее</a>
                                 `;
-                aside.append(link);
-                main.append(card);
+                    aside.append(link);
+                    main.append(card);
+                }
             }
         }
     }
-})
+)
 ;
 
 store.subscribe(() => {
-    const {catById, goodById} = store.getState();
-    const [, route, idGood] = location.hash.split('/');
-    if (goodById?.payload && route === 'good'){
-        if (location.hash.indexOf(`#/good/${idGood}`) !== -1) {
-            console.log('нужная адресная строка');
-            main.innerHTML = '';
-            if(catById.payload?.subCategories){
-                let searchGood = catById.payload.subCategories.find(good => good._id === idGood);
-                const card = document.createElement('div');
-                console.log(goodById.payload[0].subCategories)
-                card.innerHTML = `<h2>${searchGood.name}</h2>`;
-                goodById.payload[0].subCategories.forEach(good => {
-                    card.innerHTML += `<h2>${good.name}</h2>`
-                    main.append(card);
-                })
-                main.append(card);
-            }else{
-                let searchGood = catById.payload?.goods.find(good => good._id === idGood);
+        const {goodById} = store.getState();
+        console.log('я на странице описания товара');
+        const [, route, _id] = location.hash.split('/');
+        if (goodById?.payload && route === 'good') {
+
+            if (location.hash.indexOf(`#/good/${_id}`) !== -1) {
+                main.innerHTML = '';
+
+                const {_id, name, price, images, description} = goodById.payload;
                 const card = document.createElement('div');
-                card.innerHTML = `<h2>${searchGood.name}</h2>
-                      <img src="${backURL}/${searchGood.images[0].url}" />
-                       <strong>${searchGood.price}</strong>
-                       <div>${searchGood.description}</div>
+                card.innerHTML = `<h2>${name}</h2>
+                      <img src="${backURL}/${images[0].url}" />
+                       <strong>${price}</strong>
+                       <div>${description}</div>   
                  `;
                 main.append(card);
             }
         }
     }
-
-
-});
+)
+;
 
 //store.dispatch(actionPromise('delay1000', delay(1000)))
 //store.dispatch(actionPromise('delay2000', delay(2000)))