Untitled
unknown
javascript
a year ago
18 kB
6
Indexable
export async function loader({ params, context, request }: LoaderArgs) { const { handle, subcategory } = params; const { session } = context; const url = new URL(request.url); try { var reqMetaobjects = await fetchAPI("/api/shopify/metaobjects-new", { method: "POST", headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ "first": 250, "type": ["tipologia_prodotto", "tipo_di_pelle", "sostanza_funzionale", "esigenze", 'linee_prodotti', 'zone_prodotti', 'sottocategoria_prodotti', 'card_promozionali'] }) }, context, request); var listMetaobjects: TReqMetaObjects = { tipo_di_pelle: { nodes: [] }, tipologia_prodotto: { nodes: [] }, sostanza_funzionale: { nodes: [] }, esigenze: { nodes: [] }, linee_prodotti: { nodes: [] }, zone_prodotti: { nodes: [] }, sottocategoria_prodotti: { nodes: [] }, card_promozionali: { nodes: [] } }; if (reqMetaobjects.status == 200) { listMetaobjects = await reqMetaobjects.json() as TReqMetaObjects; listMetaobjects.tipo_di_pelle.nodes = listMetaobjects.tipo_di_pelle.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.tipologia_prodotto.nodes = listMetaobjects.tipologia_prodotto.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.sostanza_funzionale.nodes = listMetaobjects.sostanza_funzionale.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.esigenze.nodes = listMetaobjects.esigenze.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.linee_prodotti.nodes = listMetaobjects.linee_prodotti.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.zone_prodotti.nodes = listMetaobjects.zone_prodotti.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.sottocategoria_prodotti.nodes = listMetaobjects.sottocategoria_prodotti.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); listMetaobjects.card_promozionali.nodes = listMetaobjects.card_promozionali.nodes.map((item) => { var title = item.fields.filter((e) => e.key == "titolo"); return { ...item, displayName: title[0].value } }); } else { throw new Error(JSON.stringify(reqMetaobjects)); } } catch (error) { console.log(error) throw new Error("E101: Errore nel recuperare le tipologie di prodotti."); } var hide_filter = { tipologia_prodotto: false, tipo_di_pelle: false, esigenze: false, sostanza_funzionale: false, linee_prodotti: false, zone_prodotti: false, sottocategoria_prodotti: false } try { var filters: any = []; if (subcategory) { var filter_tp = listMetaobjects.tipologia_prodotto.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_tpl = listMetaobjects.tipo_di_pelle.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_es = listMetaobjects.esigenze.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_sf = listMetaobjects.sostanza_funzionale.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_linee = listMetaobjects.linee_prodotti.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_zone = listMetaobjects.zone_prodotti.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_sottocategoria = listMetaobjects.sottocategoria_prodotti.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); if (filter_tp.length > 0) { hide_filter.tipologia_prodotto = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "tipologia_prodotto", "value": filter_tp[0].displayName } }); } if (filter_tpl.length > 0) { hide_filter.tipo_di_pelle = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "tipo_di_pelle", "value": filter_tpl[0].displayName } }); } if (filter_es.length > 0) { hide_filter.esigenze = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "esigenze", "value": filter_es[0].displayName } }); } if (filter_sf.length > 0) { hide_filter.sostanza_funzionale = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "sostanza_funzionale", "value": filter_sf[0].displayName } }); } if (filter_linee.length > 0) { hide_filter.linee_prodotti = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "sostanza_funzionale", "value": filter_linee[0].displayName } }); } if (filter_zone.length > 0) { hide_filter.zone_prodotti = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "zone", "value": filter_linee[0].displayName } }); } if (filter_sottocategoria.length > 0) { hide_filter.sottocategoria_prodotti = true; filters.push({ "productMetafield": { "namespace": "custom", "key": "filtro_sottocategorie", "value": filter_sottocategoria[0].displayName } }); } if (!hide_filter.sostanza_funzionale && !hide_filter.tipo_di_pelle && !hide_filter.esigenze && !hide_filter.tipologia_prodotto && !hide_filter.linee_prodotti) { // throw new Response(null, { // status: 404, // statusText: "Not Found", // }) } } const params_tp = url.searchParams.get("tp"); if (params_tp && !hide_filter.tipologia_prodotto) { var list = params_tp.split(","); list.forEach((item) => { var filter_tp = listMetaobjects.tipologia_prodotto.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_tp.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "tipologia_prodotto", "value": item } }) } }) } const params_tpl = url.searchParams.get("tpl"); if (params_tpl && !hide_filter.tipo_di_pelle) { var list = params_tpl.split(","); list.forEach((item) => { var filter_tp = listMetaobjects.tipo_di_pelle.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_tp.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "tipo_di_pelle", "value": item } }) } }) } const params_es = url.searchParams.get("es"); if (params_es && !hide_filter.esigenze) { var list = params_es.split(","); list.forEach((item) => { var filter_es = listMetaobjects.esigenze.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_es.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "esigenze", "value": item } }) } }) } const params_linee = url.searchParams.get("linee"); if (params_linee && !hide_filter.linee_prodotti) { var list = params_linee.split(","); list.forEach((item) => { var filter_linee = listMetaobjects.linee_prodotti.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_linee.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "linee", "value": item } }) } }) } const params_zone = url.searchParams.get("zone"); if (params_zone && !hide_filter.zone_prodotti) { var list = params_zone.split(","); list.forEach((item) => { var filter_zone = listMetaobjects.zone_prodotti.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_zone.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "zone", "value": item } }) } }) } const params_sottocategoria = url.searchParams.get("sottocategoria"); if (params_sottocategoria && !hide_filter.sottocategoria_prodotti) { var list = params_sottocategoria.split(","); list.forEach((item) => { var filter_sottocategoria = listMetaobjects.sottocategoria_prodotti.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_sottocategoria.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "filtro_sottocategorie", "value": item } }) } }) } const params_sf = url.searchParams.get("sf"); if (params_sf && !hide_filter.sostanza_funzionale) { var list = params_sf.split(","); list.forEach((item) => { var filter_tp = listMetaobjects.sostanza_funzionale.nodes.filter((e) => e.displayName.toLowerCase() == item.toLowerCase()); if (filter_tp.length > 0) { filters.push({ "productMetafield": { "namespace": "custom", "key": "sostanza_funzionale", "value": item } }) } }) } const params_eta = url.searchParams.get("eta"); if (params_eta) { var list = params_eta.split(","); list.forEach((item) => { filters.push({ "productMetafield": { "namespace": "custom", "key": "fascia_di_et_", "value": item } }) }) } const params_travel = url.searchParams.get("travel"); if (params_travel == "1") { filters.push({ "productMetafield": { "namespace": "custom", "key": "variante_travel", "value": "true" } }); } if (params_travel == "2") { filters.push({ "productMetafield": { "namespace": "custom", "key": "variante_travel", "value": "false" } }); } var order: any = {}; const params_order = url.searchParams.get("order"); if (params_order) { switch (params_order) { case "piu-votati": order = { "sortKey": "TITLE", "sort": true } break; case "novita": order = { "sortKey": "CREATED", "sort": true } break; case "bestseller": order = { "sortKey": "BEST_SELLING", "sort": true } break; case "prezzo-crescente": order = { "sortKey": "PRICE", "sort": false } break; case "prezzo-decrescente": order = { "sortKey": "PRICE", "sort": true } break; default: break; } } else { order = { "sortKey": "CREATED", "sort": true } } const params_price = url.searchParams.get("price"); if (params_price) { var list = params_price.split("-"); filters.push({ "price": { "min": parseInt(list[0]), "max": parseInt(list[1]) } }) } // CINICA-862 Calcola il numero di card promozionali per l'handle corrente const nOfCardPromozionali = () => { if (!handle) return 0; const correspondingItems = listMetaobjects.card_promozionali.nodes.filter( (item) => item.handle?.includes(handle), ); if(!correspondingItems.length) return 0 // Hanno inserito più di 2 carte per corpo, questo sfasa i calcoli // e comunque ne mostriamo sempre solo 2 if(correspondingItems.length > 2) return 2 return correspondingItems.length }; var reqCollection = await fetchAPI("/api/shopify/products-collection", { method: "POST", headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ "collection_filters": true, "handle": handle, // CINICA-862 fetch 12 - n of card_promozionali per l'handle corrente // in questo modo le card mostrate inizialmente sono sempre 12 // e poi fetchandone con "carica altro" il numero totale rimane multiplo di 4 // Cosi l'ultima riga prima di "carica altro" è sempre full "first": (12 - nOfCardPromozionali()), "cursor": null, "filters": filters, "firstVariant": 40, "subcategory": subcategory || "", "language": params.language ? params.language.toUpperCase() : "IT", "country": "IT", ...order }) }, context, request); var collection: TSingleCollection & { list_filters: any } | null = null; if (reqCollection.status == 200) { collection = await reqCollection.json() as TSingleCollection & { list_filters: any }; } else { var error = await reqCollection.json() as any; throw new Error(JSON.stringify(error)); } } catch (error) { console.log(error) throw new Error("E101: Errore nel recuperare i dati della collection."); } var infoCollection = { "title": collection.title, "descrizione": collection.descriptionHtml, "category": false } as { title: string, descrizione: string, category: boolean }; if (subcategory) { var filter_tp = listMetaobjects.tipologia_prodotto.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_tpl = listMetaobjects.tipo_di_pelle.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_es = listMetaobjects.esigenze.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_sf = listMetaobjects.sostanza_funzionale.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_linee = listMetaobjects.linee_prodotti.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_zone = listMetaobjects.zone_prodotti.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); var filter_sottocategoria = listMetaobjects.sottocategoria_prodotti.nodes.filter((e) => toSeoUrl(e.displayName).toLowerCase() == subcategory.toLowerCase()); if (filter_tp.length > 0) { var title = filter_tp[0].displayName; var check_description = filter_tp[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } if (filter_tpl.length > 0) { var title = filter_tpl[0].displayName; var check_description = filter_tpl[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } if (filter_es.length > 0) { var title = filter_es[0].displayName; var check_description = filter_es[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } if (filter_linee.length > 0) { var title = filter_linee[0].displayName; var check_description = filter_linee[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } if (filter_zone.length > 0) { var title = filter_zone[0].displayName; var check_description = filter_zone[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } if (filter_sottocategoria.length > 0) { var title = filter_sottocategoria[0].displayName; var check_description = filter_sottocategoria[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } if (filter_sf.length > 0) { var title = filter_sf[0].displayName; var check_description = filter_sf[0].fields.filter((e) => e.key == "descrizione"); var descrizione = ""; if (check_description.length > 0 && check_description[0].value) { descrizione = toHTML(check_description[0].value); } infoCollection = { "title": title, "descrizione": descrizione, "category": true }; } } return defer({ metaobjects: listMetaobjects, collection: collection, infoCollection: infoCollection, hide_filter: hide_filter, initialFilters: filters }); }
Editor is loading...
Leave a Comment