From 1d421a3694ee950814f077e9e8197b9cbcfa9152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 18 Oct 2023 18:22:23 +0200 Subject: [PATCH] MM-52824 Migrate file_upload_settings.jsx to typescript (#24913) Automatic Merge --- ...ad_setting.jsx => file_upload_setting.tsx} | 57 +++++++++++-------- .../admin_console/schema_admin_settings.jsx | 2 +- 2 files changed, 34 insertions(+), 25 deletions(-) rename webapp/channels/src/components/admin_console/{file_upload_setting.jsx => file_upload_setting.tsx} (75%) diff --git a/webapp/channels/src/components/admin_console/file_upload_setting.jsx b/webapp/channels/src/components/admin_console/file_upload_setting.tsx similarity index 75% rename from webapp/channels/src/components/admin_console/file_upload_setting.jsx rename to webapp/channels/src/components/admin_console/file_upload_setting.tsx index e13399b321..9d5900b350 100644 --- a/webapp/channels/src/components/admin_console/file_upload_setting.jsx +++ b/webapp/channels/src/components/admin_console/file_upload_setting.tsx @@ -1,7 +1,6 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import PropTypes from 'prop-types'; import React from 'react'; import {FormattedMessage} from 'react-intl'; @@ -9,48 +8,58 @@ import * as Utils from 'utils/utils'; import Setting from './setting'; -export default class FileUploadSetting extends Setting { - static get propTypes() { - return { - id: PropTypes.string.isRequired, - label: PropTypes.node.isRequired, - helpText: PropTypes.node, - uploadingText: PropTypes.node, - onSubmit: PropTypes.func.isRequired, - disabled: PropTypes.bool, - fileType: PropTypes.string.isRequired, - error: PropTypes.string, - }; - } +type Props = { + id: string; + label: React.ReactNode; + helpText?: React.ReactNode; + uploadingText?: React.ReactNode; + onSubmit: (id: string, file: File, errorCallback: (error: string) => void) => void; + disabled: boolean; + fileType: string; + error?: string; +} - constructor(props) { +type State = { + fileName: null|string; + fileSelected: boolean; + serverError?: string; + uploading: boolean; +} + +export default class FileUploadSetting extends React.PureComponent { + fileInputRef = React.createRef(); + + constructor(props: Props) { super(props); this.state = { fileName: null, serverError: props.error, uploading: false, + fileSelected: false, }; - this.fileInputRef = React.createRef(); } handleChange = () => { - const files = this.fileInputRef.current.files; + const files = this.fileInputRef.current?.files; if (files && files.length > 0) { this.setState({fileSelected: true, fileName: files[0].name}); } }; - handleSubmit = (e) => { + handleSubmit = (e: React.MouseEvent) => { e.preventDefault(); this.setState({uploading: true}); - this.props.onSubmit(this.props.id, this.fileInputRef.current.files[0], (error) => { - this.setState({uploading: false}); - if (error) { - Utils.clearFileInput(this.fileInputRef.current); - } - }); + const file = this.fileInputRef.current?.files?.[0]; + if (file) { + this.props.onSubmit(this.props.id, file, (error) => { + this.setState({uploading: false}); + if (error && this.fileInputRef.current) { + Utils.clearFileInput(this.fileInputRef.current); + } + }); + } }; render() { diff --git a/webapp/channels/src/components/admin_console/schema_admin_settings.jsx b/webapp/channels/src/components/admin_console/schema_admin_settings.jsx index 56dd2b6cf3..e0b723d93a 100644 --- a/webapp/channels/src/components/admin_console/schema_admin_settings.jsx +++ b/webapp/channels/src/components/admin_console/schema_admin_settings.jsx @@ -10,7 +10,7 @@ import {Link} from 'react-router-dom'; import BooleanSetting from 'components/admin_console/boolean_setting'; import ColorSetting from 'components/admin_console/color_setting'; import DropdownSetting from 'components/admin_console/dropdown_setting'; -import FileUploadSetting from 'components/admin_console/file_upload_setting.jsx'; +import FileUploadSetting from 'components/admin_console/file_upload_setting'; import GeneratedSetting from 'components/admin_console/generated_setting'; import JobsTable from 'components/admin_console/jobs'; import MultiSelectSetting from 'components/admin_console/multiselect_settings';