From 5a349873f77d7d5fa74f1e7e7d0f99efa3550669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Mon, 14 Aug 2023 14:20:47 +0200 Subject: [PATCH] Class to functional component example 2 (#24183) * Class to functional component example 2 * Fix tests --- webapp/channels/src/components/copy_text.tsx | 81 +++++++++---------- .../installed_command.test.tsx.snap | 16 +--- .../installed_incoming_webhook.test.tsx.snap | 4 +- .../installed_outgoing_webhook.test.tsx.snap | 4 +- .../confirm_integration.test.tsx.snap | 16 ++-- .../installed_oauth_app.test.tsx.snap | 6 +- 6 files changed, 54 insertions(+), 73 deletions(-) diff --git a/webapp/channels/src/components/copy_text.tsx b/webapp/channels/src/components/copy_text.tsx index e4a975ea90..1db5bb392b 100644 --- a/webapp/channels/src/components/copy_text.tsx +++ b/webapp/channels/src/components/copy_text.tsx @@ -1,7 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React from 'react'; +import React, {useCallback} from 'react'; import {FormattedMessage} from 'react-intl'; import OverlayTrigger from 'components/overlay_trigger'; @@ -12,48 +12,47 @@ import {copyToClipboard} from 'utils/utils'; type Props = { value: string; - defaultMessage: string; - idMessage: string; + defaultMessage?: string; + idMessage?: string; }; -export default class CopyText extends React.PureComponent { - public static defaultProps = { - defaultMessage: 'Copy', - idMessage: 'integrations.copy', - }; - - private copyText = (e: React.MouseEvent): void => { +const CopyText = ({ + value, + defaultMessage = 'Copy', + idMessage = 'integrations.copy', +}: Props) => { + const copyText = useCallback((e: React.MouseEvent) => { e.preventDefault(); - copyToClipboard(this.props.value); - }; + copyToClipboard(value); + }, [value]); - public render() { - if (!document.queryCommandSupported('copy')) { - return null; - } - - const tooltip = ( - - - - ); - - return ( - - - - ); + if (!document.queryCommandSupported('copy')) { + return null; } -} + + const tooltip = ( + + + + ); + + return ( + + + + ); +}; + +export default React.memo(CopyText); diff --git a/webapp/channels/src/components/integrations/__snapshots__/installed_command.test.tsx.snap b/webapp/channels/src/components/integrations/__snapshots__/installed_command.test.tsx.snap index 5aa16329cb..ba4d35cc70 100644 --- a/webapp/channels/src/components/integrations/__snapshots__/installed_command.test.tsx.snap +++ b/webapp/channels/src/components/integrations/__snapshots__/installed_command.test.tsx.snap @@ -79,9 +79,7 @@ exports[`components/integrations/InstalledCommand should call onDelete function } } /> - @@ -187,9 +185,7 @@ exports[`components/integrations/InstalledCommand should call onRegenToken funct } } /> - @@ -265,9 +261,7 @@ exports[`components/integrations/InstalledCommand should match snapshot 1`] = ` } } /> - @@ -341,9 +335,7 @@ exports[`components/integrations/InstalledCommand should match snapshot, not aut } } /> - diff --git a/webapp/channels/src/components/integrations/__snapshots__/installed_incoming_webhook.test.tsx.snap b/webapp/channels/src/components/integrations/__snapshots__/installed_incoming_webhook.test.tsx.snap index bfe686d05c..6084434e7e 100644 --- a/webapp/channels/src/components/integrations/__snapshots__/installed_incoming_webhook.test.tsx.snap +++ b/webapp/channels/src/components/integrations/__snapshots__/installed_incoming_webhook.test.tsx.snap @@ -63,9 +63,7 @@ exports[`components/integrations/InstalledIncomingWebhook should match snapshot } /> - diff --git a/webapp/channels/src/components/integrations/__snapshots__/installed_outgoing_webhook.test.tsx.snap b/webapp/channels/src/components/integrations/__snapshots__/installed_outgoing_webhook.test.tsx.snap index fca0a9e4a6..9b4dc63ea7 100644 --- a/webapp/channels/src/components/integrations/__snapshots__/installed_outgoing_webhook.test.tsx.snap +++ b/webapp/channels/src/components/integrations/__snapshots__/installed_outgoing_webhook.test.tsx.snap @@ -132,9 +132,7 @@ exports[`components/integrations/InstalledOutgoingWebhook should match snapshot } } /> - diff --git a/webapp/channels/src/components/integrations/confirm_integration/__snapshots__/confirm_integration.test.tsx.snap b/webapp/channels/src/components/integrations/confirm_integration/__snapshots__/confirm_integration.test.tsx.snap index 551911427b..7812a3dc62 100644 --- a/webapp/channels/src/components/integrations/confirm_integration/__snapshots__/confirm_integration.test.tsx.snap +++ b/webapp/channels/src/components/integrations/confirm_integration/__snapshots__/confirm_integration.test.tsx.snap @@ -55,9 +55,7 @@ exports[`components/integrations/ConfirmIntegration should match snapshot, comma } } /> -

@@ -133,9 +131,7 @@ exports[`components/integrations/ConfirmIntegration should match snapshot, incom } } /> -

@@ -213,7 +209,7 @@ exports[`components/integrations/ConfirmIntegration should match snapshot, oauth } } /> - - -

diff --git a/webapp/channels/src/components/integrations/installed_oauth_app/__snapshots__/installed_oauth_app.test.tsx.snap b/webapp/channels/src/components/integrations/installed_oauth_app/__snapshots__/installed_oauth_app.test.tsx.snap index bb09396f23..ca84f3e824 100644 --- a/webapp/channels/src/components/integrations/installed_oauth_app/__snapshots__/installed_oauth_app.test.tsx.snap +++ b/webapp/channels/src/components/integrations/installed_oauth_app/__snapshots__/installed_oauth_app.test.tsx.snap @@ -111,7 +111,7 @@ exports[`components/integrations/InstalledOAuthApp should match snapshot 1`] = ` } } /> - - -