Skip to content

Commit

Permalink
Fix behavior of StarButton
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Dec 31, 2024
1 parent 226b5ba commit 0d1aace
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions web/src/components/StarButton.vue
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
<template>
<v-btn
icon
:disabled="!loggedIn"
@click="toggleStar"
>
<v-icon :color="isStarred ? 'amber darken-2' : undefined">
{{ isStarred ? 'mdi-star' : 'mdi-star-outline' }}
</v-icon>
<span class="ml-1">{{ starCount }}</span>
</v-btn>
<div>
<v-btn icon :disabled="!loggedIn" @click.prevent="toggleStar">
<v-icon :color="isStarred ? 'amber darken-2' : undefined">
{{ isStarred ? 'mdi-star' : 'mdi-star-outline' }}
</v-icon>
</v-btn>
<span class="ml-1 text-button">{{ starCount }}</span>
</div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { computed, ref, toRefs } from 'vue';
import { dandiRest, loggedIn as loggedInFunc } from '@/rest';
const props = defineProps<{
identifier: string;
initialStarCount: number;
initialIsStarred: boolean;
}>();
const { initialIsStarred, initialStarCount } = toRefs(props)
const loggedIn = computed(loggedInFunc);
const isStarred = ref(props.initialIsStarred);
const starCount = ref(props.initialStarCount);
const offset = ref(0);
const starCount = computed(() => initialStarCount.value + offset.value);
const isStarred = computed(() => (initialIsStarred.value && offset.value === 0) || (!initialIsStarred.value && offset.value === 1));
async function toggleStar() {
try {
if (isStarred.value) {
await dandiRest.unstarDandiset(props.identifier);
starCount.value--;
offset.value -= 1;
} else {
await dandiRest.starDandiset(props.identifier);
starCount.value++;
offset.value += 1;
}
isStarred.value = !isStarred.value;
} catch (error) {
console.error('Error toggling star:', error);
}
}
</script>
</script>

0 comments on commit 0d1aace

Please sign in to comment.