Explorar el Código

added helper functions

pull/1/head
Lazar Kostic hace 3 años
padre
commit
d1a5fc62a4

+ 3
- 0
package.json Ver fichero

@@ -11,6 +11,9 @@
"@types/node": "^16.7.13",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"axios": "^1.1.3",
"date-fns": "^2.29.3",
"i18next": "^22.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",

+ 1
- 0
src/constants/sessionStorage.ts Ver fichero

@@ -0,0 +1 @@
export const SESSION_STORAGE_SCOPE = 'SessionStorageScope';

+ 40
- 0
src/util/helpers/authScopeHelpers.ts Ver fichero

@@ -0,0 +1,40 @@
import { SESSION_STORAGE_SCOPE } from '../../constants/sessionStorage';

export function authScopeGetHelper(key: string) {
if (sessionStorage.getItem(SESSION_STORAGE_SCOPE)) {
return JSON.parse(sessionStorage.getItem(key)!);
}

return JSON.parse(localStorage.getItem(key)!);
}

export function authScopeStringGetHelper(key: string) {
if (sessionStorage.getItem(SESSION_STORAGE_SCOPE)) {
return sessionStorage.getItem(key);
}

return localStorage.getItem(key);
}

export function authScopeSetHelper(key: string, value: string) {
if (sessionStorage.getItem(SESSION_STORAGE_SCOPE)) {
sessionStorage.setItem(key, value);
} else {
localStorage.setItem(key, value);
}
}

export function authScopeRemoveHelper(key: string) {
if (sessionStorage.getItem(SESSION_STORAGE_SCOPE)) {
sessionStorage.removeItem(key);
} else {
localStorage.removeItem(key);
}
}

export function authScopeClearHelper() {
sessionStorage.clear();
if (!sessionStorage.getItem(SESSION_STORAGE_SCOPE)) {
localStorage.clear();
}
}

+ 44
- 0
src/util/helpers/dateHelpers.ts Ver fichero

@@ -0,0 +1,44 @@
import { format } from 'date-fns';
import { enUS } from 'date-fns/locale';
import i18next from 'i18next';

export function formatDate(date: string, fmt: string = 'MM/dd/y', locale = enUS) {
const dt = new Date(date);
return format(dt, fmt, { locale });
}

export function formatDateTime(date: string) {
const dt = new Date(date);
return format(dt, 'MM/dd/y hh:mm aa');
}

export function getDateDay(date: string) {
const dt = new Date(date);
return format(dt, 'dd');
}

export function getDateMonth(date: string) {
const dt = new Date(date);
return format(dt, 'MM');
}

export function getDateYear(date: string) {
const dt = new Date(date);
return format(dt, 'y');
}

export function formatDateTimeLocale(date: string) {
const dt = new Date(date);
return format(dt, 'MM/dd/y hh:mm aa');
}

interface Dates {
start: string;
end: string;
}
// TODO add locale
export function formatDateRange(dates: Dates) {
const start = formatDate(dates.start);
const end = formatDate(dates.end);
return i18next.t('common.date.range', { start, end });
}

+ 1
- 0
src/util/helpers/enumMappers.ts Ver fichero

@@ -0,0 +1 @@
export const parseEnumType = (typeArray: Array<number>, index: number) => typeArray[index - 1];

+ 65
- 0
src/util/helpers/randomData.ts Ver fichero

@@ -0,0 +1,65 @@
const random = (arr: Array<string>) => {
return arr[Math.floor(Math.random() * arr.length)];
};

const size = () => {
return random(['Extra Small', 'Small', 'Medium', 'Large', 'Extra Large']);
};

const color = () => {
return random(['Red', 'Green', 'Blue', 'Orange', 'Yellow']);
};

const designer = () => {
return random([
'Ralph Lauren',
'Alexander Wang',
'Grayse',
'Marc NY Performance',
'Scrapbook',
'J Brand Ready to Wear',
'Vintage Havana',
'Neiman Marcus Cashmere Collection',
'Derek Lam 10 Crosby',
'Jordan',
]);
};

const type = () => {
return random([
'Cashmere',
'Cardigans',
'Crew and Scoop',
'V-Neck',
'Shoes',
'Cowl & Turtleneck',
]);
};

const price = () => {
return (Math.random() * 100).toFixed(2);
};

function generate(count: number) {
const data = [];
for (let i = 0; i < count; i++) {
const currentColor = color();
const currentSize = size();
const currentType = type();
const currentDesigner = designer();
const currentPrice = price();

data.push({
name: `${currentDesigner} ${currentType} ${currentColor} ${currentSize}`,
color: currentColor,
size: currentSize,
designer: currentDesigner,
type: currentType,
price: currentPrice,
salesPrice: currentPrice
});
}
return data;
}

export default generate;

+ 19
- 0
src/util/helpers/rejectErrorCodeHelper.ts Ver fichero

@@ -0,0 +1,19 @@
import i18next from 'i18next';
import { AxiosError } from 'axios';

interface ErrorResponse {
Errors: { Code: string }[]
}

export const rejectErrorCodeHelper = (error: AxiosError<ErrorResponse>) => {
if (error?.response?.data?.Errors) {
const errorCode = error?.response?.data?.Errors[0]?.Code;
const errorMessage = errorCode
? i18next.t(`apiErrors.${errorCode}`)
: i18next.t('apiErrors.SomethingWentWrong');

return errorMessage;
}

return i18next.t('apiErrors.SomethingWentWrong');
};

+ 11
- 0
src/util/helpers/stringHelpers.ts Ver fichero

@@ -0,0 +1,11 @@
export function separateByUppercase(string: string) {
return string.split(/(?=[A-Z])/).join(' ');
}

export function separateByUnderscore(string: string) {
return string.replaceAll('_', ' ');
}

export function joinArrayWithComma(arr: Array<string>) {
return arr.join(', ');
}

+ 37
- 2
yarn.lock Ver fichero

@@ -1033,7 +1033,7 @@
core-js-pure "^3.25.1"
regenerator-runtime "^0.13.10"

"@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.16.3", "@babel/runtime@^7.18.9", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
"@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.16.3", "@babel/runtime@^7.17.2", "@babel/runtime@^7.18.9", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
version "7.20.1"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.1.tgz#1148bb33ab252b165a06698fde7576092a78b4a9"
integrity sha512-mrzLkl6U9YLF8qpqI7TB82PESyEGjm/0Ly91jG575eVxMMlb8fYfOXFZIJ8XfLrJZQbm7dlKry2bJmXBUEkdFg==
@@ -2656,6 +2656,15 @@ axe-core@^4.4.3:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.5.1.tgz#04d561c11b6d76d096d34e9d14ba2c294fb20cdc"
integrity sha512-1exVbW0X1O/HSr/WMwnaweyqcWOgZgLiVxdLG34pvSQk4NlYQr9OUy0JLwuhFfuVNQzzqgH57eYzkFBCb3bIsQ==

axios@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.1.3.tgz#8274250dada2edf53814ed7db644b9c2866c1e35"
integrity sha512-00tXVRwKx/FZr/IDVFt4C+f9FYairX517WoGCL6dpOntqLkZofjhu43F/Xl44UOpqa+9sLFDrG/XAnFsUYgkDA==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axobject-query@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
@@ -3490,6 +3499,11 @@ data-urls@^2.0.0:
whatwg-mimetype "^2.3.0"
whatwg-url "^8.0.0"

date-fns@^2.29.3:
version "2.29.3"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"
integrity sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==

debug@2.6.9, debug@^2.6.0, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -4440,7 +4454,7 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

follow-redirects@^1.0.0:
follow-redirects@^1.0.0, follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
@@ -4480,6 +4494,15 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

forwarded@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
@@ -4881,6 +4904,13 @@ human-signals@^2.1.0:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==

i18next@^22.0.4:
version "22.0.4"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-22.0.4.tgz#77d8871687b0ab072b38991e3887187823667e30"
integrity sha512-TOp7BTMKDbUkOHMzDlVsCYWpyaFkKakrrO3HNXfSz4EeJaWwnBScRmgQSTaWHScXVHBUFXTvShrCW8uryBYFcg==
dependencies:
"@babel/runtime" "^7.17.2"

iconv-lite@0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
@@ -7303,6 +7333,11 @@ proxy-addr@~2.0.7:
forwarded "0.2.0"
ipaddr.js "1.9.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

psl@^1.1.33:
version "1.9.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.9.0.tgz#d0df2a137f00794565fcaf3b2c00cd09f8d5a5a7"

Cargando…
Cancelar
Guardar