UNPKG

@salla.sa/twilight-components

Version:
41 lines (40 loc) 1.86 kB
/*! * Crafted with ❤ by Salla */ const DEBUG_KEY = 'salla-delivery-promise-debug'; export function log(message, data) { if (localStorage.getItem(DEBUG_KEY)) { data !== undefined ? console.log(message, data) : console.log(message); } } /** Fetch available cities for a product's delivery promises. */ export async function fetchCities(productId, keyword) { log('fetchCities start', { productId, keyword }); const queryString = keyword != null ? `?keyword=${encodeURIComponent(keyword)}` : ''; const response = await salla.api.request(salla.url.api(`products/${productId}/delivery-promises/cities${queryString}`)); if (!response.success || !Array.isArray(response.data) || response.data.length === 0) { log('fetchCities: no data', { response }); return { cities: [] }; } log('fetchCities success', { count: response.data.length }); return { cities: response.data }; } /** Fetch the delivery promise message for a given city or branch. */ export async function fetchDeliveryMessage(productId, id, optionType, isLoginCycleEnabled) { log('fetchDeliveryMessage start', { productId, id, optionType }); let queryString = ''; if (optionType === 'city' && !isLoginCycleEnabled) { queryString = `?reference_id=${id}`; } else { queryString = `?reference_id=${id}&reference_type=${optionType}`; } const response = (await salla.api.withoutNotifier(() => salla.api.request(salla.url.api(`products/${productId}/delivery-promises${queryString}`)))); if (!response.success) { log('fetchDeliveryMessage error', { response }); throw new Error(response.error?.message || 'Failed to fetch delivery promise'); } const message = response.data?.message?.trim(); log('fetchDeliveryMessage success', { message }); return message || null; }