Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to allow overwriting state values from query param population #1260

Merged
merged 8 commits into from
Jun 5, 2024

Conversation

ziggy-cyb
Copy link
Collaborator

Description

In some cases it might be necessary to allow query parameters to overwrite present state values. For example, if there is an unreachable field in your form that should be pre-populated from a query parameter, and there is a chance the user may change their selection prior to entering the service that changes the value of that unreachable field, the query parameter should overwrite this value as there is no other way for the user to change their selection.

To solve this, an option allowOverwriteFromQueryParam has been added. This option will only have an effect if the component has already been given the allowPrePopulation flag.

  • Added allowOverwriteFromQueryParam flag

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce
the testing if necessary.

  • Added unit test and modified current tests to check the value is changed where appropriate depending on the value in the state and the presence of the allowOverwiteFromQueryParam flag
  • Manual testing to check values are updating as expected

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and versioning
  • I have updated the architecture diagrams as per Contribute.md OR added an architectural decision record entry

@@ -26,6 +26,7 @@ export function FieldEdit({
required = true,
exposeToContext = false,
allowPrePopulation = false,
allowOverwriteFromQueryParam = false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe allowPrePopulationOverwrite so the flag looks more related to allowPrePopulation?

Comment on lines 76 to 84
const schemaMapping = Object.entries(schema)[0];
return {
[schemaMapping[0]]: {
schema: schemaMapping[1],
allowOverwriteFromQueryParam:
item.options.allowOverwriteFromQueryParam,
},
};
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array indexing makes it hard to follow

Suggested change
const schemaMapping = Object.entries(schema)[0];
return {
[schemaMapping[0]]: {
schema: schemaMapping[1],
allowOverwriteFromQueryParam:
item.options.allowOverwriteFromQueryParam,
},
};
})
return {
[item.name]: {
schema: schema[item.name],
allowOverwriteFromQueryParam:
item.options.allowOverwriteFromQueryParam,
},
};
})

alternatively, you can use destructuring to give schemaMapping[0,1] more meaningful names, but Object.entries(schema)[0]; still looks a bit odd imo

Suggested change
const schemaMapping = Object.entries(schema)[0];
return {
[schemaMapping[0]]: {
schema: schemaMapping[1],
allowOverwriteFromQueryParam:
item.options.allowOverwriteFromQueryParam,
},
};
})
const [key, joiSchema] = Object.entries(schema)[0];
return {
[key]: {
schema: joiSchema,
allowOverwriteFromQueryParam:
item.options.allowOverwriteFromQueryParam,
},
};
})

@ziggy-cyb ziggy-cyb merged commit 10fded0 into main Jun 5, 2024
13 checks passed
@ziggy-cyb ziggy-cyb deleted the feat/overwrite-from-query-param-flag branch June 5, 2024 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants