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AmB,GAAI,IACvB,MAAMC,EAAeG,EAAMG,OAAOC,SAAS,kBAAkBC,MAC7D,IA2BF,SAA6BR,GAC3B,SAAKA,GAAwC,IAAxBA,EAAaS,OAKpC,CAjCOC,CAAoBV,GAIvB,OAHAP,EAAgB,oFAChBD,EAAaa,UAAW,GAK1BnB,EACGR,IAAI,wDAAmDsB,KACvDhC,MAAMC,GAAa8B,EAAmBC,EAAc/B,EAASgC,WAC7DzB,OAAM,KAKLiB,EAHE,yIAG2B,IAE9BkB,SAAQ,IAAOnB,EAAaa,UAAW,GAC5C,CA/BiFO,CAAqBT,MAgFxG,C","sources":["webpack://stdcheck-exposed-wp-theme/./themes/stdcheck-exposed/src/js/services/http.js","webpack://stdcheck-exposed-wp-theme/./themes/stdcheck-exposed/src/js/components/gift-card-balance.js"],"sourcesContent":["/**\n * Service class for making HTTP requests\n */\nexport function HttpService() {\n  /**\n   * Make an HTTP request that expects a JSON response\n   *\n   * @param {*} url       The URL to make the request to\n   * @param {*} options   options for the fetch function\n   *\n   * @returns {Promise}   A promise that resolves with the JSON response data, or rejects for any of these reasons:\n   *                      1. The HTTP status code is not between 200-299\n   *                      2. The response is not valid JSON\n   *                      3. Network failures or anything that prevented the HTTP request from completing\n   */\n  function fetchJson(url, options = {}) {\n    options.headers = { Accept: 'application/json', 'Content-Type': 'application/json' };\n\n    return new Promise((resolve, reject) => {\n      fetch(url, options)\n        .then((response) => {\n          if (response.ok && response.status !== 204) {\n            response.json().then((data) => resolve(data));\n          } else if (response.ok && response.status === 204) {\n            resolve();\n          } else {\n            response.json().then((errorResponse) => {\n              errorResponse.code = response.status;\n              reject(errorResponse);\n            });\n          }\n        })\n        .catch((error) => reject(error));\n    });\n  }\n\n  return {\n    /**\n     * Make a GET request to the specified URL\n     *\n     * @param {string} url  The URL to make the request to\n     *\n     * @returns {Promise} A Promise that resolves with the response data when successful, and rejects on failure\n     */\n    get: (url) => fetchJson(url, { method: 'GET' }),\n\n    /**\n     * Make a POST request to the specified URL\n     *\n     * @param {string} url  The URL to make the request to\n     * @param {any} body    The request body\n     *\n     * @returns {Promise} A Promise that resolves with the response data when successful, and rejects on failure\n     */\n    post: (url, body) => fetchJson(url, { method: 'POST', body: JSON.stringify(body) }),\n  };\n}\n","import { HttpService } from '../services/http';\n\n/**\n * Controller for checking the balance of a gift card. This code is used in conjunction with the template located in\n * this file:\n *\n * themes/stdcheck-exposed/templates/partial/stdcheck/gift-card-balance.twig\n */\nexport function GiftCardBalanceComponent() {\n  const httpService = new HttpService();\n  const form = document.getElementById('gift-card-balance-form');\n  const errorContainer = document.getElementById('gift-card-error-container');\n  const balanceContainer = document.getElementById('gift-card-balance-container');\n  const submitButton = document.getElementById('gift-card-submit-button');\n\n  // Initializes the component\n  window.addEventListener('load', () => form.addEventListener('submit', (event) => checkGiftCardBalance(event)));\n\n  /**\n   * Checks the balance of a gift card and update the UI accordingly.\n   *\n   * @param {SubmitEvent} event the submit event object triggered by a form submission\n   */\n  function checkGiftCardBalance(event) {\n    event.preventDefault();\n    submitButton.disabled = true;\n    setErrorMessage('');\n    setGiftCardBalance('', '');\n    const giftCardCode = event.target.elements['gift-card-code'].value;\n    if (!isGiftCardCodeValid(giftCardCode)) {\n      setErrorMessage('The code you entered is not valid. Please double check your gift card code.');\n      submitButton.disabled = false;\n\n      return;\n    }\n\n    httpService\n      .get(`${process.env.API_URL}/api/v1/gift-card-balance/${giftCardCode}`)\n      .then((response) => setGiftCardBalance(giftCardCode, response.balance))\n      .catch(() => {\n        const errorMessage =\n          'Invalid. We do not have any record of your gift card. If you feel this is an error, ' +\n          'please contact customer service at 1-800-456-2323.';\n\n        setErrorMessage(errorMessage);\n      })\n      .finally(() => (submitButton.disabled = false));\n  }\n\n  /**\n   * Checks if a gift card code is valid.\n   *\n   * @param {string} giftCardCode the gift card code to validate\n   *\n   * @returns {boolean} true if the gift card code is valid, false otherwise\n   */\n  function isGiftCardCodeValid(giftCardCode) {\n    if (!giftCardCode || giftCardCode.length !== 6) {\n      return false;\n    }\n\n    return true;\n  }\n\n  /**\n   * Sets an error message on the webpage and toggles its visibility.\n   *\n   * @param {string} message the error message to display\n   */\n  function setErrorMessage(message) {\n    errorContainer.innerHTML = `<span class=\"fa fa-exclamation-circle\"></span> ${message}`;\n    if (message) {\n      errorContainer.classList.remove('hide');\n\n      return;\n    }\n\n    errorContainer.classList.add('hide');\n  }\n\n  /**\n   * Updates the displayed gift card balance on the webpage and toggles its visibility.\n   *\n   * @param {string} giftCardCode the code of the gift card\n   * @param {string} balance the balance amount of the gift card\n   */\n  function setGiftCardBalance(giftCardCode, balance) {\n    balanceContainer.innerHTML = `<span class=\"fa fa-check\"></span> Your gift card \"${giftCardCode}\" balance is $${balance}.`;\n    if (balance && giftCardCode) {\n      balanceContainer.classList.remove('hide');\n\n      return;\n    }\n\n    balanceContainer.classList.add('hide');\n  }\n}\n\nwindow.giftCardBalance = new GiftCardBalanceComponent();\n"],"names":["HttpService","fetchJson","url","options","headers","Accept","Promise","resolve","reject","fetch","then","response","ok","status","json","data","errorResponse","code","catch","error","get","method","post","body","JSON","stringify","window","giftCardBalance","httpService","form","document","getElementById","errorContainer","balanceContainer","submitButton","setErrorMessage","message","innerHTML","classList","remove","add","setGiftCardBalance","giftCardCode","balance","addEventListener","event","preventDefault","disabled","target","elements","value","length","isGiftCardCodeValid","finally","checkGiftCardBalance"],"sourceRoot":""}