Skip to content

Commit

Permalink
Refactor onClickSaveButton to improve code legibility
Browse files Browse the repository at this point in the history
Rewrite parts of click handler into helper methods.
  • Loading branch information
CodeWritingCow committed May 29, 2024
1 parent 990e70d commit 99a6593
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions src/components/SinglePropertyDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,38 +142,45 @@ const SinglePropertyDetail = ({

const onClickSaveButton = () => {
if (shouldAllowCookies) {
const localStorageData = localStorage.getItem("opa_ids");
const parsedLocalStorageData = localStorageData
? JSON.parse(localStorageData)
: {};
findPropertyIdInLocalStorage();
} else {
setShouldShowBanner(true);
}
};

if (parsedLocalStorageData.opa_ids[OPA_ID]) {
removePropertyIdFromLocalStorage(parsedLocalStorageData);
setIsPropertySavedToLocalStorage(false);
const findPropertyIdInLocalStorage = () => {
const localStorageData = localStorage.getItem("opa_ids");
const parsedLocalStorageData = localStorageData
? JSON.parse(localStorageData)
: {};

if (parsedLocalStorageData.count === 0) {
dispatch({
type: "SET_DIMENSIONS",
property: "OPA_ID",
dimensions: [],
});
setShouldFilterSavedProperties(false);
} else {
if (shouldFilterSavedProperties) {
let propertyIds = getPropertyIdsFromLocalStorage();
dispatch({
type: "SET_DIMENSIONS",
property: "OPA_ID",
dimensions: [...propertyIds],
});
}
}
} else {
savePropertyIdToLocalStorage(parsedLocalStorageData);
setIsPropertySavedToLocalStorage(true);
}
if (parsedLocalStorageData.opa_ids[OPA_ID]) {
removePropertyIdFromLocalStorage(parsedLocalStorageData);
setIsPropertySavedToLocalStorage(false);
dispatchFilterAction(parsedLocalStorageData);
} else {
setShouldShowBanner(true);
savePropertyIdToLocalStorage(parsedLocalStorageData);
setIsPropertySavedToLocalStorage(true);
}
};

const dispatchFilterAction = (data: any) => {
if (data.count === 0) {
dispatch({
type: "SET_DIMENSIONS",
property: "OPA_ID",
dimensions: [],
});
setShouldFilterSavedProperties(false);
} else {
if (shouldFilterSavedProperties) {
let propertyIds = getPropertyIdsFromLocalStorage();
dispatch({
type: "SET_DIMENSIONS",
property: "OPA_ID",
dimensions: [...propertyIds],
});
}
}
};

Expand Down

0 comments on commit 99a6593

Please sign in to comment.