<commit_before>import { createAction, createThunkAction } from 'redux-tools';
import { setComponentStateToUrl } from 'utils/stateToUrl';

import { setAreasProvider, deleteAreaProvider } from 'services/areas';
import {
  setArea,
  setAreas,
  viewArea,
  clearArea
} from 'providers/areas-provider/actions';

export const setSaveAOISaving = createAction('setSaveAOISaving');
export const resetSaveAOI = createAction('resetSaveAOI');

export const setSaveAOISettings = createThunkAction(
  'setSaveAOISettings',
  change => (dispatch, state) =>
    dispatch(
      setComponentStateToUrl({
        key: 'saveAOI',
        change,
        state
      })
    )
);

export const saveAOI = createThunkAction(
  'saveAOI',
  ({
    name,
    tags,
    fireAlerts,
    deforestationAlerts,
    monthlySummary,
    activeArea,
    viewAfterSave
  }) => (dispatch, getState) => {
    const { modalSaveAOI, location, geostore } = getState();
    if (modalSaveAOI && !modalSaveAOI.saving) {
      dispatch(setSaveAOISaving({ saving: true, error: false }));

      const { data: geostoreData } = geostore || {};
      const { id: geostoreId } = geostoreData || {};
      const { payload: { type, adm0, adm1, adm2 } } = location || {};
      const isCountry = type === 'country';
      const { id: activeAreaId, application } = activeArea || {};
      const method = activeArea && activeArea.userArea ? 'patch' : 'post';

      const postData = {
        name,
        id: activeAreaId,
        application: application || 'gfw',
        geostore: geostoreId,
        iso: {
          region: isCountry ? adm1 : null,
          subRegion: isCountry ? adm2 : null,
          country: isCountry ? adm0 : null
        },
        use: {},
        fireAlerts,
        deforestationAlerts,
        monthlySummary,
        tags: tags || [],
        public: true
      };

      setAreasProvider(postData, method)
        .then(response => {
          if (response.data && response.data.data) {
            const area = response.data.data;
            const { id, attributes } = area || {};
            dispatch(setArea({ id, ...attributes, userArea: true }));
            dispatch(setSaveAOISaving({ saving: false, error: false }));
            if (viewAfterSave) {
              dispatch(viewArea({ areaId: id }));
            }
          }
        })
        .catch(error => {
          dispatch(
            setSaveAOISaving({
              saving: false,
              error: true<commit_msg>send contextual data with aoi save<commit_after>import { createAction, createThunkAction } from 'redux-tools';
import { setComponentStateToUrl } from 'utils/stateToUrl';

import { setAreasProvider, deleteAreaProvider } from 'services/areas';
import {
  setArea,
  setAreas,
  viewArea,
  clearArea
} from 'providers/areas-provider/actions';

export const setSaveAOISaving = createAction('setSaveAOISaving');
export const resetSaveAOI = createAction('resetSaveAOI');

export const setSaveAOISettings = createThunkAction(
  'setSaveAOISettings',
  change => (dispatch, state) =>
    dispatch(
      setComponentStateToUrl({
        key: 'saveAOI',
        change,
        state
      })
    )
);

export const saveAOI = createThunkAction(
  'saveAOI',
  ({
    name,
    tags,
    fireAlerts,
    deforestationAlerts,
    monthlySummary,
    activeArea,
    viewAfterSave
  }) => (dispatch, getState) => {
    const { modalSaveAOI, location, geostore } = getState();
    if (modalSaveAOI && !modalSaveAOI.saving) {
      dispatch(setSaveAOISaving({ saving: true, error: false }));

      const { data: geostoreData } = geostore || {};
      const { id: geostoreId } = geostoreData || {};
      const { payload: { type, adm0, adm1, adm2 } } = location || {};
      const isCountry = type === 'country';
      const { id: activeAreaId, application } = activeArea || {};
      const method = activeArea && activeArea.userArea ? 'patch' : 'post';

      const postData = {
        name,
        id: activeAreaId,
        application: application || 'gfw',
        geostore: geostoreId,
        ...(type === 'country' && {
          iso: {
            region: isCountry ? adm1 : null,
            subRegion: isCountry ? adm2 : null,
            country: isCountry ? adm0 : null
          }
        }),
        ...(type === 'use' && {
          use: {
            id: adm1,
            name: adm0
          }
        }),
        ...(type === 'wdpa' && {
          wdpaid: parseInt(adm0, 10)
        }),
        fireAlerts,
        deforestationAlerts,
        monthlySummary,
        tags: tags || [],
        public: true
      };

      setAreasProvider(postData, method)
        .then(response => {
          if (response.data && response.data.data) {
            const area = response.data.data;
            const { id, attributes } = area || {};
            dispatch(setArea({ id, ...attributes, userArea: true }));
            dispatch(setSaveAOISaving({ saving: false, error: false }));
            if (viewAfterSave) {
              dispatch(viewArea({ areaId: id }));
            }
          }
        })
        .catch(error => {
          dispatch(
            setSaveAOISaving({
              saving: false,
              error: true
{'fraction_non_alphanumeric': 0.125071935545751, 'fraction_numerical': 0.003261078074045655, 'mean_word_length': 2.192896509491733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3828723', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1520, 'n_words': 349}