2018-01-09 17:52:26 +01:00
|
|
|
<template>
|
2019-01-21 15:08:22 +01:00
|
|
|
<div id="mobilizon">
|
2022-07-12 10:55:28 +02:00
|
|
|
<!-- <VueAnnouncer />
|
|
|
|
<VueSkipTo to="#main" :label="t('Skip to main content')" /> -->
|
2019-04-03 17:29:03 +02:00
|
|
|
<NavBar />
|
2022-07-12 10:55:28 +02:00
|
|
|
<div v-if="isDemoMode">
|
|
|
|
<o-notification
|
|
|
|
class="container mx-auto"
|
|
|
|
variant="danger"
|
|
|
|
:title="t('Warning').toLocaleUpperCase()"
|
2020-02-18 08:57:00 +01:00
|
|
|
closable
|
2022-07-12 10:55:28 +02:00
|
|
|
:aria-close-label="t('Close')"
|
2020-02-18 08:57:00 +01:00
|
|
|
>
|
2019-10-11 09:42:51 +02:00
|
|
|
<p>
|
2022-07-12 10:55:28 +02:00
|
|
|
{{ t("This is a demonstration site to test Mobilizon.") }}
|
|
|
|
<b>{{ t("Please do not use it in any real way.") }}</b>
|
2020-10-28 11:36:16 +01:00
|
|
|
{{
|
2022-07-12 10:55:28 +02:00
|
|
|
t(
|
2020-10-28 11:36:16 +01:00
|
|
|
"This website isn't moderated and the data that you enter will be automatically destroyed every day at 00:01 (Paris timezone)."
|
|
|
|
)
|
|
|
|
}}
|
2019-10-11 09:42:51 +02:00
|
|
|
</p>
|
2022-07-12 10:55:28 +02:00
|
|
|
</o-notification>
|
2019-10-11 09:42:51 +02:00
|
|
|
</div>
|
2022-07-12 10:55:28 +02:00
|
|
|
<ErrorComponent v-if="error" :error="error" />
|
2021-02-03 18:00:49 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
<main id="main" class="pt-4" v-else>
|
|
|
|
<router-view></router-view>
|
2019-01-21 15:08:22 +01:00
|
|
|
</main>
|
2019-04-03 17:29:03 +02:00
|
|
|
<mobilizon-footer />
|
2019-01-21 15:08:22 +01:00
|
|
|
</div>
|
2018-01-09 17:52:26 +01:00
|
|
|
</template>
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
<script lang="ts" setup>
|
2022-08-26 16:08:58 +02:00
|
|
|
import NavBar from "@/components/NavBar.vue";
|
2020-11-30 10:24:11 +01:00
|
|
|
import {
|
|
|
|
AUTH_ACCESS_TOKEN,
|
|
|
|
AUTH_USER_EMAIL,
|
|
|
|
AUTH_USER_ID,
|
|
|
|
AUTH_USER_ROLE,
|
2022-08-26 16:08:58 +02:00
|
|
|
} from "@/constants";
|
|
|
|
import { UPDATE_CURRENT_USER_CLIENT } from "@/graphql/user";
|
|
|
|
import MobilizonFooter from "@/components/PageFooter.vue";
|
2021-05-27 18:24:11 +02:00
|
|
|
import jwt_decode, { JwtPayload } from "jwt-decode";
|
2022-08-26 16:08:58 +02:00
|
|
|
import { refreshAccessToken } from "@/apollo/utils";
|
2022-07-12 10:55:28 +02:00
|
|
|
import {
|
|
|
|
reactive,
|
|
|
|
ref,
|
|
|
|
provide,
|
|
|
|
onUnmounted,
|
|
|
|
onMounted,
|
|
|
|
onBeforeMount,
|
|
|
|
inject,
|
|
|
|
defineAsyncComponent,
|
2022-08-26 16:08:58 +02:00
|
|
|
computed,
|
|
|
|
watch,
|
2022-07-12 10:55:28 +02:00
|
|
|
} from "vue";
|
2022-08-26 16:08:58 +02:00
|
|
|
import { LocationType } from "@/types/user-location.model";
|
|
|
|
import { useMutation, useQuery } from "@vue/apollo-composable";
|
|
|
|
import { initializeCurrentActor } from "@/utils/identity";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { useI18n } from "vue-i18n";
|
2022-08-26 16:08:58 +02:00
|
|
|
import { Snackbar } from "@/plugins/snackbar";
|
|
|
|
import { Notifier } from "@/plugins/notifier";
|
|
|
|
import { CONFIG } from "@/graphql/config";
|
|
|
|
import { IConfig } from "@/types/config.model";
|
|
|
|
import { useRouter } from "vue-router";
|
|
|
|
|
|
|
|
const { result: configResult } = useQuery<{ config: IConfig }>(CONFIG);
|
|
|
|
|
|
|
|
const config = computed(() => configResult.value?.config);
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const ErrorComponent = defineAsyncComponent(
|
2022-08-26 16:08:58 +02:00
|
|
|
() => import("@/components/ErrorComponent.vue")
|
2022-07-12 10:55:28 +02:00
|
|
|
);
|
2019-11-21 16:07:43 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const { t } = useI18n({ useScope: "global" });
|
2021-02-03 18:00:49 +01:00
|
|
|
|
2022-08-26 16:08:58 +02:00
|
|
|
const location = computed(() => config.value?.location);
|
2021-05-12 18:10:33 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const userLocation = reactive<LocationType>({
|
|
|
|
lon: undefined,
|
|
|
|
lat: undefined,
|
|
|
|
name: undefined,
|
|
|
|
picture: undefined,
|
|
|
|
isIPLocation: true,
|
|
|
|
accuracy: 100,
|
|
|
|
});
|
2021-05-27 18:24:11 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const updateUserLocation = (newLocation: LocationType) => {
|
|
|
|
userLocation.lat = newLocation.lat;
|
|
|
|
userLocation.lon = newLocation.lon;
|
|
|
|
userLocation.name = newLocation.name;
|
|
|
|
userLocation.picture = newLocation.picture;
|
|
|
|
userLocation.isIPLocation = newLocation.isIPLocation;
|
|
|
|
userLocation.accuracy = newLocation.accuracy;
|
|
|
|
};
|
2021-10-10 16:24:12 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
updateUserLocation({
|
|
|
|
lat: location.value?.latitude,
|
|
|
|
lon: location.value?.longitude,
|
|
|
|
name: "", // config.ipLocation.country.name,
|
|
|
|
isIPLocation: true,
|
|
|
|
accuracy: 150, // config.ipLocation.location.accuracy_radius * 1.5 || 150,
|
|
|
|
});
|
2018-12-21 15:41:34 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
provide("userLocation", {
|
|
|
|
userLocation,
|
|
|
|
updateUserLocation,
|
|
|
|
});
|
|
|
|
|
|
|
|
// const routerView = ref("routerView");
|
|
|
|
const error = ref<Error | null>(null);
|
|
|
|
const online = ref(true);
|
|
|
|
const interval = ref<number>(0);
|
|
|
|
|
|
|
|
const notifier = inject<Notifier>("notifier");
|
2021-02-03 18:00:49 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
interval.value = setInterval(async () => {
|
|
|
|
const accessToken = localStorage.getItem(AUTH_ACCESS_TOKEN);
|
|
|
|
if (accessToken) {
|
|
|
|
const token = jwt_decode<JwtPayload>(accessToken);
|
|
|
|
if (
|
|
|
|
token?.exp !== undefined &&
|
|
|
|
new Date(token.exp * 1000 - 60000) < new Date()
|
|
|
|
) {
|
|
|
|
refreshAccessToken();
|
2019-01-18 14:47:10 +01:00
|
|
|
}
|
2019-09-11 09:59:01 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
}, 60000) as unknown as number;
|
2021-05-12 18:10:33 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
onBeforeMount(async () => {
|
|
|
|
if (initializeCurrentUser()) {
|
|
|
|
await initializeCurrentActor();
|
2021-05-12 18:10:33 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
});
|
2021-05-12 18:10:33 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const snackbar = inject<Snackbar>("snackbar");
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
online.value = window.navigator.onLine;
|
|
|
|
window.addEventListener("offline", () => {
|
|
|
|
online.value = false;
|
|
|
|
showOfflineNetworkWarning();
|
|
|
|
console.debug("offline");
|
|
|
|
});
|
|
|
|
window.addEventListener("online", () => {
|
|
|
|
online.value = true;
|
|
|
|
console.debug("online");
|
|
|
|
});
|
|
|
|
document.addEventListener("refreshApp", (event: Event) => {
|
|
|
|
snackbar?.open({
|
|
|
|
queue: false,
|
|
|
|
indefinite: true,
|
|
|
|
variant: "dark",
|
|
|
|
actionText: t("Update app"),
|
|
|
|
cancelText: t("Ignore"),
|
|
|
|
message: t("A new version is available."),
|
|
|
|
onAction: async () => {
|
|
|
|
const registration = event.detail as ServiceWorkerRegistration;
|
|
|
|
try {
|
|
|
|
await refreshApp(registration);
|
|
|
|
window.location.reload();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
notifier?.error(t("An error has occured while refreshing the page."));
|
2021-06-18 18:19:04 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
},
|
2021-06-18 18:19:04 +02:00
|
|
|
});
|
2022-07-12 10:55:28 +02:00
|
|
|
});
|
|
|
|
});
|
2021-06-18 18:19:04 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
onUnmounted(() => {
|
|
|
|
clearInterval(interval.value);
|
|
|
|
interval.value = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
const { mutate: updateCurrentUser } = useMutation(UPDATE_CURRENT_USER_CLIENT);
|
2021-05-27 18:24:11 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const initializeCurrentUser = () => {
|
|
|
|
const userId = localStorage.getItem(AUTH_USER_ID);
|
|
|
|
const userEmail = localStorage.getItem(AUTH_USER_EMAIL);
|
|
|
|
const accessToken = localStorage.getItem(AUTH_ACCESS_TOKEN);
|
|
|
|
const role = localStorage.getItem(AUTH_USER_ROLE);
|
|
|
|
|
|
|
|
if (userId && userEmail && accessToken && role) {
|
|
|
|
updateCurrentUser({
|
|
|
|
id: userId,
|
|
|
|
email: userEmail,
|
|
|
|
isLoggedIn: true,
|
|
|
|
role,
|
|
|
|
});
|
|
|
|
return true;
|
2021-05-27 18:24:11 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return false;
|
|
|
|
};
|
2021-10-10 16:24:12 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const refreshApp = async (
|
|
|
|
registration: ServiceWorkerRegistration
|
|
|
|
): Promise<any> => {
|
|
|
|
const worker = registration.waiting;
|
|
|
|
if (!worker) {
|
|
|
|
return Promise.resolve();
|
2021-12-16 16:48:50 +01:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const channel = new MessageChannel();
|
2021-12-16 16:48:50 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
channel.port1.onmessage = (event) => {
|
|
|
|
if (event.data.error) {
|
|
|
|
reject(event.data);
|
|
|
|
} else {
|
|
|
|
resolve(event.data);
|
2021-10-13 12:52:57 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
};
|
|
|
|
worker?.postMessage({ type: "skip-waiting" }, [channel.port2]);
|
|
|
|
});
|
|
|
|
};
|
2021-10-10 16:24:12 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const showOfflineNetworkWarning = (): void => {
|
|
|
|
notifier?.error(t("You are offline"));
|
|
|
|
};
|
|
|
|
// const extractPageTitleFromRoute = (routeWatched: RouteLocation): string => {
|
|
|
|
// if (routeWatched.meta?.announcer?.message) {
|
|
|
|
// return routeWatched.meta?.announcer?.message();
|
|
|
|
// }
|
|
|
|
// return document.title;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// watch(route, (routeWatched) => {
|
|
|
|
// const pageTitle = extractPageTitleFromRoute(routeWatched);
|
|
|
|
// if (pageTitle) {
|
|
|
|
// // this.$announcer.polite(
|
|
|
|
// // t("Navigated to {pageTitle}", {
|
|
|
|
// // pageTitle,
|
|
|
|
// // }) as string
|
|
|
|
// // );
|
|
|
|
// }
|
|
|
|
// // Set the focus to the router view
|
|
|
|
// // https://marcus.io/blog/accessible-routing-vuejs
|
|
|
|
// setTimeout(() => {
|
|
|
|
// const focusTarget = (
|
|
|
|
// routerView.value?.$refs?.componentFocusTarget !== undefined
|
|
|
|
// ? routerView.value?.$refs?.componentFocusTarget
|
|
|
|
// : routerView.value?.$el
|
|
|
|
// ) as HTMLElement;
|
|
|
|
// if (focusTarget && focusTarget instanceof Element) {
|
|
|
|
// // Make focustarget programmatically focussable
|
|
|
|
// focusTarget.setAttribute("tabindex", "-1");
|
|
|
|
|
|
|
|
// // Focus element
|
|
|
|
// focusTarget.focus();
|
|
|
|
|
|
|
|
// // Remove tabindex from focustarget.
|
|
|
|
// // Reason: https://axesslab.com/skip-links/#update-3-a-comment-from-gov-uk
|
|
|
|
// focusTarget.removeAttribute("tabindex");
|
|
|
|
// }
|
|
|
|
// }, 0);
|
|
|
|
// });
|
|
|
|
|
2022-08-26 16:08:58 +02:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
watch(config, async (configWatched: IConfig | undefined) => {
|
|
|
|
if (configWatched) {
|
|
|
|
const { statistics } = await import("@/services/statistics");
|
|
|
|
statistics(configWatched?.analytics, {
|
|
|
|
router,
|
|
|
|
version: configWatched.version,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-07-12 10:55:28 +02:00
|
|
|
|
2022-08-26 16:08:58 +02:00
|
|
|
const isDemoMode = computed(() => config.value?.demoMode);
|
2018-01-09 17:52:26 +01:00
|
|
|
</script>
|
|
|
|
|
2019-04-03 17:29:03 +02:00
|
|
|
<style lang="scss">
|
2020-10-09 18:13:15 +02:00
|
|
|
#mobilizon {
|
|
|
|
min-height: 100vh;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
|
|
main {
|
|
|
|
flex-grow: 1;
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 16:24:12 +02:00
|
|
|
|
|
|
|
.vue-skip-to {
|
|
|
|
z-index: 40;
|
|
|
|
}
|
2018-01-09 17:52:26 +01:00
|
|
|
</style>
|