Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/statusTheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ describe('status theme', () => {
it('groups terminal statuses as settled', () => {
expect(ALL.filter(isSettled)).toEqual(['FULFILLED', 'CANCELLED', 'REFUNDED']);
});

it('falls back to a neutral theme for statuses this build does not know', () => {
expect(getStatusTheme('PARTIALLY_REFUNDED')).toEqual({
label: 'Partially refunded',
color: '#5b605c',
background: '#ecebe6',
});
});
});
17 changes: 15 additions & 2 deletions src/lib/statusTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ const STATUS_THEME: Record<OrderStatus, StatusTheme> = {
REFUNDED: { label: 'Refunded', color: '#b4412f', background: '#f8e0db' },
};

export function getStatusTheme(status: OrderStatus): StatusTheme {
return STATUS_THEME[status];
const FALLBACK_THEME: Omit<StatusTheme, 'label'> = { color: '#5b605c', background: '#ecebe6' };

/** Turns an enum value such as PARTIALLY_REFUNDED into "Partially refunded". */
function humanize(status: string): string {
const words = status.toLowerCase().split('_');
return [words[0].charAt(0).toUpperCase() + words[0].slice(1), ...words.slice(1)].join(' ');
}

/**
* Returns the display theme for an order status. Released app builds can
* receive statuses added to the API after they shipped, so unknown values get
* a neutral theme instead of crashing.
*/
export function getStatusTheme(status: OrderStatus | (string & {})): StatusTheme {
return STATUS_THEME[status as OrderStatus] ?? { ...FALLBACK_THEME, label: humanize(status) };
}

/** Orders in these states no longer change and are grouped under "Past orders". */
Expand Down
Loading