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

ref(js): Convert App to a functional component #28936

Conversation

evanpurkhiser
Copy link
Member

@evanpurkhiser evanpurkhiser commented Sep 29, 2021

This should simplify the component quite a bit.

Still WIP (this will fail tests rn), requires #28933. But am happy to have some preliminary review.

@evanpurkhiser evanpurkhiser requested a review from a team September 29, 2021 07:02
loading: false,
error: false,
needsUpgrade: ConfigStore.get('user')?.isSuperuser && ConfigStore.get('needsUpgrade'),
newsletterConsentPrompt: ConfigStore.get('user')?.flags?.newsletter_consent_prompt,
Copy link
Member Author

Choose a reason for hiding this comment

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

We no longer store either of these in state, since the component receives updates from using withConfig.

const data = await api.requestPromise('/organizations/', {query: {member: '1'}});
OrganizationsStore.load(data);
} catch {
// TODO: do something?
Copy link
Member Author

Choose a reason for hiding this comment

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

So we used to have a setState({error: true}) here... but we actually just never did anything with it. Ctrl+F state in this PR and you won't find any mention of error.

We probably should do something here though 🤔 maybe just log to Sentry

Copy link
Member

Choose a reason for hiding this comment

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

yes. I think at least log it to Sentry

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't log all the errors though. For example 401 errors are not worth sending back to sentry.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will follow up with this, since right now it isn't doing anything either, this comment just makes it more explicit.

}
data?.problems?.forEach?.(problem => {
const {id, message, url} = problem;
const type = problem.severity === 'critical' ? 'error' : 'warning';
Copy link
Member Author

@evanpurkhiser evanpurkhiser Sep 29, 2021

Choose a reason for hiding this comment

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

This used to be a getAlertTypeForProblem function. I got rid of it since it could be this single line conditional.

if (config.user !== undefined) {
newState.user = config.user;
// The app is running in local SPA mode
if (!DEPLOY_PREVIEW_CONFIG && EXPERIMENTAL_SPA) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I broke up this conditional from using else if to make it marginally more readable.

};
function clearNewsletterConsent() {
const flags = {...config.user.flags, newsletter_consent_prompt: false};
ConfigStore.set('user', {...config.user, flags});
Copy link
Member Author

Choose a reason for hiding this comment

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

Update the ConfigStore and we don't have to maintain that state here in the component :)


return this.props.children;
}
const NewsletterConsent = lazy(() => import('app/views/newsletterConsent'));
Copy link
Member Author

Choose a reason for hiding this comment

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

I converted NewsletterConsent to a lazy-load. I will need to actually verify that this works.

}

export default withApi(withConfig(App));
export default withConfig(App);
Copy link
Member Author

Choose a reason for hiding this comment

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

Would be nice if the useLegacyStore could work for ConfigStore, but unfortunately it does not because it doesn't match the interface.

const data = await api.requestPromise('/organizations/', {query: {member: '1'}});
OrganizationsStore.load(data);
} catch {
// TODO: do something?
Copy link
Member

Choose a reason for hiding this comment

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

yes. I think at least log it to Sentry

}

fetchGuides();
}
data?.problems?.forEach?.(problem => {
Copy link
Member

@priscilawebdev priscilawebdev Sep 29, 2021

Choose a reason for hiding this comment

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

should we move it to inside the try-catch? because if an error happens the data will be null anyway. can we also cast the data with a better type other than any?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I would rather the try/catch only catch API errors

<LoadingIndicator triangle>
{t('Getting a list of all of your organizations.')}
</LoadingIndicator>
<Suspense fallback={null}>
Copy link
Member

Choose a reason for hiding this comment

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

nice! never used suspense before . should we pass a loading indicator in the fallback?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's okay for now, it loads very fast

@evanpurkhiser evanpurkhiser merged commit f5684ec into master Sep 30, 2021
@evanpurkhiser evanpurkhiser deleted the evanpurkhiser/ref-js-convert-app-to-a-functional-component branch September 30, 2021 00:05
@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants