2025-07-01 02:59:08 +01:00
|
|
|
<script setup lang="ts">
|
2025-08-14 21:48:38 +01:00
|
|
|
import { CheckIcon, MailIcon } from '@modrinth/assets'
|
|
|
|
|
import { ButtonStyled } from '@modrinth/ui'
|
|
|
|
|
import { ref } from 'vue'
|
2025-07-01 02:59:08 +01:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
import { useBaseFetch } from '~/composables/fetch.js'
|
|
|
|
|
|
|
|
|
|
const auth = await useAuth()
|
|
|
|
|
const showSubscriptionConfirmation = ref(false)
|
2025-07-27 22:29:21 +02:00
|
|
|
const showSubscribeButton = useAsyncData(
|
2025-08-14 21:48:38 +01:00
|
|
|
async () => {
|
|
|
|
|
if (auth.value?.user) {
|
|
|
|
|
try {
|
|
|
|
|
const { subscribed } = await useBaseFetch('auth/email/subscribe', {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
return !subscribed
|
|
|
|
|
} catch {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ watch: [auth], server: false },
|
|
|
|
|
)
|
2025-07-01 02:59:08 +01:00
|
|
|
|
|
|
|
|
async function subscribe() {
|
2025-08-14 21:48:38 +01:00
|
|
|
try {
|
|
|
|
|
await useBaseFetch('auth/email/subscribe', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
})
|
|
|
|
|
showSubscriptionConfirmation.value = true
|
|
|
|
|
} catch {
|
|
|
|
|
// Ignored
|
|
|
|
|
} finally {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showSubscriptionConfirmation.value = false
|
|
|
|
|
showSubscribeButton.status.value = 'success'
|
|
|
|
|
showSubscribeButton.data.value = false
|
|
|
|
|
}, 2500)
|
|
|
|
|
}
|
2025-07-01 02:59:08 +01:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-08-14 21:48:38 +01:00
|
|
|
<ButtonStyled
|
|
|
|
|
v-if="showSubscribeButton.status.value === 'success' && showSubscribeButton.data.value"
|
|
|
|
|
color="brand"
|
|
|
|
|
type="outlined"
|
|
|
|
|
>
|
|
|
|
|
<button v-tooltip="`Subscribe to the Modrinth newsletter`" @click="subscribe">
|
|
|
|
|
<template v-if="!showSubscriptionConfirmation"> <MailIcon /> Subscribe </template>
|
|
|
|
|
<template v-else> <CheckIcon /> Subscribed! </template>
|
|
|
|
|
</button>
|
|
|
|
|
</ButtonStyled>
|
2025-07-01 02:59:08 +01:00
|
|
|
</template>
|