-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pci.public-ip): select default gatway by region and lowest price
ref: TAPC-2602 Signed-off-by: tsiorifamonjena <[email protected]>
- Loading branch information
1 parent
7aab13a
commit b8d17ec
Showing
5 changed files
with
72 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/manager/apps/pci-public-ip/src/api/hooks/useDefaultAvailableGateway.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useMemo } from 'react'; | ||
import { TCatalog } from '@ovh-ux/manager-pci-common'; | ||
import { useCloudCatalog } from './catalog/useCloudCatalog'; | ||
import { useAvailableHourlyPlansRegion } from './useRegions'; | ||
|
||
const getGatewayWithLowestPrice = (catalog: TCatalog, plans: string[]) => { | ||
const gatewayProducts = (catalog.addons || []) | ||
.filter((addon) => plans.includes(addon.planCode)) | ||
.sort( | ||
({ pricings: [{ price: priceA }] }, { pricings: [{ price: priceB }] }) => | ||
Number(priceA) - Number(priceB), | ||
) | ||
.filter( | ||
({ product }, _index: number, arr: { product: string }[]) => | ||
product === arr[0].product, | ||
); | ||
const [monthlyPriceObj] = | ||
(gatewayProducts || [])?.find(({ planCode }) => planCode?.includes('month')) | ||
?.pricings || []; | ||
const [hourlyPriceObj] = | ||
(gatewayProducts || []).find(({ planCode }) => planCode.includes('hour')) | ||
?.pricings || []; | ||
|
||
const size = gatewayProducts[0]?.product | ||
.split('-') | ||
.slice(-1) | ||
.join(); | ||
const pricePerMonth = monthlyPriceObj?.price; | ||
const pricePerHour = hourlyPriceObj?.price; | ||
|
||
return { | ||
size, | ||
price: { hour: Number(pricePerHour), month: Number(pricePerMonth) }, | ||
}; | ||
}; | ||
|
||
// TODO: similar use case in network and compute, think to move it to pci-common | ||
export const useDefaultAvailableGateway = (region: string) => { | ||
const { data } = useCloudCatalog(); | ||
|
||
const plans = useAvailableHourlyPlansRegion(region); | ||
|
||
return useMemo(() => (data ? getGatewayWithLowestPrice(data, plans) : null), [ | ||
data, | ||
plans, | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
packages/manager/apps/pci-public-ip/src/api/hooks/useSelectedGateway.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters