From b2adbd8b920a18e5be52c4b3b1fb66bcec91bda5 Mon Sep 17 00:00:00 2001 From: shonya3 Date: Mon, 4 Sep 2023 14:40:28 +0300 Subject: [PATCH] feat: wc: table: add sort by weight --- packages/wc/src/wc/div-table/div-table.ts | 5 +++++ packages/wc/src/wc/div-table/toOrderedBy.ts | 10 ++++++++++ packages/wc/src/wc/div-table/types.ts | 2 +- packages/wc/tsconfig.node.json | 14 +++++++------- packages/wc/vite.config.ts | 1 + 5 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 packages/wc/vite.config.ts diff --git a/packages/wc/src/wc/div-table/div-table.ts b/packages/wc/src/wc/div-table/div-table.ts index fb1af717..e135df8d 100644 --- a/packages/wc/src/wc/div-table/div-table.ts +++ b/packages/wc/src/wc/div-table/div-table.ts @@ -218,6 +218,11 @@ export class DivTableElement extends BaseElement { Weight + this.#onOrderTriangleClicked('weight')} + > `; diff --git a/packages/wc/src/wc/div-table/toOrderedBy.ts b/packages/wc/src/wc/div-table/toOrderedBy.ts index f2821f36..3df63720 100644 --- a/packages/wc/src/wc/div-table/toOrderedBy.ts +++ b/packages/wc/src/wc/div-table/toOrderedBy.ts @@ -33,6 +33,14 @@ export const byName = (order: Order, cards: readonly DivinationCardRecord[]) => }); }; +export const byWeight = (order: Order, cards: readonly DivinationCardRecord[]) => { + return Array.from(cards).sort((a, b) => { + if (order === 'asc') return (a.weight ?? 0) - (b.weight ?? 0); + if (order === 'desc') return (b.weight ?? 0) - (a.weight ?? 0); + throw new Error('invalid order'); + }); +}; + export const toOrderedBy = ( cards: readonly DivinationCardRecord[], column: Column, @@ -48,6 +56,8 @@ export const toOrderedBy = ( return byAmount(order, cards); case 'sum': return bySum(order, cards); + case 'weight': + return byWeight(order, cards); default: throw new Error('Invalid column name'); } diff --git a/packages/wc/src/wc/div-table/types.ts b/packages/wc/src/wc/div-table/types.ts index 1eea1bda..eff08db7 100644 --- a/packages/wc/src/wc/div-table/types.ts +++ b/packages/wc/src/wc/div-table/types.ts @@ -1,6 +1,6 @@ import { Order } from '@divicards/shared/types'; -export type Column = 'price' | 'amount' | 'sum' | 'name'; +export type Column = 'price' | 'amount' | 'sum' | 'name' | 'weight'; export type SortState = { [col in Column]: Order; }; diff --git a/packages/wc/tsconfig.node.json b/packages/wc/tsconfig.node.json index 9d31e2ae..d3bf4b82 100644 --- a/packages/wc/tsconfig.node.json +++ b/packages/wc/tsconfig.node.json @@ -1,9 +1,9 @@ { - "compilerOptions": { - "composite": true, - "module": "ESNext", - "moduleResolution": "Node", - "allowSyntheticDefaultImports": true - }, - "include": ["vite.config.ts"] + "compilerOptions": { + "composite": true, + "module": "ESNext", + "moduleResolution": "Node", + "allowSyntheticDefaultImports": true + }, + "include": ["vite.config.ts"] } diff --git a/packages/wc/vite.config.ts b/packages/wc/vite.config.ts new file mode 100644 index 00000000..ff8b4c56 --- /dev/null +++ b/packages/wc/vite.config.ts @@ -0,0 +1 @@ +export default {};