2022-02-04 10:36:08 +01:00
|
|
|
import { weightTypes } from '../feature/FeatureView/FeatureVariants/FeatureVariantsList/AddFeatureVariant/enums';
|
2021-06-07 10:29:08 +02:00
|
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
2020-08-03 13:33:16 +02:00
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
const dateTimeOptions = {
|
|
|
|
day: '2-digit',
|
|
|
|
month: '2-digit',
|
|
|
|
year: 'numeric',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
|
|
|
second: '2-digit',
|
2018-02-07 14:25:02 +01:00
|
|
|
};
|
2020-09-26 22:14:56 +02:00
|
|
|
|
|
|
|
const dateOptions = {
|
|
|
|
day: '2-digit',
|
|
|
|
month: '2-digit',
|
|
|
|
year: 'numeric',
|
|
|
|
};
|
|
|
|
|
2021-07-16 15:41:54 +02:00
|
|
|
export const filterByFlags = flags => r => {
|
|
|
|
if (r.flag && !flags[r.flag]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-02-26 10:42:34 +01:00
|
|
|
export const scrollToTop = () => {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
};
|
|
|
|
|
2018-02-08 12:57:36 +01:00
|
|
|
export const formatFullDateTimeWithLocale = (v, locale, tz) => {
|
|
|
|
if (tz) {
|
|
|
|
dateTimeOptions.timeZone = tz;
|
|
|
|
}
|
2018-02-07 14:25:02 +01:00
|
|
|
return new Date(v).toLocaleString(locale, dateTimeOptions);
|
2017-08-28 19:15:47 +02:00
|
|
|
};
|
2020-02-27 21:36:07 +01:00
|
|
|
|
2020-09-26 22:14:56 +02:00
|
|
|
export const formatDateWithLocale = (v, locale, tz) => {
|
|
|
|
if (tz) {
|
|
|
|
dateTimeOptions.timeZone = tz;
|
|
|
|
}
|
|
|
|
return new Date(v).toLocaleString(locale, dateOptions);
|
|
|
|
};
|
|
|
|
|
2020-02-27 21:36:07 +01:00
|
|
|
export const trim = value => {
|
|
|
|
if (value && value.trim) {
|
|
|
|
return value.trim();
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
2020-05-20 16:32:29 +02:00
|
|
|
|
|
|
|
export function updateWeight(variants, totalWeight) {
|
2022-02-04 10:36:08 +01:00
|
|
|
if (variants.length === 0) {
|
2021-10-28 13:32:29 +02:00
|
|
|
return [];
|
|
|
|
}
|
2020-08-03 13:33:16 +02:00
|
|
|
const variantMetadata = variants.reduce(
|
|
|
|
({ remainingPercentage, variableVariantCount }, variant) => {
|
|
|
|
if (variant.weight && variant.weightType === weightTypes.FIX) {
|
|
|
|
remainingPercentage -= Number(variant.weight);
|
|
|
|
} else {
|
|
|
|
variableVariantCount += 1;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
remainingPercentage,
|
|
|
|
variableVariantCount,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
{ remainingPercentage: totalWeight, variableVariantCount: 0 }
|
|
|
|
);
|
|
|
|
|
|
|
|
const { remainingPercentage, variableVariantCount } = variantMetadata;
|
|
|
|
|
|
|
|
if (remainingPercentage < 0) {
|
|
|
|
throw new Error('The traffic distribution total must equal 100%');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!variableVariantCount) {
|
2021-10-08 11:23:29 +02:00
|
|
|
throw new Error('There must be at least one variable variant');
|
2020-08-03 13:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const percentage = parseInt(remainingPercentage / variableVariantCount);
|
2020-05-20 16:32:29 +02:00
|
|
|
|
2020-08-03 13:33:16 +02:00
|
|
|
return variants.map(variant => {
|
|
|
|
if (variant.weightType !== weightTypes.FIX) {
|
|
|
|
variant.weight = percentage;
|
|
|
|
}
|
|
|
|
return variant;
|
2020-05-20 16:32:29 +02:00
|
|
|
});
|
|
|
|
}
|
2020-11-27 22:23:44 +01:00
|
|
|
|
2021-01-07 13:52:55 +01:00
|
|
|
export const modalStyles = {
|
|
|
|
overlay: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0.25)',
|
|
|
|
zIndex: 5,
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
width: '500px',
|
|
|
|
maxWidth: '90%',
|
|
|
|
margin: '0',
|
|
|
|
top: '50%',
|
|
|
|
left: '50%',
|
|
|
|
right: 'auto',
|
|
|
|
bottom: 'auto',
|
|
|
|
transform: 'translate(-50%, -50%)',
|
|
|
|
},
|
|
|
|
};
|
2021-02-09 10:14:04 +01:00
|
|
|
|
2022-02-09 14:06:20 +01:00
|
|
|
export const showPnpsFeedback = (feedbackList) => {
|
|
|
|
if (!feedbackList) return;
|
|
|
|
if (feedbackList.length > 0) {
|
|
|
|
const feedback = feedbackList.find(
|
|
|
|
feedback => feedback.feedbackId === PNPS_FEEDBACK_ID
|
2021-06-07 10:29:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!feedback) return false;
|
|
|
|
|
|
|
|
if (feedback.neverShow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feedback.given) {
|
|
|
|
const SIX_MONTHS_IN_DAYS = 182;
|
|
|
|
const now = new Date();
|
|
|
|
const difference = differenceInDays(now, new Date(feedback.given));
|
|
|
|
|
|
|
|
return difference > SIX_MONTHS_IN_DAYS;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2022-02-09 14:06:20 +01:00
|
|
|
|
|
|
|
export const PNPS_FEEDBACK_ID = 'pnps'
|