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

fix(metadata-sidebar): fix loading states #3684

Merged
merged 4 commits into from
Oct 4, 2024

Conversation

wpiesiak
Copy link
Contributor

Fix loading states for create, update and delete metadata instance

Screenshot 2024-09-26 at 6 47 15 PM (2)

@wpiesiak wpiesiak requested a review from a team as a code owner September 26, 2024 16:49
Copy link
Contributor

@mhagmajer mhagmajer left a comment

Choose a reason for hiding this comment

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

👌

@@ -93,7 +93,7 @@ function MetadataSidebarRedesign({

React.useEffect(() => {
setSelectedTemplates(templateInstances);
}, [templateInstances]);
}, [templateInstances, templateInstances.length]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is templateInstances.length needed? Shouldn't having only templateInstances as a dependency observe changes in the size of the array as well?

Copy link
Contributor Author

@wpiesiak wpiesiak Oct 1, 2024

Choose a reason for hiding this comment

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

templateInstance is an array from api cache, therefore changes in the array would not trigger this useEffect

setStatus(STATUS.LOADING);
await api
.getMetadataAPI(false)
.deleteMetadata(file, metadataInstance, () => setStatus(STATUS.SUCCESS), onApiError, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ seems like this is now just setting that status to success instead of updating the template instances. We no longer need to update template instances?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

templateInstances are linked to an array in api cache and are updated in the Metadata.js

successCallback,
(error: ElementsXhrError, code: string) =>
onApiError(error, code, messages.sidebarMetadataEditingErrorContent),
);
},
[api, file, onApiError],
);

const handleUpdateMetadataInstance = React.useCallback(
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ I imagine updates and creates should also have a loading states but don't see any setStatus usage (like delete). Should this be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For creating and updating we pass these async functions to MetadataInstanceForm in metadata-editor. From there, Formik will keep loading state until it receives either a resolve or reject status from the API.

)
: handleCreateMetadataInstance(values.metadata as MetadataTemplateInstance, () => setEditingTemplate(null));
if (isExistingMetadataInstance()) {
await handleUpdateMetadataInstance(values.metadata as MetadataTemplateInstance, operations, () =>
Copy link
Contributor

@JakubKida JakubKida Oct 1, 2024

Choose a reason for hiding this comment

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

Can we use convertTemplateToTemplateInstance instead of casting here as it's already merged ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

values.metadata has all required fields for MetadataTemplateInstance, we just need to formally convert the type. There is no reason here to use convertTemplateToTemplateInstance 🤔

setEditingTemplate(null),
);
} else {
await handleCreateMetadataInstance(values.metadata as MetadataTemplateInstance, () =>
Copy link
Contributor

@JakubKida JakubKida Oct 1, 2024

Choose a reason for hiding this comment

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

Same here, let's use convertTemplateToTemplateInstance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as above ⬆️

@mergify mergify bot merged commit 5c54eb5 into box:master Oct 4, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants