Skip to content

Commit

Permalink
feat: greatly simplify fetching logic in search
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilR8 committed Oct 4, 2024
1 parent ed980b6 commit a6e24ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defineProps({

<template>
<v-select
menu
class="filter__select"
variant="outlined"
clearable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRoute } from "vue-router";
import _isEmpty from "lodash/isEmpty";
import { formatDate, locationUrl } from "utilities/filters";
import { formatDate } from "utilities/filters";
import {
createRegResultLink,
deserializeResult,
Expand Down Expand Up @@ -175,19 +175,19 @@ const getUrl = (doc) =>
doc.type === "internal_file"
? `${apiUrl}resources/internal/files/${doc.uid}`
: doc.type === "reg_text"
? createRegResultLink(
{
date: doc.date,
headline: doc.content_headline,
part_number: doc.part_number,
section_number: doc.node_id,
section_title: doc.title,
title: doc.reg_title,
},
homeUrl,
$route.query?.q
)
: doc.url;
? createRegResultLink(
{
date: doc.date,
headline: doc.content_headline,
part_number: doc.part_number,
section_number: doc.node_id,
section_title: doc.title,
title: doc.reg_title,
},
homeUrl,
$route.query?.q
)
: doc.url;
const needsBar = (item) => item.date && item.document_id;
Expand Down
98 changes: 26 additions & 72 deletions solution/ui/regulations/eregs-vite/src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,7 @@ const getPartsLastUpdated = async () => {
const { policyDocList, getDocList, clearDocList } = useSearchResults();
// use "reactive" method to make urlParams reactive when provided/injected
// selectedParams.paramString is used as the reactive prop
const selectedParams = reactive({
paramString: "",
paramsArray: [],
});
// method to add selected params
const addSelectedParams = (paramArgs) => {
const { id, name, type } = paramArgs;
// update paramString that is used as reactive prop for watch
if (selectedParams.paramString) {
selectedParams.paramString += `&${type}=${id}`;
} else {
selectedParams.paramString = `?${type}=${id}`;
}
// create new selectedParams key for array of objects for selections
selectedParams.paramsArray.push({ id, name, type });
};
const clearSelectedParams = () => {
selectedParams.paramString = "";
selectedParams.paramsArray = [];
};
provide("selectedParams", selectedParams);
const setSelectedParams = (subjectsListRef) => (param) => {
const setSelectedParams = (param) => {
const [paramType, paramValue] = param;
if (commonRemoveList.includes(paramType)) {
Expand All @@ -188,21 +159,6 @@ const setSelectedParams = (subjectsListRef) => (param) => {
searchQuery.value = paramValue;
return;
}
const paramList = !_isArray(paramValue) ? [paramValue] : paramValue;
paramList.forEach((paramId) => {
const subject = subjectsListRef.value.results.filter(
(subjectObj) => paramId === subjectObj.id.toString()
)[0];
if (subject) {
addSelectedParams({
type: paramType,
id: paramId,
name: getSubjectName(subject),
});
}
});
};
// policyDocSubjects fetch for subject selector
Expand All @@ -217,7 +173,29 @@ const setTitle = (query) => {
document.title = `Search ${querySubString}| Medicaid & CHIP eRegulations`;
};
// called on load
getDocsOnLoad = async () => {
if (!$route.query.q) {
clearDocList();
return;
}
// wipe everything clean to start
clearSearchQuery();
// now that everything is cleaned, iterate over new query params
Object.entries($route.query).forEach((param) => {
setSelectedParams(param);
});
getDocList({
apiUrl,
pageSize,
requestParamString: getRequestParams({ queryParams: $route.query }),
query: $route.query.q,
type: $route.query.type,
});
};
const getDocSubjects = async () => {
try {
const subjectsResponse = await getSubjects({
Expand All @@ -229,28 +207,6 @@ const getDocSubjects = async () => {
console.error(error);
} finally {
policyDocSubjects.value.loading = false;
if (!$route.query.q) {
clearDocList();
return;
}
// wipe everything clean to start
clearSelectedParams();
clearSearchQuery();
// now that everything is cleaned, iterate over new query params
Object.entries($route.query).forEach((param) => {
setSelectedParams(policyDocSubjects)(param);
});
getDocList({
apiUrl,
pageSize,
requestParamString: getRequestParams({ queryParams: $route.query }),
query: $route.query.q,
type: $route.query.type,
});
}
};
Expand All @@ -273,7 +229,6 @@ watch(
setShowDropdowns(type);
// wipe everything clean to start
clearSelectedParams();
clearSearchQuery();
// set document title
Expand All @@ -298,9 +253,7 @@ watch(
}
// now that everything is cleaned, iterate over new query params
Object.entries(newQueryParams).forEach(
setSelectedParams(policyDocSubjects)
);
Object.entries(newQueryParams).forEach(setSelectedParams);
// parse $route.query to return `${key}=${value}` string
// and provide to getDocList
Expand All @@ -319,6 +272,7 @@ watch(
// fetches on page load
getPartsLastUpdated();
getDocsOnLoad();
getDocSubjects();
</script>

Expand Down

0 comments on commit a6e24ad

Please sign in to comment.