File indexing completed on 2024-12-22 05:35:27

0001 import React, { useState } from 'react';
0002 import ReactDOM from 'react-dom';
0003 import {isMobile} from 'react-device-detect';
0004 import StoreContextProvider,{Context} from './context-provider.js';
0005 import {ProductBrowseItem} from './product-browse-item';
0006 import {getNumberOfItemsPerRow, getImageHeight, chunkArray, getItemWidth, ConvertObjectToArray} from './product-browse-helpers';
0007 
0008 function ProductBrowse(){
0009     console.log('browse list type:');
0010     console.log(browseListType);
0011     return (
0012         <StoreContextProvider>
0013             <div id="product-browse">
0014                 <ProductTagGroupFilterContainer/>
0015                 <ProductBrowseFilterContainer/>
0016                 <ProductBrowseItemList />
0017             </div>
0018         </StoreContextProvider>
0019     )
0020 }
0021 
0022 function ProductBrowseFilterContainer(){
0023 
0024     let filtersBaseUrl = json_serverUrl;
0025     filtersBaseUrl += json_store_name === "ALL" ? "/" : "/s/" + json_store_name + "/";
0026     filtersBaseUrl += "browse/";
0027     if (typeof filters.category === 'number') filtersBaseUrl += "cat/" + filters.category + "/";
0028 
0029     return (
0030         <div id="product-browse-top-menu">
0031             <div className="pling-nav-tabs">
0032                 <ul className="nav nav-tabs pling-nav-tabs" id="sort">
0033                     <li className={filters.order === "latest" ? "active" : ""}>
0034                         <a href={filtersBaseUrl + "ord/latest/" + window.location.search}>Latest</a>
0035                     </li>
0036                     <li className={filters.order === "rating" ? "active" : ""}>
0037                         <a href={filtersBaseUrl + "ord/rating/" + window.location.search}>Score</a>
0038                     </li>
0039                     <li className={filters.order === "plinged" ? "active" : ""}>
0040                         <a href={filtersBaseUrl + "ord/plinged/" + window.location.search}>Plinged</a>
0041                     </li>
0042                 </ul>
0043             </div>
0044         </div>
0045     )
0046 }
0047 
0048 function ProductTagGroupFilterContainer(){
0049  
0050     const [ tagGroups, setTagGroups ] = useState([]);
0051     const [ tagGroupIds, setTagGroupIds ] = useState([]);
0052     const [ selectedTags, setSelectedTags ] = useState([]);
0053 
0054     React.useState(() => {
0055         renderTagGroups();
0056     },[])
0057 
0058     function renderTagGroups(){
0059 
0060         for ( var i in tag_group_filter){
0061             const newTagGroupIds = tagGroupIds;
0062             newTagGroupIds.push(i); 
0063             setTagGroupIds(newTagGroupIds);
0064             const tagGroup = tag_group_filter[i];
0065             for (var ii in tagGroup){
0066                 if (ii === "selected_tag"){
0067                     const newSelectedTags = selectedTags;
0068                     newSelectedTags.push(tagGroup[ii]);
0069                     setSelectedTags(newSelectedTags);
0070                 } else {
0071                     const newArray = ConvertObjectToArray(tagGroup[ii],ii);
0072                     let newTagGroupsArray = tagGroups;
0073                     newTagGroupsArray.push(newArray);
0074                     setTagGroups(newTagGroupsArray);
0075                 }
0076             }
0077         }
0078     }
0079 
0080     let tagGroupsDropdownDisplay;
0081     if (tagGroups.length > 0){
0082         tagGroupsDropdownDisplay = tagGroups.map((tagGroup,index) => (
0083             <TagGroupDropDownMenu 
0084                 key={index}
0085                 tagGroup={tagGroup}
0086                 tagGroupId={tagGroupIds[index]}
0087                 selectedTag={selectedTags[index]}
0088             />
0089         ));
0090     }
0091 
0092     return (
0093         <div id="product-tag-filter-container" style={{"width":140 * tagGroups.length + 1}}>
0094             {tagGroupsDropdownDisplay}
0095         </div>
0096     )
0097 }
0098 
0099 function TagGroupDropDownMenu(props){
0100 
0101     function onSelectTag(e){
0102         const serverUrl = json_serverUrl.split('://')[1];
0103         const ajaxUrl = "https://"+ serverUrl + "/explore/savetaggroupfilter?group_id="+props.tagGroupId+"&tag_id="+e.target.value;
0104         $.ajax({url: ajaxUrl}).done(function(res) { 
0105             window.location.reload();
0106         });
0107     }
0108 
0109     const tagsDisplay = props.tagGroup.map((tag,index) => (
0110         <option key={index} selected={tag.id === props.selectedTag} value={tag.id}>{tag.tag}</option>
0111     ));
0112 
0113     return (
0114         <div className="product-tag-group-dropdown">
0115             <select onChange={e => onSelectTag(e)}>
0116                 <option></option>
0117                 {tagsDisplay}
0118             </select>
0119         </div>
0120     )
0121 }
0122 
0123 function ProductBrowseItemList(props){
0124 
0125     if (window.location.search === "?index=7") {
0126         window.browseListType === "favorites";
0127         browseListType = "favorites";
0128     }
0129 
0130     const [ containerWidth, setContainerWidth ] = useState($('#product-browse-container').width() + 14);
0131     const [ itemsInRow, setItemsInRow ] = useState(getNumberOfItemsPerRow(browseListType,isMobile,containerWidth));
0132     const [ itemWidth, setItemWidth ] = useState(getItemWidth(browseListType,containerWidth,itemsInRow));
0133     const [ imgHeight, setImgHeight ] = useState(getImageHeight(browseListType,itemWidth));
0134 
0135     
0136     React.useEffect(() => {
0137         window.addEventListener("resize", function(event){ updateDimensions() });
0138         window.addEventListener("orientationchange",  function(event){ updateDimensions() });
0139     },[])
0140 
0141     function updateDimensions(){
0142         const newContainerWidth = $('#product-browse-container').width() + 14;
0143         setContainerWidth(newContainerWidth);
0144         const newItemsInRow = getNumberOfItemsPerRow(browseListType,isMobile,newContainerWidth);
0145         setItemsInRow(newItemsInRow);
0146         const newItemWidth = getItemWidth(browseListType,newContainerWidth,newItemsInRow)
0147         setItemWidth(newItemWidth);
0148         const newImgHeight = getImageHeight(browseListType,newItemWidth);
0149         setImgHeight(newImgHeight);
0150     }
0151 
0152     let productsRowsDisplay;
0153     if (itemsInRow){   
0154         productsRowsDisplay = chunkArray(products,itemsInRow).map((ac,index) => (
0155             <ProductBrowseItemsRow
0156                 key={index} 
0157                 rowIndex={index}
0158                 products={ac}
0159                 itemWidth={itemWidth}
0160                 imgHeight={imgHeight}
0161             />
0162         ))
0163     }
0164 
0165     return (
0166         <div id="product-browse-item-list" className={isMobile ? "mobile" : ""}>
0167             <div id="product-browse-list-container">
0168                 {productsRowsDisplay}
0169                 <ProductBrowsePagination/>
0170             </div>
0171         </div>
0172     )
0173 }
0174 
0175 function ProductBrowseItemsRow(props){
0176 
0177     const productsDisplay = props.products.map((p,index) => (
0178         <ProductBrowseItem
0179             key={index} 
0180             index={index}
0181             rowIndex={props.rowIndex}
0182             product={p}
0183             itemWidth={props.itemWidth}
0184             imgHeight={props.imgHeight}
0185         />
0186     ));
0187 
0188     return (
0189         <div className={"product-browse-item-row " + ( browseListType ? browseListType + "-row" : "")}>
0190             {productsDisplay}
0191         </div>
0192     )
0193 }
0194 
0195 function ProductBrowsePagination(){
0196     
0197     const [ totalItems, setTotalItems ] = useState(pagination.totalcount);
0198     const [ itemsPerPage, setItemsPerPage ] = useState(50);
0199     const [ currentPage, setCurrentPage ] = useState(pagination.page);
0200     const [ totalPages, setTotalPages ] = useState(Math.ceil(totalItems / itemsPerPage));
0201 
0202     const minPage = currentPage - 5 > 0 ? currentPage - 5 : 0;
0203     const maxPage = minPage + 10 < totalPages ? minPage + 10 : totalPages;
0204 
0205     let paginationArray = [];
0206     for (var i = minPage; i < maxPage; i++){ paginationArray.push(i + 1); }
0207     
0208     let pageLinkBase = json_serverUrl;
0209     pageLinkBase += is_show_real_domain_as_url === 1 ? "/" : "/s/" + json_store_name + "/";
0210     pageLinkBase += "browse/page/";
0211 
0212     let pageLinkSuffix = "/" 
0213     if (typeof filters.category === 'number') pageLinkSuffix += "cat/" + filters.category + "/";
0214     pageLinkSuffix += "ord/" + filters.order + "/";
0215 
0216     let previousButtonDisplay;
0217     if (currentPage > 1) previousButtonDisplay = <li><a href={pageLinkBase + (currentPage - 1) + pageLinkSuffix}><span className="glyphicon glyphicon-chevron-left"></span> Previous</a></li>
0218 
0219     let nextButtonDisplay;
0220     if (currentPage < totalPages) nextButtonDisplay = <li><a href={pageLinkBase + (currentPage + 1) + pageLinkSuffix}>Next <span className="glyphicon glyphicon-chevron-right"></span></a></li>
0221 
0222     const paginationDisplay = paginationArray.map((p,index) => {
0223         let pageLinkDisplay;
0224         if (currentPage === p) pageLinkDisplay = <span className="no-link">{p}</span>
0225         else pageLinkDisplay = <a href={pageLinkBase + p + pageLinkSuffix}>{p}</a>
0226         return (                
0227             <li key={index}>
0228                 {pageLinkDisplay}
0229             </li>
0230         )
0231     });
0232 
0233     return (
0234         <div id="product-browse-pagination">
0235             <ul>
0236                 {previousButtonDisplay}
0237                 {paginationDisplay}
0238                 {nextButtonDisplay}
0239             </ul>
0240         </div>
0241     )
0242 }
0243 
0244 const rootElement = document.getElementById("product-browse-container");
0245 ReactDOM.render(<ProductBrowse />, rootElement);