Skip to content

Commit

Permalink
cases status useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsongl committed Oct 30, 2020
1 parent aef2787 commit c2af254
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useCallback } from 'react';
import styled, { css } from 'styled-components';
import {
EuiBadge,
Expand Down Expand Up @@ -62,57 +62,62 @@ const CaseStatusComp: React.FC<CaseStatusProps> = ({
title,
toggleStatusCase,
value,
}) => (
<EuiFlexGroup gutterSize="l" justifyContent="flexEnd">
<EuiFlexItem grow={false}>
<MyDescriptionList compressed>
<EuiFlexGroup>
<EuiFlexItem grow={false}>
<EuiDescriptionListTitle>{i18n.STATUS}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
<EuiBadge color={badgeColor} data-test-subj="case-view-status">
{status}
</EuiBadge>
</EuiDescriptionListDescription>
}) => {
const handleToggleStatusCase = useCallback(() => {
toggleStatusCase(!isSelected);
}, [toggleStatusCase, isSelected]);
return (
<EuiFlexGroup gutterSize="l" justifyContent="flexEnd">
<EuiFlexItem grow={false}>
<MyDescriptionList compressed>
<EuiFlexGroup>
<EuiFlexItem grow={false}>
<EuiDescriptionListTitle>{i18n.STATUS}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
<EuiBadge color={badgeColor} data-test-subj="case-view-status">
{status}
</EuiBadge>
</EuiDescriptionListDescription>
</EuiFlexItem>
<EuiFlexItem>
<EuiDescriptionListTitle>{title}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
<FormattedRelativePreferenceDate data-test-subj={dataTestSubj} value={value} />
</EuiDescriptionListDescription>
</EuiFlexItem>
</EuiFlexGroup>
</MyDescriptionList>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="l" alignItems="center">
<EuiFlexItem>
<EuiButtonEmpty data-test-subj="case-refresh" iconType="refresh" onClick={onRefresh}>
{i18n.CASE_REFRESH}
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem>
<EuiDescriptionListTitle>{title}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
<FormattedRelativePreferenceDate data-test-subj={dataTestSubj} value={value} />
</EuiDescriptionListDescription>
<EuiButton
data-test-subj="toggle-case-status"
isDisabled={disabled}
iconType={icon}
isLoading={isLoading}
fill={isSelected}
onClick={handleToggleStatusCase}
>
{buttonLabel}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<CaseViewActions
caseData={caseData}
currentExternalIncident={currentExternalIncident}
disabled={disabled}
/>
</EuiFlexItem>
</EuiFlexGroup>
</MyDescriptionList>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="l" alignItems="center">
<EuiFlexItem>
<EuiButtonEmpty data-test-subj="case-refresh" iconType="refresh" onClick={onRefresh}>
{i18n.CASE_REFRESH}
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem>
<EuiButton
data-test-subj="toggle-case-status"
isDisabled={disabled}
iconType={icon}
isLoading={isLoading}
fill={isSelected}
onClick={() => toggleStatusCase(!isSelected)}
>
{buttonLabel}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<CaseViewActions
caseData={caseData}
currentExternalIncident={currentExternalIncident}
disabled={disabled}
/>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
</EuiFlexGroup>
);
</EuiFlexItem>
</EuiFlexGroup>
);
};

export const CaseStatus = React.memo(CaseStatusComp);
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ export const CaseComponent = React.memo<CaseProps>(
[allCasesLink]
);

const isSelected = useMemo(() => caseStatusData.isSelected, [caseStatusData]);
const handleToggleStatusCase = useCallback(() => {
toggleStatusCase(!isSelected);
}, [toggleStatusCase, isSelected]);

return (
<>
<HeaderWrapper>
Expand All @@ -330,7 +335,7 @@ export const CaseComponent = React.memo<CaseProps>(
disabled={!userCanCrud}
isLoading={isLoading && updateKey === 'status'}
onRefresh={handleRefresh}
toggleStatusCase={() => toggleStatusCase(!caseStatusData.isSelected)}
toggleStatusCase={handleToggleStatusCase}
{...caseStatusData}
/>
</HeaderPage>
Expand Down Expand Up @@ -364,7 +369,7 @@ export const CaseComponent = React.memo<CaseProps>(
isDisabled={!userCanCrud}
isLoading={isLoading && updateKey === 'status'}
fill={caseStatusData.isSelected}
onClick={() => toggleStatusCase(!caseStatusData.isSelected)}
onClick={handleToggleStatusCase}
>
{caseStatusData.buttonLabel}
</EuiButton>
Expand Down

0 comments on commit c2af254

Please sign in to comment.