-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[MM-18953] Migrate 'components/change_url_modal' to TypeScript #5105
Conversation
|
||
wrapper.instance().onSubmit({preventDefault: jest.fn()}); | ||
const instance = wrapper.instance() as ChangeURLModal; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 : ''; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
@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? |
@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 :) |
@devinbinnie Makes sense. 👍 |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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:
serverError: JSX.Element | string | null; | |
serverError?: React.ReactNode; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 : ''; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 👍
/** | ||
* Server error from failed channel creation | ||
*/ | ||
serverError: JSX.Element | string | null; |
There was a problem hiding this comment.
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:
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 : ''; |
There was a problem hiding this comment.
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.
/update-branch |
* 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 || ''; |
There was a problem hiding this comment.
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
There was a problem hiding this 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 👍
/update-branch |
Mattermost test server updated with git commit Access here: https://mattermost-webapp-pr-5105.test.mattermost.cloud |
There was a problem hiding this 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.
Test server destroyed |
Summary
Migrates 'components/change_url_modal' module and associated tests to TypeScript
Ticket Link
Fixes mattermost/mattermost#12473