Skip to content

Commit e2862ab

Browse files
authored
Merge pull request #1452 from AppQuality/UN-1929
rework: Use composed statuses
2 parents 9178f8a + 389ecc4 commit e2862ab

File tree

9 files changed

+55
-40
lines changed

9 files changed

+55
-40
lines changed

src/hooks/usePlan.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ const usePlan = (planId?: string) => {
9898
isFetching: isFetching || isCiFetching || isTemplateFetching,
9999
activeWorkspace,
100100
plan: undefined,
101-
...(hasCI ? { checkoutItem: ci } : {}),
102101
planComposedStatus,
103102
};
104103
}
@@ -107,10 +106,33 @@ const usePlan = (planId?: string) => {
107106
isLoading: isLoading || isCiLoading || isTemplateLoading,
108107
isFetching: isFetching || isCiFetching || isTemplateFetching,
109108
activeWorkspace,
110-
plan: { ...plan, isPurchasable: hasCI },
111-
...(hasCI ? { checkoutItem: ci } : {}),
109+
plan,
112110
planComposedStatus,
113111
};
114112
};
115113

116-
export { usePlan };
114+
const usePlanIsDraft = (planId?: string) => {
115+
const { planComposedStatus } = usePlan(planId);
116+
117+
const isDraft =
118+
!!planComposedStatus &&
119+
['PurchasableDraft', 'UnquotedDraft', 'PrequotedDraft'].includes(
120+
planComposedStatus
121+
);
122+
123+
return isDraft;
124+
};
125+
126+
const usePlanIsPurchasable = (planId?: string) => {
127+
const { planComposedStatus } = usePlan(planId);
128+
129+
const isPurchasable =
130+
!!planComposedStatus &&
131+
['PurchasableDraft', 'AwaitingPayment', 'PurchasedPlan', 'Paying'].includes(
132+
planComposedStatus
133+
);
134+
135+
return isPurchasable;
136+
};
137+
138+
export { usePlan, usePlanIsDraft, usePlanIsPurchasable };

src/pages/Plan/Controls/ConfirmPlanButton.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useState } from 'react';
33
import { useTranslation } from 'react-i18next';
44
import { useParams } from 'react-router-dom';
55
import { usePatchPlansByPidStatusMutation } from 'src/features/api';
6-
import { usePlan } from '../../../hooks/usePlan';
6+
import { usePlan, usePlanIsPurchasable } from '../../../hooks/usePlan';
77
import { BuyButton } from '../summary/components/BuyButton';
88

99
const ConfirmPlanButton = () => {
@@ -13,10 +13,11 @@ const ConfirmPlanButton = () => {
1313
const { planId } = useParams();
1414
const { plan, planComposedStatus } = usePlan(planId);
1515
const { t } = useTranslation();
16+
const isPurchasable = usePlanIsPurchasable(planId);
1617

1718
if (!plan) return null;
1819

19-
if (plan.isPurchasable) {
20+
if (isPurchasable) {
2021
return <BuyButton />;
2122
}
2223

src/pages/Plan/Controls/IconButtonMenu.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { useTranslation } from 'react-i18next';
99
import { useParams } from 'react-router-dom';
1010
import { ReactComponent as SaveTemplateIcon } from 'src/assets/icons/template.svg';
1111
import { Divider } from 'src/common/components/divider';
12+
import { usePlan, usePlanIsDraft } from '../../../hooks/usePlan';
1213
import { usePlanContext } from '../context/planContext';
13-
import { usePlan } from '../../../hooks/usePlan';
1414

1515
const OptionalTooltip = ({
1616
children,
@@ -36,7 +36,8 @@ const IconButtonMenu = () => {
3636

3737
const { setIsSaveTemplateModalOpen, setIsDeleteModalOpen } = usePlanContext();
3838
const { planId } = useParams();
39-
const { plan, planComposedStatus } = usePlan(planId);
39+
const { planComposedStatus } = usePlan(planId);
40+
const isDraft = usePlanIsDraft(planId);
4041

4142
const handleMenuClick = (value?: string) => {
4243
if (value === 'delete') {
@@ -74,14 +75,14 @@ const IconButtonMenu = () => {
7475
</>
7576
)}
7677
<OptionalTooltip
77-
show={plan?.status !== 'draft'}
78+
show={!isDraft}
7879
content={t('__PLAN_DELETE_PLAN_TOOLTIP')}
7980
>
8081
<ButtonMenu.Item
8182
data-qa="delete-action-item"
8283
type="danger"
8384
value="delete"
84-
isDisabled={plan?.status !== 'draft'}
85+
isDisabled={!isDraft}
8586
icon={<TrashIcon />}
8687
>
8788
{t('__PLAN_DELETE_PLAN_CTA')}

src/pages/Plan/Controls/SaveConfigurationButton.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { useTranslation } from 'react-i18next';
77
import { useParams } from 'react-router-dom';
88
import { useSubmit } from 'src/features/modules/useModuleConfiguration';
99
import { useValidateForm } from 'src/features/planModules';
10-
import { getPlanStatus } from 'src/pages/Dashboard/hooks/getPlanStatus';
11-
import { usePlan } from '../../../hooks/usePlan';
10+
import { usePlan, usePlanIsDraft } from '../../../hooks/usePlan';
1211

1312
const SaveConfigurationButton = () => {
1413
const { t } = useTranslation();
@@ -20,15 +19,10 @@ const SaveConfigurationButton = () => {
2019
useSubmit(planId || '');
2120

2221
const { plan } = usePlan(planId);
22+
const isDraft = usePlanIsDraft(planId);
2323

2424
if (!plan) return null;
2525

26-
const { status } = getPlanStatus({
27-
planStatus: plan.status,
28-
quote: plan.quote,
29-
t,
30-
});
31-
3226
const handleSaveConfiguration = async () => {
3327
validateForm();
3428
try {
@@ -65,7 +59,7 @@ const SaveConfigurationButton = () => {
6559
<Button
6660
type="button"
6761
size="small"
68-
disabled={isSubmitting || status !== 'draft'}
62+
disabled={isSubmitting || !isDraft}
6963
onClick={handleSaveConfiguration}
7064
>
7165
{t('__PLAN_SAVE_CONFIGURATION_CTA')}

src/pages/Plan/Controls/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Pipe } from 'src/common/components/Pipe';
77
import { useModule } from 'src/features/modules/useModule';
88
import styled from 'styled-components';
99
import { useSubmit } from '../../../features/modules/useModuleConfiguration';
10-
import { usePlan } from '../../../hooks/usePlan';
10+
import { usePlan, usePlanIsPurchasable } from '../../../hooks/usePlan';
1111
import { usePlanContext } from '../context/planContext';
1212
import { DateInThePastAlertModal } from '../modals/DateInThePastAlertModal';
1313
import { DeletePlanModal } from '../modals/DeletePlanModal';
@@ -37,6 +37,7 @@ export const Controls = () => {
3737
} = usePlanContext();
3838
const { planId } = useParams();
3939
const { plan, planComposedStatus } = usePlan(planId);
40+
const isPurchasable = usePlanIsPurchasable(planId);
4041
const { value: titleValue } = useModule('title'); // to use the current changed title value (also if plan is not saved) in delete modal
4142
const { addToast } = useToast();
4243
const { handleSubmit } = useSubmit(planId || '');
@@ -108,7 +109,7 @@ export const Controls = () => {
108109

109110
{isRequestQuotationModalOpen && (
110111
<SendRequestModal
111-
isPurchasable={plan.isPurchasable}
112+
isPurchasable={isPurchasable}
112113
onQuit={() => setRequestQuotationModalOpen(false)}
113114
/>
114115
)}

src/pages/Plan/modals/SaveAsTemplateModal/useApprovedQuote.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { usePlan } from '../../../../hooks/usePlan';
33

44
const useApprovedQuote = () => {
55
const { planId } = useParams();
6-
const { plan } = usePlan(planId);
6+
const { plan, planComposedStatus } = usePlan(planId);
77

88
const hasApprovedQuote =
9-
plan?.status === 'approved' && plan?.quote?.status === 'approved';
9+
planComposedStatus &&
10+
['Accepted', 'PurchasedPlan'].includes(planComposedStatus);
1011

11-
return { hasApprovedQuote, quote: hasApprovedQuote ? plan.quote : undefined };
12+
return { hasApprovedQuote, quote: plan?.quote };
1213
};
1314

1415
export { useApprovedQuote };

src/pages/Plan/summary/components/BuyButton.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useTranslation } from 'react-i18next';
33
import { useParams } from 'react-router-dom';
44

55
import { useModule } from 'src/features/modules/useModule';
6-
import { getPlanStatus } from 'src/pages/Dashboard/hooks/getPlanStatus';
76
import { usePlan } from '../../../../hooks/usePlan';
87
import { usePlanContext } from '../../context/planContext';
98

@@ -21,18 +20,12 @@ const BuyButton = ({
2120
const { setDateInThePastAlertModalOpen, buyPlanAction } = usePlanContext();
2221
const { value } = useModule('dates');
2322
const { planId } = useParams();
24-
const { plan } = usePlan(planId);
23+
const { plan, planComposedStatus } = usePlan(planId);
2524

2625
const { t } = useTranslation();
2726

2827
if (!plan) return null;
2928

30-
const { status } = getPlanStatus({
31-
planStatus: plan.status,
32-
quote: plan.quote,
33-
t,
34-
});
35-
3629
const checkIfDateIsValid = (dateString?: string) => {
3730
if (!dateString) {
3831
setDateInThePastAlertModalOpen(true);
@@ -65,7 +58,10 @@ const BuyButton = ({
6558
isAccent={isAccent}
6659
isPrimary={isPrimary}
6760
isStretched={isStretched}
68-
disabled={status === 'approved'}
61+
disabled={
62+
planComposedStatus &&
63+
['AwaitingPayment', 'PurchasedPlan'].includes(planComposedStatus)
64+
}
6965
onClick={handleBuyButtonClick}
7066
>
7167
{t('__PLAN_PAGE_BUY_BUTTON_LABEL')}

src/pages/Plan/summary/components/DetailsCard.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { useLocalizeRoute } from 'src/hooks/useLocalizedRoute';
1919
import { usePlanStatusLabel } from 'src/hooks/usePlanStatusLabel';
2020
import { WidgetSpecialCard } from 'src/pages/Campaign/widgetCards/common/StyledSpecialCard';
2121
import styled from 'styled-components';
22-
import { usePlan } from '../../../../hooks/usePlan';
22+
import { usePlan, usePlanIsPurchasable } from '../../../../hooks/usePlan';
2323
import { GoToCampaignButton } from '../../Controls/GoToCampaignButton';
2424
import { BuyButton } from './BuyButton';
2525
import { CancelPlanButton } from './CancelPlanButton';
@@ -173,6 +173,7 @@ export const DetailsCard = () => {
173173
const label = usePlanStatusLabel({ planStatus: planComposedStatus });
174174
const { value } = useModule('dates');
175175
const [isSubmitted, setIsSubmitted] = useState(false);
176+
const isPurchasable = usePlanIsPurchasable(planId);
176177

177178
const [patchStatus] = usePatchPlansByPidStatusMutation();
178179

@@ -187,7 +188,7 @@ export const DetailsCard = () => {
187188
};
188189

189190
const getCta = () => {
190-
if (!plan.isPurchasable) {
191+
if (!isPurchasable) {
191192
return (
192193
<Cta
193194
isSubmitted={isSubmitted}

src/pages/Plan/summary/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
44
import { useParams } from 'react-router-dom';
55
import { appTheme } from 'src/app/theme';
66
import styled from 'styled-components';
7-
import { usePlan } from '../../../hooks/usePlan';
7+
import { usePlan, usePlanIsPurchasable } from '../../../hooks/usePlan';
88
import { StickyCol } from '../common/StickyCol';
99
import { TabTitle } from '../common/TabTitle';
1010
import { usePlanContext } from '../context/planContext';
@@ -26,6 +26,7 @@ const SummaryBody = () => {
2626
const { t } = useTranslation();
2727
const { planId } = useParams();
2828
const { plan, planComposedStatus } = usePlan(planId);
29+
const isPurchasable = usePlanIsPurchasable(planId);
2930
const { setActiveTab, isPaymentInProgress } = usePlanContext();
3031

3132
if (!plan) return null;
@@ -43,10 +44,7 @@ const SummaryBody = () => {
4344
<IntroductionCard />
4445
<ActivityInfo />
4546
{planComposedStatus !== 'Paying' && <ConfirmationCard />}
46-
{planComposedStatus !== 'PurchasableDraft' &&
47-
planComposedStatus !== 'AwaitingPayment' &&
48-
planComposedStatus !== 'Paying' &&
49-
planComposedStatus !== 'PurchasedPlan' && <SaveTemplateCard />}
47+
{!isPurchasable && <SaveTemplateCard />}
5048
<GoToDashboardCard />
5149
</StyledDiv>
5250
<Button

0 commit comments

Comments
 (0)