drf-react-by-schema 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { serverEndPointType } from './context/DRFReactBySchemaContext';
2
2
  import { Item, SchemaType, modelOptionsType, DataSchemaColumnsType, ItemSchemaColumnsType, PaginatedResult } from './utils';
3
- import { GridFilterModel } from '@mui/x-data-grid';
3
+ import { GridFilterModel, GridSortModel } from '@mui/x-data-grid';
4
4
  import { AlertColor } from '@mui/material/Alert';
5
5
  type Id = string | number | null;
6
6
  interface TargetApiParams {
@@ -71,6 +71,7 @@ export interface getGenericModelListProps {
71
71
  loadedModelOptions?: modelOptionsType | boolean;
72
72
  page?: number;
73
73
  filter?: GridFilterModel;
74
+ sort?: GridSortModel;
74
75
  sumRows?: SumRowsType;
75
76
  }
76
77
  export interface SumRowsType {
@@ -83,7 +84,7 @@ interface SumRowsItem {
83
84
  suffix?: string;
84
85
  isCount?: boolean;
85
86
  }
86
- export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, page, filter, sumRows, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
87
+ export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, page, filter, sort, sumRows, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
87
88
  export declare const getGenericModel: ({ model, serverEndPoint, id, relatedModel, relatedModelId, }: {
88
89
  model: string;
89
90
  serverEndPoint: serverEndPointType | null;
package/dist/api.js CHANGED
@@ -540,7 +540,7 @@ const isLoggedIn = (serverEndPoint) => __awaiter(void 0, void 0, void 0, functio
540
540
  return usuaria;
541
541
  });
542
542
  exports.isLoggedIn = isLoggedIn;
543
- const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', columnFields, hiddenFields = ['id'], creatableFields = [], isInBatches = false, loadedSchema, loadedModelOptions, page, filter, sumRows, }) => __awaiter(void 0, void 0, void 0, function* () {
543
+ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', columnFields, hiddenFields = ['id'], creatableFields = [], isInBatches = false, loadedSchema, loadedModelOptions, page, filter, sort, sumRows, }) => __awaiter(void 0, void 0, void 0, function* () {
544
544
  let path = `${model}/${id}`;
545
545
  let schemaPath = model;
546
546
  let schema = loadedSchema;
@@ -550,17 +550,16 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
550
550
  path += `/${relatedModel}/${relatedModelId}`;
551
551
  schemaPath += `/${id}/${relatedModel}`;
552
552
  }
553
- if (page || filter || sumRows) {
554
- path += '?';
555
- }
553
+ const queryParams = [];
554
+ // SERVER-SIDE TOTALS (sumRows):
556
555
  if (sumRows) {
557
556
  const sumRowsParams = sumRows.rows.map(row => row.field).join(',');
558
- path += `sum_rows=${sumRowsParams}`;
557
+ queryParams.push(`sum_rows=${sumRowsParams}`);
559
558
  }
559
+ // SERVER-SIDE FILTERING:
560
560
  if (filter) {
561
- const filtersArr = [];
562
561
  if (filter.quickFilterValues && filter.quickFilterValues.length > 0 && filter.quickFilterValues[0]) {
563
- filtersArr.push(`search=${filter.quickFilterValues[0]}`);
562
+ queryParams.push(`search=${filter.quickFilterValues[0]}`);
564
563
  }
565
564
  for (const item of filter.items) {
566
565
  if (!item.operatorValue) {
@@ -569,18 +568,23 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
569
568
  const queryParam = item.value
570
569
  ? `columnField=${item.columnField}&operatorValue=${item.operatorValue}&value=${item.value}`
571
570
  : `columnField=${item.columnField}&operatorValue=${item.operatorValue}`;
572
- filtersArr.push(queryParam);
571
+ queryParams.push(queryParam);
573
572
  }
574
- if (sumRows) {
575
- path += '&';
573
+ }
574
+ // SERVER-SIDE SORTING:
575
+ if (sort) {
576
+ const sortParams = [];
577
+ for (const item of sort) {
578
+ sortParams.push(item.sort === 'desc' ? `-${item.field}` : item.field);
576
579
  }
577
- path += filtersArr.join('&');
580
+ queryParams.push(`ordering=${sortParams.join(',')}`);
578
581
  }
582
+ // SERVER-SIDE PAGINATION:
579
583
  if (page) {
580
- if (sumRows || filter) {
581
- path += '&';
582
- }
583
- path += `page=${page + 1}`;
584
+ queryParams.push(`page=${page + 1}`);
585
+ }
586
+ if (queryParams.length > 0) {
587
+ path += `?${queryParams.join('&')}`;
584
588
  }
585
589
  // Only get schema and columns if not in batches or in first batch:
586
590
  if (!schema) {
@@ -615,6 +615,12 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
615
615
  setPaginationModel(Object.assign(Object.assign({}, paginationModel), { filter: newFilter }));
616
616
  }
617
617
  }
618
+ : undefined, sortingMode: paginationModel ? 'server' : 'client', onSortModelChange: paginationModel
619
+ ? (newSorting) => {
620
+ if (setPaginationModel && paginationModel) {
621
+ setPaginationModel(Object.assign(Object.assign({}, paginationModel), { sort: newSorting }));
622
+ }
623
+ }
618
624
  : undefined })),
619
625
  react_1.default.createElement(ConfirmDialog_1.ConfirmDialog, { open: dialogOpen, onClose: handleDialogClose, onConfirm: handleDeleteSave }),
620
626
  react_1.default.createElement(Snackbar_1.default, { open: snackBar.open, autoHideDuration: 5000, onClose: () => {
@@ -103,6 +103,7 @@ const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, ind
103
103
  hiddenFields,
104
104
  page: paginationModel ? paginationModel.page : 0,
105
105
  filter: paginationModel ? paginationModel.filter : undefined,
106
+ sort: paginationModel ? paginationModel.sort : undefined,
106
107
  sumRows,
107
108
  };
108
109
  const paginatedData = yield (0, api_1.getGenericModelList)(loadPaginatedParams);
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { GridActionsColDef, GridColDef, GridFilterModel } from '@mui/x-data-grid';
1
+ import { GridActionsColDef, GridColDef, GridFilterModel, GridSortModel } from '@mui/x-data-grid';
2
2
  export type Id = string | number;
3
3
  export type Item = Record<string, any>;
4
4
  export type PaginatedResult = {
@@ -12,6 +12,7 @@ export interface PaginationModel {
12
12
  page: number;
13
13
  pageSize: number;
14
14
  filter?: GridFilterModel;
15
+ sort?: GridSortModel;
15
16
  }
16
17
  export type SchemaType = Record<string, Field>;
17
18
  export type modelOptionsType = Record<string, string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Components and Tools for building a React App having Django Rest Framework (DRF) as server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",