migrate currency utils

This commit is contained in:
Ryan Vazquez
2020-10-28 10:38:34 -04:00
parent df73c0645c
commit 474345a484
9 changed files with 130 additions and 111 deletions
@@ -18,13 +18,12 @@ import React, { PropsWithChildren } from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { CostGrowth } from './CostGrowth';
import {
defaultCurrencies,
Currency,
CurrencyType,
Duration,
findAlways,
} from '../../types';
import { MockConfigProvider, MockCurrencyProvider } from '../../utils/tests';
import { defaultCurrencies, CurrencyType } from '../../utils/currency';
const engineers = findAlways(defaultCurrencies, c => c.kind === null);
const usd = findAlways(defaultCurrencies, c => c.kind === CurrencyType.USD);
@@ -41,10 +40,10 @@ const MockContext = ({
currency: Currency;
engineerCost: number;
}>) => (
<MockConfigProvider engineerCost={engineerCost}>
<MockCurrencyProvider currency={currency}>{children}</MockCurrencyProvider>
</MockConfigProvider>
);
<MockConfigProvider engineerCost={engineerCost}>
<MockCurrencyProvider currency={currency}>{children}</MockCurrencyProvider>
</MockConfigProvider>
);
describe.each`
engineerCost | ratio | amount | expected
@@ -23,7 +23,8 @@ import {
MockConfigProvider,
MockBillingDateProvider,
} from '../../utils/tests';
import { AlertCost, defaultCurrencies, findAlways } from '../../types';
import { AlertCost, findAlways } from '../../types';
import { defaultCurrencies } from '../../utils/currency';
const engineers = findAlways(defaultCurrencies, c => c.kind === null);
@@ -23,9 +23,10 @@ import React, {
} from 'react';
import { configApiRef, useApi } from '@backstage/core';
import { Config as BackstageConfig } from '@backstage/config';
import { Currency, defaultCurrencies, Icon, Metric, Product } from '../types';
import { Currency, Icon, Metric, Product } from '../types';
import { getIcon } from '../utils/navigation';
import { validateMetrics } from '../utils/config';
import { defaultCurrencies } from '../utils/currency';
/*
* Config schema 2020-10-15
@@ -20,7 +20,8 @@ import React, {
useContext,
PropsWithChildren,
} from 'react';
import { Currency, defaultCurrencies, findAlways } from '../types';
import { Currency, findAlways } from '../types';
import { defaultCurrencies } from '../utils/currency';
export type CurrencyContextProps = {
currency: Currency;
@@ -26,6 +26,8 @@ import { costInsightsApiRef } from '../api';
import { MapLoadingToProps, useLoading } from './useLoading';
import { DefaultLoadingAction, Group, Maybe } from '../types';
console.log(DefaultLoadingAction)
type GroupsProviderLoadingProps = {
dispatchLoadingGroups: (isLoading: boolean) => void;
};
@@ -13,15 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Duration } from './Duration';
import { assertNever } from './Maybe';
export enum CurrencyType {
USD = 'USD',
CarbonOffsetTons = 'CARBON_OFFSET_TONS',
Beers = 'BEERS',
IceCream = 'PINTS_OF_ICE_CREAM',
}
export interface Currency {
kind: string | null;
@@ -30,49 +21,3 @@ export interface Currency {
prefix?: string;
rate?: number;
}
export const rateOf = (cost: number, duration: Duration) => {
switch (duration) {
case Duration.P1M:
case Duration.P30D:
return cost / 12;
case Duration.P90D:
case Duration.P3M:
return cost / 4;
default:
return assertNever(duration);
}
};
export const defaultCurrencies: Currency[] = [
{
kind: null,
label: 'Engineers 🛠',
unit: 'engineer',
},
{
kind: CurrencyType.USD,
label: 'US Dollars 💵',
unit: 'dollar',
prefix: '$',
rate: 1,
},
{
kind: CurrencyType.CarbonOffsetTons,
label: 'Carbon Offset Tons ♻️⚖️s',
unit: 'carbon offset ton',
rate: 3.5,
},
{
kind: CurrencyType.Beers,
label: 'Beers 🍺',
unit: 'beer',
rate: 4.5,
},
{
kind: CurrencyType.IceCream,
label: 'Pints of Ice Cream 🍦',
unit: 'ice cream pint',
rate: 5.5,
},
];
@@ -15,50 +15,3 @@
*/
export type Loading = Record<string, boolean>;
export enum DefaultLoadingAction {
UserGroups = 'user-groups',
LastCompleteBillingDate = 'billing-date',
CostInsightsInitial = 'cost-insights-initial',
CostInsightsPage = 'cost-insights-page',
}
export const INITIAL_LOADING_ACTIONS = [
DefaultLoadingAction.UserGroups,
DefaultLoadingAction.CostInsightsInitial,
];
export const getDefaultState = (loadingActions: string[]): Loading => {
return loadingActions.reduce(
(defaultState, action) => ({ ...defaultState, [action]: true }),
{},
);
};
export const getResetState = (loadingActions: string[]): Loading => {
return loadingActions.reduce(
(defaultState, action) => ({ ...defaultState, [action]: false }),
{},
);
};
export const getResetStateWithoutInitial = (
loadingActions: string[],
): Loading => {
return loadingActions.reduce((defaultState, action) => {
const loadingActionState = (INITIAL_LOADING_ACTIONS as string[]).includes(
action,
)
? false
: true;
return { ...defaultState, [action]: loadingActionState };
}, {});
};
export function getLoadingActions(products: string[]): string[] {
return ([
DefaultLoadingAction.UserGroups,
DefaultLoadingAction.CostInsightsInitial,
DefaultLoadingAction.CostInsightsPage,
] as string[]).concat(products);
}
@@ -0,0 +1,69 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assertNever, Currency, Duration } from '../types';
export enum CurrencyType {
USD = 'USD',
CarbonOffsetTons = 'CARBON_OFFSET_TONS',
Beers = 'BEERS',
IceCream = 'PINTS_OF_ICE_CREAM',
}
export const rateOf = (cost: number, duration: Duration) => {
switch (duration) {
case Duration.P1M:
case Duration.P30D:
return cost / 12;
case Duration.P90D:
case Duration.P3M:
return cost / 4;
default:
return assertNever(duration);
}
};
export const defaultCurrencies: Currency[] = [
{
kind: null,
label: 'Engineers 🛠',
unit: 'engineer',
},
{
kind: CurrencyType.USD,
label: 'US Dollars 💵',
unit: 'dollar',
prefix: '$',
rate: 1,
},
{
kind: CurrencyType.CarbonOffsetTons,
label: 'Carbon Offset Tons ♻️⚖️s',
unit: 'carbon offset ton',
rate: 3.5,
},
{
kind: CurrencyType.Beers,
label: 'Beers 🍺',
unit: 'beer',
rate: 4.5,
},
{
kind: CurrencyType.IceCream,
label: 'Pints of Ice Cream 🍦',
unit: 'ice cream pint',
rate: 5.5,
},
];
@@ -0,0 +1,48 @@
import { Loading } from '../types';
export enum DefaultLoadingAction {
UserGroups = 'user-groups',
LastCompleteBillingDate = 'billing-date',
CostInsightsInitial = 'cost-insights-initial',
CostInsightsPage = 'cost-insights-page',
}
export const INITIAL_LOADING_ACTIONS = [
DefaultLoadingAction.UserGroups,
DefaultLoadingAction.CostInsightsInitial,
];
export const getDefaultState = (loadingActions: string[]): Loading => {
return loadingActions.reduce(
(defaultState, action) => ({ ...defaultState, [action]: true }),
{},
);
};
export const getResetState = (loadingActions: string[]): Loading => {
return loadingActions.reduce(
(defaultState, action) => ({ ...defaultState, [action]: false }),
{},
);
};
export const getResetStateWithoutInitial = (
loadingActions: string[],
): Loading => {
return loadingActions.reduce((defaultState, action) => {
const loadingActionState = (INITIAL_LOADING_ACTIONS as string[]).includes(
action,
)
? false
: true;
return { ...defaultState, [action]: loadingActionState };
}, {});
};
export function getLoadingActions(products: string[]): string[] {
return ([
DefaultLoadingAction.UserGroups,
DefaultLoadingAction.CostInsightsInitial,
DefaultLoadingAction.CostInsightsPage,
] as string[]).concat(products);
}