Skip to content

Commit

Permalink
adding multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Jan 13, 2025
1 parent bfd2963 commit f46e0df
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a
- `phone` field now supports initial selected value both for country picker and number.
- `field` now support additional `suffix` content that will be displayed after the field input.
- `radio and checkbox` fields now support one `input` field that can be used as `other` option and this value will be sent with the main values.
- new `enrichment` prefill option to save all fields defined as `smart` and prefilled all forms that have that field.

### Removed

Expand Down
86 changes: 71 additions & 15 deletions src/Blocks/components/form/assets/enrichment.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Enrichment {
*/
setLocalStorageFormPrefill(formId) {
// Check if enrichment is used.
if (!this.state.getStateEnrichmentIsUsed() || !this.state.getStateEnrichmentIsPrefillUsed()) {
if (!this.state.getStateEnrichmentIsUsed() || !this.state.getStateEnrichmentIsLocalStorageUsed()) {
return;
}

Expand Down Expand Up @@ -100,13 +100,27 @@ export class Enrichment {
*
* @returns {void}
*/
setLocalStorageFormPrefillItem(formId, name) {
setLocalStorageFormPrefillField(formId, name) {
this.setLocalStorageFormPrefillFieldItem(formId, name);
this.setLocalStorageFormPrefillFieldSmart(formId, name);
}

/**
* Set localStorage value for every field - general.
*
* @returns {void}
*/
setLocalStorageFormPrefillFieldItem(formId, name) {
// Check if enrichment is used.
if (!this.state.getStateEnrichmentIsUsed() || !this.state.getStateEnrichmentIsPrefillUsed() || !this.state.getStateEnrichmentIsLocalStorageUsed()) {
if (!this.state.getStateEnrichmentIsUsed() || !this.state.getStateEnrichmentIsLocalStorageUsed()) {
return;
}

if (!this.state.getStateEnrichmentIsPrefillUsed()) {
return;
}

let valueData = this.state.getStateElementValue(name, formId);
const valueData = this.state.getStateElementValue(name, formId);

const newStorage = {
[name]: typeof valueData === 'undefined' ? '' : valueData,
Expand All @@ -119,6 +133,36 @@ export class Enrichment {
);
}

/**
* Set localStorage value for every field - smart.
*
* @returns {void}
*/
setLocalStorageFormPrefillFieldSmart(formId, name) {
// Check if enrichment is used.
if (!this.state.getStateEnrichmentIsUsed() || !this.state.getStateEnrichmentIsLocalStorageUsed()) {
return;
}

const allowedSmartTags = this.state.getStateEnrichmentAllowedSmart();

if (!allowedSmartTags) {
return;
}

if (!allowedSmartTags.includes(name)) {
return;
}

const valueData = this.state.getStateElementValue(name, formId);

const newStorage = {
[name]: typeof valueData === 'undefined' ? '' : valueData,
};

this.setLocalStorage(newStorage, this.state.getStateEnrichmentSmartStorageName());
}

/**
* Set localStorage value.
*
Expand Down Expand Up @@ -536,19 +580,25 @@ export class Enrichment {
onLocalstoragePrefillEvent = (event) => {
const { formId } = event.detail;

let data = {};

try {
data = JSON.parse(this.getLocalStorage(this.state.getStateEnrichmentFormPrefillStorageName(formId)));
} catch {
return;
}
if (this.state.getStateEnrichmentAllowedSmart().length) {
const smartData = JSON.parse(this.getLocalStorage(this.state.getStateEnrichmentSmartStorageName()));

if (smartData) {
this.prefillByLocalstorageData(formId, smartData);
}
}

if (this.state.getStateEnrichmentIsPrefillUsed()) {
const formData = JSON.parse(this.getLocalStorage(this.state.getStateEnrichmentFormPrefillStorageName(formId)));

if (!data) {
if (formData) {
this.prefillByLocalstorageData(formId, formData);
}
}
} catch {
return;
}

this.prefillByLocalstorageData(formId, data);
};

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -580,8 +630,14 @@ export class Enrichment {
setUrlParamsFormPrefill: (formId) => {
this.setUrlParamsFormPrefill(formId);
},
setLocalStorageFormPrefillItem: (formId, name) => {
this.setLocalStorageFormPrefillItem(formId, name);
setLocalStorageFormPrefillField: (formId, name) => {
this.setLocalStorageFormPrefillField(formId, name);
},
setLocalStorageFormPrefillFieldItem: (formId, name) => {
this.setLocalStorageFormPrefillFieldItem(formId, name);
},
setLocalStorageFormPrefillFieldSmart: (formId, name) => {
this.setLocalStorageFormPrefillFieldSmart(formId, name);
},
setLocalStorage: (newStorage, storageName, expiration) => {
this.setLocalStorage(newStorage, storageName, expiration);
Expand Down
2 changes: 2 additions & 0 deletions src/Blocks/components/form/assets/state-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const StateEnum = {
ENRICHMENT_EXPIRATION: 'expiration',
ENRICHMENT_EXPIRATION_PREFILL: 'expirationPrefill',
ENRICHMENT_ALLOWED: 'allowed',
ENRICHMENT_ALLOWED_SMART: 'allowedSmart',

GEOLOCATION: 'geolocation',

Expand Down Expand Up @@ -275,6 +276,7 @@ export function setStateInitial() {
setState([StateEnum.ENRICHMENT_EXPIRATION], enrichment.expiration, StateEnum.ENRICHMENT);
setState([StateEnum.ENRICHMENT_EXPIRATION_PREFILL], enrichment.expirationPrefill, StateEnum.ENRICHMENT);
setState([StateEnum.ENRICHMENT_ALLOWED], Object.values(enrichment.allowed), StateEnum.ENRICHMENT);
setState([StateEnum.ENRICHMENT_ALLOWED_SMART], Object.values(enrichment.allowedSmart), StateEnum.ENRICHMENT);
setState([StateEnum.NAME], 'es-storage', StateEnum.ENRICHMENT);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Blocks/components/form/assets/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,15 @@ export class State {
getStateEnrichmentAllowed = () => {
return getState([StateEnum.ENRICHMENT_ALLOWED], StateEnum.ENRICHMENT);
};
getStateEnrichmentAllowedSmart = () => {
return getState([StateEnum.ENRICHMENT_ALLOWED_SMART], StateEnum.ENRICHMENT);
};
getStateEnrichmentStorageName = () => {
return getState([StateEnum.NAME], StateEnum.ENRICHMENT);
};
getStateEnrichmentSmartStorageName = () => {
return `${getState([StateEnum.NAME], StateEnum.ENRICHMENT)}-smart`;
};
getStateEnrichmentFormPrefillStorageName = (formId) => {
return `${getState([StateEnum.NAME], StateEnum.ENRICHMENT)}-${this.getStateFormFid(formId)}`;
};
Expand Down
12 changes: 6 additions & 6 deletions src/Blocks/components/form/assets/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ export class Utils {
setStateValues(name, value, formId);
this.setFieldFilledState(formId, name);

this.enrichment.setLocalStorageFormPrefillItem(formId, name);
this.enrichment.setLocalStorageFormPrefillField(formId, name);

this.conditionalTags.setField(formId, name);
}
Expand Down Expand Up @@ -950,7 +950,7 @@ export class Utils {

setStateValues(name, value, formId);

this.enrichment.setLocalStorageFormPrefillItem(formId, name);
this.enrichment.setLocalStorageFormPrefillField(formId, name);

this.conditionalTags.setField(formId, name);

Expand Down Expand Up @@ -999,7 +999,7 @@ export class Utils {

this.setFieldFilledState(formId, name);

this.enrichment.setLocalStorageFormPrefillItem(formId, name);
this.enrichment.setLocalStorageFormPrefillField(formId, name);

this.conditionalTags.setField(formId, name);
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ export class Utils {
setStateValues(name, { ...newValue, ...value }, formId);
this.setFieldFilledState(formId, name);

this.enrichment.setLocalStorageFormPrefillItem(formId, name);
this.enrichment.setLocalStorageFormPrefillField(formId, name);

this.conditionalTags.setField(formId, name);
}
Expand Down Expand Up @@ -1126,7 +1126,7 @@ export class Utils {
}
}

this.enrichment.setLocalStorageFormPrefillItem(formId, name);
this.enrichment.setLocalStorageFormPrefillField(formId, name);

this.conditionalTags.setField(formId, name);
}
Expand Down Expand Up @@ -1175,7 +1175,7 @@ export class Utils {
}
}

this.enrichment.setLocalStorageFormPrefillItem(formId, name);
this.enrichment.setLocalStorageFormPrefillField(formId, name);

this.conditionalTags.setField(formId, name);

Expand Down
1 change: 1 addition & 0 deletions src/Enqueue/Blocks/EnqueueBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ public function enqueueBlockFrontendScript(string $hook): void
'isUsed' => true,
'isUsedPrefill' => UtilsSettingsHelper::isOptionCheckboxChecked(SettingsEnrichment::SETTINGS_ENRICHMENT_PREFILL_USE_KEY, SettingsEnrichment::SETTINGS_ENRICHMENT_PREFILL_USE_KEY),
'isUsedPrefillUrl' => UtilsSettingsHelper::isOptionCheckboxChecked(SettingsEnrichment::SETTINGS_ENRICHMENT_PREFILL_URL_USE_KEY, SettingsEnrichment::SETTINGS_ENRICHMENT_PREFILL_URL_USE_KEY),
'allowedSmart' => \array_values(\array_filter(\explode(\PHP_EOL, UtilsSettingsHelper::getOptionValueAsJson(SettingsEnrichment::SETTINGS_ENRICHMENT_ALLOWED_SMART_TAGS_KEY, 1)))),
],
FiltersOuputMock::getEnrichmentManualMapFilterValue($this->enrichment->getEnrichmentConfig())['config'] ?? [],
);
Expand Down
29 changes: 27 additions & 2 deletions src/Enrichment/SettingsEnrichment.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class SettingsEnrichment implements UtilsSettingGlobalInterface, ServiceInterfac
*/
public const SETTINGS_ENRICHMENT_ALLOWED_TAGS_KEY = 'enrichment-allowed-tags';

/**
* Allowed smart tags key.
*/
public const SETTINGS_ENRICHMENT_ALLOWED_SMART_TAGS_KEY = 'enrichment-allowed-smart-tags';

/**
* Allowed tags map key.
*/
Expand Down Expand Up @@ -166,7 +171,7 @@ public function getSettingsGlobalData(): array
'textareaSaveAsJson' => true,
// translators: %s will be replaced with local validation patterns.
'textareaFieldHelp' => \sprintf(\__('
Enter one URL parameter per line.
Enter one parameter per line.
<br/><br />
Parameters are stored in browser storage for optional additional processing later.<br />
Some commonly used parameters are included by default.%s', 'eightshift-forms'), $enrichment['settings']),
Expand Down Expand Up @@ -271,7 +276,27 @@ function ($item) {
],
],
],
]
[
'component' => 'tab',
'tabLabel' => \__('Prefill smart', 'eightshift-forms'),
'tabContent' => [
[
'component' => 'textarea',
'textareaName' => UtilsSettingsHelper::getOptionName(self::SETTINGS_ENRICHMENT_ALLOWED_SMART_TAGS_KEY),
'textareaFieldLabel' => \__('Add custom enrichment smart parameters', 'eightshift-forms'),
'textareaIsMonospace' => true,
'textareaSaveAsJson' => true,
// translators: %s will be replaced with local validation patterns.
'textareaFieldHelp' => \sprintf(\__('
Enter one parameter per line.
<br/><br />
Parameters are stored in browser storage for optional additional processing later.<br />
These parameters will be automatically set on every form you have.', 'eightshift-forms'), $enrichment['settings']),
'textareaValue' => UtilsSettingsHelper::getOptionValueAsJson(self::SETTINGS_ENRICHMENT_ALLOWED_SMART_TAGS_KEY, 1),
],
],
],
],
],
];
}
Expand Down
4 changes: 3 additions & 1 deletion src/Hooks/FiltersOuputMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function getEnrichmentManualMapFilterValue(array $config): array
{
$settings = '';
$filterUsed = false;
$settingsFields = [];
$settingsFields = $config['allowed'] ?? [];

$filterName = UtilsHooksHelper::getFilterName(['enrichment', 'manualMap']);

Expand All @@ -53,6 +53,8 @@ public static function getEnrichmentManualMapFilterValue(array $config): array
}
}

$settingsFields = \array_unique($settingsFields);

$settings .= \__('Additional parameters were provided through code', 'eightshift-forms');
$settings .= '<ul>';
foreach ($filterData as $key => $value) {
Expand Down

0 comments on commit f46e0df

Please sign in to comment.