diff --git a/anet-dictionary.yml b/anet-dictionary.yml index 62977ce6eb..945ebae7af 100644 --- a/anet-dictionary.yml +++ b/anet-dictionary.yml @@ -476,6 +476,84 @@ fields: location: format: LAT_LON + customFields: + multipleButtons: + type: enumset + label: Choose one or more of the options + helpText: Help text for choosing multiple values + choices: + opt1: + label: Option 1 + opt2: + label: Option 2 + opt3: + label: Option 3 + inputFieldName: + type: text + label: Text field + placeholder: Placeholder text for input field + helpText: Help text for text field + colourOptions: + type: enum + label: Choose one of the colours + helpText: Help text for choosing colours + choices: + GREEN: + label: Green + color: '#c2ffb3' + AMBER: + label: Amber + color: '#ffe396' + RED: + label: Red + color: '#ff8279' + textareaFieldName: + type: text + label: Textarea field + placeholder: Placeholder text for textarea field + helpText: Help text for textarea field + componentClass: textarea + style: + height: 200px + visibleWhen: $[?(@.colourOptions === 'GREEN')] + numberFieldName: + type: number + typeError: Number field must be a number + label: Number field + placeholder: Placeholder text for number field + helpText: Help text for number field + validations: + - type: integer + - type: min + params: [5] + - type: max + params: [100] + visibleWhen: $[?((@.colourOptions === 'GREEN')||(@.colourOptions === 'RED'))] + nlt: + type: date + label: Not later than date + helpText: Help text for date field + nlt_dt: + type: datetime + label: Not later than datetime + helpText: Help text for datetime field + arrayFieldName: + type: array_of_objects + label: Array of objects + helpText: Here you can add as many objects as needed + addButtonLabel: Add an object + objectLabel: Object + objectFields: + textF: + type: text + label: Object text + placeholder: Placeholder text for object text field + helpText: Help text for object text field + dateF: + type: date + label: Object date + helpText: Help text for object date field + visibleWhen: $[?(@.colourOptions === 'GREEN')] position: name: 'Position Name' diff --git a/client/src/components/advancedSelectWidget/MultiTypeAdvancedSelectComponent.js b/client/src/components/advancedSelectWidget/MultiTypeAdvancedSelectComponent.js index e2747e3cf1..6ce20ebec6 100644 --- a/client/src/components/advancedSelectWidget/MultiTypeAdvancedSelectComponent.js +++ b/client/src/components/advancedSelectWidget/MultiTypeAdvancedSelectComponent.js @@ -94,7 +94,7 @@ const widgetPropsLocation = { overlayColumns: ["Name"], filterDefs: entityFilters, queryParams: { status: Model.STATUS.ACTIVE }, - fields: Models.Location.autocompleteQuery, + fields: Models.Location.autocompleteQueryWithNotes, addon: LOCATIONS_ICON } diff --git a/client/src/models/Location.js b/client/src/models/Location.js index 47361adf1f..98bce1ee4c 100644 --- a/client/src/models/Location.js +++ b/client/src/models/Location.js @@ -1,4 +1,7 @@ -import Model from "components/Model" +import Model, { + createCustomFieldsSchema, + GRAPHQL_NOTES_FIELDS +} from "components/Model" import { convertLatLngToMGRS, convertMGRSToLatLng } from "geoUtils" import _isEmpty from "lodash/isEmpty" import LOCATIONS_ICON from "resources/locations.png" @@ -17,6 +20,11 @@ export default class Location extends Model { REPORT_APPROVAL: "REPORT_APPROVAL" } + // create yup schema for the customFields, based on the customFields config + static customFieldsSchema = createCustomFieldsSchema( + Settings.fields.location.customFields + ) + static yupSchema = yup .object() .shape({ @@ -118,10 +126,14 @@ export default class Location extends Model { .nullable() .default([]) }) + // not actually in the database, the database contains the JSON customFields + .concat(Location.customFieldsSchema) .concat(Model.yupSchema) static autocompleteQuery = "uuid, name" + static autocompleteQueryWithNotes = `${this.autocompleteQuery} ${GRAPHQL_NOTES_FIELDS}` + static hasCoordinates(location) { return ( location && diff --git a/client/src/pages/locations/Edit.js b/client/src/pages/locations/Edit.js index d65e4052bb..701c70a384 100644 --- a/client/src/pages/locations/Edit.js +++ b/client/src/pages/locations/Edit.js @@ -1,9 +1,11 @@ import { DEFAULT_SEARCH_PROPS, PAGE_PROPS_NO_NAV } from "actions" import API from "api" import { gql } from "apollo-boost" +import { initInvisibleFields } from "components/CustomFields" +import { DEFAULT_CUSTOM_FIELDS_PARENT } from "components/Model" import { - PageDispatchersPropType, mapPageDispatchersToProps, + PageDispatchersPropType, useBoilerplate } from "components/Page" import RelatedObjectNotes, { @@ -13,6 +15,8 @@ import { Location } from "models" import React from "react" import { connect } from "react-redux" import { useParams } from "react-router-dom" +import Settings from "settings" +import utils from "utils" import LocationForm from "./Form" const GQL_GET_LOCATION = gql` @@ -52,7 +56,8 @@ const GQL_GET_LOCATION = gql` avatar(size: 32) } } - } + } + customFields ${GRAPHQL_NOTES_FIELDS} } } @@ -75,8 +80,14 @@ const LocationEdit = ({ pageDispatchers }) => { if (done) { return result } - + if (data) { + data.location[DEFAULT_CUSTOM_FIELDS_PARENT] = utils.parseJsonSafe( + data.location.customFields + ) + } const location = new Location(data ? data.location : {}) + // mutates the object + initInvisibleFields(location, Settings.fields.location.customFields) return (