summaryrefslogtreecommitdiff
path: root/src/pages/google_merchant/products/index.js
blob: d3cdc5140130119f3aaf820c252d21200d2c172d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import productSearchApi from '@/lib/product/api/productSearchApi'
import variantSearchApi from '@/lib/product/api/variantSearchApi'
import _ from 'lodash-contrib'

const limit = 5000

export async function getServerSideProps() {
  const queries = {
    limit: 1,
    priceFrom: 1,
    fq: 'image_s:["" TO *]'
  }
  const products = await variantSearchApi({ query: _.toQuery(queries) })
  const { numFound } = products.response
  const pageTotal = Math.ceil(numFound / limit)

  return { props: { pageTotal, numFound } }
}

export default function GoogleMerchantPage({ pageTotal, numFound }) {
  const numberArray = Array.from({ length: pageTotal }, (_, index) => index)
  return (
    <div className='grid grid-cols-1 gap-y-3 p-4'>
      <h1 className='text-h-lg font-semibold mb-3'>Google Merchant:</h1>
      {numberArray.map((number) => {
        const currentPage = number + 1
        const remainingProducts = numFound - limit * number
        const productCount = currentPage == pageTotal ? remainingProducts : limit
        return (
          <a
            key={number}
            href={`/google_merchant/products/${currentPage}.xml`}
            className='block font-medium text-indigo-600'
            target='_blank'
            rel='noreferrer'
          >
            Page {currentPage} - ({productCount}) Products
          </a>
        )
      })}
    </div>
  )
}