feat: Aggiunti altri tipi e interfacce per i Model

This commit is contained in:
Maicol Battistini 2022-01-07 14:22:14 +01:00
parent 5e4069930e
commit ed7c801309
No known key found for this signature in database
GPG Key ID: 4FDB0F87CDB1D34A
3 changed files with 26 additions and 11 deletions

View File

@ -12,7 +12,11 @@ import type {
VnodeDOM
} from 'mithril';
import {Model} from '../../Models';
import {
IModel,
IndexedModel,
Model
} from '../../Models';
import type {
FieldT,
SelectT,
@ -32,7 +36,7 @@ export type ColumnT = {
id?: string
title: string
type?: 'checkbox' | 'numeric'
valueModifier?: (instance: Model, property: string) => any
valueModifier?: (instance: IModel<IndexedModel>, property: string) => any
};
export type SectionT = {
id?: string
@ -45,11 +49,9 @@ export type SectionT = {
| Record<string, TextFieldT | TextAreaT | SelectT>
};
export type ColumnsT = Record<string, string | ColumnT>;
export type RowsT = Collection<Model>;
export type RowsT = Collection<IModel<IndexedModel>>;
export type SectionsT = Record<string, SectionT> | SectionT[];
export type IndexedModel = Model & {[prop: string]: any};
/**
* @abstract
*/
@ -60,7 +62,10 @@ export class RecordsPage extends Page {
dialogs: Children[];
recordDialogMaxWidth: string | number = 'auto';
model: typeof Model;
customSetter: (model: Model, fields: Collection<File | string>) => void;
customSetter: (
model: IModel<IndexedModel>,
fields: Collection<File | string>
) => void;
/**
* What fields should take precedence when saving the record
@ -128,7 +133,7 @@ export class RecordsPage extends Page {
}
return this.rows
.map((instance: IndexedModel, index: string) => (
.map((instance: IModel<IndexedModel>, index: string) => (
<TableRow
key={index}
data-model-id={instance.getId()}
@ -149,7 +154,7 @@ export class RecordsPage extends Page {
async updateRecord(id: number) {
// @ts-ignore
const response = await this.model.find(id);
const instance = response.getData() as IndexedModel;
const instance = response.getData() as IModel<IndexedModel>;
const dialog = $('mwc-dialog#add-record-dialog');
dialog
@ -329,7 +334,7 @@ export class RecordsPage extends Page {
if (isFormValid(form)) {
// @ts-ignore
// eslint-disable-next-line new-cap
const instance = new this.model() as IndexedModel;
const instance = new this.model() as IModel<IndexedModel>;
if (this.customSetter) {
this.customSetter(instance, collect(getFormData(form)));

View File

@ -4,10 +4,20 @@ import {
} from 'coloquent';
import {snakeCase} from 'lodash';
export interface IndexedModel extends Model {
[prop: string]: any;
}
export interface InstantiableModel<T extends IndexedModel> {
new (): T;
}
export type IModel<T extends IndexedModel> = InstanceType<InstantiableModel<T>>;
/**
* The base model for all models.
*/
export default abstract class Model extends BaseModel {
export abstract class Model extends BaseModel {
jsonApiType: string = '';
constructor() {

View File

@ -1 +1 @@
export {default as Model} from './Model';
export * from './Model';