Skip to content

Commit d764ef5

Browse files
committed
feat(other): implement simple routing panel in other tab
1 parent bd39629 commit d764ef5

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

client-app/src/desktop/AppModel.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ export class AppModel extends HoistAppModel {
193193
{name: 'pinPad', path: '/pinPad'},
194194
{name: 'placeholder', path: '/placeholder'},
195195
{name: 'popups', path: '/popups'},
196-
{name: 'timestamp', path: '/timestamp'}
196+
{name: 'timestamp', path: '/timestamp'},
197+
{
198+
name: 'simpleRouting',
199+
path: '/simpleRouting',
200+
children: [{name: 'recordId', path: '/:recordId'}]
201+
}
197202
]
198203
},
199204
{

client-app/src/desktop/tabs/other/OtherTab.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {pinPadPanel} from './PinPadPanel';
1717
import {placeholderPanel} from './PlaceholderPanel';
1818
import {popupsPanel} from './PopupsPanel';
1919
import {relativeTimestampPanel} from './relativetimestamp/RelativeTimestampPanel';
20+
import {simpleRoutingPanel} from './routing/SimpleRoutingPanel';
2021

2122
export const otherTab = hoistCmp.factory(() =>
2223
tabContainer({
@@ -44,7 +45,8 @@ export const otherTab = hoistCmp.factory(() =>
4445
{id: 'pinPad', title: 'PIN Pad', content: pinPadPanel},
4546
{id: 'placeholder', title: 'Placeholder', content: placeholderPanel},
4647
{id: 'popups', content: popupsPanel},
47-
{id: 'timestamp', content: relativeTimestampPanel}
48+
{id: 'timestamp', content: relativeTimestampPanel},
49+
{id: 'simpleRouting', content: simpleRoutingPanel}
4850
]
4951
},
5052
className: 'toolbox-tab'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {HoistModel, hoistCmp, creates, XH} from '@xh/hoist/core';
2+
import {grid, GridModel} from '@xh/hoist/cmp/grid';
3+
import {StoreRecordId} from '@xh/hoist/data';
4+
import {Icon} from '@xh/hoist/icon';
5+
import {panel} from '@xh/hoist/desktop/cmp/panel';
6+
import {wrapper} from '../../../common';
7+
import React from 'react';
8+
9+
export const simpleRoutingPanel = hoistCmp.factory({
10+
displayName: 'SimpleRoutingPanel',
11+
model: creates(() => new SimpleRoutingPanelModel()),
12+
13+
render({model}) {
14+
return wrapper({
15+
description: [
16+
<p>
17+
Hoist provides functionality for route parameters to interact with UI
18+
components. Below is a simple grid whose selected record can be maintained by a
19+
route parameter.{' '}
20+
</p>,
21+
<p>
22+
E.g. URLs like <code>https://toolbox.xh.io/app/other/simpleRouting/123</code>,
23+
where <code>123</code> is a record ID and that record is (auto) selected in the
24+
grid. Additionally, the route parameter will be updated when the user selects a
25+
different record in the grid.
26+
</p>
27+
],
28+
item: panel({
29+
title: 'Simple Routing',
30+
icon: Icon.gridPanel(),
31+
className: 'tb-simple-routing-panel',
32+
item: grid({model: model.gridModel}),
33+
height: 600,
34+
width: 600
35+
})
36+
});
37+
}
38+
});
39+
40+
class SimpleRoutingPanelModel extends HoistModel {
41+
gridModel = new GridModel({
42+
columns: [
43+
{field: 'id', flex: 0},
44+
{field: 'company', flex: 1}
45+
]
46+
});
47+
48+
constructor() {
49+
super();
50+
this.addReaction({
51+
track: () => XH.routerState.params,
52+
run: () => this.updateGridSelectionOnRouteChange(),
53+
fireImmediately: true
54+
});
55+
this.addReaction({
56+
track: () => this.gridModel.selectedId,
57+
run: () =>
58+
this.updateRouteOnGridSelectionChange(
59+
XH.routerState.name,
60+
this.gridModel.selectedId
61+
),
62+
fireImmediately: true
63+
});
64+
}
65+
66+
async updateGridSelectionOnRouteChange() {
67+
if (
68+
!XH.routerState.params.recordId ||
69+
XH.routerState.params.recordId === this.gridModel.selectedId
70+
)
71+
return;
72+
await this.gridModel.selectAsync(Number(XH.routerState.params.recordId));
73+
}
74+
75+
updateRouteOnGridSelectionChange(name: string, selectedId: StoreRecordId) {
76+
if (
77+
!name.startsWith('default.other.simpleRouting') ||
78+
!selectedId ||
79+
XH.routerState.params.recordId === selectedId
80+
)
81+
return;
82+
XH.navigate('default.other.simpleRouting.recordId', {recordId: selectedId});
83+
}
84+
85+
override async doLoadAsync(loadSpec) {
86+
const {trades} = await XH.fetchJson({url: 'trade'});
87+
this.gridModel.loadData(trades);
88+
await this.updateGridSelectionOnRouteChange();
89+
}
90+
}

0 commit comments

Comments
 (0)