From 2235366ce35b33938d6952073825b59bb65f44cd Mon Sep 17 00:00:00 2001 From: Laila Los <44241786+ElectronicBlueberry@users.noreply.github.com> Date: Tue, 6 Dec 2022 19:06:29 +0100 Subject: [PATCH] convert user composable to ts --- client/src/composables/{user.js => user.ts} | 29 ++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) rename client/src/composables/{user.js => user.ts} (53%) diff --git a/client/src/composables/user.js b/client/src/composables/user.ts similarity index 53% rename from client/src/composables/user.js rename to client/src/composables/user.ts index 4d2c42245e5..0f777bb36bc 100644 --- a/client/src/composables/user.js +++ b/client/src/composables/user.ts @@ -1,12 +1,16 @@ import { computed, onMounted, inject, ref, unref } from "vue"; +import type { Ref } from "vue"; +import type { Store } from "vuex"; +// TODO: support computed for "noFetch" /** * composable user store wrapper - * @param { boolean | ref } noFetch when true, the user will not be fetched from the server + * @param noFetch when true, the user will not be fetched from the server * @returns currentUser computed */ -export function useCurrentUser(noFetch = false) { - const store = inject("store"); +export function useCurrentUser(noFetch: boolean | Ref = false) { + // TODO: add store typing + const store = inject("store") as Store; const currentUser = computed(() => store.getters["user/currentUser"]); @@ -16,14 +20,19 @@ export function useCurrentUser(noFetch = false) { } }); - const addFavoriteTool = async (toolId) => await store.dispatch("user/addFavoriteTool", toolId); - const removeFavoriteTool = async (toolId) => await store.dispatch("user/removeFavoriteTool", toolId); + const addFavoriteTool = async (toolId: string) => { + await store.dispatch("user/addFavoriteTool", toolId); + }; + + const removeFavoriteTool = async (toolId: string) => { + await store.dispatch("user/removeFavoriteTool", toolId); + }; return { currentUser, addFavoriteTool, removeFavoriteTool }; } // temporarily stores tags which have not yet been fetched from the backend -const localTags = ref([]); +const localTags = ref([]); /** * Keeps tracks of the tags the current user has used. @@ -32,7 +41,7 @@ export function useUserTags() { const { currentUser } = useCurrentUser(true); const userTags = computed(() => { - let tags; + let tags: string[]; if (currentUser.value) { tags = [...currentUser.value.tags_used, ...localTags.value]; @@ -40,10 +49,12 @@ export function useUserTags() { tags = localTags.value; } - return tags.map((tag) => tag.replace(/^name:/, "#")); + const tagSet = new Set(tags); + + return Array.from(tagSet).map((tag) => tag.replace(/^name:/, "#")); }); - const addLocalTag = (tag) => { + const addLocalTag = (tag: string) => { localTags.value.push(tag); };