forked from yeatmanlab/roar-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeSelector.vue
150 lines (123 loc) · 4.66 KB
/
HomeSelector.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<div v-if="isLoading">
<div class="text-center col-full">
<AppSpinner />
<p class="text-center">{{ $t('homeSelector.loading') }}</p>
</div>
</div>
<div v-else>
<HomeParticipant v-if="isParticipant" />
<HomeAdministrator v-else-if="isAdminUser" />
</div>
<ConsentModal
v-if="!isLoading && showConsent && isAdminUser"
:consent-text="confirmText"
:consent-type="consentType"
:on-confirm="updateConsent"
/>
</template>
<script setup>
import { computed, defineAsyncComponent, onMounted, ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { storeToRefs } from 'pinia';
import _isEmpty from 'lodash/isEmpty';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import useUserType from '@/composables/useUserType';
import useUserDataQuery from '@/composables/queries/useUserDataQuery';
import useUserClaimsQuery from '@/composables/queries/useUserClaimsQuery';
import useUpdateConsentMutation from '@/composables/mutations/useUpdateConsentMutation';
import { CONSENT_TYPES } from '@/constants/consentTypes';
import { APP_ROUTES } from '@/constants/routes';
import { isLevante } from '@/helpers';
const HomeParticipant = defineAsyncComponent(() => import('@/pages/HomeParticipant.vue'));
const HomeAdministrator = defineAsyncComponent(() => import('@/pages/HomeAdministrator.vue'));
const ConsentModal = defineAsyncComponent(() => import('@/components/ConsentModal.vue'));
const authStore = useAuthStore();
const { roarfirekit, ssoProvider } = storeToRefs(authStore);
const router = useRouter();
const i18n = useI18n();
const { mutateAsync: updateConsentStatus } = useUpdateConsentMutation();
if (ssoProvider.value) {
console.log('Detected SSO authentication, redirecting...');
router.replace({ path: APP_ROUTES.SSO });
}
const gameStore = useGameStore();
const { requireRefresh } = storeToRefs(gameStore);
const initialized = ref(false);
let unsubscribe;
const init = () => {
if (unsubscribe) unsubscribe();
initialized.value = true;
};
unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useUserDataQuery(null, {
enabled: initialized,
});
const { isLoading: isLoadingClaims, data: userClaims } = useUserClaimsQuery({
enabled: initialized,
});
const { isAdmin, isSuperAdmin, isParticipant } = useUserType(userClaims);
const isAdminUser = computed(() => isAdmin.value || isSuperAdmin.value);
const isLoading = computed(() => {
// @NOTE: In addition to the loading states, we also check if user data and user claims are loaded as due to the
// current application initialization flow, the userData and userClaims queries initially reset. Once this is improved
// these additional checks can be removed.
return !initialized.value || isLoadingUserData.value || isLoadingClaims.value || !userData.value || !userClaims.value;
});
const showConsent = ref(false);
const consentType = computed(() => {
if (isAdminUser.value) {
return CONSENT_TYPES.TOS;
} else {
return i18n.locale.value.includes('es') ? CONSENT_TYPES.ASSENT_ES : CONSENT_TYPES.ASSENT;
}
});
const confirmText = ref('');
const consentVersion = ref('');
async function updateConsent() {
await updateConsentStatus({ consentType, consentVersion });
}
async function checkConsent() {
if (isLevante || !isAdminUser.value) return;
const consentStatus = userData.value?.legal?.[consentType.value];
const consentDoc = await authStore.getLegalDoc(consentType.value);
consentVersion.value = consentDoc.version;
if (!consentStatus?.[consentDoc.version]) {
confirmText.value = consentDoc.text;
showConsent.value = true;
return;
}
const legalDocs = consentStatus?.[consentDoc.version] || [];
if (!Array.isArray(legalDocs)) return;
const signedBeforeAugFirst = legalDocs.some((doc) => isSignedBeforeAugustFirst(doc.dateSigned));
if (signedBeforeAugFirst) {
confirmText.value = consentDoc.text;
showConsent.value = true;
}
}
function isSignedBeforeAugustFirst(signedDate) {
const currentDate = new Date();
const augustFirstThisYear = new Date(currentDate.getFullYear(), 7, 1); // August 1st of the current year
return new Date(signedDate) < augustFirstThisYear;
}
watch(
[userData, isAdminUser],
async ([updatedUserData, updatedAdminUserState]) => {
if (!_isEmpty(updatedUserData) && updatedAdminUserState) {
await checkConsent();
}
},
{ immediate: true },
);
onMounted(async () => {
if (requireRefresh.value) {
requireRefresh.value = false;
router.go(0);
}
if (roarfirekit.value.restConfig) init();
});
</script>