|
| 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