11import React , { useEffect } from "react" ;
2+ import { useGlobalStore , useFPI } from "fdk-core/utils" ; // Importing hooks from fdk-core utilities
3+ import { Helmet } from "react-helmet-async" ; // Importing Helmet for managing changes to the document head
4+ import styles from "../styles/style.css" ; // Importing CSS styles
5+ import { ProductCard } from "../components/ProductCard" ; // Importing the ProductCard component
26
3- import { useGlobalStore , useFPI } from "fdk-core/utils" ;
4- import { Helmet } from "react-helmet-async" ;
5- import styles from "../styles/style.css" ;
6- import { ProductCard } from "../components/ProductCard" ;
7+ // GraphQL query as a template literal string to fetch products with dynamic variables
8+ const PLP_PRODUCTS = `query products(
9+ $first: Int
10+ $pageNo: Int
11+ $after: String
12+ $pageType: String
13+ ) {
14+ products(
15+ first: $first
16+ pageNo: $pageNo
17+ after: $after
18+ pageType: $pageType
19+ ) {
20+ page {
21+ current
22+ next_id
23+ has_previous
24+ has_next
25+ item_total
26+ type
27+ size
28+ }
29+ items {
30+ price {
31+ effective {
32+ currency_code
33+ currency_symbol
34+ max
35+ min
36+ }
37+ }
38+ media {
39+ alt
40+ type
41+ url
42+ }
43+ slug
44+ name
45+ }
46+ }
47+ }
48+ ` ;
749
8- export function Component ( { props } ) {
9- const fpi = useFPI ( ) ;
10- const products = useGlobalStore ( fpi . getters . PRODUCTS ) ;
50+ // Functional component definition using destructuring to access props
51+ export function Component ( { title = 'Extension Title Default' } ) {
52+ const fpi = useFPI ( ) ; // Using a custom hook to access functional programming interface
53+ const products = useGlobalStore ( fpi . getters . PRODUCTS ) ; // Accessing global store to retrieve products
54+ const productItems = products ?. items ?? [ ] ; // Nullish coalescing operator to handle undefined products
1155
12- const productItems = products ?. data ?. items ?? [ ] ;
56+ // useEffect to perform side effects, in this case, data fetching
1357 useEffect ( ( ) => {
58+ // Checking if productItems is empty and then executing GraphQL query
1459 if ( ! productItems . length ) {
15- fpi . catalog . getProducts ( { } ) ;
60+ const payload = {
61+ enableFilter : true , // Enabling filter option in the query
62+ first : 12 , // Number of products to fetch
63+ pageNo : 1 , // Initial page number
64+ pageType : "number" , // Type of pagination
65+ } ;
66+ fpi . executeGQL ( PLP_PRODUCTS , payload ) ;
1667 }
17- } , [ ] ) ;
68+ } , [ productItems . length , fpi ] ) ; // Dependency array to limit the execution of useEffect
1869
19- const title = props ?. title ?. value ?? 'Extension Title Default'
70+ // Conditionally rendering based on the availability of product items
71+ if ( ! productItems . length ) {
72+ return < h2 > No Products Found</ h2 > ; // Display message when no products are found
73+ }
2074
2175 return (
2276 < div >
2377 < Helmet >
24- < title > { title } </ title >
78+ < title > { title } </ title > // Setting the title of the page using Helmet
2579 </ Helmet >
2680 < h1 > Products List</ h1 >
27-
28- { ! productItems . length ? (
29- < h2 > No Products</ h2 >
30- ) : (
31- < div class = { styles . container } >
32- { productItems . map ( ( product ) => (
33- < ProductCard product = { product } key = { product . slug } />
34- ) ) }
35- </ div >
36- ) }
81+ < div className = { styles . container } >
82+ { productItems . map ( ( product ) => (
83+ < ProductCard product = { product } key = { product . slug } /> // Rendering ProductCard components for each product
84+ ) ) }
85+ </ div >
3786 </ div >
3887 ) ;
3988}
4089
41- Component . serverFetch = ( { fpi } ) => fpi . catalog . getProducts ( { } ) ;
90+ // Server-side fetching logic for server-side rendering (SSR)
91+ Component . serverFetch = ( { fpi } ) => {
92+ const payload = {
93+ enableFilter : true ,
94+ first : 12 ,
95+ pageNo : 1 ,
96+ pageType : "number" ,
97+ } ;
98+
99+ fpi . custom . setValue ( "test-extension" , true ) ; // Custom settings for the server-side execution context
100+
101+ return fpi . executeGQL ( PLP_PRODUCTS , payload ) ; // Executing GraphQL query on the server
102+ } ;
42103
104+ // Exporting component settings for potential dynamic UI management or configuration panels
43105export const settings = {
44106 label : "Product List" ,
45107 name : "product-list" ,
@@ -49,7 +111,7 @@ export const settings = {
49111 label : "Page Title" ,
50112 type : "text" ,
51113 default : "Extension Title" ,
52- info : "Page Title" ,
114+ info : "Set the page title for the product list." // Description for the property
53115 } ,
54116 ] ,
55117 blocks : [ ] ,
0 commit comments