Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Preserve scroll position in app store (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankchhabra committed Oct 15, 2022
1 parent 29724eb commit a13ed30
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/layouts/ContentLayout.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<transition :name="isMobileDevice ? 'mobile-content-container-transition' : 'content-container-transition'" mode="out-in" appear>
<div class="content-container">
<div id="content-container" class="content-container">
<div class="header">
<router-link :to="{name: 'home'}"
class="btn-close-page cursor-pointer"
Expand Down
7 changes: 7 additions & 0 deletions src/store/modules/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import API from "@/helpers/api";

// Initial state
const state = () => ({
appStoreScrollTop: 0, // to preserve app store scroll position when user switches back-and-forth between app listings
installed: [],
store: [],
installing: [],
Expand All @@ -16,6 +17,9 @@ const state = () => ({

// Functions to update the state directly
const mutations = {
setAppStoreScrollTop(state, appStoreScrollTop) {
state.appStoreScrollTop = appStoreScrollTop;
},
setInstalledApps(state, apps) {
const alphabeticallySortedApps = apps.sort((a, b) => a.name.localeCompare(b.name));
state.installed = alphabeticallySortedApps;
Expand Down Expand Up @@ -88,6 +92,9 @@ const mutations = {

// Functions to get data from the API
const actions = {
updateAppStoreScrollTop({ commit }, scrollTop) {
commit("setAppStoreScrollTop", scrollTop);
},
async getInstalledApps({ commit }) {
const installedApps = await API.get(`${process.env.VUE_APP_MANAGER_API_URL}/v1/apps?installed=1`);
if (installedApps) {
Expand Down
24 changes: 22 additions & 2 deletions src/views/AppStore/AppStore.vue
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default {
},
computed: {
...mapState({
scrollTop: (state) => state.apps.appStoreScrollTop,
appStore: (state) => state.apps.store,
updating: (state) => state.apps.updating,
appStoreSearchIndex: (state) => state.apps.searchIndex,
Expand Down Expand Up @@ -231,6 +232,10 @@ export default {
}
},
methods: {
onScroll: function(event) {
// update scroll position
this.$store.dispatch("apps/updateAppStoreScrollTop", event.target.scrollTop);
},
updateAll: function() {
this.appsWithUpdate
.forEach(app => {
Expand All @@ -244,9 +249,24 @@ export default {
created() {
this.$store.dispatch("apps/getAppStore");
// autofocus search input
// https://stackoverflow.com/a/63485725
this.$nextTick(() => this.$refs.searchInput.focus());
this.$nextTick(() => {
// autofocus search input
this.$refs.searchInput.focus()
// set previous scroll position
const container = document.getElementById("content-container");
if (this.scrollTop) {
container.scrollTop = this.scrollTop;
}
// watch scroll position
container.addEventListener('scroll', this.onScroll);
});
},
beforeDestroy() {
const container = document.getElementById("content-container");
container.removeEventListener('scroll', this.onScroll);
},
components: {
CardWidget,
Expand Down

0 comments on commit a13ed30

Please sign in to comment.