Skip to content

Commit

Permalink
Clean up FormContext.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
minkyngkm committed Dec 16, 2024
1 parent 627f268 commit 4687861
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 128 deletions.
194 changes: 66 additions & 128 deletions static/js/src/advantage/distributor/utils/FormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React, { createContext, useEffect, useMemo, useState } from "react";
import {
ChannelProduct,
DistributorProductTypes as ProductTypes,
Support,
SLA,
Durations,
SubscriptionItem,
Currencies,
Expand All @@ -15,9 +13,9 @@ import {
TechnicalUserContact,
ProductListings,
DISTRIBUTOR_SELECTOR_KEYS,
getLocalStorageItem,
} from "./utils";
import { Offer, OfferItem } from "advantage/offers/types";
import { UserSubscriptionMarketplace } from "advantage/api/enum";
import { Offer } from "advantage/offers/types";

interface FormContext {
productType: ProductTypes;
Expand Down Expand Up @@ -82,106 +80,72 @@ export const FormProvider = ({
initialChannelProductList = defaultValues.channelProductList,
children,
}: FormProviderProps) => {
const localSubscriptionList = localStorage.getItem(
DISTRIBUTOR_SELECTOR_KEYS.SUBSCRIPTION_LIST,
);
const localProductType = localStorage.getItem(
DISTRIBUTOR_SELECTOR_KEYS.PRODUCT_TYPE,
);
const localDuration = localStorage.getItem(
DISTRIBUTOR_SELECTOR_KEYS.DURATION,
);
const localCurrency = localStorage.getItem(
DISTRIBUTOR_SELECTOR_KEYS.CURRENCY,
);
const localTechnicalUserContact = localStorage.getItem(
DISTRIBUTOR_SELECTOR_KEYS.TECHNICAL_USER_CONTACT,
);
const localOffer = localStorage.getItem(DISTRIBUTOR_SELECTOR_KEYS.OFFER_DATA);

const [subscriptionList, setSubscriptionList] = useState<SubscriptionItem[]>(
localSubscriptionList
? JSON.parse(localSubscriptionList)
: initialSubscriptionList,
getLocalStorageItem(
DISTRIBUTOR_SELECTOR_KEYS.SUBSCRIPTION_LIST,
initialSubscriptionList,
),
);
const [productType, setProductType] = useState<ProductTypes>(
localProductType ? JSON.parse(localProductType) : initialType,
getLocalStorageItem(DISTRIBUTOR_SELECTOR_KEYS.PRODUCT_TYPE, initialType),
);
const [duration, setDuration] = useState<Durations>(
localDuration ? JSON.parse(localDuration) : initialDuration,
getLocalStorageItem(DISTRIBUTOR_SELECTOR_KEYS.DURATION, initialDuration),
);
const [currency, setCurrency] = useState<Currencies>(

Check warning on line 95 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L95

Added line #L95 was not covered by tests
getLocalStorageItem(DISTRIBUTOR_SELECTOR_KEYS.CURRENCY, initialCurrency),
);
const [technicalUserContact, setTechnicalUserContact] =
useState<TechnicalUserContact>(
localTechnicalUserContact
? JSON.parse(localTechnicalUserContact)
: initialTechnicalUserContact,
getLocalStorageItem(
DISTRIBUTOR_SELECTOR_KEYS.TECHNICAL_USER_CONTACT,
initialTechnicalUserContact,
),
);
const [currency, setCurrency] = useState<Currencies>(
localCurrency ? JSON.parse(localCurrency) : initialCurrency,
);
const [products, setProducts] = useState<ChannelProduct[] | null>(null);
const [offer, setOffer] = useState<Offer | null>(
localOffer ? JSON.parse(localOffer) : null,
getLocalStorageItem(DISTRIBUTOR_SELECTOR_KEYS.OFFER_DATA, null),
);
const [channelProductList, setChannelProductList] = useState<ProductListings>(
initialChannelProductList,
);

const updatedChannelProductList = useMemo(() => {
const rawChannelProductListings = window.channelProductList;
const rawChannelProductListings = window.channelProductList || {};
const offerExclusiveGroup = offer?.exclusion_group || "";
const updatedChannelProductList: ProductListings = {};

const getDuration = (effectiveDays: number | undefined): number | null => {
if (effectiveDays === 365) return 1;
if (effectiveDays === 730) return 2;
if (effectiveDays === 1095) return 3;
return null;
};

const updateProductListing = (listing: any) => {
const {
id,
price,
currency,
product,
marketplace,
exclusion_group,
effective_days,
} = listing;

const duration = getDuration(effective_days);
const newName =
`${product?.id}-${duration}y-channel-${currency}`.toLowerCase();

updatedChannelProductList[newName] = {
id: newName,
longId: id,
name: newName,
price: {
value: price,
currency: currency,
},
productID: product?.id as ValidProductID,
productName: product?.name,
marketplace: marketplace as UserSubscriptionMarketplace,
exclusion_group: exclusion_group || "",
effective_days,
};
};

if (rawChannelProductListings) {
Object.values(rawChannelProductListings).forEach((listing: any) => {
const { exclusion_group = "" } = listing;

// Add listings whose exclusion_group matches offerExclusiveGroup
if (offerExclusiveGroup === exclusion_group) {
updateProductListing(listing);
}
});
}
const updatedListings: ProductListings = {};

Check warning on line 116 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L116

Added line #L116 was not covered by tests

const getDuration = (effectiveDays?: number): number | null =>

Check warning on line 118 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L118

Added line #L118 was not covered by tests
effectiveDays === 365
? 1

Check warning on line 120 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L120

Added line #L120 was not covered by tests
: effectiveDays === 730
? 2

Check warning on line 122 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L122

Added line #L122 was not covered by tests
: effectiveDays === 1095
? 3
: null;

Check warning on line 125 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L124-L125

Added lines #L124 - L125 were not covered by tests

Object.values(rawChannelProductListings).forEach((listing: any) => {

Check warning on line 127 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L127

Added line #L127 was not covered by tests
if (listing.exclusion_group === offerExclusiveGroup) {
const { id, price, currency, product, effective_days } = listing;
const duration = getDuration(effective_days);

Check warning on line 130 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L129-L130

Added lines #L129 - L130 were not covered by tests
const newName =
`${product?.id}-${duration}y-channel-${currency}`.toLowerCase();

Check warning on line 132 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L132

Added line #L132 was not covered by tests

updatedListings[newName] = {

Check warning on line 134 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L134

Added line #L134 was not covered by tests
id: newName,
longId: id,
name: newName,
price: { value: price, currency },
productID: product?.id as ValidProductID,
productName: product?.name,
marketplace: listing.marketplace,
exclusion_group: listing.exclusion_group || "",
effective_days,
};
}
});

return updatedChannelProductList;
return updatedListings;

Check warning on line 148 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L148

Added line #L148 was not covered by tests
}, [offer, window.channelProductList]);

useEffect(() => {
Expand All @@ -193,20 +157,17 @@ export const FormProvider = ({
}, [updatedChannelProductList]);

const filteredProducts = useMemo(() => {
const productIds: ValidProductID[] = subscriptionList.map((subscription) =>
getProductId(
subscription.type as ProductTypes,
subscription.support as Support,
subscription.sla as SLA,
),
const productIds = subscriptionList.map((subscription) =>
getProductId(subscription.type, subscription.support, subscription.sla),

Check warning on line 161 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L160-L161

Added lines #L160 - L161 were not covered by tests
);
const validproducts: string[] = productIds.map(
(productId: ValidProductID) =>
`${productId}-${duration}y-channel-${currency}`,
);
return validproducts.map(
(validproduct) => channelProductList[validproduct],

const validProducts = productIds.map(
(productId) => `${productId}-${duration}y-channel-${currency}`,

Check warning on line 165 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L164-L165

Added lines #L164 - L165 were not covered by tests
);

return validProducts
.map((id) => channelProductList[id])
.filter((product) => !!product);

Check warning on line 170 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L168-L170

Added lines #L168 - L170 were not covered by tests
}, [duration, currency, subscriptionList, channelProductList]);

useEffect(() => {
Expand All @@ -216,56 +177,33 @@ export const FormProvider = ({
useEffect(() => {
if (!offer) return;

setSubscriptionItems(
offer.items,
subscriptionList,
setSubscriptionList,
setCurrency,
setDuration,
);
}, [offer]);

const setSubscriptionItems = (
items: OfferItem[],
subscriptionList: SubscriptionItem[] | null,
setSubscriptionList: (list: SubscriptionItem[]) => void,
setCurrency: (currency: Currencies) => void,
setDuration: (duration: Durations) => void,
) => {
const preSetItems: SubscriptionItem[] = [];
let preSetCurrency: Currencies | null = null;
let preSetDuration: Durations | null = null;

Check warning on line 182 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L180-L182

Added lines #L180 - L182 were not covered by tests

if (subscriptionList?.length === 0 && items.length > 0) {
items.forEach((item) => {
if (subscriptionList.length === 0 && offer.items.length > 0) {
offer.items.forEach((item) => {
const preSetItem = getPreSelectedItem(item);

Check warning on line 186 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L185-L186

Added lines #L185 - L186 were not covered by tests
if (preSetItem) {
preSetItems.push(preSetItem);
}
if (preSetItem) preSetItems.push(preSetItem);

preSetCurrency = preSetCurrency || getPreCurrency(item);
preSetDuration = preSetDuration || getPreDuration(item);
});

// Update subscription list
if (preSetItems.length > 0) {
setSubscriptionList(preSetItems);

Check warning on line 194 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L194

Added line #L194 was not covered by tests
localStorage.setItem(
DISTRIBUTOR_SELECTOR_KEYS.SUBSCRIPTION_LIST,
JSON.stringify(preSetItems),
);
}

// Update currency
if (preSetCurrency) {
setCurrency(preSetCurrency);

Check warning on line 201 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L201

Added line #L201 was not covered by tests
localStorage.setItem(
DISTRIBUTOR_SELECTOR_KEYS.CURRENCY,
JSON.stringify(preSetCurrency),
);
}

// Update duration
if (preSetDuration) {
setDuration(preSetDuration);

Check warning on line 208 in static/js/src/advantage/distributor/utils/FormContext.tsx

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/FormContext.tsx#L208

Added line #L208 was not covered by tests
localStorage.setItem(
Expand All @@ -274,14 +212,14 @@ export const FormProvider = ({
);
}
}
};
}, [offer]);

useEffect(() => {
if (!localTechnicalUserContact) {
if ((!technicalUserContact.name || !technicalUserContact.email) && offer) {
setTechnicalUserContact({
name: offer?.technical_contact_name,
email: offer?.technical_contact_email,
} as TechnicalUserContact);
name: offer.technical_contact_name || "",
email: offer.technical_contact_email || "",
});
}
}, [offer]);

Expand Down
5 changes: 5 additions & 0 deletions static/js/src/advantage/distributor/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,8 @@ export const DISTRIBUTOR_SELECTOR_KEYS = {
PRODUCT_TYPE: "distributor-selector-productType",
PRODUCT_LISTING: "distributor-product-listing",
} as const;

export const getLocalStorageItem = <T>(key: string, defaultValue: T): T => {
const item = localStorage.getItem(key);

Check warning on line 306 in static/js/src/advantage/distributor/utils/utils.ts

View check run for this annotation

Codecov / codecov/patch

static/js/src/advantage/distributor/utils/utils.ts#L306

Added line #L306 was not covered by tests
return item ? JSON.parse(item) : defaultValue;
};

0 comments on commit 4687861

Please sign in to comment.