Skip to content

Commit 39be21f

Browse files
authored
Merge pull request #132 from marcodejongh/add_hold_search_unselect
Allow unselecting of selected holds in the holds filter
2 parents f19ad4b + 6d07490 commit 39be21f

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

app/components/board-renderer/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BoardName } from '@/lib/types';
22

33
export type LitUpHolds = string;
44

5-
export type HoldState = 'OFF' | 'STARTING' | 'FINISH' | 'HAND' | 'FOOT';
5+
export type HoldState = 'OFF' | 'STARTING' | 'FINISH' | 'HAND' | 'FOOT' | 'ANY' | 'NOT';
66
export type HoldsArray = Array<HoldRenderData>;
77

88
export type HoldColor = string;
@@ -14,7 +14,8 @@ export type HoldRenderData = {
1414
cy: number;
1515
r: number;
1616
};
17-
export type LitUpHoldsMap = Record<HoldCode, { state: HoldState; color: string; displayColor: string }>;
17+
export type LitupHold = { state: HoldState; color: string; displayColor: string };
18+
export type LitUpHoldsMap = Record<HoldCode, LitupHold>;
1819

1920
// If adding mroe boards be sure to increment the DB version number for indexeddb
2021
export const supported_boards: BoardName[] = ['kilter', 'tension'];

app/components/search-drawer/climb-hold-search-form.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
'use client';
22
import React from 'react';
3-
import { BoardDetails } from '@/app/lib/types';
4-
import { HOLD_STATE_MAP, LitUpHoldsMap } from '../board-renderer/types';
3+
import { BoardDetails, BoardName, HoldCode, HoldFilterKey, HoldState } from '@/app/lib/types';
4+
import { HOLD_STATE_MAP, LitupHold, LitUpHoldsMap } from '../board-renderer/types';
55
import BoardRenderer from '../board-renderer/board-renderer';
66
import { useUISearchParams } from '@/app/components/queue-control/ui-searchparams-provider';
77
import { Select } from 'antd';
88

9-
export type HoldCode = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 42 | 43 | 44 | 45 | 12 | 13 | 14 | 15;
10-
export type BoardName = 'tension' | 'kilter';
11-
export type HoldState = 'STARTING' | 'HAND' | 'FOOT' | 'FINISH' | 'OFF' | 'ANY' | 'NOT';
12-
139
interface ClimbHoldSearchFormProps {
1410
boardDetails: BoardDetails;
1511
}
@@ -31,31 +27,32 @@ const ClimbHoldSearchForm: React.FC<ClimbHoldSearchFormProps> = ({ boardDetails
3127

3228
const handleHoldClick = (holdId: number) => {
3329
const stateCode = getStateCode(selectedState, boardDetails.board_name as BoardName);
34-
const updates: Record<string, any> = {};
35-
const holdKey = `hold_${holdId}`;
36-
30+
const updates: Record<string, HoldState | null> = {};
31+
const holdKey: HoldFilterKey = `hold_${holdId}`;
32+
let displayInfo: LitupHold | null = null;
3733
// Handle ANY state (remove filter)
3834
if (selectedState === 'ANY') {
3935
updates[holdKey] = 'ANY';
4036
}
4137
// Handle NOT state
4238
else if (selectedState === 'NOT') {
4339
updates[holdKey] = 'NOT';
44-
}
45-
// Handle other states
46-
else {
47-
//@ts-expect-error fix later
40+
} else {
4841
const currentValue = uiSearchParams[holdKey];
4942
if (currentValue === stateCode?.toString()) {
50-
updates[holdKey] = undefined;
51-
} else {
52-
updates[holdKey] = stateCode;
43+
updates[holdKey] = null;
44+
} else if (stateCode !== null) {
45+
const stateInfo = HOLD_STATE_MAP[boardDetails.board_name][stateCode];
46+
const holdState: HoldState = stateInfo.name;
47+
updates[holdKey] = holdState;
5348
}
5449
}
55-
50+
5651
// Handle mirrored hold
5752
const hold = boardDetails.holdsData.find((h) => h.id === holdId);
5853
if (hold?.mirroredHoldId) {
54+
//TODO: When on a board with mirrored holds, we should search an OR for the
55+
// two possible hold ids
5956
const mirrorKey = `hold_${hold.mirroredHoldId}`;
6057
updates[mirrorKey] = updates[holdKey];
6158
}
@@ -68,42 +65,45 @@ const ClimbHoldSearchForm: React.FC<ClimbHoldSearchFormProps> = ({ boardDetails
6865
delete newSelectedHolds[hold.mirroredHoldId];
6966
}
7067
} else {
71-
let displayInfo;
7268
if (selectedState === 'NOT') {
7369
displayInfo = {
7470
state: 'NOT',
7571
color: '#FF0000', // Red color for NOT state
72+
displayColor: '#FF0000', // Red color for NOT state
7673
};
7774
} else if (selectedState === 'ANY') {
7875
displayInfo = {
7976
state: 'ANY',
80-
color: '#b76e79',
77+
color: '#00CCCC',
78+
displayColor: '#00CCCC',
8179
};
8280
} else if (stateCode !== null) {
81+
// This branch is needed for when adding search by foot/hand/etc
8382
const stateInfo = HOLD_STATE_MAP[boardDetails.board_name as BoardName][stateCode];
8483
displayInfo = {
8584
state: stateInfo.name,
86-
color: stateInfo.displayColor || stateInfo.color,
85+
color: stateInfo.color,
86+
displayColor: stateInfo.displayColor || stateInfo.color,
8787
};
8888
}
89+
8990

9091
if (displayInfo) {
91-
//@ts-expect-error cbf
92-
newSelectedHolds[holdId] = displayInfo;
92+
if (!newSelectedHolds[holdId] || newSelectedHolds[holdId].state !== displayInfo.state) {
93+
newSelectedHolds[holdId] = displayInfo;
94+
} else {
95+
delete newSelectedHolds[holdId];
96+
}
9397
}
9498
}
9599

96100
updateFilters({
97-
//@ts-expect-error will fix later
98101
holdsFilter: newSelectedHolds,
99102
});
100103
};
101104

102105
const clearAllHolds = () => {
103-
const updates: Record<string, undefined> = {};
104-
105106
updateFilters({
106-
//@ts-expect-error
107107
holdsFilter: {},
108108
});
109109
};

app/lib/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ export type SetsResponse = {
5656

5757
// Search Request Type
5858
export type HoldCode = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 42 | 43 | 44 | 45 | 12 | 13 | 14 | 15;
59-
export type HoldState = 'STARTING' | 'HAND' | 'FOOT' | 'FINISH' | 'OFF' | 'ANY';
59+
export type HoldState = 'STARTING' | 'HAND' | 'FOOT' | 'FINISH' | 'OFF' | 'ANY' | 'NOT';
6060

6161
export type HoldStateFilter = {
6262
holdId: string;
6363
stateCode: HoldCode;
6464
};
6565

66+
export type HoldFilterKey = `hold_${number}`;
67+
export type HoldFilterValue = HoldState | null;
68+
export type HoldsFilter = Partial<Record<HoldFilterKey, HoldFilterValue>>;
69+
6670
export type SearchRequest = {
6771
gradeAccuracy: number;
6872
maxGrade: number;
@@ -75,7 +79,8 @@ export type SearchRequest = {
7579
onlyClassics: boolean;
7680
settername: string;
7781
setternameSuggestion: string;
78-
holdsFilter: HoldStateFilter;
82+
holdsFilter: HoldsFilter;
83+
[key: `hold_${number}`]: HoldFilterValue; // Allow dynamic hold keys directly in the search params
7984
};
8085

8186
export type SearchRequestPagination = SearchRequest & {

app/lib/url-utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export const DEFAULT_SEARCH_PARAMS: SearchRequestPagination = {
8787
onlyClassics: false,
8888
settername: '',
8989
setternameSuggestion: '',
90-
//@ts-expect-error fix later
9190
holdsFilter: {},
9291
page: 0,
9392
pageSize: PAGE_LIMIT,

0 commit comments

Comments
 (0)