From 9538360dfae968159cafd15b1c94e6f57b9f3594 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 18 Jan 2025 23:14:06 +0900 Subject: [PATCH] #: Modify PaginationModel --- capacitor/src/model/PaginationMode.tsx | 33 ++++++++++++++++++++++---- electron/src/model/PaginationMode.tsx | 33 ++++++++++++++++++++++---- react/src/model/PaginationModel.tsx | 33 ++++++++++++++++++++++---- 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/capacitor/src/model/PaginationMode.tsx b/capacitor/src/model/PaginationMode.tsx index 5211b3b9..436600b4 100644 --- a/capacitor/src/model/PaginationMode.tsx +++ b/capacitor/src/model/PaginationMode.tsx @@ -18,16 +18,39 @@ export class PaginationModel { pageNum: number, pageSize: number, stream: linq.IEnumerable + ); + + constructor(otherPaginationModel: any); + + constructor( + pageNum?: number, + pageSize?: number, + stream?: linq.IEnumerable ) { makeAutoObservable(this); - if (pageNum < 1) { + if (typeof pageNum === "object" && typeof pageSize === "undefined" && typeof stream === "undefined") { + const otherPaginationModel = pageNum as any; + this.pageNum = otherPaginationModel.pageNum; + this.pageSize = otherPaginationModel.pageSize; + this.totalRecord = otherPaginationModel.totalRecord; + this.totalPage = otherPaginationModel.totalPage; + this.list = otherPaginationModel.list; + if (!(typeof this.pageNum === "number" && typeof this.pageSize === "number" && typeof this.totalRecord === "number" && typeof this.totalPage === "number" && typeof this.list === "object" && this.list instanceof Array)) { + throw new Error("Incorrect paging behavior"); + } + return; + } + if (!(typeof pageNum === "number" && typeof pageSize === "number" && typeof stream === "object")) { + return; + } + if (pageNum! < 1) { throw new Error("The page number cannot be less than 1"); } - if (pageSize < 1) { + if (pageSize! < 1) { throw new Error("The page size cannot be less than 1"); } - if (pageNum !== Math.floor(pageNum)) { + if (pageNum !== Math.floor(pageNum!)) { throw new Error("The page number must be an integer"); } @@ -35,9 +58,9 @@ export class PaginationModel { throw new Error("The page size must be an integer"); } - const totalRecord = stream.count(); + const totalRecord = (stream as linq.IEnumerable).count(); const totalPage = Math.floor(mathjs.divide(totalRecord, pageSize)) + mathjs.mod(totalRecord, pageSize) > 0 ? 1 : 0; - const list = stream + const list = (stream as linq.IEnumerable) .skip(mathjs.multiply(pageNum - 1, pageSize)) .take(pageSize) .toArray(); diff --git a/electron/src/model/PaginationMode.tsx b/electron/src/model/PaginationMode.tsx index 5211b3b9..436600b4 100644 --- a/electron/src/model/PaginationMode.tsx +++ b/electron/src/model/PaginationMode.tsx @@ -18,16 +18,39 @@ export class PaginationModel { pageNum: number, pageSize: number, stream: linq.IEnumerable + ); + + constructor(otherPaginationModel: any); + + constructor( + pageNum?: number, + pageSize?: number, + stream?: linq.IEnumerable ) { makeAutoObservable(this); - if (pageNum < 1) { + if (typeof pageNum === "object" && typeof pageSize === "undefined" && typeof stream === "undefined") { + const otherPaginationModel = pageNum as any; + this.pageNum = otherPaginationModel.pageNum; + this.pageSize = otherPaginationModel.pageSize; + this.totalRecord = otherPaginationModel.totalRecord; + this.totalPage = otherPaginationModel.totalPage; + this.list = otherPaginationModel.list; + if (!(typeof this.pageNum === "number" && typeof this.pageSize === "number" && typeof this.totalRecord === "number" && typeof this.totalPage === "number" && typeof this.list === "object" && this.list instanceof Array)) { + throw new Error("Incorrect paging behavior"); + } + return; + } + if (!(typeof pageNum === "number" && typeof pageSize === "number" && typeof stream === "object")) { + return; + } + if (pageNum! < 1) { throw new Error("The page number cannot be less than 1"); } - if (pageSize < 1) { + if (pageSize! < 1) { throw new Error("The page size cannot be less than 1"); } - if (pageNum !== Math.floor(pageNum)) { + if (pageNum !== Math.floor(pageNum!)) { throw new Error("The page number must be an integer"); } @@ -35,9 +58,9 @@ export class PaginationModel { throw new Error("The page size must be an integer"); } - const totalRecord = stream.count(); + const totalRecord = (stream as linq.IEnumerable).count(); const totalPage = Math.floor(mathjs.divide(totalRecord, pageSize)) + mathjs.mod(totalRecord, pageSize) > 0 ? 1 : 0; - const list = stream + const list = (stream as linq.IEnumerable) .skip(mathjs.multiply(pageNum - 1, pageSize)) .take(pageSize) .toArray(); diff --git a/react/src/model/PaginationModel.tsx b/react/src/model/PaginationModel.tsx index 5211b3b9..436600b4 100644 --- a/react/src/model/PaginationModel.tsx +++ b/react/src/model/PaginationModel.tsx @@ -18,16 +18,39 @@ export class PaginationModel { pageNum: number, pageSize: number, stream: linq.IEnumerable + ); + + constructor(otherPaginationModel: any); + + constructor( + pageNum?: number, + pageSize?: number, + stream?: linq.IEnumerable ) { makeAutoObservable(this); - if (pageNum < 1) { + if (typeof pageNum === "object" && typeof pageSize === "undefined" && typeof stream === "undefined") { + const otherPaginationModel = pageNum as any; + this.pageNum = otherPaginationModel.pageNum; + this.pageSize = otherPaginationModel.pageSize; + this.totalRecord = otherPaginationModel.totalRecord; + this.totalPage = otherPaginationModel.totalPage; + this.list = otherPaginationModel.list; + if (!(typeof this.pageNum === "number" && typeof this.pageSize === "number" && typeof this.totalRecord === "number" && typeof this.totalPage === "number" && typeof this.list === "object" && this.list instanceof Array)) { + throw new Error("Incorrect paging behavior"); + } + return; + } + if (!(typeof pageNum === "number" && typeof pageSize === "number" && typeof stream === "object")) { + return; + } + if (pageNum! < 1) { throw new Error("The page number cannot be less than 1"); } - if (pageSize < 1) { + if (pageSize! < 1) { throw new Error("The page size cannot be less than 1"); } - if (pageNum !== Math.floor(pageNum)) { + if (pageNum !== Math.floor(pageNum!)) { throw new Error("The page number must be an integer"); } @@ -35,9 +58,9 @@ export class PaginationModel { throw new Error("The page size must be an integer"); } - const totalRecord = stream.count(); + const totalRecord = (stream as linq.IEnumerable).count(); const totalPage = Math.floor(mathjs.divide(totalRecord, pageSize)) + mathjs.mod(totalRecord, pageSize) > 0 ? 1 : 0; - const list = stream + const list = (stream as linq.IEnumerable) .skip(mathjs.multiply(pageNum - 1, pageSize)) .take(pageSize) .toArray();