convert user composable to ts

This commit is contained in:
Laila Los
2022-12-08 14:05:53 +01:00
parent e7a1184258
commit 2235366ce3
@@ -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<boolean> } 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<boolean> = false) {
// TODO: add store typing
const store = inject("store") as Store<unknown>;
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<string[]>([]);
/**
* 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);
};