Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Query Parameters #382

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

<script lang="ts">
import {
defineComponent, computed,
defineComponent, computed, onMounted, watch,
} from 'vue';
import { injectKey } from '@/presentation/injectionSymbols';
import TooltipWrapper from '@/presentation/components/Shared/TooltipWrapper.vue';
Expand Down Expand Up @@ -132,8 +132,45 @@ export default defineComponent({
collection: currentCollection.value,
});
});
updateQueryParam(type);
}

function updateQueryParam(type: RecommendationStatusType) {
const url = new URL(window.location.href);
const presetValue = RecommendationStatusType[type].toLowerCase();
url.searchParams.set('preset', presetValue);
window.history.pushState({}, '', url);
}

function getRecommendationFromQueryParam(): RecommendationStatusType | undefined {
const url = new URL(window.location.href);
const preset = url.searchParams.get('preset');
if (preset) {
const typeKey = Object.keys(RecommendationStatusType)
.find((key) => key.toLowerCase() === preset.toLowerCase());
return typeKey ? RecommendationStatusType[typeKey as keyof typeof RecommendationStatusType] : undefined;
}
return undefined;
}

onMounted(() => {
const typeFromQuery = getRecommendationFromQueryParam();
if (typeFromQuery !== undefined && typeFromQuery !== currentRecommendationStatusType.value) {
selectRecommendationStatusType(typeFromQuery);
}
});

watch(currentRecommendationStatusType, (newType) => {
updateQueryParam(newType);
});

window.addEventListener('popstate', () => {
const typeFromQuery = getRecommendationFromQueryParam();
if (typeFromQuery !== undefined && typeFromQuery !== currentRecommendationStatusType.value) {
selectRecommendationStatusType(typeFromQuery);
}
});

return {
RecommendationStatusType,
currentRecommendationStatusType,
Expand Down
41 changes: 40 additions & 1 deletion src/presentation/components/Scripts/Menu/TheOsChanger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue';
import {
defineComponent, computed, onMounted, watch,
} from 'vue';
import { injectKey } from '@/presentation/injectionSymbols';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { getOperatingSystemDisplayName } from '@/presentation/components/Shared/OperatingSystemNames';
Expand Down Expand Up @@ -49,8 +51,45 @@ export default defineComponent({
modifyCurrentContext((context) => {
context.changeContext(newOs);
});
updateQueryParam(newOs);
}

function updateQueryParam(os: OperatingSystem) {
const url = new URL(window.location.href);
url.searchParams.set('collection', OperatingSystem[os]);
window.history.pushState({}, '', url);
}

function getOsFromQueryParam(): OperatingSystem | undefined {
const url = new URL(window.location.href);
const collection = url.searchParams.get('collection');
if (collection) {
const osKey = Object.keys(OperatingSystem).find(
(key) => key.toLowerCase() === collection.toLowerCase(),
);
return osKey ? OperatingSystem[osKey as keyof typeof OperatingSystem] : undefined;
}
return undefined;
}

onMounted(() => {
const osFromQuery = getOsFromQueryParam();
if (osFromQuery !== undefined && osFromQuery !== currentOs.value) {
changeOs(osFromQuery);
}
});

watch(currentOs, (newOs) => {
updateQueryParam(newOs);
});

window.addEventListener('popstate', () => {
const osFromQuery = getOsFromQueryParam();
if (osFromQuery !== undefined && osFromQuery !== currentOs.value) {
changeOs(osFromQuery);
}
});

return {
allOses,
currentOs,
Expand Down
33 changes: 30 additions & 3 deletions src/presentation/components/TheSearchBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<script lang="ts">
import {
defineComponent, ref, watch, computed,
defineComponent, ref, watch, computed, onMounted,
} from 'vue';
import { injectKey } from '@/presentation/injectionSymbols';
import { NonCollapsing } from '@/presentation/components/Scripts/View/Cards/NonCollapsingDirective';
Expand All @@ -41,7 +41,10 @@ export default defineComponent({

const searchQuery = ref<string | undefined>();

watch(searchQuery, (newFilter) => updateFilter(newFilter));
watch(searchQuery, (newFilter) => {
updateFilter(newFilter);
updateQueryParam(newFilter);
});

function updateFilter(newFilter: string | undefined) {
modifyCurrentState((state) => {
Expand All @@ -53,7 +56,31 @@ export default defineComponent({
}
});
}

function updateQueryParam(query: string | undefined) {
const url = new URL(window.location.href);
if (query) {
url.searchParams.set('q', query);
} else {
url.searchParams.delete('q');
}
window.history.pushState({}, '', url);
}
// Read the 'q' parameter from the URL when the component is mounted
onMounted(() => {
const url = new URL(window.location.href);
const queryParam = url.searchParams.get('q');
if (queryParam) {
searchQuery.value = queryParam;
updateFilter(queryParam);
}
});
// Handle browser back/forward navigation
window.addEventListener('popstate', () => {
const url = new URL(window.location.href);
const queryParam = url.searchParams.get('q');
searchQuery.value = queryParam || undefined;
updateFilter(searchQuery.value);
});
onStateChange((newState) => {
updateFromInitialFilter(newState.filter.currentFilter);
events.unsubscribeAllAndRegister([
Expand Down