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

Local unit changes modal #1584

Merged
merged 4 commits into from
Dec 18, 2024
Merged

Conversation

shreeyash07
Copy link
Collaborator

@shreeyash07 shreeyash07 commented Dec 12, 2024

Addresses:

Depends on:

Changes

  • Add changes modal in the local unit form
  • find changes logic between the value change and local unit response

This PR doesn't introduce:

  • typos
  • conflict markers
  • unwanted comments
  • temporary files, auto-generated files or secret keys
  • console.log meant for debugging
  • codegen errors

@shreeyash07 shreeyash07 requested a review from samshara December 12, 2024 05:15
Copy link

changeset-bot bot commented Dec 12, 2024

⚠️ No Changeset found

Latest commit: 0c4bffc

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@samshara samshara force-pushed the feature/changes-modal branch from 11f0b07 to 05aaf87 Compare December 13, 2024 09:20
@samshara samshara force-pushed the feature/changes-modal branch from 05aaf87 to 3e9dde2 Compare December 13, 2024 09:37
@samshara samshara force-pushed the feature/changes-modal branch from 3e9dde2 to 7a862f1 Compare December 13, 2024 10:01
shreeyash07 and others added 2 commits December 17, 2024 10:49
- Add function to get changes name and value
- Add new file for key and label of the field
app/src/utils/common.ts Outdated Show resolved Hide resolved
app/src/utils/common.ts Show resolved Hide resolved
Comment on lines +127 to +128
const flattenedValues = flattenObject(newValues);
const flattenedOldValues = flattenObject(oldValues);
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if we can use this approach. This fails if/when we have array elements.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we will refactor this later. will add a TODO

Comment on lines +344 to +347
health: {
affiliation: health?.affiliation,
functionality: health?.functionality,
health_facility_type: health?.health_facility_type,
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to write these out individually? Why not just spread this out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

when we compare the value later, the comparison function checks for _display values. We don't need these values for form fields. So we didn't spread them out.

Comment on lines 56 to 79
export function compareArrays(newArray: unknown[], oldArray: unknown[]): boolean {
if (newArray.length !== oldArray.length) {
return false;
}
return newArray.every((id) => oldArray.includes(id));
}

export function flattenObject<T extends Record<string, unknown>>(
inputObject: T,
prefix?: string,
): Record<string, unknown> {
return Object.entries(inputObject).reduce((acc, [key, value]) => {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
return { ...acc, ...flattenObject(value as Record<string, unknown>, newKey) };
}
return { ...acc, [newKey]: value };
}, {} as Record<string, unknown>);
}

export function getLastSegment(str: string, delimiter: string) {
const parts = str.split(delimiter);
return parts[parts.length - 1];
}
Copy link
Member

Choose a reason for hiding this comment

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

write tests

Copy link
Member

Choose a reason for hiding this comment

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

Added a TODO comment.

app/src/utils/common.ts Outdated Show resolved Hide resolved
Comment on lines 122 to 126
function getChangedFormFields(
newValues: PartialLocalUnits,
oldValues: LocalUnitResponse,
formFieldOptions: LocalUnitOptions,
) {
Copy link
Member

Choose a reason for hiding this comment

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

Write return type

Suggested change
function getChangedFormFields(
newValues: PartialLocalUnits,
oldValues: LocalUnitResponse,
formFieldOptions: LocalUnitOptions,
) {
function getChangedFormFields(
newValues: PartialLocalUnits,
oldValues: LocalUnitResponse,
formFieldOptions: LocalUnitOptions,
): { key: string; value: string }[] {

Comment on lines 142 to 163
const actualKey = getLastSegment(key, '.');
if (Array.isArray(newValue) && Array.isArray(oldValue)) {
if (!compareArrays(newValue, oldValue)) {
const options: Option[] = formFieldOptions?.[actualKey as keyof LocalUnitOptions];
const valuesLabels = newValue.map(
(v: number) => options.find((option: Option) => option.id === v),
).filter(isDefined).map((option) => option.name).join(', ');
changedValues.push({ key, value: valuesLabels });
}
} else if (newValue !== oldValue) {
const options: Option[] = formFieldOptions?.[actualKey as keyof LocalUnitOptions];
if (isDefined(options)) {
const valueLabel = options.find(
(option: Option) => option.id === newValue,
)?.name;
changedValues.push({ key, value: valueLabel });
} else if (typeof newValue === 'boolean') {
changedValues.push({ key, value: newValue ? 'Yes' : 'No' }); // TODO: use translations
} else {
changedValues.push({ key, value: newValue as string });
}
}
Copy link
Member

Choose a reason for hiding this comment

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

add few comments on what is being done to make it more readable

@samshara samshara merged commit 3a0cd23 into project/local-unit-v2 Dec 18, 2024
9 of 10 checks passed
@samshara samshara deleted the feature/changes-modal branch December 18, 2024 05: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.

4 participants