Skip to content

Commit

Permalink
fix: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Nov 24, 2024
1 parent d14e062 commit e4b2e83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
9 changes: 7 additions & 2 deletions apps/ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ApiSpace as OffchainApiSpace } from '@/networks/offchain/api/types';
// UI
export type NotificationType = 'error' | 'warning' | 'success';

export type EmailSubscriptionType =
| 'summary'
| 'newProposal'
| 'closedProposal';

export type ProposalState =
| 'pending'
| 'active'
Expand Down Expand Up @@ -306,8 +311,8 @@ export type User = {
} & Partial<UserProfile>;

export type EmailSubscription = {
status: string;
subscriptions: string[];
status: 'NOT_SUBSCRIBED' | 'UNVERIFIED' | 'VERIFIED';
subscriptions: EmailSubscriptionType[];
};

export type UserActivity = {
Expand Down
65 changes: 28 additions & 37 deletions apps/ui/src/views/Settings/EmailNotifications.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" setup>
import { clone } from '@/helpers/utils';
import { getValidator } from '@/helpers/validation';
import { EmailSubscriptionType } from '@/types';
useTitle('Email notifications');
Expand Down Expand Up @@ -56,7 +57,6 @@ const SUBSCRIPTIONS_TYPE = [
];
const usersStore = useUsersStore();
const { modalAccountWithoutDismissOpen } = useModal();
const { web3 } = useWeb3();
const user = computed(() => usersStore.getUser(web3.value.account));
Expand All @@ -66,9 +66,9 @@ const loading = computed(
const form: Ref<{
email: string;
subscriptions: string[];
subscriptions: EmailSubscriptionType[];
}> = ref(clone(CREATE_SUBSCRIBE_FORM_STATE));
const subscriptions = reactive({
const subscriptions = reactive<Record<EmailSubscriptionType, boolean>>({
summary: true,
newProposal: true,
closedProposal: true
Expand All @@ -94,40 +94,47 @@ watch(
if (user.value?.emailSubscription?.subscriptions) {
Object.keys(subscriptions).forEach(key => {
subscriptions[key] =
user.value?.emailSubscription?.subscriptions.includes(key);
user.value?.emailSubscription?.subscriptions.includes(
key as EmailSubscriptionType
);
});
}
},
{ immediate: true }
);
watch(
[() => web3.value.account, () => web3.value.authLoading],
([account, authLoading]) => {
if (!account && !authLoading) {
modalAccountWithoutDismissOpen.value = true;
}
},
{ immediate: true }
);
watchEffect(async () => {
formValidated.value = false;
formErrors.value = await formValidator.validateAsync(form);
formValidated.value = true;
});
onUnmounted(() => {
modalAccountWithoutDismissOpen.value = false;
});
</script>

<template>
<UiLabel label="Email notifications" />
<UiLoading v-if="loading" class="p-4 block" />
<div v-else-if="user" class="p-4 max-w-[640px]">
<template v-if="user.emailSubscription?.status === 'UNVERIFIED'">
<div class="p-4 max-w-[640px]">
<UiLoading v-if="loading" class="block" />
<template
v-else-if="!user || user.emailSubscription?.status === 'NOT_SUBSCRIBED'"
>
<h3 class="text-md leading-6">Receive email notifications</h3>
<div class="mb-3">
Stay updated with the latest and important updates directly on your
inbox.
</div>

<div class="s-box">
<UiForm
:error="formErrors"
:model-value="form"
:definition="DEFINITION"
/>
<UiButton @click="handleCreateSubscribeClick">Subscribe now</UiButton>
</div>
</template>

<template v-else-if="user.emailSubscription?.status === 'UNVERIFIED'">
<h3 class="text-md leading-6">Confirm your email</h3>
<div class="mb-3">
We've sent an email to your email address.
Expand Down Expand Up @@ -163,21 +170,5 @@ onUnmounted(() => {
</UiButton>
</div>
</template>
<template v-else>
<h3 class="text-md leading-6">Receive email notifications</h3>
<div class="mb-3">
Stay updated with the latest and important updates directly on your
inbox.
</div>

<div class="s-box">
<UiForm
:error="formErrors"
:model-value="form"
:definition="DEFINITION"
/>
<UiButton @click="handleCreateSubscribeClick">Subscribe now</UiButton>
</div>
</template>
</div>
</template>

0 comments on commit e4b2e83

Please sign in to comment.