Skip to content

Commit

Permalink
feat: files requested automatic and files refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Sep 29, 2023
1 parent 5654a90 commit 2358076
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 56 deletions.
9 changes: 6 additions & 3 deletions src/main/webapp/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { useRouter } from 'vue-router';
import { useTheme } from 'vuetify';
const configurationStore = useConfigurationStore();
const { resetState } = configurationStore;
const { selectedFile, isSelectedFile, isConfirmation } = storeToRefs(configurationStore);
const { refresh, resetState } = configurationStore;
const { lastNavigation, selectedFile, isSelectedFile, isConfirmation } = storeToRefs(configurationStore);
const router = useRouter();
router.beforeEach(() => {
router.beforeEach((to) => {
resetState();
if (to.name != undefined && to.name != null) lastNavigation.value = to.name as string;
refresh(true, true);
});
const theme = useTheme();
Expand Down Expand Up @@ -52,6 +54,7 @@ const deleteItem = async (result: Response) => {
if (result == Response.yes && isSelectedFile.value) {
try {
await deleteFile(selectedFile.value!);
refresh(true);
} catch (e) {
errorHandler(e);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/webapp/src/components/BottomNavigation.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
<script setup lang="ts">
import { useConfigurationStore } from '@/stores/configurationStore';
import { useI18n } from 'vue-i18n';
import { useDisplay } from 'vuetify';
const configurationStore = useConfigurationStore();
const { refresh } = configurationStore;
const { mobile } = useDisplay();
const { t } = useI18n();
</script>

<template>
<v-bottom-navigation v-if="mobile" grow color="primary" class="pa-2">
<v-btn :to="{ name: 'projects' }" rounded="xl">
<v-btn :to="{ name: 'projects' }" rounded="xl" @click="refresh">
<v-icon icon="fas fa-folder" />
{{ t('navigation.item.projects') }}
</v-btn>
<v-btn :to="{ name: 'favorites' }" rounded="xl">
<v-btn :to="{ name: 'favorites' }" rounded="xl" @click="refresh">
<v-icon icon="fas fa-star" />
{{ t('navigation.item.favorites') }}
</v-btn>
<v-btn :to="{ name: 'shared' }" rounded="xl">
<v-btn :to="{ name: 'shared' }" rounded="xl" @click="refresh">
<v-icon icon="fas fa-share-nodes" />
{{ t('navigation.item.shared') }}
</v-btn>
<v-btn :to="{ name: 'public' }" rounded="xl">
<v-btn :to="{ name: 'public' }" rounded="xl" @click="refresh">
<v-icon icon="fas fa-globe" />
{{ t('navigation.item.public') }}
</v-btn>
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/src/components/drawers/NavigationDrawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { storeToRefs } from 'pinia';
import { useI18n } from 'vue-i18n';
const configurationStore = useConfigurationStore();
const { refresh } = configurationStore;
const { isSettings } = storeToRefs(configurationStore);
const { t } = useI18n();
Expand All @@ -20,27 +21,31 @@ const { t } = useI18n();
:to="{ name: 'projects' }"
rounded="xl"
class="mb-2"
@click="refresh"
/>
<v-list-item
prepend-icon="fas fa-star"
:title="t('navigation.item.favorites')"
:to="{ name: 'favorites' }"
rounded="xl"
class="mb-2"
@click="refresh"
/>
<v-list-item
prepend-icon="fas fa-share-nodes"
:title="t('navigation.item.shared')"
:to="{ name: 'shared' }"
rounded="xl"
class="mb-2"
@click="refresh"
/>
<v-list-item
prepend-icon="fas fa-globe"
:title="t('navigation.item.public')"
:to="{ name: 'public' }"
rounded="xl"
class="mb-2"
@click="refresh"
/>
</v-list>
<div class="flex-grow-1"></div>
Expand Down
1 change: 0 additions & 1 deletion src/main/webapp/src/components/layouts/FilesLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import FileCard from '@/components/FileCard.vue';
defineProps<{
files: Array<any> | undefined;
list?: boolean;
}>();
</script>

Expand Down
17 changes: 9 additions & 8 deletions src/main/webapp/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Navigation } from '@/types/enums/Navigation';
import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
Expand All @@ -9,23 +10,23 @@ const router = createRouter({
component: () => import('@/views/HomeView.vue'),
children: [
{
path: 'projects',
name: 'projects',
path: Navigation.projects,
name: Navigation.projects,
component: () => import('@/views/home/ProjectsView.vue'),
},
{
path: 'favorites',
name: 'favorites',
path: Navigation.favorites,
name: Navigation.favorites,
component: () => import('@/views/home/FavoritesView.vue'),
},
{
path: 'shared',
name: 'shared',
path: Navigation.shared,
name: Navigation.shared,
component: () => import('@/views/home/SharedView.vue'),
},
{
path: 'public',
name: 'public',
path: Navigation.public,
name: Navigation.public,
component: () => import('@/views/home/PublicView.vue'),
},
],
Expand Down
52 changes: 52 additions & 0 deletions src/main/webapp/src/stores/configurationStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getConfiguration } from '@/services/configurationService';
import { getFiles, getPublic, getShared, getStarred } from '@/services/fileService';
import type { Configuration } from '@/types/configurationType';
import { Navigation } from '@/types/enums/Navigation';
import { errorHandler } from '@/utils/axiosUtils';
import { differenceInMilliseconds } from 'date-fns';
import debounce from 'lodash.debounce';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';

Expand All @@ -23,8 +27,52 @@ export const useConfigurationStore = defineStore('configuration', () => {

const isInit = computed<boolean>(() => configuration.value != undefined);

/* -- Gestion de la navigation -- */

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

/* -- Gestion des fichier -- */

let lastUpdated = new Date();

const files = ref<Array<any> | undefined>();

const loadFiles = async (requestedFiles: Navigation | string) => {
try {
let response;
switch (requestedFiles) {
case Navigation.projects:
response = await getFiles();
break;
case Navigation.favorites:
response = await getStarred();
break;
case Navigation.shared:
response = await getShared();
break;
case Navigation.public:
response = await getPublic();
break;
}
files.value = response.data;
} catch (e) {
errorHandler(e);
}
};

const refresh = (instant?: boolean, loading?: boolean) => {
if (instant || differenceInMilliseconds(new Date(), lastUpdated) > 5000) {
if (loading) files.value = undefined;
const launch = debounce(() => {
if (lastNavigation.value != undefined) {
loadFiles(lastNavigation.value);
lastUpdated = new Date();
}
}, 200);
launch();
}
};

const selectedFile = ref<number | undefined>();
const isSelectedFile = computed<boolean>(() => selectedFile.value != undefined);
const isShare = ref<boolean>(false);
Expand All @@ -45,6 +93,10 @@ export const useConfigurationStore = defineStore('configuration', () => {
return {
init,
isInit,
lastNavigation,
files,
loadFiles,
refresh,
selectedFile,
isSelectedFile,
isShare,
Expand Down
6 changes: 6 additions & 0 deletions src/main/webapp/src/types/enums/Navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum Navigation {
projects = 'projects',
favorites = 'favorites',
shared = 'shared',
public = 'public',
}
14 changes: 4 additions & 10 deletions src/main/webapp/src/views/home/FavoritesView.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<script setup lang="ts">
import FilesLayout from '@/components/layouts/FilesLayout.vue';
import { getStarred } from '@/services/fileService';
import { ref } from 'vue';
import { useConfigurationStore } from '@/stores/configurationStore';
import { storeToRefs } from 'pinia';
const files = ref(undefined);
const getData = async () => {
const result = await getStarred();
files.value = result.data;
};
getData();
const configurationStore = useConfigurationStore();
const { files } = storeToRefs(configurationStore);
</script>

<template>
Expand Down
14 changes: 4 additions & 10 deletions src/main/webapp/src/views/home/ProjectsView.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<script setup lang="ts">
import FilesLayout from '@/components/layouts/FilesLayout.vue';
import { getFiles } from '@/services/fileService';
import { ref } from 'vue';
import { useConfigurationStore } from '@/stores/configurationStore';
import { storeToRefs } from 'pinia';
const files = ref(undefined);
const getData = async () => {
const result = await getFiles();
files.value = result.data;
};
getData();
const configurationStore = useConfigurationStore();
const { files } = storeToRefs(configurationStore);
</script>

<template>
Expand Down
14 changes: 4 additions & 10 deletions src/main/webapp/src/views/home/PublicView.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<script setup lang="ts">
import FilesLayout from '@/components/layouts/FilesLayout.vue';
import { getPublic } from '@/services/fileService';
import { ref } from 'vue';
import { useConfigurationStore } from '@/stores/configurationStore';
import { storeToRefs } from 'pinia';
const files = ref(undefined);
const getData = async () => {
const result = await getPublic();
files.value = result.data;
};
getData();
const configurationStore = useConfigurationStore();
const { files } = storeToRefs(configurationStore);
</script>

<template>
Expand Down
14 changes: 4 additions & 10 deletions src/main/webapp/src/views/home/SharedView.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<script setup lang="ts">
import FilesLayout from '@/components/layouts/FilesLayout.vue';
import { getShared } from '@/services/fileService';
import { ref } from 'vue';
import { useConfigurationStore } from '@/stores/configurationStore';
import { storeToRefs } from 'pinia';
const files = ref(undefined);
const getData = async () => {
const result = await getShared();
files.value = result.data;
};
getData();
const configurationStore = useConfigurationStore();
const { files } = storeToRefs(configurationStore);
</script>

<template>
Expand Down

0 comments on commit 2358076

Please sign in to comment.