| 1 | + | import React, { FunctionComponent } from 'react'; |
| 2 | + | import { graphql, useFragment, useMutation } from 'react-relay'; |
| 3 | + | import { Formik, Form, Field } from 'formik'; |
| 4 | + | import * as R from 'ramda'; |
| 5 | + | import * as Yup from 'yup'; |
| 6 | + | import makeStyles from '@mui/styles/makeStyles'; |
| 7 | + | import { FormikConfig } from 'formik/dist/types'; |
| 8 | + | import { useFormatter } from '../../../../components/i18n'; |
| 9 | + | import TextField from '../../../../components/TextField'; |
| 10 | + | import { SubscriptionFocus } from '../../../../components/Subscription'; |
| 11 | + | import CreatedByField from '../../common/form/CreatedByField'; |
| 12 | + | import ObjectMarkingField from '../../common/form/ObjectMarkingField'; |
| 13 | + | import MarkDownField from '../../../../components/MarkDownField'; |
| 14 | + | import { |
| 15 | + | convertCreatedBy, |
| 16 | + | convertMarkings, |
| 17 | + | convertStatus, |
| 18 | + | } from '../../../../utils/Edition'; |
| 19 | + | import StatusField from '../../common/form/StatusField'; |
| 20 | + | import { Theme } from '../../../../components/Theme'; |
| 21 | + | import { CountryEditionOverview_country$key } from './__generated__/CountryEditionOverview_country.graphql'; |
| 22 | + | import { Option } from '../../common/form/ReferenceField'; |
| 23 | + | import CommitMessage from '../../common/form/CommitMessage'; |
| 24 | + | import { adaptFieldValue } from '../../../../utils/String'; |
| 25 | + | |
| 26 | + | const useStyles = makeStyles<Theme>((theme) => ({ |
| 27 | + | drawerPaper: { |
| 28 | + | minHeight: '100vh', |
| 29 | + | width: '50%', |
| 30 | + | position: 'fixed', |
| 31 | + | overflow: 'hidden', |
| 32 | + | transition: theme.transitions.create('width', { |
| 33 | + | easing: theme.transitions.easing.sharp, |
| 34 | + | duration: theme.transitions.duration.enteringScreen, |
| 35 | + | }), |
| 36 | + | padding: '30px 30px 30px 30px', |
| 37 | + | }, |
| 38 | + | createButton: { |
| 39 | + | position: 'fixed', |
| 40 | + | bottom: 30, |
| 41 | + | right: 30, |
| 42 | + | }, |
| 43 | + | importButton: { |
| 44 | + | position: 'absolute', |
| 45 | + | top: 30, |
| 46 | + | right: 30, |
| 47 | + | }, |
| 48 | + | })); |
| 49 | + | |
| 50 | + | const countryMutationFieldPatch = graphql` |
| 51 | + | mutation CountryEditionOverviewFieldPatchMutation( |
| 52 | + | $id: ID! |
| 53 | + | $input: [EditInput]! |
| 54 | + | $commitMessage: String |
| 55 | + | $references: [String] |
| 56 | + | ) { |
| 57 | + | countryEdit(id: $id) { |
| 58 | + | fieldPatch( |
| 59 | + | input: $input |
| 60 | + | commitMessage: $commitMessage |
| 61 | + | references: $references |
| 62 | + | ) { |
| 63 | + | ...CountryEditionOverview_country |
| 64 | + | ...Country_country |
| 65 | + | } |
| 66 | + | } |
| 67 | + | } |
| 68 | + | `; |
| 69 | + | |
| 70 | + | export const countryEditionOverviewFocus = graphql` |
| 71 | + | mutation CountryEditionOverviewFocusMutation($id: ID!, $input: EditContext!) { |
| 72 | + | countryEdit(id: $id) { |
| 73 | + | contextPatch(input: $input) { |
| 74 | + | id |
| 75 | + | } |
| 76 | + | } |
| 77 | + | } |
| 78 | + | `; |
| 79 | + | |
| 80 | + | const countryMutationRelationAdd = graphql` |
| 81 | + | mutation CountryEditionOverviewRelationAddMutation( |
| 82 | + | $id: ID! |
| 83 | + | $input: StixMetaRelationshipAddInput! |
| 84 | + | ) { |
| 85 | + | countryEdit(id: $id) { |
| 86 | + | relationAdd(input: $input) { |
| 87 | + | from { |
| 88 | + | ...CountryEditionOverview_country |
| 89 | + | } |
| 90 | + | } |
| 91 | + | } |
| 92 | + | } |
| 93 | + | `; |
| 94 | + | |
| 95 | + | const countryMutationRelationDelete = graphql` |
| 96 | + | mutation CountryEditionOverviewRelationDeleteMutation( |
| 97 | + | $id: ID! |
| 98 | + | $toId: StixRef! |
| 99 | + | $relationship_type: String! |
| 100 | + | ) { |
| 101 | + | countryEdit(id: $id) { |
| 102 | + | relationDelete( |
| 103 | + | toId: $toId, |
| 104 | + | relationship_type: $relationship_type |
| 105 | + | ) { |
| 106 | + | ...CountryEditionOverview_country |
| 107 | + | } |
| 108 | + | } |
| 109 | + | } |
| 110 | + | `; |
| 111 | + | |
| 112 | + | const countryEditionOverviewFragment = graphql` |
| 113 | + | fragment CountryEditionOverview_country on Country { |
| 114 | + | id |
| 115 | + | name |
| 116 | + | description |
| 117 | + | latitude |
| 118 | + | longitude |
| 119 | + | createdBy { |
| 120 | + | ... on Identity { |
| 121 | + | id |
| 122 | + | name |
| 123 | + | entity_type |
| 124 | + | } |
| 125 | + | } |
| 126 | + | objectMarking { |
| 127 | + | edges { |
| 128 | + | node { |
| 129 | + | id |
| 130 | + | definition |
| 131 | + | definition_type |
| 132 | + | } |
| 133 | + | } |
| 134 | + | } |
| 135 | + | status { |
| 136 | + | id |
| 137 | + | order |
| 138 | + | template { |
| 139 | + | name |
| 140 | + | color |
| 141 | + | } |
| 142 | + | } |
| 143 | + | workflowEnabled |
| 144 | + | } |
| 145 | + | `; |
| 146 | + | |
| 147 | + | const countryValidation = (t: (v: string) => string) => Yup.object().shape({ |
| 148 | + | name: Yup.string().required(t('This field is required')), |
| 149 | + | description: Yup.string() |
| 150 | + | .min(3, t('The value is too short')) |
| 151 | + | .max(5000, t('The value is too long')) |
| 152 | + | .required(t('This field is required')), |
| 153 | + | latitude: Yup.number().typeError(t('This field must be a number')), |
| 154 | + | longitude: Yup.number().typeError(t('This field must be a number')), |
| 155 | + | x_opencti_workflow_id: Yup.object(), |
| 156 | + | }); |
| 157 | + | |
| 158 | + | interface CountryEditionOverviewProps { |
| 159 | + | countryRef: CountryEditionOverview_country$key, |
| 160 | + | context: ReadonlyArray<{ |
| 161 | + | readonly focusOn: string | null; |
| 162 | + | readonly name: string; |
| 163 | + | } | null> | null |
| 164 | + | enableReferences?: boolean |
| 165 | + | handleClose: () => void |
| 166 | + | } |
| 167 | + | |
| 168 | + | interface CountryEditionFormValues { |
| 169 | + | message: string |
| 170 | + | references: Option[], |
| 171 | + | x_opencti_workflow_id: Option |
| 172 | + | createdBy: Option |
| 173 | + | objectMarking: Option[] |
| 174 | + | } |
| 175 | + | |
| 176 | + | const CountryEditionOverviewComponent: FunctionComponent<CountryEditionOverviewProps> = ({ |
| 177 | + | countryRef, |
| 178 | + | context, |
| 179 | + | enableReferences = false, |
| 180 | + | handleClose, |
| 181 | + | }) => { |
| 182 | + | const { t } = useFormatter(); |
| 183 | + | const country = useFragment(countryEditionOverviewFragment, countryRef); |
| 184 | + | |
| 185 | + | const createdBy = convertCreatedBy(country); |
| 186 | + | const objectMarking = convertMarkings(country); |
| 187 | + | const status = convertStatus(t, country); |
| 188 | + | |
| 189 | + | const [commitRelationAdd] = useMutation(countryMutationRelationAdd); |
| 190 | + | const [commitRelationDelete] = useMutation(countryMutationRelationDelete); |
| 191 | + | const [commitFieldPatch] = useMutation(countryMutationFieldPatch); |
| 192 | + | const [commitEditionFocus] = useMutation(countryEditionOverviewFocus); |
| 193 | + | |
| 194 | + | const handleChangeFocus = (name: string) => { |
| 195 | + | commitEditionFocus({ |
| 196 | + | variables: { |
| 197 | + | id: country.id, |
| 198 | + | input: { |
| 199 | + | focusOn: name, |
| 200 | + | }, |
| 201 | + | }, |
| 202 | + | }); |
| 203 | + | }; |
| 204 | + | |
| 205 | + | const onSubmit: FormikConfig<CountryEditionFormValues>['onSubmit'] = (values, { setSubmitting }) => { |
| 206 | + | const commitMessage = values.message; |
| 207 | + | const references = R.pluck('value', values.references || []); |
| 208 | + | const inputValues = R.pipe( |
| 209 | + | R.dissoc('message'), |
| 210 | + | R.dissoc('references'), |
| 211 | + | R.assoc('x_opencti_workflow_id', values.x_opencti_workflow_id?.value), |
| 212 | + | R.assoc('createdBy', values.createdBy?.value), |
| 213 | + | R.assoc('objectMarking', R.pluck('value', values.objectMarking)), |
| 214 | + | R.toPairs, |
| 215 | + | R.map((n) => ({ |
| 216 | + | key: n[0], |
| 217 | + | value: adaptFieldValue(n[1]), |
| 218 | + | })), |
| 219 | + | )(values); |
| 220 | + | commitFieldPatch({ |
| 221 | + | variables: { |
| 222 | + | id: country.id, |
| 223 | + | input: inputValues, |
| 224 | + | commitMessage: |
| 225 | + | commitMessage && commitMessage.length > 0 ? commitMessage : null, |
| 226 | + | references, |
| 227 | + | }, |
| 228 | + | onCompleted: () => { |
| 229 | + | setSubmitting(false); |
| 230 | + | handleClose(); |
| 231 | + | }, |
| 232 | + | }); |
| 233 | + | }; |
| 234 | + | |
| 235 | + | const handleChangeCreatedBy = (_: string, value: Option) => { |
| 236 | + | if (!enableReferences) { |
| 237 | + | commitRelationAdd({ |
| 238 | + | variables: { |
| 239 | + | id: country.id, |
| 240 | + | input: { key: 'createdBy', value: value.value ?? '' }, |
| 241 | + | }, |
| 242 | + | }); |
| 243 | + | } |
| 244 | + | }; |
| 245 | + | |
| 246 | + | const handleChangeObjectMarking = (_: string, values: Option[]) => { |
| 247 | + | if (!enableReferences) { |
| 248 | + | const currentMarkingDefinitions = (country.objectMarking?.edges ?? []).map((n) => ({ label: n?.node.definition, value: n?.node.id })); |
| 249 | + | const added = R.difference(values, currentMarkingDefinitions).at(0); |
| 250 | + | const removed = R.difference(currentMarkingDefinitions, values).at(0); |
| 251 | + | if (added) { |
| 252 | + | commitRelationAdd({ |
| 253 | + | variables: { |
| 254 | + | id: country.id, |
| 255 | + | input: { |
| 256 | + | toId: added.value, |
| 257 | + | relationship_type: 'object-marking', |
| 258 | + | }, |
| 259 | + | }, |
| 260 | + | }); |
| 261 | + | } |
| 262 | + | if (removed) { |
| 263 | + | commitRelationDelete({ |
| 264 | + | variables: { |
| 265 | + | id: country.id, |
| 266 | + | toId: removed.value, |
| 267 | + | relationship_type: 'object-marking', |
| 268 | + | }, |
| 269 | + | }); |
| 270 | + | } |
| 271 | + | } |
| 272 | + | }; |
| 273 | + | const handleSubmitField = (name: string, value: Option | string) => { |
| 274 | + | if (!enableReferences) { |
| 275 | + | let finalValue: unknown = value as string; |
| 276 | + | if (name === 'x_opencti_workflow_id') { |
| 277 | + | finalValue = (value as Option).value; |
| 278 | + | } |
| 279 | + | countryValidation(t) |
| 280 | + | .validateAt(name, { [name]: value }) |
| 281 | + | .then(() => { |
| 282 | + | commitFieldPatch({ |
| 283 | + | variables: { |
| 284 | + | id: country.id, |
| 285 | + | input: { key: name, value: finalValue ?? '' }, |
| 286 | + | }, |
| 287 | + | }); |
| 288 | + | }) |
| 289 | + | .catch(() => false); |
| 290 | + | } |
| 291 | + | }; |
| 292 | + | |
| 293 | + | const initialValues = R.pipe( |
| 294 | + | R.assoc('createdBy', createdBy), |
| 295 | + | R.assoc('objectMarking', objectMarking), |
| 296 | + | R.assoc('x_opencti_workflow_id', status), |
| 297 | + | R.pick([ |
| 298 | + | 'name', |
| 299 | + | 'description', |
| 300 | + | 'latitude', |
| 301 | + | 'longitude', |
| 302 | + | 'createdBy', |
| 303 | + | 'objectMarking', |
| 304 | + | 'x_opencti_workflow_id', |
| 305 | + | ]), |
| 306 | + | )(country); |
| 307 | + | |
| 308 | + | return ( |
| 309 | + | <Formik |
| 310 | + | enableReinitialize={true} |
| 311 | + | initialValues={initialValues as never} |
| 312 | + | validationSchema={countryValidation(t)} |
| 313 | + | onSubmit={onSubmit} |
| 314 | + | > |
| 315 | + | {({ |
| 316 | + | submitForm, |
| 317 | + | isSubmitting, |
| 318 | + | validateForm, |
| 319 | + | setFieldValue, |
| 320 | + | values, |
| 321 | + | }) => ( |
| 322 | + | <Form style={{ margin: '20px 0 20px 0' }}> |
| 323 | + | <Field |
| 324 | + | component={TextField} |
| 325 | + | variant="standard" |
| 326 | + | name="name" |
| 327 | + | label={t('Name')} |
| 328 | + | fullWidth={true} |
| 329 | + | onFocus={handleChangeFocus} |
| 330 | + | onSubmit={handleSubmitField} |
| 331 | + | helperText={ |
| 332 | + | <SubscriptionFocus context={context} fieldName="name" /> |
| 333 | + | } |
| 334 | + | /> |
| 335 | + | <Field |
| 336 | + | component={MarkDownField} |
| 337 | + | name="description" |
| 338 | + | label={t('Description')} |
| 339 | + | fullWidth={true} |
| 340 | + | multiline={true} |
| 341 | + | rows="4" |
| 342 | + | style={{ marginTop: 20 }} |
| 343 | + | onFocus={handleChangeFocus} |
| 344 | + | onSubmit={handleSubmitField} |
| 345 | + | helperText={ |
| 346 | + | <SubscriptionFocus context={context} fieldName="description" /> |
| 347 | + | } |
| 348 | + | /> |
| 349 | + | <Field |
| 350 | + | component={TextField} |
| 351 | + | variant="standard" |
| 352 | + | style={{ marginTop: 20 }} |
| 353 | + | name="latitude" |
| 354 | + | label={t('Latitude')} |
| 355 | + | fullWidth={true} |
| 356 | + | onFocus={handleChangeFocus} |
| 357 | + | onSubmit={handleSubmitField} |
| 358 | + | helperText={ |
| 359 | + | <SubscriptionFocus context={context} fieldName="latitude" /> |
| 360 | + | } |
| 361 | + | /> |
| 362 | + | <Field |
| 363 | + | component={TextField} |
| 364 | + | variant="standard" |
| 365 | + | style={{ marginTop: 20 }} |
| 366 | + | name="longitude" |
| 367 | + | label={t('Longitude')} |
| 368 | + | fullWidth={true} |
| 369 | + | onFocus={handleChangeFocus} |
| 370 | + | onSubmit={handleSubmitField} |
| 371 | + | helperText={ |
| 372 | + | <SubscriptionFocus context={context} fieldName="longitude" /> |
| 373 | + | } |
| 374 | + | /> |
| 375 | + | {country?.workflowEnabled && ( |
| 376 | + | <StatusField |
| 377 | + | name="x_opencti_workflow_id" |
| 378 | + | type="Country" |
| 379 | + | onFocus={handleChangeFocus} |
| 380 | + | onChange={handleSubmitField} |
| 381 | + | setFieldValue={setFieldValue} |
| 382 | + | style={{ marginTop: 20 }} |
| 383 | + | helpertext={ |
| 384 | + | <SubscriptionFocus |
| 385 | + | context={context} |
| 386 | + | fieldName="x_opencti_workflow_id" |
| 387 | + | /> |
| 388 | + | } |
| 389 | + | /> |
| 390 | + | )} |
| 391 | + | <CreatedByField |
| 392 | + | name="createdBy" |
| 393 | + | style={{ marginTop: 20, width: '100%' }} |
| 394 | + | setFieldValue={setFieldValue} |
| 395 | + | helpertext={ |
| 396 | + | <SubscriptionFocus context={context} fieldName="createdBy" /> |
| 397 | + | } |
| 398 | + | onChange={handleChangeCreatedBy} |
| 399 | + | /> |
| 400 | + | <ObjectMarkingField |
| 401 | + | name="objectMarking" |
| 402 | + | style={{ marginTop: 20, width: '100%' }} |
| 403 | + | helpertext={ |
| 404 | + | <SubscriptionFocus |
| 405 | + | context={context} |
| 406 | + | fieldname="objectMarking" |
| 407 | + | /> |
| 408 | + | } |
| 409 | + | onChange={handleChangeObjectMarking} |
| 410 | + | /> |
| 411 | + | {enableReferences && ( |
| 412 | + | <CommitMessage |
| 413 | + | submitForm={submitForm} |
| 414 | + | disabled={isSubmitting} |
| 415 | + | validateForm={validateForm} |
| 416 | + | setFieldValue={setFieldValue} |
| 417 | + | values={values} |
| 418 | + | id={country.id} |
| 419 | + | /> |
| 420 | + | )} |
| 421 | + | </Form> |
| 422 | + | )} |
| 423 | + | </Formik> |
| 424 | + | ); |
| 425 | + | }; |
| 426 | + | |
| 427 | + | export default CountryEditionOverviewComponent; |
| 428 | + | |