Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

[MM-18953] Migrate 'components/change_url_modal' to TypeScript #5105

Merged
merged 5 commits into from
Apr 3, 2020

Conversation

haydenhw
Copy link
Contributor

Summary

Migrates 'components/change_url_modal' module and associated tests to TypeScript

Ticket Link

Fixes mattermost/mattermost#12473

@haydenhw haydenhw closed this Mar 23, 2020
@haydenhw haydenhw deleted the MM-18953 branch March 23, 2020 06:21
@haydenhw haydenhw restored the MM-18953 branch March 23, 2020 07:00
@haydenhw haydenhw reopened this Mar 23, 2020
@devinbinnie devinbinnie requested a review from a team March 23, 2020 13:40
@ghost ghost requested review from cpoile and hahmadia and removed request for a team March 23, 2020 13:40
@hahmadia hahmadia added 2: Dev Review Requires review by a core commiter 3: QA Review Requires review by a QA tester labels Mar 23, 2020

wrapper.instance().onSubmit({preventDefault: jest.fn()});
const instance = wrapper.instance() as ChangeURLModal;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The trickiest part of this PR for me was fixing the TS error that appeared when calling methods like onSubmit() on wrapper.instance():
TS2339: Property 'onSubmit' does not exist on type 'Component<{}, {}, any>'.

I found the solution to explicitly declare the type of the instance() method on this SO post.

Curious to see what reviewers think of this.

Copy link
Member

Choose a reason for hiding this comment

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

Fine by me, especially since it's a test. You know what you're casting after all.

e.preventDefault();
const url = this.refs.urlinput.value;
const url = this.urlInput.current ? this.urlInput.current.value : '';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I left this simply as const url = this.urlInput.current.value; Typescript gave me this error:
TS2531: Object is possibly 'null'

Not sure if there's a cleaner fix for this than the ternary expression I used?

Copy link
Member

Choose a reason for hiding this comment

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

That seems fine to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep same for me! I don't see a problem with this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I just wanted to make an update. Currently our ESlinter doesn't allow this (not setup correctly for Typescript) however in such scenarios, Typescript best practices is to use the optional conditioning (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining). So in this case it would look something like
this.urlInput?.current?.value.

Copy link
Contributor

Choose a reason for hiding this comment

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

UPDATE! We have updated ESlint. You can now do
const url = this.urlInput?.current?.value and you should not see the Object is possibly null issue. Let me know how it goes!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Optional conditioning... very cool! It worked out well.

Thanks for the docs link.

@cpoile
Copy link
Member

cpoile commented Mar 25, 2020

@devinbinnie I don't think I'm a good fit for this PR -- I've only done a little TS, so I'm still on the learning curve. Can you switch someone else in please?

@devinbinnie
Copy link
Member

@cpoile It's important that all staff members get a handle on TypeScript, since all projects are being converted over to use it. Please review, and if you have questions/need any advice, please let me know and I'll be happy to assist :)

@cpoile
Copy link
Member

cpoile commented Mar 25, 2020

@devinbinnie Makes sense. 👍

Copy link
Member

@cpoile cpoile left a comment

Choose a reason for hiding this comment

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

Just a minor question. Otherwise nice work @haydenhw, thank you!


wrapper.instance().onSubmit({preventDefault: jest.fn()});
const instance = wrapper.instance() as ChangeURLModal;
Copy link
Member

Choose a reason for hiding this comment

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

Fine by me, especially since it's a test. You know what you're casting after all.

/**
* Server error from failed channel creation
*/
serverError: JSX.Element | string | null;
Copy link
Member

Choose a reason for hiding this comment

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

Will this work if it's a JSX.Node? That should allow null assignment (not sure about string though).

Copy link
Contributor

Choose a reason for hiding this comment

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

The equivalent for PropTypes.node in typescript is actually React.ReactNode. Could we change this to the following:

Suggested change
serverError: JSX.Element | string | null;
serverError?: React.ReactNode;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed !

super(props);
this.urlInput = React.createRef();
Copy link
Member

Choose a reason for hiding this comment

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

👍

e.preventDefault();
const url = this.refs.urlinput.value;
const url = this.urlInput.current ? this.urlInput.current.value : '';
Copy link
Member

Choose a reason for hiding this comment

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

That seems fine to me.

Copy link
Contributor

@hahmadia hahmadia left a comment

Choose a reason for hiding this comment

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

Great work! I really like the new types introduced in the function parameters. Helps to keep things tidy!
Just have a few minor suggestions. Otherwise, great work 👍

components/change_url_modal/change_url_modal.tsx Outdated Show resolved Hide resolved
components/change_url_modal/change_url_modal.tsx Outdated Show resolved Hide resolved
components/change_url_modal/change_url_modal.tsx Outdated Show resolved Hide resolved
components/change_url_modal/change_url_modal.tsx Outdated Show resolved Hide resolved
/**
* Server error from failed channel creation
*/
serverError: JSX.Element | string | null;
Copy link
Contributor

Choose a reason for hiding this comment

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

The equivalent for PropTypes.node in typescript is actually React.ReactNode. Could we change this to the following:

Suggested change
serverError: JSX.Element | string | null;
serverError?: React.ReactNode;

e.preventDefault();
const url = this.refs.urlinput.value;
const url = this.urlInput.current ? this.urlInput.current.value : '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep same for me! I don't see a problem with this.

@hahmadia
Copy link
Contributor

/update-branch

mattermod and others added 3 commits March 25, 2020 11:39
 * Add TS conditionals to optional props

* Change this.refs.urlInput.value --> this.urlInput?.current?.value

* Change JSX.Element -->  React.ReactNode

* Remove Props type export
@@ -146,7 +146,7 @@ export default class ChangeURLModal extends React.PureComponent<Props, State> {

onSubmit = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
const url = this.urlInput.current ? this.urlInput.current.value : '';
const url = this.urlInput?.current?.value || '';
Copy link
Contributor Author

@haydenhw haydenhw Mar 27, 2020

Choose a reason for hiding this comment

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

Still using a conditional here to make sure url is always a string since the cleanUpUrlable function isn't set up to take null values

Copy link
Contributor

@hahmadia hahmadia left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks for the quick changes. LGTM 👍

@cpoile cpoile removed the 2: Dev Review Requires review by a core commiter label Mar 29, 2020
@jgilliam17 jgilliam17 added the Setup Cloud Test Server Setup a test server using Mattermost Cloud label Mar 31, 2020
@hahmadia
Copy link
Contributor

hahmadia commented Apr 1, 2020

/update-branch

@mattermod
Copy link
Contributor

Mattermost test server updated with git commit 597f9945020c656e41decfba509a69bbff9f211f.

Access here: https://mattermost-webapp-pr-5105.test.mattermost.cloud

Copy link
Contributor

@jgilliam17 jgilliam17 left a comment

Choose a reason for hiding this comment

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

Thank you @haydenhw

Tested, looks good to merge.

  • Tested around Change Channel URL modal, verified errors as expected.

@jgilliam17 jgilliam17 added QA Review Done and removed 3: QA Review Requires review by a QA tester Setup Cloud Test Server Setup a test server using Mattermost Cloud labels Apr 3, 2020
@mattermod
Copy link
Contributor

Test server destroyed

@hahmadia hahmadia added the 4: Reviews Complete All reviewers have approved the pull request label Apr 3, 2020
@hahmadia hahmadia merged commit f7fae46 into mattermost:master Apr 3, 2020
@hanzei hanzei added this to the v5.24.0 milestone Apr 3, 2020
@amyblais amyblais added Changelog/Not Needed Does not require a changelog entry Docs/Not Needed Does not require documentation labels Apr 6, 2020
@jgilliam17 jgilliam17 added the Tests/Done Release tests have been written label May 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4: Reviews Complete All reviewers have approved the pull request Changelog/Not Needed Does not require a changelog entry Docs/Not Needed Does not require documentation QA Review Done Tests/Done Release tests have been written
Projects
None yet
8 participants