add notifiaction handling, error handling
This commit is contained in:
parent
ce708f5209
commit
1af288aa62
21 changed files with 4864 additions and 224 deletions
|
|
@ -11,24 +11,21 @@ import {
|
|||
PathArrowSolid,
|
||||
Clock,
|
||||
HistoricShield,
|
||||
Developer,
|
||||
} from "@iconoir/vue";
|
||||
import SpeedDial from "primevue/speeddial";
|
||||
|
||||
const router = useRouter();
|
||||
const modalStore = useModalStore();
|
||||
const categories = [
|
||||
{ name: "Home", icon: Home, url: "/" },
|
||||
{ name: "Calendar", icon: Calendar, url: "/calendar" },
|
||||
{ name: "Clients", icon: Community, url: "/clients" },
|
||||
{ name: "Jobs", icon: Hammer, url: "/jobs" },
|
||||
{ name: "Routes", icon: PathArrowSolid, url: "/routes" },
|
||||
{ name: "Time Sheets", icon: Clock, url: "/timesheets" },
|
||||
{ name: "Warranties", icon: HistoricShield, url: "/warranties" },
|
||||
|
||||
const developmentButtons = ref([
|
||||
{
|
||||
name: "Create New",
|
||||
icon: MultiplePagesPlus,
|
||||
label: "Error Handling Demo",
|
||||
command: () => {
|
||||
router.push("/dev/error-handling-demo");
|
||||
},
|
||||
},
|
||||
];
|
||||
]);
|
||||
|
||||
const createButtons = ref([
|
||||
{
|
||||
|
|
@ -70,6 +67,22 @@ const createButtons = ref([
|
|||
},
|
||||
},
|
||||
]);
|
||||
|
||||
const categories = ref([
|
||||
{ name: "Home", icon: Home, url: "/" },
|
||||
{ name: "Calendar", icon: Calendar, url: "/calendar" },
|
||||
{ name: "Clients", icon: Community, url: "/clients" },
|
||||
{ name: "Jobs", icon: Hammer, url: "/jobs" },
|
||||
{ name: "Routes", icon: PathArrowSolid, url: "/routes" },
|
||||
{ name: "Time Sheets", icon: Clock, url: "/timesheets" },
|
||||
{ name: "Warranties", icon: HistoricShield, url: "/warranties" },
|
||||
{
|
||||
name: "Create New",
|
||||
icon: MultiplePagesPlus,
|
||||
buttons: createButtons,
|
||||
},
|
||||
{ name: "Development", icon: Developer, buttons: developmentButtons },
|
||||
]);
|
||||
const handleCategoryClick = (category) => {
|
||||
router.push(category.url);
|
||||
};
|
||||
|
|
@ -94,7 +107,7 @@ const handleCategoryClick = (category) => {
|
|||
</button>
|
||||
</template>
|
||||
<template v-else>
|
||||
<SpeedDial :model="createButtons" direction="down" type="linear" radius="50">
|
||||
<SpeedDial :model="category.buttons" direction="down" type="linear" radius="50">
|
||||
<template #button="{ toggleCallback }">
|
||||
<button
|
||||
class="sidebar-button"
|
||||
|
|
|
|||
438
frontend/src/components/common/NotificationDisplay.vue
Normal file
438
frontend/src/components/common/NotificationDisplay.vue
Normal file
|
|
@ -0,0 +1,438 @@
|
|||
<template>
|
||||
<div class="notification-container" :class="positionClass">
|
||||
<TransitionGroup name="notification" tag="div" class="notification-list">
|
||||
<div
|
||||
v-for="notification in activeNotifications"
|
||||
:key="notification.id"
|
||||
:class="notificationClass(notification)"
|
||||
class="notification"
|
||||
@click="markAsSeen(notification.id)"
|
||||
>
|
||||
<!-- Notification Header -->
|
||||
<div class="notification-header">
|
||||
<div class="notification-icon">
|
||||
<i :class="getIcon(notification.type)"></i>
|
||||
</div>
|
||||
<div class="notification-content">
|
||||
<h4 v-if="notification.title" class="notification-title">
|
||||
{{ notification.title }}
|
||||
</h4>
|
||||
<p class="notification-message">{{ notification.message }}</p>
|
||||
</div>
|
||||
<button
|
||||
@click.stop="dismissNotification(notification.id)"
|
||||
class="notification-close"
|
||||
type="button"
|
||||
>
|
||||
<i class="mdi mdi-close"></i>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Notification Actions -->
|
||||
<div
|
||||
v-if="notification.actions && notification.actions.length > 0"
|
||||
class="notification-actions"
|
||||
>
|
||||
<button
|
||||
v-for="action in notification.actions"
|
||||
:key="action.label"
|
||||
@click.stop="handleAction(action, notification)"
|
||||
:class="action.variant || 'primary'"
|
||||
class="notification-action-btn"
|
||||
type="button"
|
||||
>
|
||||
<i v-if="action.icon" :class="action.icon"></i>
|
||||
{{ action.label }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Progress Bar for timed notifications -->
|
||||
<div
|
||||
v-if="!notification.persistent && notification.duration > 0"
|
||||
class="notification-progress"
|
||||
>
|
||||
<div
|
||||
class="notification-progress-bar"
|
||||
:style="{ animationDuration: notification.duration + 'ms' }"
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
</TransitionGroup>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { computed } from "vue";
|
||||
import { useNotificationStore } from "../../stores/notifications-primevue";
|
||||
|
||||
export default {
|
||||
name: "NotificationDisplay",
|
||||
setup() {
|
||||
const notificationStore = useNotificationStore();
|
||||
|
||||
const activeNotifications = computed(() => notificationStore.activeNotifications);
|
||||
|
||||
const positionClass = computed(
|
||||
() => `notification-container--${notificationStore.position}`,
|
||||
);
|
||||
|
||||
const notificationClass = (notification) => [
|
||||
`notification--${notification.type}`,
|
||||
{
|
||||
"notification--seen": notification.seen,
|
||||
"notification--persistent": notification.persistent,
|
||||
},
|
||||
];
|
||||
|
||||
const getIcon = (type) => {
|
||||
const icons = {
|
||||
success: "mdi mdi-check-circle",
|
||||
error: "mdi mdi-alert-circle",
|
||||
warning: "mdi mdi-alert",
|
||||
info: "mdi mdi-information",
|
||||
};
|
||||
return icons[type] || icons.info;
|
||||
};
|
||||
|
||||
const dismissNotification = (id) => {
|
||||
notificationStore.dismissNotification(id);
|
||||
};
|
||||
|
||||
const markAsSeen = (id) => {
|
||||
notificationStore.markAsSeen(id);
|
||||
};
|
||||
|
||||
const handleAction = (action, notification) => {
|
||||
if (action.handler) {
|
||||
action.handler(notification);
|
||||
}
|
||||
|
||||
// Auto-dismiss notification after action unless specified otherwise
|
||||
if (action.dismissAfter !== false) {
|
||||
dismissNotification(notification.id);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
activeNotifications,
|
||||
positionClass,
|
||||
notificationClass,
|
||||
getIcon,
|
||||
dismissNotification,
|
||||
markAsSeen,
|
||||
handleAction,
|
||||
};
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.notification-container {
|
||||
position: fixed;
|
||||
z-index: 9999;
|
||||
pointer-events: none;
|
||||
max-width: 400px;
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
/* Position variants */
|
||||
.notification-container--top-right {
|
||||
top: 0;
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.notification-container--top-left {
|
||||
top: 0;
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.notification-container--bottom-right {
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.notification-container--bottom-left {
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.notification-container--top-center {
|
||||
top: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
}
|
||||
|
||||
.notification-container--bottom-center {
|
||||
bottom: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
}
|
||||
|
||||
.notification-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.notification {
|
||||
pointer-events: auto;
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
border-left: 4px solid;
|
||||
overflow: hidden;
|
||||
min-width: 320px;
|
||||
max-width: 100%;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
/* Notification type variants */
|
||||
.notification--success {
|
||||
border-left-color: #10b981;
|
||||
}
|
||||
|
||||
.notification--error {
|
||||
border-left-color: #ef4444;
|
||||
}
|
||||
|
||||
.notification--warning {
|
||||
border-left-color: #f59e0b;
|
||||
}
|
||||
|
||||
.notification--info {
|
||||
border-left-color: #3b82f6;
|
||||
}
|
||||
|
||||
.notification-header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
padding: 1rem;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.notification-icon {
|
||||
flex-shrink: 0;
|
||||
font-size: 1.25rem;
|
||||
margin-top: 0.125rem;
|
||||
}
|
||||
|
||||
.notification--success .notification-icon {
|
||||
color: #10b981;
|
||||
}
|
||||
|
||||
.notification--error .notification-icon {
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.notification--warning .notification-icon {
|
||||
color: #f59e0b;
|
||||
}
|
||||
|
||||
.notification--info .notification-icon {
|
||||
color: #3b82f6;
|
||||
}
|
||||
|
||||
.notification-content {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.notification-title {
|
||||
margin: 0 0 0.25rem 0;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.notification-message {
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
color: #6b7280;
|
||||
line-height: 1.4;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.notification-close {
|
||||
flex-shrink: 0;
|
||||
background: none;
|
||||
border: none;
|
||||
color: #9ca3af;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
border-radius: 4px;
|
||||
transition: all 0.2s;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.notification-close:hover {
|
||||
color: #6b7280;
|
||||
background-color: #f3f4f6;
|
||||
}
|
||||
|
||||
.notification-actions {
|
||||
padding: 0 1rem 1rem 1rem;
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
margin-top: -0.25rem;
|
||||
}
|
||||
|
||||
.notification-action-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border: 1px solid #d1d5db;
|
||||
background: white;
|
||||
color: #374151;
|
||||
border-radius: 6px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.notification-action-btn:hover {
|
||||
background: #f9fafb;
|
||||
border-color: #9ca3af;
|
||||
}
|
||||
|
||||
.notification-action-btn.primary {
|
||||
background: #3b82f6;
|
||||
border-color: #3b82f6;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.notification-action-btn.primary:hover {
|
||||
background: #2563eb;
|
||||
}
|
||||
|
||||
.notification-action-btn.danger {
|
||||
background: #ef4444;
|
||||
border-color: #ef4444;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.notification-action-btn.danger:hover {
|
||||
background: #dc2626;
|
||||
}
|
||||
|
||||
.notification-progress {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 3px;
|
||||
background: rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.notification-progress-bar {
|
||||
height: 100%;
|
||||
background: currentColor;
|
||||
opacity: 0.3;
|
||||
animation: progress-decrease linear forwards;
|
||||
transform-origin: left;
|
||||
}
|
||||
|
||||
.notification--success .notification-progress-bar {
|
||||
background: #10b981;
|
||||
}
|
||||
|
||||
.notification--error .notification-progress-bar {
|
||||
background: #ef4444;
|
||||
}
|
||||
|
||||
.notification--warning .notification-progress-bar {
|
||||
background: #f59e0b;
|
||||
}
|
||||
|
||||
.notification--info .notification-progress-bar {
|
||||
background: #3b82f6;
|
||||
}
|
||||
|
||||
@keyframes progress-decrease {
|
||||
from {
|
||||
width: 100%;
|
||||
}
|
||||
to {
|
||||
width: 0%;
|
||||
}
|
||||
}
|
||||
|
||||
/* Transition animations */
|
||||
.notification-enter-active {
|
||||
transition: all 0.3s ease-out;
|
||||
}
|
||||
|
||||
.notification-leave-active {
|
||||
transition: all 0.3s ease-in;
|
||||
}
|
||||
|
||||
.notification-enter-from {
|
||||
opacity: 0;
|
||||
transform: translateX(100%);
|
||||
}
|
||||
|
||||
.notification-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateX(100%);
|
||||
}
|
||||
|
||||
/* Adjustments for left-positioned containers */
|
||||
.notification-container--top-left .notification-enter-from,
|
||||
.notification-container--bottom-left .notification-enter-from,
|
||||
.notification-container--top-left .notification-leave-to,
|
||||
.notification-container--bottom-left .notification-leave-to {
|
||||
transform: translateX(-100%);
|
||||
}
|
||||
|
||||
/* Adjustments for center-positioned containers */
|
||||
.notification-container--top-center .notification-enter-from,
|
||||
.notification-container--bottom-center .notification-enter-from,
|
||||
.notification-container--top-center .notification-leave-to,
|
||||
.notification-container--bottom-center .notification-leave-to {
|
||||
transform: translateY(-100%);
|
||||
}
|
||||
|
||||
.notification-container--bottom-center .notification-enter-from,
|
||||
.notification-container--bottom-center .notification-leave-to {
|
||||
transform: translateY(100%);
|
||||
}
|
||||
|
||||
/* Hover effects */
|
||||
.notification:hover {
|
||||
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.notification--seen {
|
||||
opacity: 0.95;
|
||||
}
|
||||
|
||||
/* Responsive adjustments */
|
||||
@media (max-width: 640px) {
|
||||
.notification-container {
|
||||
left: 0;
|
||||
right: 0;
|
||||
max-width: none;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.notification-container--top-center,
|
||||
.notification-container--bottom-center {
|
||||
transform: none;
|
||||
}
|
||||
|
||||
.notification {
|
||||
min-width: auto;
|
||||
}
|
||||
|
||||
.notification-header {
|
||||
padding: 0.75rem;
|
||||
}
|
||||
|
||||
.notification-actions {
|
||||
padding: 0 0.75rem 0.75rem 0.75rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -35,20 +35,44 @@ import Tab from "primevue/tab";
|
|||
import TabPanels from "primevue/tabpanels";
|
||||
import TabPanel from "primevue/tabpanel";
|
||||
import Api from "../../api";
|
||||
import ApiWithToast from "../../api-toast";
|
||||
import { useLoadingStore } from "../../stores/loading";
|
||||
import { useErrorStore } from "../../stores/errors";
|
||||
|
||||
const loadingStore = useLoadingStore();
|
||||
const errorStore = useErrorStore();
|
||||
const clientNames = ref([]);
|
||||
const client = ref({});
|
||||
const { clientName } = defineProps({
|
||||
clientName: { type: String, required: true },
|
||||
});
|
||||
|
||||
const getClientNames = async (type) => {
|
||||
loadingStore.setLoading(true);
|
||||
try {
|
||||
const names = await Api.getCustomerNames(type);
|
||||
clientNames.value = names;
|
||||
} catch (error) {
|
||||
errorStore.addError(error.message || "Error fetching client names");
|
||||
} finally {
|
||||
loadingStore.setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getClient = async (name) => {
|
||||
loadingStore.setLoading(true);
|
||||
const clientData = await ApiWithToast.makeApiCall(() => Api.getClient(name));
|
||||
client.value = clientData || {};
|
||||
loadingStore.setLoading(false);
|
||||
};
|
||||
|
||||
onMounted(async () => {
|
||||
if (clientName === "new") {
|
||||
// Logic for creating a new client
|
||||
console.log("Creating a new client");
|
||||
} else {
|
||||
// Logic for fetching and displaying existing client data
|
||||
const clientData = await Api.getClient(clientName);
|
||||
client.value = clientData;
|
||||
await getClient(clientName);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
664
frontend/src/components/pages/ErrorHandlingDemo.vue
Normal file
664
frontend/src/components/pages/ErrorHandlingDemo.vue
Normal file
|
|
@ -0,0 +1,664 @@
|
|||
<template>
|
||||
<div class="error-handling-example">
|
||||
<h3>PrimeVue Toast Error Handling Demo</h3>
|
||||
|
||||
<!-- Error Display Section -->
|
||||
<div class="error-section" v-if="errorStore.hasAnyError">
|
||||
<h4>Current Component Errors (for debugging):</h4>
|
||||
<div class="error-list">
|
||||
<div v-if="errorStore.lastError" class="error-item global-error">
|
||||
<strong>Global Error:</strong> {{ errorStore.lastError.message }}
|
||||
<button @click="errorStore.clearGlobalError()" class="clear-btn">Clear</button>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(error, component) in errorStore.componentErrors"
|
||||
:key="component"
|
||||
v-if="error"
|
||||
class="error-item component-error"
|
||||
>
|
||||
<strong>{{ component }} Error:</strong> {{ error.message }}
|
||||
<button @click="errorStore.clearComponentError(component)" class="clear-btn">
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Demo Buttons -->
|
||||
<div class="demo-section">
|
||||
<h4>Test PrimeVue Toast Notifications:</h4>
|
||||
<div class="button-group">
|
||||
<Button
|
||||
@click="testSuccessfulApiCall"
|
||||
label="Successful API Call"
|
||||
class="p-button-success"
|
||||
:loading="loadingStore.getComponentLoading('demo-success')"
|
||||
/>
|
||||
|
||||
<Button
|
||||
@click="testFailingApiCall"
|
||||
label="Failing API Call"
|
||||
class="p-button-danger"
|
||||
:loading="loadingStore.getComponentLoading('demo-fail')"
|
||||
/>
|
||||
|
||||
<Button
|
||||
@click="testRetryApiCall"
|
||||
label="API Call with Retry"
|
||||
class="p-button-warning"
|
||||
:loading="loadingStore.getComponentLoading('demo-retry')"
|
||||
/>
|
||||
|
||||
<Button
|
||||
@click="testDirectToasts"
|
||||
label="Direct Toast Messages"
|
||||
class="p-button-info"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="button-group">
|
||||
<Button @click="testComponentError" label="Component Error" severity="secondary" />
|
||||
|
||||
<Button @click="testGlobalError" label="Global Error" severity="secondary" />
|
||||
|
||||
<Button @click="clearAllErrors" label="Clear All Errors" severity="secondary" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Real API Demo with ApiWithToast -->
|
||||
<div class="api-demo-section">
|
||||
<h4>Real API Integration with ApiWithToast:</h4>
|
||||
<div class="button-group">
|
||||
<Button
|
||||
@click="loadClientData"
|
||||
label="Load Client Data"
|
||||
class="p-button-primary"
|
||||
:loading="loadingStore.getComponentLoading('clients')"
|
||||
/>
|
||||
|
||||
<Button
|
||||
@click="loadJobData"
|
||||
label="Load Job Data"
|
||||
class="p-button-primary"
|
||||
:loading="loadingStore.getComponentLoading('jobs')"
|
||||
/>
|
||||
|
||||
<Button
|
||||
@click="createTestClient"
|
||||
label="Create Test Client"
|
||||
class="p-button-success"
|
||||
:loading="loadingStore.getComponentLoading('form')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div v-if="clientData.length > 0" class="data-preview">
|
||||
<h5>Client Data ({{ clientData.length }} items):</h5>
|
||||
<pre>{{ JSON.stringify(clientData.slice(0, 2), null, 2) }}</pre>
|
||||
</div>
|
||||
|
||||
<div v-if="clientStatusData" class="data-preview">
|
||||
<h5>Client Status Counts:</h5>
|
||||
<div class="status-grid">
|
||||
<div
|
||||
v-for="(count, status) in clientStatusData"
|
||||
:key="status"
|
||||
class="status-item"
|
||||
>
|
||||
<strong>{{ status }}:</strong> {{ count }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Error History -->
|
||||
<div class="history-section">
|
||||
<h4>Error History:</h4>
|
||||
<Button
|
||||
@click="showHistory = !showHistory"
|
||||
:label="`${showHistory ? 'Hide' : 'Show'} History (${errorStore.errorHistory.length})`"
|
||||
severity="secondary"
|
||||
/>
|
||||
|
||||
<div v-if="errorStore.errorHistory.length === 0 && showHistory" class="no-history">
|
||||
<p>
|
||||
No errors in history yet. Try clicking the error buttons above to generate some
|
||||
errors!
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- Debug info -->
|
||||
<div v-if="showHistory" class="debug-info">
|
||||
<p><strong>Debug Info:</strong></p>
|
||||
<p>History array length: {{ errorStore.errorHistory.length }}</p>
|
||||
<p>
|
||||
Recent errors method returns: {{ errorStore.getRecentErrors(5).length }} items
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div v-if="showHistory && errorStore.errorHistory.length > 0" class="history-list">
|
||||
<div
|
||||
v-for="error in errorStore.getRecentErrors(5)"
|
||||
:key="error.id"
|
||||
class="history-item"
|
||||
>
|
||||
<div class="history-header">
|
||||
<span class="error-type">{{ error.type }}</span>
|
||||
<span class="error-source">{{ error.source }}</span>
|
||||
<span class="error-time">{{ formatTime(error.timestamp) }}</span>
|
||||
</div>
|
||||
<div class="error-message">{{ error.message }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from "vue";
|
||||
import Button from "primevue/button";
|
||||
import { useErrorStore } from "@/stores/errors";
|
||||
import { useLoadingStore } from "@/stores/loading";
|
||||
import ApiWithToast from "@/api-toast";
|
||||
|
||||
const errorStore = useErrorStore();
|
||||
const loadingStore = useLoadingStore();
|
||||
|
||||
const clientData = ref([]);
|
||||
const clientStatusData = ref(null);
|
||||
const showHistory = ref(false);
|
||||
|
||||
// Test functions using ApiWithToast
|
||||
const testSuccessfulApiCall = async () => {
|
||||
try {
|
||||
await ApiWithToast.makeApiCall(
|
||||
async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate delay
|
||||
return { success: true };
|
||||
},
|
||||
{
|
||||
componentName: "demo-success",
|
||||
showSuccessToast: true,
|
||||
successMessage: "API call completed successfully!",
|
||||
loadingMessage: "Processing request...",
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
// Error toast shown automatically
|
||||
console.log("Error was handled automatically");
|
||||
}
|
||||
};
|
||||
|
||||
const testFailingApiCall = async () => {
|
||||
try {
|
||||
await ApiWithToast.makeApiCall(
|
||||
async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate delay
|
||||
throw new Error("This is a simulated API failure for demo purposes");
|
||||
},
|
||||
{
|
||||
componentName: "demo-fail",
|
||||
showErrorToast: true,
|
||||
customErrorMessage: "Demo API call failed as expected",
|
||||
loadingMessage: "Attempting to fail...",
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("Error was handled by ApiWithToast");
|
||||
}
|
||||
};
|
||||
|
||||
const testRetryApiCall = async () => {
|
||||
let attempts = 0;
|
||||
|
||||
try {
|
||||
await ApiWithToast.makeApiCall(
|
||||
async () => {
|
||||
attempts++;
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
if (attempts < 3) {
|
||||
throw new Error(`Attempt ${attempts} failed - will retry`);
|
||||
}
|
||||
|
||||
return { success: true, attempts };
|
||||
},
|
||||
{
|
||||
componentName: "demo-retry",
|
||||
retryCount: 2,
|
||||
retryDelay: 1000,
|
||||
showSuccessToast: true,
|
||||
successMessage: `Success after ${attempts} attempts!`,
|
||||
customErrorMessage: "All retry attempts failed",
|
||||
loadingMessage: "Retrying operation...",
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("Retry test completed");
|
||||
}
|
||||
};
|
||||
|
||||
const testDirectToasts = () => {
|
||||
// Test different toast types directly through error store
|
||||
errorStore.setSuccess("This is a success message!");
|
||||
|
||||
setTimeout(() => {
|
||||
errorStore.setInfo("This is an info message!");
|
||||
}, 500);
|
||||
|
||||
setTimeout(() => {
|
||||
errorStore.setWarning("This is a warning message!");
|
||||
}, 1000);
|
||||
|
||||
setTimeout(() => {
|
||||
errorStore.setGlobalError(new Error("This is an error message!"));
|
||||
}, 1500);
|
||||
};
|
||||
|
||||
const testComponentError = () => {
|
||||
errorStore.setComponentError(
|
||||
"demo-component",
|
||||
new Error("This is a component-specific error"),
|
||||
);
|
||||
// Error notification will be shown automatically!
|
||||
};
|
||||
|
||||
const testGlobalError = () => {
|
||||
errorStore.setGlobalError(
|
||||
new Error("This is a global error that affects the entire application"),
|
||||
);
|
||||
// Error notification will be shown automatically!
|
||||
};
|
||||
|
||||
const clearAllErrors = () => {
|
||||
errorStore.clearAllErrors();
|
||||
errorStore.setSuccess("All errors cleared!");
|
||||
};
|
||||
|
||||
// Real API integration using ApiWithToast
|
||||
const loadClientData = async () => {
|
||||
try {
|
||||
const result = await ApiWithToast.getPaginatedClientDetails(
|
||||
{ page: 0, pageSize: 5 },
|
||||
{},
|
||||
[],
|
||||
{
|
||||
showSuccessToast: true,
|
||||
successMessage: "Client data loaded successfully!",
|
||||
},
|
||||
);
|
||||
|
||||
clientData.value = result?.data || [];
|
||||
|
||||
// Also load client status counts
|
||||
const statusResult = await ApiWithToast.getClientStatusCounts();
|
||||
clientStatusData.value = statusResult;
|
||||
} catch (error) {
|
||||
console.log("Client data loading failed, but error was handled automatically");
|
||||
}
|
||||
};
|
||||
|
||||
const loadJobData = async () => {
|
||||
try {
|
||||
const result = await ApiWithToast.getPaginatedJobDetails(
|
||||
{ page: 0, pageSize: 5 },
|
||||
{},
|
||||
[],
|
||||
{
|
||||
showSuccessToast: true,
|
||||
successMessage: "Job data loaded successfully!",
|
||||
},
|
||||
);
|
||||
console.log("Job data loaded:", result);
|
||||
} catch (error) {
|
||||
console.log("Job data loading failed, but error was handled automatically");
|
||||
}
|
||||
};
|
||||
|
||||
const createTestClient = async () => {
|
||||
const testClient = {
|
||||
customer_name: `Demo Client ${Date.now()}`,
|
||||
mobile_no: "555-0199",
|
||||
email: `demo${Date.now()}@example.com`,
|
||||
status: "Active",
|
||||
};
|
||||
|
||||
try {
|
||||
await ApiWithToast.createClient(testClient);
|
||||
// Success toast shown automatically
|
||||
// Reload client data to show the change
|
||||
await loadClientData();
|
||||
} catch (error) {
|
||||
console.log("Client creation failed, but error was handled automatically");
|
||||
}
|
||||
};
|
||||
|
||||
// Utility functions
|
||||
const formatTime = (timestamp) => {
|
||||
return new Date(timestamp).toLocaleTimeString();
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
// Show a welcome notification using the integrated error store
|
||||
errorStore.setInfo(
|
||||
"🎉 Error Store now automatically creates PrimeVue Toast notifications! No need to import both stores anymore.",
|
||||
"Welcome",
|
||||
);
|
||||
|
||||
// Add some sample errors to history for demonstration (without showing notifications)
|
||||
setTimeout(() => {
|
||||
errorStore.setComponentError(
|
||||
"demo-component",
|
||||
new Error("Sample component error from page load"),
|
||||
false,
|
||||
);
|
||||
errorStore.setApiError(
|
||||
"demo-api",
|
||||
new Error("Sample API error from initialization"),
|
||||
false,
|
||||
);
|
||||
|
||||
// Clear the component errors so they don't show in the error section, but keep them in history
|
||||
setTimeout(() => {
|
||||
errorStore.clearComponentError("demo-component");
|
||||
errorStore.clearApiError("demo-api");
|
||||
}, 100);
|
||||
}, 1000);
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.error-handling-example {
|
||||
padding: 2rem;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.info-banner {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border-radius: 12px;
|
||||
padding: 2rem;
|
||||
margin-bottom: 2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.info-banner h4 {
|
||||
color: white;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.info-banner p {
|
||||
margin-bottom: 0.5rem;
|
||||
font-size: 1rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.info-banner strong {
|
||||
color: #ffd700;
|
||||
}
|
||||
|
||||
.error-section {
|
||||
background: var(--surface-card);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
margin-bottom: 2rem;
|
||||
border-left: 4px solid var(--orange-500);
|
||||
}
|
||||
|
||||
.error-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.error-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 0.5rem;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.global-error {
|
||||
background: #fee2e2;
|
||||
border-left: 4px solid #ef4444;
|
||||
}
|
||||
|
||||
.component-error {
|
||||
background: #fef3c7;
|
||||
border-left: 4px solid #f59e0b;
|
||||
}
|
||||
|
||||
.demo-section,
|
||||
.api-demo-section,
|
||||
.history-section {
|
||||
margin-bottom: 2rem;
|
||||
background: var(--surface-card);
|
||||
padding: 1.5rem;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.button-group {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn.success {
|
||||
background: #10b981;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.success:hover:not(:disabled) {
|
||||
background: #059669;
|
||||
}
|
||||
|
||||
.btn.error {
|
||||
background: #ef4444;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.error:hover:not(:disabled) {
|
||||
background: #dc2626;
|
||||
}
|
||||
|
||||
.btn.warning {
|
||||
background: #f59e0b;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.warning:hover:not(:disabled) {
|
||||
background: #d97706;
|
||||
}
|
||||
|
||||
.btn.info {
|
||||
background: #3b82f6;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.info:hover:not(:disabled) {
|
||||
background: #2563eb;
|
||||
}
|
||||
|
||||
.btn.primary {
|
||||
background: #6366f1;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.primary:hover:not(:disabled) {
|
||||
background: #4f46e5;
|
||||
}
|
||||
|
||||
.btn.secondary {
|
||||
background: #6b7280;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.secondary:hover:not(:disabled) {
|
||||
background: #4b5563;
|
||||
}
|
||||
|
||||
.clear-btn {
|
||||
background: #f3f4f6;
|
||||
border: 1px solid #d1d5db;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.clear-btn:hover {
|
||||
background: #e5e7eb;
|
||||
}
|
||||
|
||||
.data-preview {
|
||||
margin-top: 1rem;
|
||||
padding: 1rem;
|
||||
background: var(--surface-ground);
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--surface-border);
|
||||
}
|
||||
|
||||
.data-preview pre {
|
||||
margin: 0;
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-color);
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.status-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 1rem;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.status-item {
|
||||
padding: 0.75rem;
|
||||
background: var(--surface-card);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 6px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.history-list {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.history-item {
|
||||
padding: 0.75rem;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
margin-bottom: 0.5rem;
|
||||
background: white;
|
||||
}
|
||||
|
||||
.history-header {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.25rem;
|
||||
font-size: 0.75rem;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.error-type {
|
||||
font-weight: 600;
|
||||
text-transform: capitalize;
|
||||
}
|
||||
|
||||
.error-source {
|
||||
color: #3b82f6;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
font-size: 0.875rem;
|
||||
color: #374151;
|
||||
}
|
||||
|
||||
h3,
|
||||
h4,
|
||||
h5 {
|
||||
color: var(--text-color);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.75rem;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
h4 {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
color: var(--primary-color);
|
||||
}
|
||||
|
||||
h5 {
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
color: var(--text-color-secondary);
|
||||
}
|
||||
|
||||
/* Keep some button styles for legacy buttons like clear-btn */
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn.secondary {
|
||||
background: #6b7280;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn.secondary:hover:not(:disabled) {
|
||||
background: #4b5563;
|
||||
}
|
||||
|
||||
.no-history {
|
||||
margin-top: 1rem;
|
||||
padding: 1rem;
|
||||
background: var(--surface-ground);
|
||||
border-radius: 6px;
|
||||
color: var(--text-color-secondary);
|
||||
text-align: center;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.debug-info {
|
||||
margin-top: 1rem;
|
||||
padding: 0.75rem;
|
||||
background: var(--yellow-50);
|
||||
border: 1px solid var(--yellow-200);
|
||||
border-radius: 6px;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.debug-info p {
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
</style>
|
||||
Loading…
Add table
Add a link
Reference in a new issue