# Models

The models of the admin have to be in the parent src/config folder

## root

The root file must import all the

- Resource transform function
- Resource options
- Resource model
  Othe all the resources

## Resource transform function

Function used to display additional informations about the model on their listing/detail view

```javascript
export const item = obj => ({
    infos: {
        info1: obj.something,
        ...
        info4: obj.somethingElse,
    },
})
```

### Resource options

The options is an object having the following possible attributes

```javascript
export const options = {
  titleList: 'string', //List title
  titleUpdate: 'string', //Edit title
  titleCreate: 'string', //Add title
  titleDetail: 'string', //Detail title
  successCreate: 'string, // Success create message
  successUpdate: 'string, // Success update message
  filters: {  // filter options
    ...
  },
  exportable: boolean, // show export action on listing view
  confirmOnSave: boolean, // show confirm modal when saving
  noDelete: boolean, // disabled or not delete
  noDetail: boolean, // disabled or not view
  noUpdate: boolean, // disabled or not edit
  noCreate: boolean, // disabled or not create
  // Other config
  standalone: boolean, // when listData (= subresource) that is a standalone API => avoid to add the ?parent type and id in the url (since standalone)
  separated: boolean, // when subs (= subresource) that has a separatated view (sometime with another submenu)
  // legend options
  legend: {
    titleDetail: 'string', // title of the legend
    infos: {
      info1: 'string', // Detail about info
      info1Detail: 'string', // Detail text for edit/view
      // up to info4
    },
  },
  callApiOptions: {
    // Add options to detail call api
    detail: {
      noSuccess: true,
    },
  },
  baseNav: // base url for subnav link
  nav:[ // subnavigation
    {
      title: 'title',
      link: 'absolute_url',
      maxWidth: 50,
      minWidth: 50,
    },
    // no link => separator
    {
      title: 'Data',
      icon: 'database',
    },
  ],
  headers: [ // array of headers
    {
      disableSort: boolean, // disableSort
      noOrder: boolean, // disableSort alias
      type: 'status' | 'date' | 'small' | '',
      key: 'string', // data key in the object,
      title: 'string', // title of the column,
      list: 'array', // array of result when list
      values: 'string', // path into the content for the list,
      noUpdate: 'boolean', // disable fast editing on column.
      fieldName: 'string', // name of the field object if !== to the key
      pathIdKey: 'exemple._id', // path to id key used for update.
      link: 'string', // reference of transform funciton key dynamic link
      // For link
      id: 'string', // data key in the object for the id of the linked resource,
      resource: 'string', // resource name (for linked resource)
      resourceType: 'detail' | 'edit' | 'listing', // type of target
      searchParameters: Object, // additional search parameters for link,
      avoidContentText: 'boolean' // Avoid rendering header text with <TextItem>
      otherResource: 'string', // choose model who must be based the field in FastUpdate case ( Example: check Infamy Project )
      objectToSend: 'string', // Path to the object who must be send in generic FastUpdate ( to use when otherResource is Used )
    }
  ],
  // Complementary form
  complementary: {
    // Message success
    title: 'Post comment',
    success: 'Success Message',
    api: '',
    // Other model
    fields: complementaryModel,
  },
  //  Other actions forms models
  actions: [
    {
      title: 'Action1',
      // api path
      api: '',
      success: 'Success Message',
    },
    {
      title: 'Action2',
      // api path
      api: '',
      success: 'Success Message',
    },
  ],
}
```

## Model object

Object structure describing the object (object of field)

### fields

#### structural (view OR edit)

- Type column

```javascript
const model={
  ...,
  column:{
    type: 'column',
    fields: {
      // any structure
    }
  }
}
```

- Type section

```javascript
const model={
  ...,
  section:{
    type: 'section',
    fields: {
      // any structure
    }
  }
}
```

- Type list

```javascript
const model={
  ...,
  list:{
    type: 'list',
    label: 'list',
    labelAdd: 'add in list',
    fields: {
      // any structure
    },
  }
}
```

### form item

- General structure

```javascript
const model={
  ...,
  elem: {
    type: 'string' | 'link' | 'textarea' | 'multi' | 'multiarea' | 'text' | 'date' | 'datetime' | 'picture' | 'select' | 'bool' | 'wysiwyg' | 'schedule',
    first: boolean, // disable margin top (for top elements)
    required: boolean, // required or not
    name: string, // path in the data model
    disabled: boolean, // disabled or not
    label: string, // label to display,
    hideDetail: boolean, // Hide field for detail view
    hideUpdate: boolean, // Hide field for update view,
    hideCreate: boolean, // Hide field for create view,
  }
}
```

- link

Attach link to value

```javascript
const model={
  ...,
  elem: {
    type: 'link',
    resource: 'string', // resource name (for linked resource)
    resourceType: 'detail' | 'edit' | 'listing', // type of target
    link: 'string', // path of the value in the data object
    id: 'string', // same as link
    searchParameters: Object, // additional search parameters
  }
```

- Text

```javascript
const model={
  ...,
  elem: {
    type: 'text',
    api: Object, // api parameters to give
    modelValue: { value: '\_id', text: 'title.fr' },
  }
}
```

- Datetime

```javascript
const model={
  ...,
  elem: {
    type: 'datetime',
    dateFormat: 'LL',
    timeFormat: 'HH:mm'
  }
}
```

- Select

Additional data

```javascript
const model={
  ...,
  elem: {
    type: 'select',
    values: 'string' || Array<any>, // items for the customSelect
    list: 'Array', // items for the customSelect
    asObject: boolean, // tel customSelect how to select value
    apiPayload: Object, // api parameters to give
    api: Object, // allias of apiPayload
    modelValues: { value: '\_id', text: 'title.fr' }, // values to use in the model to display and save data
  }
}
```

- Picture

Additional data

```javascript
const model={
  ...,
  elem: {
    ...,
    type: 'picture',
    s3Upload: 'boolean', // If S3 Upload is used
    aspectRatio: 1.25, // aspect ratio for the cropper
    displayValue: 'string', // if returned value is an object eg: { thumb: .... , full: ...} choose path to display eg:('full')
    saveValue: 'string', // key in the result object to store in the input value,
    asObject: // similar to saveValue but says to the element to key the entire response (if not provided try to get res.url || res.default || res.picture || res)
    label: 'picture 4',
    name: 'pictures.pic4',
    apiPath: 'string', // path of the auto upload api in the apis object
    url: string, // api for the auto upload (alternative to apiPath) /!\ if s3Upload === true url must be api to get upload link
  }
}
```
