Newer
Older

Timothee P
committed
import store from '@/store';
import featureAPI from '@/services/feature-api';
Sébastien DA ROCHA
committed
Sébastien DA ROCHA
committed
let projectBase = 'projet';
if (window.location.pathname.includes('projet-partage')) {
projectBase = 'projet-partage';
Sébastien DA ROCHA
committed
const routes = [
{
path: '/',
name: 'index',
Sébastien DA ROCHA
committed
},
{
path: `${projectBase === 'projet' ? '': `/${projectBase}/:slug`}/connexion/`,
Sébastien DA ROCHA
committed
name: 'login',
Sébastien DA ROCHA
committed
},
{
path: `${projectBase === 'projet' ? '': `/${projectBase}/:slug`}/my_account/`,
Sébastien DA ROCHA
committed
name: 'my_account',
Sébastien DA ROCHA
committed
},
{
path: `${projectBase === 'projet' ? '': '/' + projectBase}/mentions/`,
Sébastien DA ROCHA
committed
name: 'mentions',
component: () => import('../views/FlatPages/LegalMentions.vue')
Sébastien DA ROCHA
committed
},
{
path: `${projectBase === 'projet' ? '': '/' + projectBase}/aide/`,
Sébastien DA ROCHA
committed
name: 'aide',
component: () => import('../views/FlatPages/Help.vue')
Sébastien DA ROCHA
committed
},
// * PROJECT
{
path: '/creer-projet/',
name: 'project_create',
Sébastien DA ROCHA
committed
},
{
Sébastien DA ROCHA
committed
name: 'project_detail',
props: true,
component: () => import('../views/Project/ProjectDetail.vue'),
},
{
path: `/${projectBase}/:slug/signalement/lister/`,
name: 'liste-signalements',
component: () => import('../views/Project/FeaturesListAndMap.vue')
Sébastien DA ROCHA
committed
},
{
Sébastien DA ROCHA
committed
name: 'project_edit',
Sébastien DA ROCHA
committed
},
{
path: '/projet-type/',
name: 'project_type_list',
Sébastien DA ROCHA
committed
},
{
path: '/creer-projet/create_from/:slug/',
name: 'project_create_from',
Sébastien DA ROCHA
committed
},
{
path: `/${projectBase}/:slug/administration-carte/`,
Sébastien DA ROCHA
committed
name: 'project_mapping',
Sébastien DA ROCHA
committed
},
{
Sébastien DA ROCHA
committed
name: 'project_members',
Sébastien DA ROCHA
committed
},

Timothee P
committed
{
path: `/${projectBase}/:slug/signalement-filtre/`,
name: 'details-signalement-filtre',
component: () => import('../views/Feature/FeatureDetail.vue')
},
Sébastien DA ROCHA
committed
// * FEATURE TYPE
{
path: `/${projectBase}/:slug/type-signalement/ajouter/`,
Sébastien DA ROCHA
committed
name: 'ajouter-type-signalement',
props: true,
component: () => import('../views/FeatureType/FeatureTypeEdit.vue')
Sébastien DA ROCHA
committed
},
{
path: `/${projectBase}/:slug/type-signalement/ajouter/create_from/:slug_type_signal`,
Sébastien DA ROCHA
committed
name: 'dupliquer-type-signalement',
component: () => import('../views/FeatureType/FeatureTypeEdit.vue')
Sébastien DA ROCHA
committed
},
{
path: `/${projectBase}/:slug/type-signalement/:feature_type_slug/`,
Sébastien DA ROCHA
committed
name: 'details-type-signalement',
component: () => import('../views/FeatureType/FeatureTypeDetail.vue')
Sébastien DA ROCHA
committed
},
{
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/editer/`,
Sébastien DA ROCHA
committed
name: 'editer-type-signalement',
component: () => import('../views/FeatureType/FeatureTypeEdit.vue')
Sébastien DA ROCHA
committed
},
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/affichage/`,
name: 'editer-affichage-signalement',
component: () => import('../views/FeatureType/FeatureTypeDisplay.vue')
Sébastien DA ROCHA
committed
// * FEATURE
{
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/signalement/ajouter/`,
Sébastien DA ROCHA
committed
name: 'ajouter-signalement',
Sébastien DA ROCHA
committed
},
{
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/signalement/:slug_signal`,
Sébastien DA ROCHA
committed
name: 'details-signalement',

Timothee P
committed
component: () => import('../views/Feature/FeatureDetail.vue'),

Timothee P
committed
/**
* Handles routing logic before entering the details-signalement route.
* This function manages access and navigation based on user permissions and feature data.
*/

Timothee P
committed
beforeEnter: async (to, from, next) => {
try {
const { slug, slug_type_signal, slug_signal } = to.params;

Timothee P
committed
// Retrieve the project details from the store

Timothee P
committed
const project = await store.dispatch('projects/GET_PROJECT', slug);

Timothee P
committed
// Prepare query based on the project settings for feature browsing

Timothee P
committed
const query = { ordering: project.feature_browsing_default_sort };

Timothee P
committed
// Check if the default filter of the project is set to feature_type and apply it
if (project.feature_browsing_default_filter) { // when feature_type is the default filter of the project,

Timothee P
committed
query['feature_type_slug'] = slug_type_signal; // set feature_type slug in query
}

Timothee P
committed
// Get the feature's position based on the feature slug and query settings

Timothee P
committed
const offset = await featureAPI.getFeaturePosition(slug, slug_signal, query);

Timothee P
committed
// Decide next routing based on the offset result
next({
name: 'details-signalement-filtre',
params: { slug },
query: { ...query, offset }
});
// API return no content when user is not allowed to see the feature or isn't connected

Timothee P
committed
if (store.state.user) {
// If the user is connected, display information that he's not allowed to view the feature

Timothee P
committed
store.commit('DISPLAY_MESSAGE', { comment: 'Vous n\'avez pas accès à ce signalement avec cet utilisateur', level: 'negative' });
// and redirect to main page

Timothee P
committed
next({ path: '/' });
} else {
// If the user is not connected, remove other messages to avoid displaying twice that the user is not connected
store.commit('CLEAR_MESSAGES');
// display information that user need to be connected

Timothee P
committed
store.commit('DISPLAY_MESSAGE', { comment: 'Vous n\'avez pas accès à ce signalement hors connexion, veuillez-vous connecter au préalable', level: 'negative' });
// Then redirect to login page

Timothee P
committed
if (store.state.configuration.VUE_APP_LOGIN_URL) {
// If the login is through SSO, redirect to external login page (if the instance accepts a redirect_url it would be caught before when requesting user_info with GET_USER_INFO)

Timothee P
committed
setTimeout(() => { // delay switching page to allow the info message to be read by user
window.open(store.state.configuration.VUE_APP_LOGIN_URL);
}, 1500);
} else {
// In a classic installation, redirect to the login page of this application

Timothee P
committed
next({ name: 'login' });
}
} else {
store.commit('DISPLAY_MESSAGE', { comment: 'Désolé, une erreur est survenue pendant la recherche du signalement', level: 'negative' });
next({ path: '/' });

Timothee P
committed
} catch (error) {
console.error('error', error);
store.commit('DISPLAY_MESSAGE', { comment: `Désolé, une erreur est survenue pendant la recherche du signalement - ${error}`, level: 'negative' });
next({ path: '/' });
}
}
Sébastien DA ROCHA
committed
},
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/offline`,
Sébastien DA ROCHA
committed
{
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/signalement/:slug_signal/editer/`,
Sébastien DA ROCHA
committed
name: 'editer-signalement',
Sébastien DA ROCHA
committed
},
{
path: `/${projectBase}/:slug/type-signalement/:slug_type_signal/editer-signalements-attributs/`,
name: 'editer-attribut-signalement',
component: () => import('../views/Feature/FeatureEdit.vue')
},
Sébastien DA ROCHA
committed
path: '/projet/:slug/catalog/:feature_type_slug',
name: 'catalog-import',
component: () => import('../views/Catalog.vue')
},
path: '/projet/:slug/type-signalement/:slug_type_signal/signalement/:slug_signal/attachment-preview/',
name: 'attachment-preview',
component: () => import('../views/AttachmentPreview.vue')
Sébastien DA ROCHA
committed
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('../views/NotFound.vue') },
Sébastien DA ROCHA
committed
const router = new VueRouter({
mode: 'history',
Sébastien DA ROCHA
committed
routes,
routerHistory: [],
scrollBehavior(to, from, savedPosition) { //* record each route change to keep scroll position
Sébastien DA ROCHA
committed
const fromHistory = Boolean(savedPosition);
if (fromHistory && this.options.routerHistory.length > 0) {
this.options.routerHistory.splice(-1, 1);
} else {
this.options.routerHistory.push(from);
}
return savedPosition || { x: 0, y: 0 };
},
Sébastien DA ROCHA
committed