1- import { customElement } from '@mantou/gem/lib/decorators' ;
1+ import { customElement , type Emitter , effect , emitter , mounted } from '@mantou/gem/lib/decorators' ;
22import { createState , GemElement , html } from '@mantou/gem/lib/element' ;
33import type { ContextMenuItem } from 'duoyun-ui/elements/contextmenu' ;
44import { ContextMenu } from 'duoyun-ui/elements/contextmenu' ;
@@ -16,6 +16,8 @@ import { fetchItemsWithArgs } from './api';
1616import 'duoyun-ui/patterns/table' ;
1717import 'duoyun-ui/elements/button' ;
1818
19+ import { history } from '@mantou/gem/lib/history' ;
20+
1921const pagination = createPaginationStore < Item > ( {
2022 storageKey : 'users' ,
2123 cacheItems : true ,
@@ -38,6 +40,8 @@ type NewItem = Modify<typeof initItem, { social?: string[] }>;
3840
3941@customElement ( 'console-page-users' )
4042export class ConsolePageItemElement extends GemElement {
43+ @emitter loaded : Emitter ;
44+
4145 #state = createState ( {
4246 pagination : pagination ,
4347 paginationMap : new Map ( [ [ '' , pagination ] ] ) ,
@@ -220,13 +224,14 @@ export class ConsolePageItemElement extends GemElement {
220224 header : `Edit: ${ r . id } ` ,
221225 query : [ 'id' , r . id ] ,
222226 formItems : this . getFormItems ( true ) ,
227+ prepareClose : ( data ) => {
228+ console . log ( data ) ;
229+ } ,
223230 prepareOk : async ( data ) => {
224231 await sleep ( 1000 ) ;
225232 console . log ( data ) ;
226233 this . #state. pagination . updateItem ( data ) ;
227234 } ,
228- } ) . catch ( ( data ) => {
229- console . log ( data ) ;
230235 } ) ;
231236 } ;
232237
@@ -237,22 +242,37 @@ export class ConsolePageItemElement extends GemElement {
237242 header : `Create` ,
238243 query : [ 'new' , true ] ,
239244 formItems : this . getFormItems ( ) ,
245+ prepareClose : ( data ) => {
246+ console . log ( data ) ;
247+ } ,
240248 prepareOk : async ( data ) => {
241249 await sleep ( 1000 ) ;
242250 console . log ( data ) ;
243251 throw new Error ( 'No implement!' ) ;
244252 } ,
245- } ) . catch ( ( data ) => {
246- console . log ( data ) ;
247253 } ) ;
248254 } ;
249255
256+ @mounted ( )
257+ #init = async ( ) => {
258+ const { query } = history . getParams ( ) ;
259+ if ( query . get ( 'new' ) ) {
260+ this . onCreate ( ) ;
261+ }
262+ const id = query . get ( 'id' ) ;
263+ if ( id ) {
264+ await new Promise ( ( res ) => this . addEventListener ( 'loaded' , res , { once : true } ) ) ;
265+ const item = this . #state. pagination . store . items [ id ] ;
266+ item && this . onUpdate ( item ) ;
267+ }
268+ } ;
269+
250270 /**
251271 * 将搜索/过滤结果储存进内存 `PaginationStore`
252272 * 被根据 `searchAndFilterKey` 进行缓存
253273 * 这里使用页面级缓存,切换页面后将被清除
254274 */
255- #onFetch = ( { detail } : CustomEvent < FetchEventDetail > ) => {
275+ #onFetch = async ( { detail } : CustomEvent < FetchEventDetail > ) => {
256276 let newPagination = this . #state. paginationMap . get ( detail . pageKey ) ;
257277 if ( ! newPagination ) {
258278 newPagination = createPaginationStore < Item > ( {
@@ -262,7 +282,8 @@ export class ConsolePageItemElement extends GemElement {
262282 this . #state. paginationMap . set ( detail . pageKey , newPagination ) ;
263283 }
264284 this . #state( { pagination : newPagination } ) ;
265- newPagination . updatePage ( fetchItemsWithArgs , detail ) ;
285+ await newPagination . updatePage ( fetchItemsWithArgs , detail ) ;
286+ this . loaded ( ) ;
266287 } ;
267288
268289 render = ( ) => {
0 commit comments