-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeospatialField.ts
More file actions
147 lines (120 loc) · 4.07 KB
/
GeospatialField.ts
File metadata and controls
147 lines (120 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { type GeospatialFieldComponent } from '@defra/forms-model'
import { type ArraySchema } from 'joi'
import { type ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'
import {
FormComponent,
isGeospatialState
} from '~/src/server/plugins/engine/components/FormComponent.js'
import { geospatialSchema } from '~/src/server/plugins/engine/components/helpers/geospatial.js'
import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
import {
type ErrorMessageTemplateList,
type FormPayload,
type FormState,
type FormStateValue,
type FormSubmissionError,
type FormSubmissionState,
type GeospatialState
} from '~/src/server/plugins/engine/types.js'
export class GeospatialField extends FormComponent {
declare options: GeospatialFieldComponent['options']
declare formSchema: ArraySchema<GeospatialState>
declare stateSchema: ArraySchema<GeospatialState>
constructor(
def: GeospatialFieldComponent,
props: ConstructorParameters<typeof ComponentBase>[1]
) {
super(def, props)
const { options } = def
let formSchema = geospatialSchema.label(this.label).required()
formSchema = formSchema.max(50)
if (options.required !== false) {
formSchema = formSchema.min(1)
}
this.formSchema = formSchema
this.stateSchema = formSchema.default(null)
this.options = options
}
getFormValueFromState(state: FormSubmissionState) {
const { name } = this
return this.getFormValue(state[name])
}
getFormValue(value?: FormStateValue | FormState) {
return this.isValue(value) ? value : undefined
}
getDisplayStringFromFormValue(features: GeospatialState | undefined): string {
if (!features?.length) {
return ''
}
const unit = features.length === 1 ? 'location' : 'locations'
return `Added ${features.length} ${unit}`
}
getDisplayStringFromState(state: FormSubmissionState) {
const features = this.getFormValueFromState(state)
return this.getDisplayStringFromFormValue(features)
}
getContextValueFromFormValue(
features: GeospatialState | undefined
): string[] | null {
return features?.map(({ id }) => id) ?? null
}
getContextValueFromState(state: FormSubmissionState) {
const features = this.getFormValueFromState(state)
return this.getContextValueFromFormValue(features)
}
getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {
const viewModel = super.getViewModel(payload, errors)
const value =
typeof viewModel.value === 'string'
? viewModel.value
: JSON.stringify(viewModel.value, null, 2)
return {
...viewModel,
value
}
}
getErrors(errors?: FormSubmissionError[]): FormSubmissionError[] | undefined {
const fieldErrors = super.getErrors(errors)
fieldErrors?.forEach((err) => {
if (err.name === 'description') {
err.href = `#description_${err.path[1]}`
err.text = `Enter description for location ${Number(err.path[1]) + 1}`
}
})
return fieldErrors
}
getViewErrors(
errors?: FormSubmissionError[]
): FormSubmissionError[] | undefined {
return this.getErrors(errors)
}
isValue(value?: FormStateValue | FormState): value is GeospatialState {
return isGeospatialState(value)
}
/**
* For error preview page that shows all possible errors on a component
*/
getAllPossibleErrors(): ErrorMessageTemplateList {
const staticErrors = GeospatialField.getAllPossibleErrors()
return {
...staticErrors,
advancedSettingsErrors: [...staticErrors.advancedSettingsErrors]
}
}
/**
* Static version of getAllPossibleErrors that doesn't require a component instance.
*/
static getAllPossibleErrors(): ErrorMessageTemplateList {
return {
baseErrors: [
{ type: 'required', template: messageTemplate.selectRequired },
{
type: 'array.min',
template: '{{#title}} must contain at least 1 items'
},
{ type: 'object.invalidjson', template: messageTemplate.format }
],
advancedSettingsErrors: []
}
}
}