-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(other): implement simple routing panel in other tab
- Loading branch information
1 parent
bd39629
commit d764ef5
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
client-app/src/desktop/tabs/other/routing/SimpleRoutingPanel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import {HoistModel, hoistCmp, creates, XH} from '@xh/hoist/core'; | ||
import {grid, GridModel} from '@xh/hoist/cmp/grid'; | ||
import {StoreRecordId} from '@xh/hoist/data'; | ||
import {Icon} from '@xh/hoist/icon'; | ||
import {panel} from '@xh/hoist/desktop/cmp/panel'; | ||
import {wrapper} from '../../../common'; | ||
import React from 'react'; | ||
|
||
export const simpleRoutingPanel = hoistCmp.factory({ | ||
displayName: 'SimpleRoutingPanel', | ||
model: creates(() => new SimpleRoutingPanelModel()), | ||
|
||
render({model}) { | ||
return wrapper({ | ||
description: [ | ||
<p> | ||
Hoist provides functionality for route parameters to interact with UI | ||
components. Below is a simple grid whose selected record can be maintained by a | ||
route parameter.{' '} | ||
</p>, | ||
<p> | ||
E.g. URLs like <code>https://toolbox.xh.io/app/other/simpleRouting/123</code>, | ||
where <code>123</code> is a record ID and that record is (auto) selected in the | ||
grid. Additionally, the route parameter will be updated when the user selects a | ||
different record in the grid. | ||
</p> | ||
], | ||
item: panel({ | ||
title: 'Simple Routing', | ||
icon: Icon.gridPanel(), | ||
className: 'tb-simple-routing-panel', | ||
item: grid({model: model.gridModel}), | ||
height: 600, | ||
width: 600 | ||
}) | ||
}); | ||
} | ||
}); | ||
|
||
class SimpleRoutingPanelModel extends HoistModel { | ||
gridModel = new GridModel({ | ||
columns: [ | ||
{field: 'id', flex: 0}, | ||
{field: 'company', flex: 1} | ||
] | ||
}); | ||
|
||
constructor() { | ||
super(); | ||
this.addReaction({ | ||
track: () => XH.routerState.params, | ||
run: () => this.updateGridSelectionOnRouteChange(), | ||
fireImmediately: true | ||
}); | ||
this.addReaction({ | ||
track: () => this.gridModel.selectedId, | ||
run: () => | ||
this.updateRouteOnGridSelectionChange( | ||
XH.routerState.name, | ||
this.gridModel.selectedId | ||
), | ||
fireImmediately: true | ||
}); | ||
} | ||
|
||
async updateGridSelectionOnRouteChange() { | ||
if ( | ||
!XH.routerState.params.recordId || | ||
XH.routerState.params.recordId === this.gridModel.selectedId | ||
) | ||
return; | ||
await this.gridModel.selectAsync(Number(XH.routerState.params.recordId)); | ||
} | ||
|
||
updateRouteOnGridSelectionChange(name: string, selectedId: StoreRecordId) { | ||
if ( | ||
!name.startsWith('default.other.simpleRouting') || | ||
!selectedId || | ||
XH.routerState.params.recordId === selectedId | ||
) | ||
return; | ||
XH.navigate('default.other.simpleRouting.recordId', {recordId: selectedId}); | ||
} | ||
|
||
override async doLoadAsync(loadSpec) { | ||
const {trades} = await XH.fetchJson({url: 'trade'}); | ||
this.gridModel.loadData(trades); | ||
await this.updateGridSelectionOnRouteChange(); | ||
} | ||
} |