Skip to content

Commit

Permalink
feat: wc: table: add sort by weight
Browse files Browse the repository at this point in the history
  • Loading branch information
shonya3 committed Sep 4, 2023
1 parent 53b5931 commit b2adbd8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/wc/src/wc/div-table/div-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ export class DivTableElement extends BaseElement {
</th>
<th>
<span class="column-name"> Weight </span>
<wc-order-triangle
?active=${this.column === 'weight'}
order=${this.column === 'weight' ? this.order : 'unordered'}
@click=${() => this.#onOrderTriangleClicked('weight')}
></wc-order-triangle>
</th>
</tr>
</thead>`;
Expand Down
10 changes: 10 additions & 0 deletions packages/wc/src/wc/div-table/toOrderedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/wc/src/wc/div-table/types.ts
Original file line number Diff line number Diff line change
@@ -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;
};
14 changes: 7 additions & 7 deletions packages/wc/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -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"]
}
1 change: 1 addition & 0 deletions packages/wc/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};

0 comments on commit b2adbd8

Please sign in to comment.