Skip to content

Commit

Permalink
feat: add CRUD actions confirmation toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Aug 21, 2024
1 parent ae3b581 commit 01b6b40
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
12 changes: 8 additions & 4 deletions components/TokenDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<script setup lang="ts">
import { createItem, customEndpoint, updateItem } from '@directus/sdk';
import { formatDate } from '~/utils/date-formatters';
import { sendToast } from '~/utils/send-toast';
import { sendErrorToast, sendToast } from '~/utils/send-toast';
const props = defineProps({
token: {
Expand Down Expand Up @@ -231,7 +231,7 @@
emit('generate', response.id, token);
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
Expand All @@ -251,9 +251,11 @@
expire: expire.value && expire.value.toISOString().split('T')[0],
}));
sendToast('success', 'Done', 'Token info was successfully updated');
emit('save');
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
Expand All @@ -276,9 +278,11 @@
value: token,
}));
sendToast('success', 'Done', 'Token was successfully regenerated');
emit('regenerate', response.id, token);
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
</script>
6 changes: 3 additions & 3 deletions pages/credits.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
import type { PageState } from 'primevue/paginator';
import { useAuth } from '~/store/auth';
import { formatDateForTable } from '~/utils/date-formatters';
import { sendToast } from '~/utils/send-toast';
import { sendErrorToast } from '~/utils/send-toast';
useHead({
title: 'Credits -',
Expand Down Expand Up @@ -114,7 +114,7 @@
return { total: total[0]?.amount || 0, additions, deductions };
} catch (e) {
sendToast(e);
sendErrorToast(e);
throw e;
}
}, { default: () => ({ total: 0, additions: [], deductions: [] }) });
Expand Down Expand Up @@ -156,7 +156,7 @@
creditsChangesCount.value = additionsCount + deductionsCount;
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
loading.value = false;
Expand Down
8 changes: 4 additions & 4 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
import isEmpty from 'lodash/isEmpty';
import CountryFlag from 'vue-country-flag-next';
import { useAuth } from '~/store/auth';
import { sendToast } from '~/utils/send-toast';
import { sendErrorToast } from '~/utils/send-toast';
useHead({
title: 'Overview -',
Expand All @@ -159,7 +159,7 @@
}));
return result;
} catch (e) {
sendToast(e);
sendErrorToast(e);
throw e;
}
}, { default: () => [] });
Expand All @@ -182,7 +182,7 @@
}));
return result;
} catch (e) {
sendToast(e);
sendErrorToast(e);
throw e;
}
}, { default: () => [] });
Expand All @@ -204,7 +204,7 @@
}));
return result;
} catch (e) {
sendToast(e);
sendErrorToast(e);
throw e;
}
}, { default: () => [] });
Expand Down
6 changes: 3 additions & 3 deletions pages/probes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@
import CountryFlag from 'vue-country-flag-next';
import { useAuth } from '~/store/auth';
import { initGoogleMap } from '~/utils/init-google-map';
import { sendToast } from '~/utils/send-toast';
import { sendErrorToast } from '~/utils/send-toast';
const config = useRuntimeConfig();
Expand Down Expand Up @@ -389,7 +389,7 @@
probes.value = adoptedProbes;
probesCount.value = count;
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
loading.value = false;
Expand Down Expand Up @@ -526,7 +526,7 @@
const updatedProbe = await $directus.request(updateItem('gp_adopted_probes', id, newData));
probes.value = [ ...probes.value.map(probe => probe.id === updatedProbe.id ? updatedProbe : probe) ];
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
</script>
8 changes: 4 additions & 4 deletions pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<script setup lang="ts">
import { customEndpoint, deleteUser, updateMe } from '@directus/sdk';
import { useAuth } from '~/store/auth';
import { sendToast } from '~/utils/send-toast';
import { sendErrorToast } from '~/utils/send-toast';
useHead({
title: 'Settings -',
Expand Down Expand Up @@ -164,7 +164,7 @@
toast.add({ severity: 'success', summary: 'Saved', detail: 'All settings saved', life: 4000 });
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
saveLoading.value = false;
Expand Down Expand Up @@ -196,7 +196,7 @@
toast.add({ severity: 'success', summary: 'Synced', detail: 'GitHub data synced', life: 4000 });
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
loadingIconId.value = null;
Expand All @@ -210,7 +210,7 @@
await $directus.request(deleteUser(user.id!));
reloadNuxtApp();
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
</script>
12 changes: 8 additions & 4 deletions pages/tokens.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
import type { PageState } from 'primevue/paginator';
import { useAuth } from '~/store/auth';
import { formatDate, getRelativeTimeString } from '~/utils/date-formatters';
import { sendToast } from '~/utils/send-toast';
import { sendErrorToast, sendToast } from '~/utils/send-toast';
useHead({
title: 'Tokens -',
Expand Down Expand Up @@ -192,7 +192,7 @@
tokens.value = gpTokens;
tokensCount.value = count;
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
loading.value = false;
Expand Down Expand Up @@ -283,8 +283,10 @@
expandedRows.value = { [id]: true };
tokenToRegenerate.value = null;
regenerateDialog.value = false;
sendToast('success', 'Done', 'Token was successfully regenerated');
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
Expand Down Expand Up @@ -312,8 +314,10 @@
await loadLazyData();
tokenToDelete.value = null;
deleteDialog.value = false;
sendToast('success', 'Done', 'Token was deleted');
} catch (e) {
sendToast(e);
sendErrorToast(e);
}
};
</script>
8 changes: 7 additions & 1 deletion utils/send-toast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { ToastMessageOptions } from 'primevue/toast';
import { useToastService } from '~/composables/useToastService';

export const sendToast = (error: unknown) => {
export const sendToast = (severity: ToastMessageOptions['severity'], summary: string, detail: string = '') => {
const toastService = useToastService();
toastService.add({ severity, summary, detail, life: 5000 });
};

export const sendErrorToast = (error: unknown) => {
const toastService = useToastService();
const e = error as DashboardError;

Expand Down

0 comments on commit 01b6b40

Please sign in to comment.