Skip to content

Commit

Permalink
Replace all userStudentData queries with composable
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianoertel committed Aug 20, 2024
1 parent 3840229 commit 7b516f6
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 94 deletions.
13 changes: 4 additions & 9 deletions src/components/tasks/TaskCrowding.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand All @@ -29,7 +28,7 @@ const router = useRouter();
const gameStarted = ref(false);
const authStore = useAuthStore();
const gameStore = useGameStore();
const { isFirekitInit, roarfirekit, uid } = storeToRefs(authStore);
const { isFirekitInit, roarfirekit } = storeToRefs(authStore);
const initialized = ref(false);
let unsubscribe;
Expand All @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
13 changes: 4 additions & 9 deletions src/components/tasks/TaskFluency.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand All @@ -29,7 +28,7 @@ const router = useRouter();
const gameStarted = ref(false);
const authStore = useAuthStore();
const gameStore = useGameStore();
const { isFirekitInit, roarfirekit, uid } = storeToRefs(authStore);
const { isFirekitInit, roarfirekit } = storeToRefs(authStore);
const initialized = ref(false);
let unsubscribe;
Expand All @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskLetter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
13 changes: 4 additions & 9 deletions src/components/tasks/TaskLevante.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand All @@ -29,7 +28,7 @@ const router = useRouter();
const gameStarted = ref(false);
const authStore = useAuthStore();
const gameStore = useGameStore();
const { isFirekitInit, roarfirekit, uid } = storeToRefs(authStore);
const { isFirekitInit, roarfirekit } = storeToRefs(authStore);
const initialized = ref(false);
let unsubscribe;
Expand All @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskMEP.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskMultichoice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskPA.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskRan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskSRE.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskSWR.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, onBeforeUnmount, watch, ref } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
11 changes: 3 additions & 8 deletions src/components/tasks/TaskVocab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import useUserStudentDataQuery from '@/composables/queries/useUserStudentDataQuery';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
Expand Down Expand Up @@ -45,12 +44,8 @@ unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', uid, 'studentData'],
queryFn: () => fetchDocById('users', uid.value, ['studentData']),
keepPreviousData: true,
const { isLoading: isLoadingUserData, data: userData } = useUserStudentDataQuery({
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
Expand Down
6 changes: 3 additions & 3 deletions src/composables/queries/useUserStudentDataQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from '@tanstack/vue-query';
import { storeToRefs } from 'pinia';
import { useAuthStore } from '@/store/auth';
import { fetchDocById } from '@/helpers/query/utils';
import { USER_DATA_QUERY_KEY } from '@/constants/queryKeys';
import { USER_STUDENT_DATA_QUERY_KEY } from '@/constants/queryKeys';
import { FIRESTORE_COLLECTIONS } from '@/constants/firebase';

/**
Expand All @@ -13,10 +13,10 @@ import { FIRESTORE_COLLECTIONS } from '@/constants/firebase';
*/
const useUserStudentDataQuery = (queryOptions = undefined) => {
const authStore = useAuthStore();
const { uid, userQueryKeyIndex } = storeToRefs(authStore);
const { uid } = storeToRefs(authStore);

return useQuery({
queryKey: [USER_DATA_QUERY_KEY, uid.value, userQueryKeyIndex.value],
queryKey: [USER_STUDENT_DATA_QUERY_KEY, uid.value],
queryFn: () => fetchDocById(FIRESTORE_COLLECTIONS.USERS, uid.value, ['studentData']),
...queryOptions,
});
Expand Down
1 change: 1 addition & 0 deletions src/constants/queryKeys.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const USER_DATA_QUERY_KEY = 'user';
export const USER_STUDENT_DATA_QUERY_KEY = 'user-student';
export const USER_CLAIMS_QUERY_KEY = 'user-claims';

0 comments on commit 7b516f6

Please sign in to comment.