2020-08-03 13:33:16 +02:00
|
|
|
import { weightTypes } from '../feature/variant/enums';
|
|
|
|
|
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-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) {
|
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) {
|
|
|
|
throw new Error('There must be atleast one variable variant');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
export function loadNameFromHash() {
|
|
|
|
let field = '';
|
|
|
|
try {
|
|
|
|
[, field] = document.location.hash.match(/name=([a-z0-9-_.]+)/i);
|
|
|
|
} catch (e) {
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
}
|
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
|
|
|
|
2021-05-07 08:31:25 +02:00
|
|
|
export const updateIndexInArray = (array, index, newValue) =>
|
|
|
|
array.map((v, i) => (i === index ? newValue : v));
|