Explorar el Código

npm compile refine

CR026
Jason Chuang hace 2 días
padre
commit
394ce437f9
Se han modificado 3 ficheros con 197 adiciones y 194 borrados
  1. +147
    -133
      src/utils/HttpUtils.js
  2. +49
    -46
      src/utils/registerValidation.js
  3. +1
    -15
      src/utils/sanitizeHtml.js

+ 147
- 133
src/utils/HttpUtils.js Ver fichero

@@ -1,174 +1,188 @@
import axios from "axios";
import { FILE_UP_POST, FILE_DOWN_GET } from "../utils/ApiPathConst";
import axios from 'axios';
import { FILE_UP_POST, FILE_DOWN_GET } from '../utils/ApiPathConst';
import qs from 'qs';

export const get = ({ url, params, onSuccess, onFail, onError, onFinally }) => {
axios.get(url, {
params,
paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }) // <-- FIX
}).then(
(response) => { onResponse(response, onSuccess, onFail); }
).catch((error) => {
return handleError(error, onError);
}).finally(() => {
if (typeof onFinally === 'function') {
onFinally();
}
axios
.get(url, {
params,
paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }) // <-- FIX
})
.then((response) => {
onResponse(response, onSuccess, onFail);
})
.catch((error) => {
return handleError(error, onError);
})
.finally(() => {
if (typeof onFinally === 'function') {
onFinally();
}
});
};

//TODO
export const put = ({ url, params, onSuccess, onFail, onError }) => {
axios.put(url, params).then(
(response) => { onResponse(response, onSuccess, onFail); }
).catch(error => {
return handleError(error, onError);
axios
.put(url, params)
.then((response) => {
onResponse(response, onSuccess, onFail);
})
.catch((error) => {
return handleError(error, onError);
});
};

export const patch = ({ url, params, onSuccess, onFail, onError }) => {
axios.patch(url, params).then(
(response) => { onResponse(response, onSuccess, onFail); }
).catch(error => {
return handleError(error, onError);
axios
.patch(url, params)
.then((response) => {
onResponse(response, onSuccess, onFail);
})
.catch((error) => {
return handleError(error, onError);
});
};

export const del = ({ url, params, onSuccess, onFail, onError }) => {
axios.delete(url, { params }).then(
(response) => { onResponse(response, onSuccess, onFail); }
).catch(error => {
return handleError(error, onError);
axios
.delete(url, { params })
.then((response) => {
onResponse(response, onSuccess, onFail);
})
.catch((error) => {
return handleError(error, onError);
});
};

export const post = ({ url, params, onSuccess, onFail, onError, headers }) => {
headers = headers ? headers : {
"Content-Type": "application/json"
};

axios.post(url, params,
{
headers: headers
}).then(
(response) => { onResponse(response, onSuccess, onFail); }
).catch(error => {
return handleError(error, onError);
});
headers = headers
? headers
: {
'Content-Type': 'application/json'
};

axios
.post(url, params, {
headers: headers
})
.then((response) => {
onResponse(response, onSuccess, onFail);
})
.catch((error) => {
return handleError(error, onError);
});
};

export const postWithFiles = ({ url, params, files, onSuccess, onFail, onError }) => {
var formData = new FormData();
for (let i = 0; i < files.length; i++) {
const file = files[i]
formData.append("multipartFileList", file);
var formData = new FormData();
for (let i = 0; i < files.length; i++) {
const file = files[i];
formData.append('multipartFileList', file);
}
if (params)
for (var key in params) {
if (typeof params[key] === 'object') {
formData.append(key, JSON.stringify(params[key]));
} else {
formData.append(key, params[key]);
}
}
if (params)
for (var key in params) {
if (typeof (params[key]) === 'object') {
formData.append(key, JSON.stringify(params[key]));
} else {
formData.append(key, params[key]);
}
}

axios.post(url, formData,
{ headers: { "Content-Type": "multipart/form-data" } })
.then(
(response) => { onResponse(response, onSuccess, onFail); }
).catch(error => {
return handleError(error, onError);
});
axios
.post(url, formData, { headers: { 'Content-Type': 'multipart/form-data' } })
.then((response) => {
onResponse(response, onSuccess, onFail);
})
.catch((error) => {
return handleError(error, onError);
});
};

export const fileDownload = ({ url, fileId, skey, params, method, onResponse, onError }) => {
if (!url) {
url = FILE_DOWN_GET + "/" + fileId + "/" + skey
}
if (method == 'post') {
axios.post(url, params,
{
responseType: 'blob',
headers: {
"Content-Type": "application/json"
}
}
).then(
(response) => {
fileDownloadResponse(response, onResponse)
}
).catch(error => {
return handleError(error, onError);
});

} else {
axios.get(url,
{
responseType: 'blob',
params: params
}
).then(
(response) => {
fileDownloadResponse(response, onResponse)
}
).catch(error => {
return handleError(error, onError);
});
}
if (!url) {
url = FILE_DOWN_GET + '/' + fileId + '/' + skey;
}
if (method == 'post') {
axios
.post(url, params, {
responseType: 'blob',
headers: {
'Content-Type': 'application/json'
}
})
.then((response) => {
fileDownloadResponse(response, onResponse);
})
.catch((error) => {
return handleError(error, onError);
});
} else {
axios
.get(url, {
responseType: 'blob',
params: params
})
.then((response) => {
fileDownloadResponse(response, onResponse);
})
.catch((error) => {
return handleError(error, onError);
});
}
};

const fileDownloadResponse = (response, onResponse) => {
const cd = response.headers?.['content-disposition'];
const fn = cd?.split('filename=')[1]?.replaceAll('"','')?.trim() || 'export.xlsx';
const url = URL.createObjectURL(response.data);
const a = document.createElement('a');
a.href = url;
a.download = fn;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
onResponse?.();
const cd = response.headers?.['content-disposition'];
const fn = cd?.split('filename=')[1]?.replaceAll('"', '')?.trim() || 'export.xlsx';
const url = URL.createObjectURL(response.data);
const a = document.createElement('a');
a.href = url;
a.download = fn;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
onResponse?.();
};

export const fileUpload = ({ refType, refId, files, refCode, onSuccess, onFail, onError }) => {
postWithFiles({
url: FILE_UP_POST,
params: {
refType: refType,
refId: refId,
refCode: refCode
},
files: files,
onSuccess: onSuccess,
onFail: onFail,
onError: onError
});
postWithFiles({
url: FILE_UP_POST,
params: {
refType: refType,
refId: refId,
refCode: refCode
},
files: files,
onSuccess: onSuccess,
onFail: onFail,
onError: onError
});
};


const onResponse = (response, onSuccess, onFail) => {
if (response.status >= 300 || response.status < 200) {
console.log("onFail");
if (onFail) {
onFail(response);
} else {
console.log(response);
}
return;
if (response.status >= 300 || response.status < 200) {
console.log('onFail');
if (onFail) {
onFail(response);
} else {
console.log(response);
}
return;
}

if (onSuccess) {
onSuccess(response?.data);
}
}
if (onSuccess) {
onSuccess(response?.data);
}
};

const handleError = (error, onError) => {
if (onError) {
return onError(error);
} else {
// console.log(error);
return false;
}
}
if (onError) {
return onError(error);
} else {
// console.log(error);
return false;
}
};

+ 49
- 46
src/utils/registerValidation.js Ver fichero

@@ -1,18 +1,18 @@
export const ADDRESS_LINE_MAX_LENGTH = 40;

export function isAddressLineWithinLimit(line, maxLen = ADDRESS_LINE_MAX_LENGTH) {
if (line == null || line === '') return true;
return line.length <= maxLen;
if (line == null || line === '') return true;
return line.length <= maxLen;
}

/** Step-0 gate: line1 required; lines 1–3 each <= maxLen */
export function isRegisterAddressValid(data, maxLen = ADDRESS_LINE_MAX_LENGTH) {
return (
data.address1 !== '' &&
isAddressLineWithinLimit(data.address1, maxLen) &&
isAddressLineWithinLimit(data.address2, maxLen) &&
isAddressLineWithinLimit(data.address3, maxLen)
);
return (
data.address1 !== '' &&
isAddressLineWithinLimit(data.address1, maxLen) &&
isAddressLineWithinLimit(data.address2, maxLen) &&
isAddressLineWithinLimit(data.address3, maxLen)
);
}

/**
@@ -20,48 +20,51 @@ export function isRegisterAddressValid(data, maxLen = ADDRESS_LINE_MAX_LENGTH) {
* Packs at comma boundaries where possible.
*/
export function splitAddressIntoLines(address, maxLen = ADDRESS_LINE_MAX_LENGTH) {
const empty = { address1: '', address2: '', address3: '' };
if (address == null || address === '') return empty;
const empty = { address1: '', address2: '', address3: '' };
if (address == null || address === '') return empty;

const trimmed = address.trim();
if (trimmed.length <= maxLen) {
return { address1: trimmed, address2: '', address3: '' };
}
const trimmed = address.trim();
if (trimmed.length <= maxLen) {
return { address1: trimmed, address2: '', address3: '' };
}

const segments = trimmed.split(',').map((s) => s.trim()).filter(Boolean);
const lines = [];
let current = '';
const segments = trimmed
.split(',')
.map((s) => s.trim())
.filter(Boolean);
const lines = [];
let current = '';

const pushCurrent = () => {
if (current) {
lines.push(current);
current = '';
}
};
const pushCurrent = () => {
if (current) {
lines.push(current);
current = '';
}
};

for (const segment of segments) {
const candidate = current ? `${current}, ${segment}` : segment;
if (candidate.length <= maxLen) {
current = candidate;
} else if (segment.length <= maxLen) {
pushCurrent();
current = segment;
} else {
pushCurrent();
let remaining = segment;
while (remaining.length > maxLen) {
lines.push(remaining.slice(0, maxLen));
remaining = remaining.slice(maxLen);
}
current = remaining;
}
if (lines.length >= 3) break;
for (const segment of segments) {
const candidate = current ? `${current}, ${segment}` : segment;
if (candidate.length <= maxLen) {
current = candidate;
} else if (segment.length <= maxLen) {
pushCurrent();
current = segment;
} else {
pushCurrent();
let remaining = segment;
while (remaining.length > maxLen) {
lines.push(remaining.slice(0, maxLen));
remaining = remaining.slice(maxLen);
}
current = remaining;
}
pushCurrent();
if (lines.length >= 3) break;
}
pushCurrent();

return {
address1: lines[0] ?? '',
address2: lines[1] ?? '',
address3: lines[2] ?? '',
};
return {
address1: lines[0] ?? '',
address2: lines[1] ?? '',
address3: lines[2] ?? ''
};
}

+ 1
- 15
src/utils/sanitizeHtml.js Ver fichero

@@ -38,21 +38,7 @@ const ALLOWED_TAGS = [
'ul'
];

const ALLOWED_ATTR = [
'href',
'title',
'target',
'rel',
'src',
'alt',
'width',
'height',
'colspan',
'rowspan',
'scope',
'class',
'style'
];
const ALLOWED_ATTR = ['href', 'title', 'target', 'rel', 'src', 'alt', 'width', 'height', 'colspan', 'rowspan', 'scope', 'class', 'style'];

if (typeof window !== 'undefined') {
DOMPurify.addHook('afterSanitizeAttributes', (node) => {


Cargando…
Cancelar
Guardar