Skip to content

Commit

Permalink
make favorites button reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectronicBlueberry committed Sep 2, 2024
1 parent 16977c0 commit 5065668
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
31 changes: 20 additions & 11 deletions client/src/components/Panels/Buttons/FavoritesButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { library } from "@fortawesome/fontawesome-svg-core";
import { faStar } from "@fortawesome/free-regular-svg-icons";
import { faStar as faRegStar } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { watchImmediate } from "@vueuse/core";
import { BButton } from "bootstrap-vue";
import { storeToRefs } from "pinia";
import { computed, ref, watch } from "vue";
Expand All @@ -12,18 +13,29 @@ import { useUserStore } from "@/stores/userStore";
library.add(faStar, faRegStar);
interface Props {
query: string;
value?: boolean;
modelValue?: boolean;
query?: string;
}
const props = defineProps<Props>();
const currentValue = computed(() => props.value ?? props.modelValue ?? false);
const toggle = ref(false);
watchImmediate(
() => currentValue.value,
(val) => (toggle.value = val)
);
const emit = defineEmits<{
(e: "onFavorites", filter: string): void;
(e: "change", toggled: boolean): void;
(e: "update:modelValue", toggled: boolean): void;
}>();
const { isAnonymous } = storeToRefs(useUserStore());
const FAVORITES = ["#favorites", "#favs", "#favourites"];
const toggle = ref(false);
const tooltipText = computed(() => {
if (isAnonymous.value) {
Expand All @@ -40,17 +52,14 @@ const tooltipText = computed(() => {
watch(
() => props.query,
() => {
toggle.value = FAVORITES.includes(props.query);
toggle.value = FAVORITES.includes(props.query ?? "");
}
);
function onFavorites() {
function toggleFavorites() {
toggle.value = !toggle.value;
if (toggle.value) {
emit("onFavorites", "#favorites");
} else {
emit("onFavorites", "");
}
emit("update:modelValue", toggle.value);
emit("change", toggle.value);
}
</script>

Expand All @@ -63,7 +72,7 @@ function onFavorites() {
aria-label="Show favorite tools"
:disabled="isAnonymous"
:title="tooltipText"
@click="onFavorites">
@click="toggleFavorites">
<FontAwesomeIcon :icon="toggle ? faRegStar : faStar" />
</BButton>
</template>
10 changes: 9 additions & 1 deletion client/src/components/Panels/ToolPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ function onInsertWorkflow(workflowId: string | undefined, workflowName: string)
function onInsertWorkflowSteps(workflowId: string, workflowStepCount: number | undefined) {
emit("onInsertWorkflowSteps", workflowId, workflowStepCount);
}
function onFavorites(favorites: boolean) {
if (favorites) {
query.value = "#favorites";
} else {
query.value = "";
}
}
</script>

<template>
Expand Down Expand Up @@ -169,7 +177,7 @@ function onInsertWorkflowSteps(workflowId: string, workflowStepCount: number | u
</template>
</PanelViewMenu>
<div v-if="!showAdvanced" class="panel-header-buttons">
<FavoritesButton :query="query" @onFavorites="(q) => (query = q)" />
<FavoritesButton :query="query" @toggleFavorites="onFavorites" />
</div>
</div>
</div>
Expand Down

0 comments on commit 5065668

Please sign in to comment.