Create centralized status management system
- Add statusHelpers.ts with single source of truth for all status labels/colors - Update AdminOrders to use centralized helpers - Add utility functions: canRefundOrder, canCancelOrder, canMarkAsPaid - Improve consistency across payment status handling Benefits: - Consistent Indonesian labels everywhere - DRY principle - no more duplicate switch statements - Easy to update status styling in one place - Reusable across all components 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
124
src/lib/statusHelpers.ts
Normal file
124
src/lib/statusHelpers.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Centralized status management for consistent labels, colors, and badges
|
||||
* Single source of truth for all status-related UI
|
||||
*/
|
||||
|
||||
export type PaymentStatus = 'paid' | 'pending' | 'failed' | 'cancelled' | 'refunded' | 'partially_refunded';
|
||||
export type ConsultingSlotStatus = 'pending_payment' | 'confirmed' | 'completed' | 'cancelled';
|
||||
|
||||
/**
|
||||
* Get Indonesian label for payment status
|
||||
*/
|
||||
export const getPaymentStatusLabel = (status: PaymentStatus | string | null): string => {
|
||||
switch (status) {
|
||||
case 'paid':
|
||||
return 'Lunas';
|
||||
case 'pending':
|
||||
return 'Pending';
|
||||
case 'failed':
|
||||
return 'Gagal';
|
||||
case 'cancelled':
|
||||
return 'Dibatalkan';
|
||||
case 'refunded':
|
||||
return 'Refund';
|
||||
case 'partially_refunded':
|
||||
return 'Refund Sebagian';
|
||||
default:
|
||||
return status || 'Pending';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get CSS class for payment status badge
|
||||
*/
|
||||
export const getPaymentStatusColor = (status: PaymentStatus | string | null): string => {
|
||||
switch (status) {
|
||||
case 'paid':
|
||||
return 'bg-brand-accent text-white';
|
||||
case 'pending':
|
||||
return 'bg-amber-500 text-white';
|
||||
case 'failed':
|
||||
return 'bg-red-500 text-white';
|
||||
case 'cancelled':
|
||||
return 'bg-destructive text-white';
|
||||
case 'refunded':
|
||||
return 'bg-purple-500 text-white';
|
||||
case 'partially_refunded':
|
||||
return 'bg-purple-500/80 text-white';
|
||||
default:
|
||||
return 'bg-secondary text-primary';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get label for consulting slot status
|
||||
*/
|
||||
export const getConsultingSlotStatusLabel = (status: ConsultingSlotStatus | string): string => {
|
||||
switch (status) {
|
||||
case 'pending_payment':
|
||||
return 'Menunggu Pembayaran';
|
||||
case 'confirmed':
|
||||
return 'Terkonfirmasi';
|
||||
case 'completed':
|
||||
return 'Selesai';
|
||||
case 'cancelled':
|
||||
return 'Dibatalkan';
|
||||
default:
|
||||
return status;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get CSS class for consulting slot status badge
|
||||
*/
|
||||
export const getConsultingSlotStatusColor = (status: ConsultingSlotStatus | string): string => {
|
||||
switch (status) {
|
||||
case 'pending_payment':
|
||||
return 'bg-amber-500 text-white';
|
||||
case 'confirmed':
|
||||
return 'bg-green-500 text-white';
|
||||
case 'completed':
|
||||
return 'bg-blue-500 text-white';
|
||||
case 'cancelled':
|
||||
return 'bg-destructive text-white';
|
||||
default:
|
||||
return 'bg-secondary text-primary';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get label for product type
|
||||
*/
|
||||
export const getProductTypeLabel = (type: string): string => {
|
||||
switch (type) {
|
||||
case 'consulting':
|
||||
return 'Konsultasi';
|
||||
case 'webinar':
|
||||
return 'Webinar';
|
||||
case 'bootcamp':
|
||||
return 'Bootcamp';
|
||||
default:
|
||||
return type;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if order can be refunded
|
||||
*/
|
||||
export const canRefundOrder = (paymentStatus: PaymentStatus | string | null, refundedAt: string | null = null): boolean => {
|
||||
return paymentStatus === 'paid' && !refundedAt;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if order can be cancelled
|
||||
*/
|
||||
export const canCancelOrder = (paymentStatus: PaymentStatus | string | null, refundedAt: string | null = null): boolean => {
|
||||
return !refundedAt && paymentStatus !== 'cancelled' && paymentStatus !== 'refunded' && paymentStatus !== 'partially_refunded';
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if order can be marked as paid
|
||||
*/
|
||||
export const canMarkAsPaid = (paymentStatus: PaymentStatus | string | null, refundedAt: string | null = null): boolean => {
|
||||
return !refundedAt && paymentStatus !== 'paid' && paymentStatus !== 'refunded' && paymentStatus !== 'partially_refunded';
|
||||
};
|
||||
Reference in New Issue
Block a user