How to eliminate mirroring the records?

How to eliminate mirroring the records?


0

I’ve got GraphQl code like this :

const queryGraphQL = {
    query: `query {
  products(searchInput: {
    parameters: [{
      id: 915
      values:[4095,4170]
    }
    ]

  },
          settingsInput: {limit:35 }) {
    products {
      producer{
        link
        name
        id
      }
      awardedParameters {
        id
        name
        values {
          name
          link
        }
      }

    }
  }
}`,
}
const fetchData = async () => {
    try {
        const response = await fetch('/graphql/v1/', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/json',
            },
            body: JSON.stringify(queryGraphQL),
        })
        const dataJson = await response.json()
        return dataJson
    } catch (error) {
        console.error('AJAX fetchDataGraphQL() Error:', error)
        return false
    }
}

const init = async () => {
    const data = await fetchData()

    if(!data) return
    
    const products = data.data.products.products
    const upcomingContainer = document.querySelector('#main_cms .products.--onstore')
    
    if(!upcomingContainer) return
    console.log(products)
    const html = products.reduce((prev, curr)=>{
      const date = curr.awardedParameters.find(el=>el.name === 'Kolejna dostawa').values[0].name
      const url = document.querySelector(`.main_producers__wrapper div[id="${curr.producer.id}"]`).dataset['src']
      const link3 = curr.awardedParameters.find(el=>el.name === 'Kolejna dostawa').values[0].link 
      
      if (!url) return ''
      
      return prev + `<a href="${link3}?filter_producer=${curr.producer.id}" class="col-12 product_link">
      <div class="product mb-2">
          <span class="product__icon_2"><img src="${url}" alt=${curr.producer.name}/></span>
          <span class="producer__name">${curr.producer.name}</span>
          <div class="off_store"></div>
          
          <div class="product__date">${date}</div>
      </div>
      </a>`
    },'')
   upcomingContainer.innerHTML = html
    return data
}

I’m building the template structure with this HTML code :

<div class="products --onstore d-flex flex-wrap mt-md-5">
    <a href="" class="col-12 product_link">
        <div class="product col-12 mb-2">
            <span class="product__icon_2 skeleton skeleton-image"><img src=""></img></span>
            <span class="producer__name skeleton skeleton-producer">
                 <span><iai:variable vid="Dostawa marki"/></span>
            </span>
            <div class="on_store skeleton skeleton-store">
                <span><iai:variable vid="Na magazynie"/></span>
            </div>
            <div class="product__date skeleton skeleton-date"></div>
        </div>
     </a>
</div>

And my question is, if there is any option to eliminate mirroring the records ? I mean if I have 3 the same result, for example 3

product__date
-------------------
product 1 12.12.22.
product 2 12.12.22.
product 3 12.12.22.

I want to show only one product with date 12.12.22r. and search for another records with different dates ?

I tried some kind of sort by date, byb this doesn’t work. Any ideas how should it looks like?

1

  • 1

    Use group by on date to get DISTINCT values (idea from SQL). In GraphQL, use this group.

    – Jishan Shaikh

    Dec 12, 2022 at 6:29


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *