diff --git a/client/src/api/data-sources.js b/client/src/api/data-sources.js index 645336f3..72574ff2 100644 --- a/client/src/api/data-sources.js +++ b/client/src/api/data-sources.js @@ -1,7 +1,8 @@ import axios from 'axios'; +import { isCachedResponseValid } from '@/api/util'; import { useAuthStore } from '@/stores/auth'; import { useDataSourceStore } from '@/stores/data-source'; -import { isCachedResponseValid } from '@/api/util'; +import { useSearchStore } from '@/stores/search'; const DATA_SOURCES_BASE = `${import.meta.env.VITE_VUE_API_BASE_URL}/data-sources`; const HEADERS_BASE = { @@ -14,13 +15,19 @@ const HEADERS_BASIC = { export async function createDataSource(data) { const auth = useAuthStore(); + const dataSourceStore = useDataSourceStore(); + const searchStore = useSearchStore(); - return await axios.post(DATA_SOURCES_BASE, data, { + const response = await axios.post(DATA_SOURCES_BASE, data, { headers: { ...HEADERS_BASE, authorization: `Bearer ${auth.$state.tokens.accessToken.value}`, }, }); + + dataSourceStore.clearCache(); + searchStore.clearCache(); + return response; } export async function getDataSource(id) { diff --git a/client/src/api/search.js b/client/src/api/search.js index a55f4fdc..121436b2 100644 --- a/client/src/api/search.js +++ b/client/src/api/search.js @@ -1,7 +1,6 @@ import axios from 'axios'; import { ENDPOINTS } from './constants'; import { useAuthStore } from '@/stores/auth'; -import _isEqual from 'lodash/isEqual'; import { useSearchStore } from '@/stores/search'; import { isCachedResponseValid } from '@/api/util'; @@ -14,9 +13,9 @@ const HEADERS_BASIC = { authorization: `Basic ${import.meta.env.VITE_ADMIN_API_KEY}`, }; -export async function search(params) { +export async function search(location_id) { const searchStore = useSearchStore(); - const cached = searchStore.getSearchFromCache(JSON.stringify(params)); + const cached = searchStore.getSearchFromCache(location_id); if ( cached && @@ -32,21 +31,25 @@ export async function search(params) { const response = await axios.get( `${SEARCH_BASE}/${ENDPOINTS.SEARCH.RESULTS}`, { - params, + params: { + location_id, + }, headers: HEADERS_BASIC, }, ); - searchStore.setSearchToCache(JSON.stringify(params), response); + searchStore.setSearchToCache(location_id, response); return response; } -export async function followSearch(params) { +export async function followSearch(location_id) { const auth = useAuthStore(); return await axios.post(`${SEARCH_BASE}/${ENDPOINTS.SEARCH.FOLLOW}`, null, { - params, + params: { + location_id, + }, headers: { ...HEADERS, Authorization: `Bearer ${auth.$state.tokens.accessToken.value}`, @@ -77,7 +80,7 @@ export async function getFollowedSearches() { return response; } -export async function getFollowedSearch(params) { +export async function getFollowedSearch(location_id) { const auth = useAuthStore(); if (!auth.isAuthenticated()) return false; @@ -85,18 +88,20 @@ export async function getFollowedSearch(params) { try { const response = await getFollowedSearches(); - return response.data.data.find((search) => { - return _isEqual(search, params); - }); + return response.data.data.find( + ({ location_id: followed_id }) => followed_id === Number(location_id), + ); } catch (error) { return null; } } -export async function deleteFollowedSearch(params) { +export async function deleteFollowedSearch(location_id) { const auth = useAuthStore(); return await axios.delete(`${SEARCH_BASE}/${ENDPOINTS.SEARCH.FOLLOW}`, { - params, + params: { + location_id, + }, headers: { ...HEADERS, Authorization: `Bearer ${auth.$state.tokens.accessToken.value}`, diff --git a/client/src/components/SearchForm.vue b/client/src/components/SearchForm.vue index 25c222c6..5c05bc73 100644 --- a/client/src/components/SearchForm.vue +++ b/client/src/components/SearchForm.vue @@ -229,10 +229,16 @@ function buildParams(values) { const obj = {}; /* Handle record from typeahead input */ - const recordFilteredByParamsKeys = (({ state, county, locality }) => ({ + const recordFilteredByParamsKeys = (({ state, county, locality, + location_id, + }) => ({ + state, + county, + locality, + location_id, // If no selected record, fall back to the initial search }))(selectedRecord.value ?? initiallySearchedRecord.value); diff --git a/client/src/pages/profile.vue b/client/src/pages/profile.vue index 1e697ba9..0aebfdf2 100644 --- a/client/src/pages/profile.vue +++ b/client/src/pages/profile.vue @@ -117,7 +117,7 @@ async function signOutWithRedirect() { async function unFollow(followed) { const text = getLocationText(followed); try { - await deleteFollowedSearch(followed); + await deleteFollowedSearch(followed.location_id); toast.success(`Un-followed search for ${text}`); reload(); } catch (error) { diff --git a/client/src/pages/search/results.vue b/client/src/pages/search/results.vue index dd12cfea..c9056411 100644 --- a/client/src/pages/search/results.vue +++ b/client/src/pages/search/results.vue @@ -36,19 +36,7 @@ to follow this search

-
- +

{ @@ -168,7 +158,7 @@ export const useSearchData = defineBasicLoader( // Local caching to skip even the pinia method in case of only the hash changing while on the route. _isEqual(params, query.value) && data.value ? data.value - : await search(route.query); + : await search(route.query.location_id); // On initial fetch - get hash const hash = normalizeLocaleForHash(searched, response.data); @@ -194,14 +184,9 @@ export const useSearchData = defineBasicLoader( export const useFollowedData = defineBasicLoader( '/search/results', async (route) => { - if (isOnlyHashChanged(route, previousRoute.value)) { - previousRoute.value = route; - return isPreviousRouteFollowed.value; - } - try { const params = route.query; - const isFollowed = await getFollowedSearch(params); + const isFollowed = await getFollowedSearch(params.location_id); previousRoute.value = route; isPreviousRouteFollowed.value = isFollowed; return isFollowed; @@ -215,18 +200,22 @@ function isOnlyHashChanged(currentRoute, previousRoute) { // If we don't have a previous route to compare against, return false if (!previousRoute) return false; - // Check if queries are equal - const areQueriesEqual = _isEqual(currentRoute.query, previousRoute.query); +// function isOnlyHashChanged(currentRoute, previousRoute) { +// // If we don't have a previous route to compare against, return false +// if (!previousRoute) return false; - // Check if paths are equal - const arePathsEqual = currentRoute.path === previousRoute.path; +// // Check if queries are equal +// const areQueriesEqual = _isEqual(currentRoute.query, previousRoute.query); - // Check if only the hash is different - const hasHashChanged = currentRoute.hash !== previousRoute.hash; +// // Check if paths are equal +// const arePathsEqual = currentRoute.path === previousRoute.path; - // Return true if everything is the same except the hash - return areQueriesEqual && arePathsEqual && hasHashChanged; -} +// // Check if only the hash is different +// const hasHashChanged = currentRoute.hash !== previousRoute.hash; + +// // Return true if everything is the same except the hash +// return areQueriesEqual && arePathsEqual && hasHashChanged; +// }