UNPKG

kupos-ui-components-lib

Version:

A reusable UI components package

335 lines (307 loc) 9.33 kB
const getAmenityName = rawAnemity => { switch (rawAnemity) { case "mobile ticket": return "RESULTS_PAGE.MOBILE_TICKET"; break; case "charging plug": return "RESULTS_PAGE.CHARGING_PLUG"; break; case "wifi": return "RESULTS_PAGE.WIFI"; break; case "movie": return "RESULTS_PAGE.ENTERTAINMENT"; break; case "baggage": return "RESULTS_PAGE.BAGGAGE"; break; case "restrooms": return "RESULTS_PAGE.RESTROOMS"; break; case "air condtion": return "RESULTS_PAGE.AIR_CONDITIONER"; break; case "snacks new": return "RESULTS_PAGE.SNACKS"; break; case "coffee": return "RESULTS_PAGE.COFFEE"; break; case "gaming": return "RESULTS_PAGE.GAMING"; break; default: return rawAnemity; break; } }; const commonService = { currency(amount: number, currencySign?: string) { const formattedAmount = amount .toString() .replace(/\B(?=(\d{3})+(?!\d))/g, "."); const sign = currencySign || "$"; return sign + formattedAmount; }, copyObject: ob => { if (!ob) return {}; return JSON.parse(JSON.stringify(ob)); }, getServiceFilters: (routes, metaData, busTerminals, seatTypesOriginal) => { let filters = { operators: { title: "BUS_COMAPNIES", type: "operator", options: [], }, seatTypes: { title: "SEAT_TYPES", type: "seat_types", options: [], }, amenities: { title: "AMENITIES", type: "amenities", // options: [ // { trText: 'CHARGING_PLUG', label: 'Charging Plug', value: 'charging_plug' }, // { trText: 'MOBILE_TICKET', label: 'Wifi', value: 'mobile_ticket' }, // { trText: 'WIFI', label: 'Snacks New', value: 'wifi' }, // { trText: 'SNACKS_NEW', label: 'Mobile Ticket', value: 'snacks_new' }, // ], options: [], }, tipo: { title: "TIPO", type: "tipo", options: [], }, special_departure: { title: "SPECIAL_DEPARTURE", type: "special_departure", options: [], }, }; let seatTypesSet = new Set(); let seatTypes = []; let operators = []; let amenities = []; let service_type_set = {}; let special_dept_set = {}; let seatTypesSet2 = new Set(); let seatTypes2 = []; for (let r of routes) { let fareStr = r.seat_types; let fareArray = []; // Collect fare and label pairs for (let f of fareStr) { let type = f.label; let fare = f.fare; fareArray.push({ type: type, fare: fare }); } // Sort fareArray based on fare fareArray.sort((a, b) => a.fare - b.fare); // Add sorted labels to seatTypesSet for (let item of fareArray) { seatTypesSet.add(item.type); seatTypesSet2.add(item); } // Convert set to array seatTypes = [...seatTypesSet]; seatTypes2 = [...seatTypesSet2]; //=========================================== // for (let r of routes) { // // let fareStr = r.fare_str.split(","); // let fareStr = r.seat_types; // for (let f of fareStr) { // // let type = f.split(":")[0]; // let type = f.label; // // let type = f.split(":")[0].replace(/ /g, "-").toLowerCase(); // // let type2 = seatTypesOriginal[type] ? seatTypesOriginal[type].label : type; // // if (type.toLowerCase().indexOf("semi") > -1) { // // type2 = "Semi Cama"; // // } else if (type.toLowerCase().indexOf("vent") > -1) { // // type2 = "Salon Sin Vent"; // // } else if (type.toLowerCase().indexOf("salon") > -1) { // // type2 = "Salon Cama"; // // } else if (type.toLowerCase().indexOf("ejecutivo") > -1) { // // type2 = "Cama Ejecutivo"; // // } else if (type.toLowerCase().indexOf("suite") > -1) { // // type2 = "Cama Suite"; // // } else if (type.toLowerCase().indexOf("vip") > -1) { // // type2 = "Cama VIP"; // // } else if (type.toLowerCase().indexOf("preferente") > -1) { // // type2 = "Preferente"; // // } else if (type.toLowerCase() == "cama") { // // type2 = "Cama"; // // } else if (type.toLowerCase() == "clasico") { // // type2 = "Clasico"; // // } // // else if (type.toLowerCase() == "premium") { // // type2 = "Premium"; // // } // //todo // // if (seatTypes.indexOf(type2) === -1) { // // // seatTypes.push(type2); // // seatTypes.add(type2); // // seatTypes = new Array(...seatTypes); // // } // seatTypesSet.add(type); // } //seatTypes = new Array(...seatTypesSet); if (operators.indexOf(r.operator_service_name) === -1) { operators.push(r.operator_service_name); } if (metaData) { let amArr = r?.operator_details?.[4]; for (let a of amArr) { if (a) { let am = metaData?.amenities[a]?.split(".")[0]?.toLowerCase(); if (amenities.indexOf(am) === -1) { amenities.push(am); } } } } if (r.is_direct_trip) { service_type_set["DIRECT_FILTER"] = "Servicio Directo"; } if (Object.keys(r.pet_seat_info).length) { service_type_set["PET_FRIENDLY"] = "Pet Friendly"; } if (r.is_change_ticket) { service_type_set["FLEXIBLE"] = "Pasaje flexible"; } const labelId = r.boarding_stages.split("|")[0]; const showTopLabel = busTerminals[labelId] && busTerminals[labelId].split("|")[1] === "true" && busTerminals[labelId].split("|")[0]; if (showTopLabel) { special_dept_set[labelId] = showTopLabel; } } if (special_dept_set) { for (let [key, value] of Object.entries(special_dept_set)) { if (key) { filters.special_departure.options.push({ label: value, value: key, spText: value, }); } } } if (service_type_set) { for (let [key, value] of Object.entries(service_type_set)) { if (key) { filters.tipo.options.push({ label: key, value: value, trText: key, }); } } } //===========sorting pending here========= seatTypes.sort((a, b) => a.fare - b.fare); for (let s of seatTypes) { if (s) filters.seatTypes.options.push({ label: s, // label: s.replace(/-/g, " "), value: s, }); } for (let o of operators.sort()) { filters.operators.options.push({ label: o, value: o }); } if (metaData) { for (let a of amenities) { if (a) filters.amenities.options.push({ label: getAmenityName(a.split(".")[0].split("_").join(" ")), value: a, }); } } return filters; }, getServiceTypeLabelForFilters: service_type => { switch (service_type) { case "service_type": return "Tipo de servicio"; case "special_departure": return "Punto de embarque"; case "seat_type": return "Tipos de asiento"; case "train_type": return "Tipo de servicio"; default: return ""; } }, getSeatNameForFilters: rawSeat => { switch (rawSeat) { case "SALON CAMA": case "Salon Cama": return "Salón Cama"; case "CLASICO": case "Clasico": return "Clásico"; case "Salon Sin Vent": return "Salón S.V."; case "Salon Preferente": return "Salón Preferente"; case "SALON PREFERENTE": case "Salon Preferente": return "Salón Preferente"; case "SALON": return "Salón "; case "Salon": return "Salón "; case "BIO BIO SUPERIOR": case "Bio Bio Superior": return "Bio Bio Superior"; case "SALON CAMA P": return ""; case "SALON CAMA IND": case "Salon Cama Ind": return "Salón Cama IND"; case "SalonBasic": return "Salón B."; case "SalonStandard": return "Salón S."; case "PrefBasic": return "Pref. Basic"; case "PrefStandard": return "Pref. Standard"; case "PrefPremium": return "Pref. Premium"; case "Salón (SV)": return "Salón (SV)"; case "Preferente": return "Preferente"; case "Estándar": return "Estándar"; case "Estándar Plus": return "Estándar Plus"; case "Salón": return "Salón"; default: return commonService.capitalize(rawSeat); } }, capitalize: str => { if (str) { let strArr = str.split(" "); for (let i = 0; i < strArr.length; i++) { strArr[i] = strArr[i][0] && strArr[i][0].toUpperCase() + strArr[i].substr(1).toLowerCase(); } return strArr.join(" "); } return ""; }, }; export default commonService;