@cloud-ru/uikit-product-calculator 0.33.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/CHANGELOG.md +1508 -0
  2. package/LICENSE +201 -0
  3. package/README.md +85 -0
  4. package/package.json +75 -0
  5. package/src/components/Calculator/Calculator.tsx +79 -0
  6. package/src/components/Calculator/index.ts +1 -0
  7. package/src/components/CalculatorContent/CalculatorContent.tsx +38 -0
  8. package/src/components/CalculatorContent/index.ts +1 -0
  9. package/src/components/CalculatorContent/styles.module.scss +79 -0
  10. package/src/components/Catalog/Catalog.tsx +225 -0
  11. package/src/components/Catalog/components/PrivateCardHeader.tsx +63 -0
  12. package/src/components/Catalog/components/index.ts +1 -0
  13. package/src/components/Catalog/components/styles.module.scss +35 -0
  14. package/src/components/Catalog/index.ts +1 -0
  15. package/src/components/Catalog/styles.module.scss +104 -0
  16. package/src/components/Controls/AlertControl/AlertControl.tsx +44 -0
  17. package/src/components/Controls/AlertControl/index.ts +1 -0
  18. package/src/components/Controls/ArrayControl/ArrayControl.tsx +105 -0
  19. package/src/components/Controls/ArrayControl/index.ts +1 -0
  20. package/src/components/Controls/ArrayControl/styles.module.scss +33 -0
  21. package/src/components/Controls/CarouselControl/CarouselControl.tsx +173 -0
  22. package/src/components/Controls/CarouselControl/index.ts +1 -0
  23. package/src/components/Controls/CarouselControl/styles.module.scss +32 -0
  24. package/src/components/Controls/Control/Control.tsx +205 -0
  25. package/src/components/Controls/Control/index.ts +1 -0
  26. package/src/components/Controls/ObjectControl/ObjectControl.tsx +150 -0
  27. package/src/components/Controls/ObjectControl/index.ts +1 -0
  28. package/src/components/Controls/ObjectControl/styles.module.scss +18 -0
  29. package/src/components/Controls/ObjectDecorator/ObjectDecorator.tsx +23 -0
  30. package/src/components/Controls/ObjectDecorator/index.ts +1 -0
  31. package/src/components/Controls/ObjectDecorator/styles.module.scss +15 -0
  32. package/src/components/Controls/SegmentedControl/SegmentedControl.tsx +79 -0
  33. package/src/components/Controls/SegmentedControl/SegmentedControlCard.tsx +38 -0
  34. package/src/components/Controls/SegmentedControl/index.ts +1 -0
  35. package/src/components/Controls/SegmentedControl/styles.module.scss +8 -0
  36. package/src/components/Controls/SelectControl/SelectControl.tsx +172 -0
  37. package/src/components/Controls/SelectControl/index.ts +1 -0
  38. package/src/components/Controls/SelectControl/styles.module.scss +5 -0
  39. package/src/components/Controls/SliderControl/SliderControl.tsx +92 -0
  40. package/src/components/Controls/SliderControl/index.ts +1 -0
  41. package/src/components/Controls/StepperControl/StepperControl.tsx +160 -0
  42. package/src/components/Controls/StepperControl/index.ts +1 -0
  43. package/src/components/Controls/StepperControl/styles.module.scss +16 -0
  44. package/src/components/Controls/TableControl/TableControl.tsx +162 -0
  45. package/src/components/Controls/TableControl/helperComponents/StepperWithAllowedValues.tsx +50 -0
  46. package/src/components/Controls/TableControl/helperComponents/index.ts +1 -0
  47. package/src/components/Controls/TableControl/helperComponents/styles.module.scss +53 -0
  48. package/src/components/Controls/TableControl/index.ts +1 -0
  49. package/src/components/Controls/TableControl/styles.module.scss +104 -0
  50. package/src/components/Controls/ToggleCard/ToggleCard.tsx +46 -0
  51. package/src/components/Controls/ToggleCard/index.ts +1 -0
  52. package/src/components/Controls/ToggleCard/styles.module.scss +11 -0
  53. package/src/components/Controls/ToggleCardsControl/ToggleCardsControl.tsx +93 -0
  54. package/src/components/Controls/ToggleCardsControl/index.ts +1 -0
  55. package/src/components/Controls/ToggleCardsControl/styles.module.scss +40 -0
  56. package/src/components/Controls/ToggleControl/ToggleControl.tsx +59 -0
  57. package/src/components/Controls/ToggleControl/components/SwitchRow.tsx +98 -0
  58. package/src/components/Controls/ToggleControl/components/index.ts +1 -0
  59. package/src/components/Controls/ToggleControl/components/styles.module.scss +110 -0
  60. package/src/components/Controls/ToggleControl/index.ts +1 -0
  61. package/src/components/Controls/ToggleObjectControl/ToggleObjectControl.tsx +111 -0
  62. package/src/components/Controls/ToggleObjectControl/index.ts +1 -0
  63. package/src/components/Controls/ToggleObjectControl/styles.module.scss +13 -0
  64. package/src/components/Controls/constants.ts +16 -0
  65. package/src/components/Controls/index.tsx +26 -0
  66. package/src/components/Controls/types.ts +181 -0
  67. package/src/components/Controls/utils.ts +49 -0
  68. package/src/components/Disclaimer/Disclaimer.tsx +33 -0
  69. package/src/components/Disclaimer/index.ts +1 -0
  70. package/src/components/Disclaimer/styles.module.scss +14 -0
  71. package/src/components/HeaderContainer/index.tsx +32 -0
  72. package/src/components/HeaderContainer/styles.module.scss +24 -0
  73. package/src/components/PriceSummary/PriceSummary.tsx +158 -0
  74. package/src/components/PriceSummary/components/PricePeriodSelect/PricePeriodSelect.tsx +81 -0
  75. package/src/components/PriceSummary/components/PricePeriodSelect/index.ts +1 -0
  76. package/src/components/PriceSummary/components/ProductCard/ProductCard.tsx +85 -0
  77. package/src/components/PriceSummary/components/ProductCard/index.ts +1 -0
  78. package/src/components/PriceSummary/components/ProductCard/styles.module.scss +7 -0
  79. package/src/components/PriceSummary/components/ProductListActions/ProductListActions.tsx +118 -0
  80. package/src/components/PriceSummary/components/ProductListActions/index.ts +1 -0
  81. package/src/components/PriceSummary/components/ProductListActions/styles.module.scss +38 -0
  82. package/src/components/PriceSummary/components/index.ts +3 -0
  83. package/src/components/PriceSummary/hooks.ts +36 -0
  84. package/src/components/PriceSummary/index.ts +1 -0
  85. package/src/components/PriceSummary/styles.module.scss +110 -0
  86. package/src/components/PriceSummary/utils/getTotalPrice.ts +19 -0
  87. package/src/components/PriceSummary/utils/index.ts +2 -0
  88. package/src/components/PriceSummary/utils/transformToProductCardData.ts +36 -0
  89. package/src/components/ProductHeadline/ProductPageHeadline.tsx +134 -0
  90. package/src/components/ProductHeadline/components/PriceCount.tsx +93 -0
  91. package/src/components/ProductHeadline/components/index.ts +1 -0
  92. package/src/components/ProductHeadline/components/styles.module.scss +52 -0
  93. package/src/components/ProductHeadline/index.ts +1 -0
  94. package/src/components/ProductHeadline/styles.module.scss +32 -0
  95. package/src/components/ProductPage/ProductPage.tsx +105 -0
  96. package/src/components/ProductPage/index.ts +1 -0
  97. package/src/components/ProductPage/styles.module.scss +13 -0
  98. package/src/components/Welcome/Welcome.tsx +138 -0
  99. package/src/components/Welcome/index.ts +1 -0
  100. package/src/components/Welcome/styles.module.scss +155 -0
  101. package/src/components/index.ts +3 -0
  102. package/src/config/config.tsx +27 -0
  103. package/src/config/index.ts +2 -0
  104. package/src/config/platforms/advanced/catalog.ts +82 -0
  105. package/src/config/platforms/advanced/constants.ts +23 -0
  106. package/src/config/platforms/advanced/index.ts +5 -0
  107. package/src/config/platforms/advanced/platform.tsx +13 -0
  108. package/src/config/platforms/advanced/product-config/AdvancedCloudBackup.ts +76 -0
  109. package/src/config/platforms/advanced/product-config/AdvancedCloudContainerEngine.tsx +260 -0
  110. package/src/config/platforms/advanced/product-config/AdvancedCloudServer.tsx +526 -0
  111. package/src/config/platforms/advanced/product-config/AdvancedDataAsYouUse.ts +159 -0
  112. package/src/config/platforms/advanced/product-config/AdvancedDataLakeInsight.ts +134 -0
  113. package/src/config/platforms/advanced/product-config/AdvancedDataWarehouseService.ts +94 -0
  114. package/src/config/platforms/advanced/product-config/AdvancedDcsMemcached.ts +69 -0
  115. package/src/config/platforms/advanced/product-config/AdvancedDcsRedis.ts +220 -0
  116. package/src/config/platforms/advanced/product-config/AdvancedDmsKafka.tsx +186 -0
  117. package/src/config/platforms/advanced/product-config/AdvancedDmsRabbitMq.ts +202 -0
  118. package/src/config/platforms/advanced/product-config/AdvancedDocumentDatabase.tsx +323 -0
  119. package/src/config/platforms/advanced/product-config/AdvancedElasticLoadBalance.tsx +252 -0
  120. package/src/config/platforms/advanced/product-config/AdvancedElasticSearch.ts +365 -0
  121. package/src/config/platforms/advanced/product-config/AdvancedFunctionGraph.tsx +65 -0
  122. package/src/config/platforms/advanced/product-config/AdvancedMapReduce.tsx +394 -0
  123. package/src/config/platforms/advanced/product-config/AdvancedMySqlDataBase.ts +417 -0
  124. package/src/config/platforms/advanced/product-config/AdvancedNetwork.ts +146 -0
  125. package/src/config/platforms/advanced/product-config/AdvancedObjectStorage.ts +232 -0
  126. package/src/config/platforms/advanced/product-config/AdvancedPostgreSqlDatabase.ts +402 -0
  127. package/src/config/platforms/advanced/product-config/AdvancedScalableFile.tsx +161 -0
  128. package/src/config/platforms/advanced/product-config/AdvancedSqlDatabase.ts +380 -0
  129. package/src/config/platforms/advanced/product-config/index.ts +21 -0
  130. package/src/config/platforms/advanced/products.ts +267 -0
  131. package/src/config/platforms/evolution/catalog.tsx +107 -0
  132. package/src/config/platforms/evolution/constants.ts +80 -0
  133. package/src/config/platforms/evolution/index.ts +5 -0
  134. package/src/config/platforms/evolution/platform.tsx +13 -0
  135. package/src/config/platforms/evolution/product-config/EvolutionArenadataDb.ts +151 -0
  136. package/src/config/platforms/evolution/product-config/EvolutionArtifactRegistry.ts +57 -0
  137. package/src/config/platforms/evolution/product-config/EvolutionBareMetal.tsx +172 -0
  138. package/src/config/platforms/evolution/product-config/EvolutionCloudServer.tsx +407 -0
  139. package/src/config/platforms/evolution/product-config/EvolutionCloudServerFreeTier.tsx +185 -0
  140. package/src/config/platforms/evolution/product-config/EvolutionCloudServerGpu.tsx +301 -0
  141. package/src/config/platforms/evolution/product-config/EvolutionContainerApps.ts +84 -0
  142. package/src/config/platforms/evolution/product-config/EvolutionContainerAppsFreeTier.ts +29 -0
  143. package/src/config/platforms/evolution/product-config/EvolutionKubernetes.ts +388 -0
  144. package/src/config/platforms/evolution/product-config/EvolutionManagedMetastore.ts +38 -0
  145. package/src/config/platforms/evolution/product-config/EvolutionManagedRedis.ts +161 -0
  146. package/src/config/platforms/evolution/product-config/EvolutionManagedSpark.ts +360 -0
  147. package/src/config/platforms/evolution/product-config/EvolutionManagedTrino.ts +53 -0
  148. package/src/config/platforms/evolution/product-config/EvolutionMlInference.tsx +703 -0
  149. package/src/config/platforms/evolution/product-config/EvolutionPostgreSql.ts +217 -0
  150. package/src/config/platforms/evolution/product-config/EvolutionPublicIp.ts +39 -0
  151. package/src/config/platforms/evolution/product-config/EvolutionSnatGateway.ts +22 -0
  152. package/src/config/platforms/evolution/product-config/EvolutionStorageS3.ts +223 -0
  153. package/src/config/platforms/evolution/product-config/EvolutionStorageS3FreeTier.ts +97 -0
  154. package/src/config/platforms/evolution/product-config/index.ts +17 -0
  155. package/src/config/platforms/evolution/products.ts +260 -0
  156. package/src/config/platforms/index.ts +3 -0
  157. package/src/config/platforms/vmware/catalog.ts +19 -0
  158. package/src/config/platforms/vmware/constants.ts +6 -0
  159. package/src/config/platforms/vmware/index.ts +5 -0
  160. package/src/config/platforms/vmware/platform.tsx +13 -0
  161. package/src/config/platforms/vmware/product-config/VmWareCloudBackup.ts +56 -0
  162. package/src/config/platforms/vmware/product-config/VmWareVirtualDataCenter.ts +309 -0
  163. package/src/config/platforms/vmware/product-config/VmWareVirtualMachinesBackup.ts +58 -0
  164. package/src/config/platforms/vmware/product-config/VmWareVirtualWorkspaces.tsx +210 -0
  165. package/src/config/platforms/vmware/product-config/index.ts +4 -0
  166. package/src/config/platforms/vmware/products.ts +63 -0
  167. package/src/config/styles.module.scss +9 -0
  168. package/src/config/utils/disk.ts +94 -0
  169. package/src/config/utils/diskPostgreSqlMySQL.ts +94 -0
  170. package/src/config/utils/eip.ts +104 -0
  171. package/src/config/utils/index.ts +52 -0
  172. package/src/config/utils/obs.ts +100 -0
  173. package/src/constants.ts +56 -0
  174. package/src/contexts/CalculatorContext.tsx +193 -0
  175. package/src/contexts/ProductContext.ts +25 -0
  176. package/src/contexts/index.ts +2 -0
  177. package/src/hooks/index.ts +6 -0
  178. package/src/hooks/useAdaptive.ts +20 -0
  179. package/src/hooks/useCatalogCardClick.ts +37 -0
  180. package/src/hooks/useProductClick.ts +14 -0
  181. package/src/hooks/useProductDelete.ts +46 -0
  182. package/src/index.ts +6 -0
  183. package/src/services/constants.ts +5 -0
  184. package/src/services/getFetcherFn.ts +57 -0
  185. package/src/services/getInitPrice.ts +64 -0
  186. package/src/services/getOnDownloadFileClick.ts +37 -0
  187. package/src/services/getOnShareClick.ts +56 -0
  188. package/src/services/index.ts +4 -0
  189. package/src/types/CalculatorType.ts +16 -0
  190. package/src/types/CatalogConfig.ts +13 -0
  191. package/src/types/Category.ts +14 -0
  192. package/src/types/FetcherFn.ts +26 -0
  193. package/src/types/FormValues.ts +4 -0
  194. package/src/types/Platform.ts +24 -0
  195. package/src/types/Price.ts +18 -0
  196. package/src/types/Product.ts +39 -0
  197. package/src/types/ProductState.ts +11 -0
  198. package/src/types/State.ts +7 -0
  199. package/src/types/index.ts +10 -0
  200. package/src/utils/filterNonEmptyArrays.ts +13 -0
  201. package/src/utils/formatNumber.ts +5 -0
  202. package/src/utils/getPrice.ts +38 -0
  203. package/src/utils/index.ts +5 -0
  204. package/src/utils/parseKeyToDataTest.ts +14 -0
  205. package/src/utils/value.ts +11 -0
@@ -0,0 +1,100 @@
1
+ import { ValueOf } from '@snack-uikit/utils';
2
+
3
+ import { CONTROL, ObjectControl, SelectSingleControl, StepperControl } from '../../components';
4
+
5
+ export const UnitsOfCalculationItem = {
6
+ Gb: 'gb',
7
+ Tb: 'tb',
8
+ };
9
+
10
+ export const unitsOfCalculationItems = [
11
+ {
12
+ value: UnitsOfCalculationItem.Gb,
13
+ label: 'ГБ',
14
+ },
15
+ {
16
+ value: UnitsOfCalculationItem.Tb,
17
+ label: 'ТБ',
18
+ },
19
+ ];
20
+
21
+ type GetObsProps = {
22
+ space: Pick<
23
+ Partial<StepperControl>,
24
+ 'defaultValue' | 'accessorKey' | 'uiProps' | 'watchedControls' | 'relateFn' | 'onChangeFn'
25
+ > & {
26
+ label?: string;
27
+ };
28
+ units: Pick<
29
+ Partial<SelectSingleControl>,
30
+ 'accessorKey' | 'items' | 'uiProps' | 'watchedControls' | 'relateFn' | 'onChangeFn'
31
+ > & {
32
+ defaultValue?: ValueOf<typeof UnitsOfCalculationItem>;
33
+ };
34
+ };
35
+
36
+ export function getObs({ space, units }: GetObsProps): ObjectControl {
37
+ const {
38
+ accessorKey: spaceKey = 'obs.space',
39
+ label: spaceLabel = 'Объем хранилища',
40
+ uiProps: spaceUiProps,
41
+ defaultValue: spaceDefaultValue = 0,
42
+ ...restSpace
43
+ } = space;
44
+
45
+ const {
46
+ accessorKey: unitsKey = 'obs.unitsOfCalculation',
47
+ items: unitsItems = unitsOfCalculationItems,
48
+ uiProps: unitUiProps,
49
+ defaultValue: unitsDefaultValue = UnitsOfCalculationItem.Gb,
50
+ ...restUnits
51
+ } = units;
52
+ return {
53
+ type: CONTROL.Object,
54
+ ui: [{ controls: ['space', 'unitsOfCalculation'] }],
55
+ controls: {
56
+ space: {
57
+ type: CONTROL.Stepper,
58
+ decoratorProps: {
59
+ label: spaceLabel,
60
+ },
61
+ accessorKey: spaceKey,
62
+ defaultValue: spaceDefaultValue,
63
+ uiProps: {
64
+ min: 0,
65
+ max: 9_999_999_900,
66
+ postfix: 'ГБ',
67
+ showHint: false,
68
+ ...spaceUiProps,
69
+ },
70
+ watchedControls: { unitsOfCalculation: unitsKey },
71
+ relateFn: ({ unitsOfCalculation }) => {
72
+ if (unitsOfCalculation === UnitsOfCalculationItem.Tb) {
73
+ return {
74
+ uiProps: {
75
+ max: 9_765_620,
76
+ postfix: 'ТБ',
77
+ },
78
+ };
79
+ }
80
+ },
81
+ ...restSpace,
82
+ },
83
+ unitsOfCalculation: {
84
+ type: CONTROL.SelectSingle,
85
+ accessorKey: unitsKey,
86
+ decoratorProps: {
87
+ label: '',
88
+ },
89
+ defaultValue: unitsDefaultValue,
90
+ items: unitsItems,
91
+ uiProps: {
92
+ showClearButton: false,
93
+ searchable: false,
94
+ ...unitUiProps,
95
+ },
96
+ ...restUnits,
97
+ },
98
+ },
99
+ };
100
+ }
@@ -0,0 +1,56 @@
1
+ import { Price } from './types';
2
+
3
+ export const PLATFORM = {
4
+ Advanced: 'advanced',
5
+ MlSpace: 'mlspace',
6
+ VmWare: 'vmware',
7
+ Evolution: 'evolution',
8
+ Test: 'test',
9
+ };
10
+
11
+ export const DEFAULT_PRICE: Price = {
12
+ priceHourNds: 0,
13
+ priceDayNds: 0,
14
+ priceMonthNds: 0,
15
+ pricePartnersHourNds: 0,
16
+ pricePartnersDayNds: 0,
17
+ pricePartnersMonthNds: 0,
18
+ };
19
+
20
+ export const CATEGORY = {
21
+ Popular: 'popular',
22
+ FreeTier: 'free-tier',
23
+ Computations: 'computations',
24
+ Network: 'network',
25
+ Containerization: 'containerization',
26
+ Storage: 'storage',
27
+ Database: 'database',
28
+ Development: 'development',
29
+ Analytic: 'analytic',
30
+ ML: 'ml-ai-tools',
31
+ } as const;
32
+
33
+ export const DEFAULT_CATEGORY = CATEGORY.Popular;
34
+
35
+ export const CALCULATOR_DATA_TEST_ID = 'calculator';
36
+
37
+ export enum WorkingHoursSpecification {
38
+ Hour = 'hour',
39
+ Day = 'day',
40
+ Month = 'month',
41
+ }
42
+
43
+ export const WORKING_HOURS_ITEMS = [
44
+ {
45
+ value: WorkingHoursSpecification.Month,
46
+ label: 'Месяц',
47
+ },
48
+ {
49
+ value: WorkingHoursSpecification.Day,
50
+ label: 'День',
51
+ },
52
+ {
53
+ value: WorkingHoursSpecification.Hour,
54
+ label: 'Час',
55
+ },
56
+ ];
@@ -0,0 +1,193 @@
1
+ import { createContext, PropsWithChildren, useContext, useState } from 'react';
2
+
3
+ import { LAYOUT_TYPE, LayoutType } from '@cloud-ru/uikit-product-utils';
4
+
5
+ import { CalculatorProps } from '../components';
6
+ import { getDefaultValues } from '../components/Controls/utils';
7
+ import { DEFAULT_CATEGORY, DEFAULT_PRICE, PLATFORM } from '../constants';
8
+ import { CatalogConfig, FetcherFn, FormValues, PlatformType, PRICE_PERIOD, PricePeriod, ProductState } from '../types';
9
+ import { CALCULATOR_TYPE, CalculatorType } from '../types/CalculatorType';
10
+
11
+ export type SelectedProduct = { id: string; idx: number } | null;
12
+
13
+ export type ProductsState = Record<string, ProductState[]>;
14
+
15
+ type CalculatorContextType = {
16
+ selectedPlatform: PlatformType;
17
+ setSelectedPlatform(platform: PlatformType): void;
18
+
19
+ selectedCategory: string;
20
+ setSelectedCategory(category: string): void;
21
+
22
+ catalogOpen: boolean;
23
+ setCatalogOpen(open: boolean): void;
24
+
25
+ pricePeriod: PricePeriod;
26
+ setPricePeriod(period: PricePeriod): void;
27
+
28
+ selectedProduct: SelectedProduct;
29
+ setSelectedProduct(value: SelectedProduct): void;
30
+
31
+ layoutType: LayoutType;
32
+ calculatorType: CalculatorType;
33
+
34
+ config: CatalogConfig;
35
+
36
+ fetcherFn: FetcherFn;
37
+
38
+ products: ProductsState;
39
+ setProducts(products: FormValues): void;
40
+
41
+ actions: CalculatorProps['actions'];
42
+ onAnalyticsClick: NonNullable<CalculatorProps['onAnalyticsClick']>;
43
+ };
44
+
45
+ const noop = () => {};
46
+
47
+ export const CalculatorContext = createContext<CalculatorContextType>({
48
+ selectedCategory: DEFAULT_CATEGORY,
49
+ setSelectedCategory: noop,
50
+
51
+ selectedPlatform: PLATFORM.Evolution,
52
+ setSelectedPlatform: noop,
53
+
54
+ catalogOpen: false,
55
+ setCatalogOpen: noop,
56
+
57
+ pricePeriod: PRICE_PERIOD.Month,
58
+ setPricePeriod: noop,
59
+
60
+ selectedProduct: null,
61
+ setSelectedProduct: noop,
62
+
63
+ calculatorType: CALCULATOR_TYPE.Main,
64
+ layoutType: LAYOUT_TYPE.Desktop,
65
+
66
+ config: {} as CatalogConfig,
67
+ fetcherFn: (async () => undefined) as unknown as FetcherFn,
68
+
69
+ products: {} as ProductsState,
70
+ setProducts: noop,
71
+
72
+ actions: {},
73
+ onAnalyticsClick: noop,
74
+ });
75
+
76
+ export const useCalculatorContext = () => useContext(CalculatorContext);
77
+
78
+ type CalculatorContextProviderProps = PropsWithChildren<
79
+ Pick<
80
+ CalculatorProps,
81
+ | 'actions'
82
+ | 'calculatorType'
83
+ | 'fetcherFn'
84
+ | 'iniState'
85
+ | 'initialActiveProduct'
86
+ | 'config'
87
+ | 'layoutType'
88
+ | 'onAnalyticsClick'
89
+ >
90
+ >;
91
+
92
+ export function CalculatorContextProvider({
93
+ layoutType = LAYOUT_TYPE.Desktop,
94
+ calculatorType = CALCULATOR_TYPE.Main,
95
+
96
+ fetcherFn,
97
+
98
+ config,
99
+ iniState,
100
+ initialActiveProduct,
101
+
102
+ actions,
103
+ onAnalyticsClick,
104
+
105
+ children,
106
+ }: CalculatorContextProviderProps) {
107
+ const [selectedPlatform, setSelectedPlatform] = useState<PlatformType>(() => {
108
+ if (initialActiveProduct) {
109
+ return config.products?.[initialActiveProduct].platform;
110
+ }
111
+
112
+ return config.platforms[0].id;
113
+ });
114
+ const [selectedCategory, setSelectedCategory] = useState<string>(config.catalog[selectedPlatform][0].id);
115
+ const [selectedProduct, setSelectedProduct] = useState<SelectedProduct | null>(() => {
116
+ if (iniState && Object.keys(iniState).length > 0) {
117
+ return { id: Object.keys(iniState)[0], idx: 0 };
118
+ }
119
+
120
+ if (initialActiveProduct) {
121
+ return { id: initialActiveProduct, idx: 0 };
122
+ }
123
+
124
+ return null;
125
+ });
126
+ const [open, setOpen] = useState<boolean>(false);
127
+
128
+ const [pricePeriod, setPricePeriod] = useState<PricePeriod>(PRICE_PERIOD.Month);
129
+
130
+ const [products, setProducts] = useState<ProductsState>(() => {
131
+ if (iniState && Object.keys(iniState).length > 0) {
132
+ return iniState;
133
+ }
134
+
135
+ if (initialActiveProduct) {
136
+ const controls = config.products[initialActiveProduct].formConfig?.controls;
137
+
138
+ if (controls) {
139
+ return {
140
+ [initialActiveProduct]: [
141
+ {
142
+ data: { ...getDefaultValues(controls ?? {}), productQuantity: 1 },
143
+ price: DEFAULT_PRICE,
144
+ },
145
+ ],
146
+ };
147
+ }
148
+ }
149
+
150
+ return {};
151
+ });
152
+
153
+ const onAnalyticsClickCalculator = (value: string, control: string) => {
154
+ if (selectedProduct && onAnalyticsClick) {
155
+ onAnalyticsClick(value, `${control}-${selectedProduct.id}`);
156
+ }
157
+ };
158
+
159
+ return (
160
+ <CalculatorContext.Provider
161
+ value={{
162
+ selectedCategory,
163
+ setSelectedCategory,
164
+
165
+ selectedPlatform,
166
+ setSelectedPlatform,
167
+
168
+ pricePeriod,
169
+ setPricePeriod,
170
+
171
+ selectedProduct,
172
+ setSelectedProduct,
173
+
174
+ catalogOpen: open,
175
+ setCatalogOpen: setOpen,
176
+
177
+ layoutType,
178
+ calculatorType,
179
+ config,
180
+
181
+ fetcherFn,
182
+
183
+ products,
184
+ setProducts,
185
+
186
+ actions,
187
+ onAnalyticsClick: onAnalyticsClickCalculator,
188
+ }}
189
+ >
190
+ {children}
191
+ </CalculatorContext.Provider>
192
+ );
193
+ }
@@ -0,0 +1,25 @@
1
+ import { createContext, useContext } from 'react';
2
+
3
+ import { DEFAULT_PRICE } from '../constants';
4
+ import { AnyType } from '../types';
5
+ import { Price } from '../types/Price';
6
+
7
+ type ProductPageContextType = {
8
+ value: AnyType;
9
+ onChange(value: AnyType): void;
10
+
11
+ price?: Price;
12
+ priceList?: Record<string, Price>;
13
+ };
14
+
15
+ const noop = () => {};
16
+
17
+ export const ProductContext = createContext<ProductPageContextType>({
18
+ value: {},
19
+ onChange: noop,
20
+
21
+ price: DEFAULT_PRICE,
22
+ priceList: {},
23
+ });
24
+
25
+ export const useProductContext = () => useContext(ProductContext);
@@ -0,0 +1,2 @@
1
+ export * from './CalculatorContext';
2
+ export * from './ProductContext';
@@ -0,0 +1,6 @@
1
+ export * from './useAdaptive';
2
+
3
+ export * from './useCatalogCardClick';
4
+
5
+ export * from './useProductClick';
6
+ export * from './useProductDelete';
@@ -0,0 +1,20 @@
1
+ import { LAYOUT_TYPE } from '@cloud-ru/uikit-product-utils';
2
+
3
+ import { useCalculatorContext } from '../contexts';
4
+
5
+ export function useAdaptive() {
6
+ const { layoutType } = useCalculatorContext();
7
+
8
+ const isMobile = layoutType === LAYOUT_TYPE.Mobile;
9
+ const isTablet = layoutType === LAYOUT_TYPE.Tablet;
10
+ const isDesktopSmall = layoutType === LAYOUT_TYPE.DesktopSmall;
11
+ const isDesktop = layoutType === LAYOUT_TYPE.Desktop;
12
+
13
+ return {
14
+ layoutType,
15
+ isMobile,
16
+ isTablet,
17
+ isDesktopSmall,
18
+ isDesktop,
19
+ };
20
+ }
@@ -0,0 +1,37 @@
1
+ import { getDefaultValues } from '../components/Controls/utils';
2
+ import { DEFAULT_CATEGORY, DEFAULT_PRICE } from '../constants';
3
+ import { ProductsState, useCalculatorContext } from '../contexts';
4
+
5
+ export function useCatalogCardClick() {
6
+ const {
7
+ setSelectedProduct,
8
+ config,
9
+ setCatalogOpen,
10
+ products,
11
+ setProducts,
12
+ setSelectedCategory,
13
+ actions: { onProductSelect },
14
+ } = useCalculatorContext();
15
+
16
+ return (productId: string) => {
17
+ onProductSelect?.(productId);
18
+
19
+ setProducts((oldValue: ProductsState) => ({
20
+ ...oldValue,
21
+ [productId]: (oldValue[productId] ?? []).concat([
22
+ {
23
+ data: { ...getDefaultValues(config.products[productId].formConfig?.controls ?? {}), productQuantity: 1 },
24
+ price: DEFAULT_PRICE,
25
+ },
26
+ ]),
27
+ }));
28
+
29
+ setSelectedProduct({
30
+ id: productId,
31
+ idx: products[productId]?.length || 0,
32
+ });
33
+
34
+ setSelectedCategory(DEFAULT_CATEGORY);
35
+ setCatalogOpen(false);
36
+ };
37
+ }
@@ -0,0 +1,14 @@
1
+ import { useCallback } from 'react';
2
+
3
+ import { useCalculatorContext } from '../contexts';
4
+
5
+ export function useProductClick() {
6
+ const { setSelectedProduct } = useCalculatorContext();
7
+
8
+ return useCallback(
9
+ (productId: string, idx: number) => {
10
+ setSelectedProduct({ id: productId, idx });
11
+ },
12
+ [setSelectedProduct],
13
+ );
14
+ }
@@ -0,0 +1,46 @@
1
+ import { useCalculatorContext } from '../contexts';
2
+ import { FormValues } from '../types';
3
+ import { filterNonEmptyArrays } from '../utils/filterNonEmptyArrays';
4
+
5
+ const deleteItem = (productId: string, idx: number) => (oldValue: FormValues) => {
6
+ const oldArrayValue = oldValue[productId ?? 0];
7
+
8
+ if (!oldArrayValue) {
9
+ return oldValue;
10
+ }
11
+
12
+ oldArrayValue.splice(idx ?? 0, 1);
13
+
14
+ const newValue = {
15
+ ...oldValue,
16
+ [productId]: oldArrayValue,
17
+ };
18
+
19
+ return { ...filterNonEmptyArrays(newValue) };
20
+ };
21
+
22
+ export function useProductDelete() {
23
+ const {
24
+ setSelectedProduct,
25
+ selectedProduct,
26
+ products,
27
+ setProducts,
28
+ actions: { onProductDelete },
29
+ } = useCalculatorContext();
30
+
31
+ return (productId: string, idx: number) => {
32
+ setProducts(deleteItem(productId, idx));
33
+
34
+ onProductDelete?.(productId);
35
+
36
+ if (selectedProduct?.id === productId && selectedProduct?.idx === idx) {
37
+ const newProductId = Object.keys(products).find(productId => products[productId].length > 0);
38
+
39
+ if (newProductId) {
40
+ setSelectedProduct({ id: newProductId, idx: 0 });
41
+ } else {
42
+ setSelectedProduct(null);
43
+ }
44
+ }
45
+ };
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './components';
2
+ export * from './constants';
3
+ export * from './types';
4
+ export * from './config';
5
+ export * from './services';
6
+ export * from './config';
@@ -0,0 +1,5 @@
1
+ export const DEFAULT_BACKEND_HOST = 'https://api.cloud.ru';
2
+ export const DEFAULT_BASE_URL = 'https://cloud.ru';
3
+ export const DEFAULT_HEADERS = {
4
+ 'Content-Type': 'application/json;charset=utf-8',
5
+ };
@@ -0,0 +1,57 @@
1
+ import { CALCULATOR_TYPE, FetcherFnProps } from '../types';
2
+ import { DEFAULT_BACKEND_HOST, DEFAULT_HEADERS } from './constants';
3
+
4
+ const kebabRegex = /[A-Z]+(?![a-z])|[A-Z]/g;
5
+
6
+ function convertToKebab(value: string) {
7
+ return value.replaceAll(kebabRegex, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
8
+ }
9
+
10
+ function prepareProductType(productName: string) {
11
+ if (!productName) throw new Error(`no productName`);
12
+
13
+ switch (productName) {
14
+ case 'cloudContainerKubernetes':
15
+ return 'cloud-container-engine';
16
+ case 'ruGpt_3':
17
+ return 'rugpт-3'; // мб rugpt-3?
18
+ case 'ruDallЕ':
19
+ return 'rudall-е';
20
+
21
+ default:
22
+ return convertToKebab(productName);
23
+ }
24
+ }
25
+
26
+ const CALCULATOR_PRICES_URL = '/calculator/v2/prices/';
27
+
28
+ type GetFetcherFnParams = {
29
+ headers?: HeadersInit;
30
+ };
31
+
32
+ /**
33
+ *
34
+ * Функция для отправки запросов на цены
35
+ * @function helper
36
+ */
37
+ export function getFetcherFn(backendHost: string = DEFAULT_BACKEND_HOST, params: GetFetcherFnParams = {}) {
38
+ return async function fetcherFn({ formData, productId, calculatorType }: FetcherFnProps) {
39
+ if (!formData) return undefined;
40
+
41
+ const productTypeKebab = prepareProductType(productId);
42
+
43
+ let url = backendHost + CALCULATOR_PRICES_URL + productTypeKebab;
44
+ if (calculatorType === CALCULATOR_TYPE.Partners) url = url + '?calculator_type=partners';
45
+
46
+ const res = await fetch(url, {
47
+ method: 'POST',
48
+ headers: { ...DEFAULT_HEADERS, ...params.headers },
49
+ body: formData,
50
+ });
51
+
52
+ if (!res.ok) throw new Error(`fetcher`);
53
+ const response = await res.json();
54
+ if (!response.success) throw new Error(`fetcher - success false`);
55
+ return response.data;
56
+ };
57
+ }
@@ -0,0 +1,64 @@
1
+ import { CALCULATOR_TYPE, FetcherFn, FormValues, Price, ProductState } from '../types';
2
+
3
+ /**
4
+ *
5
+ * Функция для получения стартовой цены
6
+ * @function helper
7
+ */
8
+ export async function getInitPrice(products: Record<string, Partial<ProductState>[] | null>, fetcherFn: FetcherFn) {
9
+ const productsWithData = Object.entries(products).filter(([, items]) => items && items?.length > 0) as [
10
+ string,
11
+ ProductState[],
12
+ ][];
13
+
14
+ const productPrices = (await Promise.all(
15
+ productsWithData.map(async ([id, items]) => {
16
+ const priceItems = await Promise.all(
17
+ items.map(async ({ data }) => {
18
+ const state: { price?: Price; priceList?: FormValues; data?: FormValues } = {};
19
+
20
+ try {
21
+ const res = (await fetcherFn({
22
+ formData: JSON.stringify(data),
23
+ productId: id,
24
+ calculatorType: CALCULATOR_TYPE.Main,
25
+ })) as
26
+ | {
27
+ price: Price;
28
+ priceList?: FormValues;
29
+ values?: FormValues;
30
+ }
31
+ | undefined;
32
+
33
+ if (res?.price) state.price = res?.price;
34
+ if (res?.priceList) state.priceList = res.priceList;
35
+ if (res?.values) state.data = res.priceList;
36
+ } catch {
37
+ /* empty */
38
+ }
39
+
40
+ return state;
41
+ }),
42
+ );
43
+
44
+ return [id, priceItems];
45
+ }),
46
+ )) as [string, ProductState[]][];
47
+
48
+ return productsWithData.reduce(
49
+ (res, [id, items], idx) => {
50
+ res[id] = items.map((item, id) => {
51
+ const priceItem = productPrices[idx][1][id];
52
+
53
+ return {
54
+ ...item,
55
+ ...priceItem,
56
+ data: { ...item.data, ...priceItem.data },
57
+ };
58
+ });
59
+
60
+ return res;
61
+ },
62
+ {} as Record<string, ProductState[]>,
63
+ );
64
+ }
@@ -0,0 +1,37 @@
1
+ /* eslint-disable @cloud-ru/ssr-safe-react/domApi */
2
+ import { State } from '../types';
3
+ import { DEFAULT_BACKEND_HOST, DEFAULT_HEADERS } from './constants';
4
+
5
+ const DOWNLOADED_FILENAME = 'Расчет_стоимости_CloudRu.xlsx';
6
+ const CALCULATOR_RESULT_EXCEL_URL = '/calculator-result/v2/excel';
7
+
8
+ /**
9
+ *
10
+ * Функция для выгрузки расчета по ценам
11
+ * @function helper
12
+ */
13
+ export function getOnDownloadFileClick(backendHost: string = DEFAULT_BACKEND_HOST) {
14
+ return async function onDownloadFileClick(state: State) {
15
+ const urlBe = backendHost + CALCULATOR_RESULT_EXCEL_URL;
16
+
17
+ const res = await fetch(urlBe, {
18
+ method: 'POST',
19
+ headers: DEFAULT_HEADERS,
20
+ body: JSON.stringify(state),
21
+ });
22
+
23
+ if (!res.ok) throw new Error(`fetcher`);
24
+
25
+ const blob = await res.blob();
26
+
27
+ const a = document.createElement('a');
28
+ document.body.appendChild(a);
29
+
30
+ const url = window.URL.createObjectURL(blob);
31
+ a.href = url;
32
+ a.download = DOWNLOADED_FILENAME;
33
+ a.click();
34
+ window.URL.revokeObjectURL(url);
35
+ a.remove();
36
+ };
37
+ }