drf-react-by-schema 0.6.1 → 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,7 @@
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
+ import { AlertColor } from '@mui/material/Alert';
4
5
  type Id = string | number | null;
5
6
  interface TargetApiParams {
6
7
  path: string;
@@ -70,8 +71,20 @@ export interface getGenericModelListProps {
70
71
  loadedModelOptions?: modelOptionsType | boolean;
71
72
  page?: number;
72
73
  filter?: GridFilterModel;
74
+ sort?: GridSortModel;
75
+ sumRows?: SumRowsType;
73
76
  }
74
- export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, page, filter, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
77
+ export interface SumRowsType {
78
+ rows: SumRowsItem[];
79
+ severity?: AlertColor;
80
+ }
81
+ interface SumRowsItem {
82
+ field: string;
83
+ prefix?: string;
84
+ suffix?: string;
85
+ isCount?: boolean;
86
+ }
87
+ export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, page, filter, sort, sumRows, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
75
88
  export declare const getGenericModel: ({ model, serverEndPoint, id, relatedModel, relatedModelId, }: {
76
89
  model: string;
77
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, }) => __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,16 +550,16 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
550
550
  path += `/${relatedModel}/${relatedModelId}`;
551
551
  schemaPath += `/${id}/${relatedModel}`;
552
552
  }
553
- if (page || filter) {
554
- path += '?';
555
- }
556
- if (page) {
557
- path += `page=${page + 1}`;
553
+ const queryParams = [];
554
+ // SERVER-SIDE TOTALS (sumRows):
555
+ if (sumRows) {
556
+ const sumRowsParams = sumRows.rows.map(row => row.field).join(',');
557
+ queryParams.push(`sum_rows=${sumRowsParams}`);
558
558
  }
559
+ // SERVER-SIDE FILTERING:
559
560
  if (filter) {
560
- const filtersArr = [];
561
561
  if (filter.quickFilterValues && filter.quickFilterValues.length > 0 && filter.quickFilterValues[0]) {
562
- filtersArr.push(`search=${filter.quickFilterValues[0]}`);
562
+ queryParams.push(`search=${filter.quickFilterValues[0]}`);
563
563
  }
564
564
  for (const item of filter.items) {
565
565
  if (!item.operatorValue) {
@@ -568,12 +568,23 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
568
568
  const queryParam = item.value
569
569
  ? `columnField=${item.columnField}&operatorValue=${item.operatorValue}&value=${item.value}`
570
570
  : `columnField=${item.columnField}&operatorValue=${item.operatorValue}`;
571
- filtersArr.push(queryParam);
571
+ queryParams.push(queryParam);
572
572
  }
573
- if (page) {
574
- 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);
575
579
  }
576
- path += filtersArr.join('&');
580
+ queryParams.push(`ordering=${sortParams.join(',')}`);
581
+ }
582
+ // SERVER-SIDE PAGINATION:
583
+ if (page) {
584
+ queryParams.push(`page=${page + 1}`);
585
+ }
586
+ if (queryParams.length > 0) {
587
+ path += `?${queryParams.join('&')}`;
577
588
  }
578
589
  // Only get schema and columns if not in batches or in first batch:
579
590
  if (!schema) {
@@ -589,6 +600,7 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
589
600
  }
590
601
  }
591
602
  let rowCount = 0;
603
+ let sumRowsTotals = null;
592
604
  let data = [];
593
605
  if (!id || (id && !relatedModelId)) {
594
606
  if (isInBatches) {
@@ -601,6 +613,9 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
601
613
  const dataRaw = 'results' in ret ? ret.results : ret;
602
614
  if ('results' in ret) {
603
615
  rowCount = ret.count;
616
+ if (sumRows) {
617
+ sumRowsTotals = ret.sum_rows;
618
+ }
604
619
  }
605
620
  data = dataRaw.map((row) => {
606
621
  const newRow = {};
@@ -624,10 +639,10 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
624
639
  }
625
640
  if (loadedSchema) {
626
641
  // DEBUG console.log({ path, data });
627
- return { data, rowCount };
642
+ return { data, rowCount, sumRowsTotals };
628
643
  }
629
644
  // DEBUG console.log({ path, data, columns, schema });
630
- return { data, columns, schema, modelOptions, rowCount };
645
+ return { data, columns, schema, modelOptions, rowCount, sumRowsTotals };
631
646
  });
632
647
  exports.getGenericModelList = getGenericModelList;
633
648
  const getGenericModel = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', }) => __awaiter(void 0, void 0, void 0, function* () {
@@ -578,6 +578,10 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
578
578
  setPreparedColumns,
579
579
  showQuickFilter: true,
580
580
  quickFilterProps: { debounceMs: 500 },
581
+ getRowsToExport: (e) => {
582
+ console.log(e);
583
+ return [];
584
+ },
581
585
  },
582
586
  footer: {
583
587
  name,
@@ -611,6 +615,12 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
611
615
  setPaginationModel(Object.assign(Object.assign({}, paginationModel), { filter: newFilter }));
612
616
  }
613
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
+ }
614
624
  : undefined })),
615
625
  react_1.default.createElement(ConfirmDialog_1.ConfirmDialog, { open: dialogOpen, onClose: handleDialogClose, onConfirm: handleDeleteSave }),
616
626
  react_1.default.createElement(Snackbar_1.default, { open: snackBar.open, autoHideDuration: 5000, onClose: () => {
@@ -1,11 +1,7 @@
1
1
  /// <reference types="react" />
2
- import { AlertColor } from '@mui/material/Alert';
3
2
  import { GridRowId } from '@mui/x-data-grid';
4
3
  import { Item } from '../utils';
5
- export interface SumRowsType {
6
- rows: Item[];
7
- severity?: AlertColor;
8
- }
4
+ import { SumRowsType } from '../api';
9
5
  interface DataTotalsProps {
10
6
  data?: Item[];
11
7
  sumRows?: SumRowsType;
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ import { SumRowsType } from '../api';
3
+ interface DataTotalsServerProps {
4
+ sumRows?: SumRowsType;
5
+ totals?: null | Record<string, number>;
6
+ }
7
+ declare const DataTotalsServer: ({ sumRows, totals }: DataTotalsServerProps) => JSX.Element;
8
+ export default DataTotalsServer;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const Alert_1 = __importDefault(require("@mui/material/Alert"));
8
+ const List_1 = __importDefault(require("@mui/material/List"));
9
+ const ListItem_1 = __importDefault(require("@mui/material/ListItem"));
10
+ const react_number_format_1 = require("react-number-format");
11
+ ;
12
+ const DataTotalsServer = ({ sumRows, totals }) => {
13
+ return (react_1.default.createElement(react_1.default.Fragment, null, sumRows && sumRows.rows.length > 0 &&
14
+ react_1.default.createElement(Alert_1.default, { severity: sumRows.severity || 'info' },
15
+ react_1.default.createElement(List_1.default, { dense: true }, sumRows.rows.map(row => (react_1.default.createElement(ListItem_1.default, { key: `sumRows_${row.field}` },
16
+ react_1.default.createElement(react_number_format_1.NumericFormat, { value: totals ? totals[`${row.field}_total`] : 0, thousandSeparator: '.', decimalSeparator: ',', displayType: 'text', decimalScale: row.isCount ? 0 : 2, fixedDecimalScale: true, prefix: row.prefix, suffix: row.suffix }))))))));
17
+ };
18
+ exports.default = DataTotalsServer;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
- import { SumRowsType } from './DataTotals';
3
2
  import { GridEnrichedBySchemaColDef } from '../utils';
3
+ import { SumRowsType } from '../api';
4
4
  interface GenericModelListProps {
5
5
  columnFields: string[];
6
6
  hiddenFields?: string[];
@@ -42,6 +42,7 @@ const Button_1 = __importDefault(require("@mui/material/Button"));
42
42
  const AddCircleOutline_1 = __importDefault(require("@mui/icons-material/AddCircleOutline"));
43
43
  const DataGridBySchemaEditable_1 = __importDefault(require("./DataGridBySchemaEditable"));
44
44
  const DataTotals_1 = __importDefault(require("./DataTotals"));
45
+ const DataTotalsServer_1 = __importDefault(require("./DataTotalsServer"));
45
46
  const styles_1 = require("../styles");
46
47
  const api_1 = require("../api");
47
48
  const DRFReactBySchemaContext_1 = require("../context/DRFReactBySchemaContext");
@@ -102,6 +103,8 @@ const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, ind
102
103
  hiddenFields,
103
104
  page: paginationModel ? paginationModel.page : 0,
104
105
  filter: paginationModel ? paginationModel.filter : undefined,
106
+ sort: paginationModel ? paginationModel.sort : undefined,
107
+ sumRows,
105
108
  };
106
109
  const paginatedData = yield (0, api_1.getGenericModelList)(loadPaginatedParams);
107
110
  if (paginatedData && typeof paginatedData !== 'boolean') {
@@ -142,6 +145,8 @@ const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, ind
142
145
  : typeof data.rowCount !== undefined
143
146
  ? data.rowCount
144
147
  : 0 })),
145
- react_1.default.createElement(DataTotals_1.default, { data: data.data, sumRows: sumRows, visibleRows: visibleRows })))));
148
+ paginationMode === 'client'
149
+ ? react_1.default.createElement(DataTotals_1.default, { data: data.data, sumRows: sumRows, visibleRows: visibleRows })
150
+ : react_1.default.createElement(DataTotalsServer_1.default, { sumRows: sumRows, totals: data.sumRowsTotals })))));
146
151
  };
147
152
  exports.default = GenericModelList;
package/dist/utils.d.ts CHANGED
@@ -1,16 +1,18 @@
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 = {
5
5
  count: number;
6
6
  next: number;
7
7
  previous: number;
8
+ sum_rows: null | Record<string, number>;
8
9
  results: Item[];
9
10
  };
10
11
  export interface PaginationModel {
11
12
  page: number;
12
13
  pageSize: number;
13
14
  filter?: GridFilterModel;
15
+ sort?: GridSortModel;
14
16
  }
15
17
  export type SchemaType = Record<string, Field>;
16
18
  export type modelOptionsType = Record<string, string>;
@@ -54,9 +56,10 @@ interface GridActionsBySchemaColDef extends GridActionsColDef {
54
56
  export type GridEnrichedBySchemaColDef = GridBySchemaColDef | GridActionsBySchemaColDef;
55
57
  export interface DataSchemaColumnsType {
56
58
  data: Item[];
57
- rowCount?: number;
58
59
  schema: SchemaType;
59
60
  modelOptions: modelOptionsType;
61
+ rowCount?: number;
62
+ sumRowsTotals?: null | Record<string, number>;
60
63
  columns?: GridEnrichedBySchemaColDef[];
61
64
  }
62
65
  export interface ItemSchemaColumnsType {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.6.1",
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",