Skip to content

Commit

Permalink
Merge pull request #1520 from hlxsites/contact-search
Browse files Browse the repository at this point in the history
contact serach page fix
  • Loading branch information
dev-rajneeshkumar authored Oct 21, 2024
2 parents c4a536b + 4b038af commit 59f69ea
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
59 changes: 49 additions & 10 deletions blocks/get-in-touch/get-in-touch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ffetch from '../../scripts/ffetch.js';
import { getCookie } from '../../scripts/scripts.js';

let DEFAULT_CMP = '';
Expand All @@ -15,7 +16,7 @@ function hubSpotFinalUrl(hubspotUrl, paramName) {
searchParams.delete('region');
searchParams.delete('return_url');

returnURL.set('region', queryParams.get('region'));
returnURL.set('region', queryParams.get('region') || REGION);

if (paramName === 'general') {
searchParams.delete(COMMENTS);
Expand Down Expand Up @@ -65,10 +66,10 @@ function createMap(block, mapUrl) {
observer.observe(block);
}

function regenerateForm(hubspotUrl, params, region) {
function regenerateForm(hubspotUrl, params) {
const hubspotIframe = document.querySelector('.get-in-touch-form');
if (hubspotUrl) {
const hubUrl = hubSpotFinalUrl(hubspotUrl, params, region);
const hubUrl = hubSpotFinalUrl(hubspotUrl, params);
hubspotUrl.href = hubUrl.href;
hubspotIframe.querySelector('iframe').setAttribute('src', hubspotUrl);
}
Expand Down Expand Up @@ -96,17 +97,27 @@ function scrollToForm(link, hubspotUrl, region) {
});
}

export default function decorate(block) {
export default async function decorate(block) {
const queryParams = new URLSearchParams(window.location.search);
const hubspotUrl = block.querySelector('[href*="https://info.moleculardevices.com"]');
const mapUrl = block.querySelector('[href*="https://maps.google.com"]');

/* set success msg */
if (queryParams.has('msg') && queryParams.get('msg') === 'success') {
const getInTouchBlock = document.querySelector('.get-in-touch');
const successMsg = block.lastElementChild.firstElementChild;
successMsg.classList.add('hubspot-success');
hubspotUrl.closest('div').replaceWith(successMsg);
block.lastElementChild.remove();
createMap(block, mapUrl);
setTimeout(() => {
if (getInTouchBlock) {
window.scroll({
top: getInTouchBlock.offsetTop - 100,
behavior: 'smooth',
});
}
}, 1000);
} else {
block.lastElementChild.remove(); // success message we don't need for this case
createForm(block, hubspotUrl);
Expand All @@ -115,14 +126,42 @@ export default function decorate(block) {

/* get region on tab click */
const tabLinks = document.querySelectorAll('.regional-contacts-wrapper .tab-wrapper > a');
tabLinks.forEach((link) => {
link.addEventListener('click', () => {
const regionName = link.hash.split('#')[1] || queryParams.get('region');
REGION = regionName;
regenerateForm(hubspotUrl, '', REGION);
if (tabLinks) {
tabLinks.forEach((link) => {
link.addEventListener('click', () => {
const regionName = link.hash.split('#')[1] || queryParams.get('region');
REGION = regionName;
regenerateForm(hubspotUrl, '');
setTimeout(() => {
document.getElementById('country').selectedIndex = 1;
}, 500);
});
});
});
}

/* get region on country change */
const distributors = await ffetch('/contact/local-distibutors.json').withFetch(fetch).all();
const searchButton = document.querySelector('#searchButton > button');
const countrySelect = document.getElementById('country');

if (window.location.pathname === '/contact-search') {
REGION = distributors.filter(
(dist) => dist.DisplayCountry === countrySelect.value)[0].Region.toLowerCase();
} else {
document.getElementById('country').selectedIndex = 1;
}

if (searchButton) {
searchButton.addEventListener('click', (event) => {
event.preventDefault();
const countryObj = distributors.filter(
(dist) => dist.DisplayCountry === countrySelect.value)[0].Region.toLowerCase();
REGION = countryObj || queryParams.get('region');
regenerateForm(hubspotUrl, '');
});
}

/* scroll to form on click of inquiry links */
const inquiryLinks = ['General Inquiry Form', 'Sales Inquiry Form', 'Contact Local Team', 'Service plans/warranty'];
const links = document.querySelectorAll('a[title]');
links.forEach((link) => {
Expand Down
17 changes: 15 additions & 2 deletions blocks/local-distributor/local-distributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,20 @@ export default async function decorate(block) {
),
];
} else {
countryList = [...new Set(distributors.map(({ DisplayCountry }) => DisplayCountry))];
const countryName = params.country;
if (countryName) {
const region = distributors.filter(
(dist) => dist.Country === countryName)[0].Region.toLowerCase();
countryList = [
...new Set(
distributors
.filter(({ Region }) => Region.toLowerCase().includes(region) > 0)
.map(({ DisplayCountry }) => DisplayCountry),
),
];
} else {
countryList = [...new Set(distributors.map(({ DisplayCountry }) => DisplayCountry))];
}
}

const searchButtton = document.querySelector('.tab-wrapper');
Expand Down Expand Up @@ -235,7 +248,7 @@ export default async function decorate(block) {
productFamilyList.map((family) => family.DisplayPrimaryProducts));
document.querySelector('.local-distributor > div').lastElementChild.innerHTML = formWrapper;
document.querySelector('.local-distributor').appendChild(searchResult);
const searchButton = document.getElementById('searchButton');
const searchButton = document.querySelector('#searchButton > button');

searchButton.addEventListener('click', () => {
// eslint-disable-next-line no-unused-expressions
Expand Down

0 comments on commit 59f69ea

Please sign in to comment.