-
Notifications
You must be signed in to change notification settings - Fork 5
/
HomeSelector.vue
146 lines (119 loc) · 4.45 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
<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';
const HomeParticipant = defineAsyncComponent(() => import('@/pages/HomeParticipant.vue'));
const HomeAdministrator = defineAsyncComponent(() => import('@/pages/HomeAdministrator.vue'));
const ConsentModal = defineAsyncComponent(() => import('@/components/ConsentModal.vue'));
const isLevante = import.meta.env.MODE === 'LEVANTE';
const authStore = useAuthStore();
const { roarfirekit, authFromClever, authFromClassLink } = storeToRefs(authStore);
const router = useRouter();
const i18n = useI18n();
const { mutateAsync: updateConsentStatus } = useUpdateConsentMutation();
if (authFromClever.value) {
console.log('Detected Clever authentication, routing to CleverLanding page');
router.push({ name: 'CleverLanding' });
} else if (authFromClassLink.value) {
console.log('Detected ClassLink authentication, routing to ClassLinkLanding page');
router.push({ name: 'ClassLinkLanding' });
}
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(() => isLoadingClaims.value || isLoadingUserData.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] || [];
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>