Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Improve readablility of isDataValid(data: FilterProps) #157

Closed
kryr34 opened this issue Jan 9, 2024 · 0 comments
Closed

Improve readablility of isDataValid(data: FilterProps) #157

kryr34 opened this issue Jan 9, 2024 · 0 comments
Assignees
Labels
frontend refactor Something is messed up, or fixing something from mess.

Comments

@kryr34
Copy link
Contributor

kryr34 commented Jan 9, 2024

the point is the code is too unreadable, you don't even notice there's a duplicated if (data.minPrice !== null && parseInt(data.minPrice.toString()) < 0)

Originally posted by @kryr34 in #134 (comment)

const isValidNumber = (value: string | number | null | undefined): boolean => {
return typeof value === 'number' || (typeof value === 'string' && !isNaN(parseInt(value)));
};
const isInteger = (value: string | number | null | undefined): boolean => {
return typeof value === 'number' || (typeof value === 'string' && /^-?\d+$/.test(value));
};
const isEmpty = (value: string | number | null | undefined): boolean => {
return value === '' || value === null ? true : false;
};
const toNumber = (input: string | null) => {
if (!input) return null;
const output = Number(input);
return isNaN(output) ? null : output;
};
const toSortBy = (input: string | null) => {
switch (input) {
case 'price':
return 'price';
case 'stock':
return 'stock';
case 'sales':
return 'sales';
case 'relevancy':
return 'relevancy';
default:
return null;
}
};
const toOrderBy = (input: string | null) => {
switch (input) {
case 'asc':
return 'asc';
case 'desc':
return 'desc';
default:
return null;
}
};
// eslint-disable-next-line react-refresh/only-export-components
export const isDataValid = (data: FilterProps) => {
if (typeof data.maxPrice !== 'number' && data.maxPrice !== null && data.maxPrice !== '') {
if (!isValidNumber(data.maxPrice)) {
alert('Please enter numbers for max price');
return false;
}
}
if (typeof data.minPrice !== 'number' && data.minPrice !== null && data.minPrice !== '') {
if (!isValidNumber(data.minPrice)) {
alert('Please enter numbers for min price');
return false;
}
}
if (typeof data.maxStock !== 'number' && data.maxStock !== null && data.maxStock !== '') {
if (!isValidNumber(data.maxStock)) {
alert('Please enter numbers for max stock');
return false;
}
}
if (typeof data.minStock !== 'number' && data.minStock !== null && data.minStock !== '') {
if (!isValidNumber(data.minStock)) {
alert('Please enter numbers for min stock');
return false;
}
}
if (
(!isInteger(data.maxPrice) && data.maxPrice !== null && data.maxPrice.toString() !== '') ||
(!isInteger(data.minPrice) && data.minPrice !== null && data.minPrice.toString() !== '') ||
(!isInteger(data.maxStock) && data.maxStock !== null && data.maxStock.toString() !== '') ||
(!isInteger(data.minStock) && data.minStock !== null && data.minStock.toString() !== '')
) {
alert("can't enter float numbers!");
return false;
}
if (data.minPrice !== null && parseInt(data.minPrice.toString()) < 0) {
alert("min price can't negative numbers");
return false;
}
if (data.maxPrice !== null && parseInt(data.maxPrice.toString()) < 0) {
alert("max price can't negative numbers");
return false;
}
if (data.minStock !== null && parseInt(data.minStock.toString()) < 0) {
alert("min stock can't negative numbers");
return false;
}
if (data.maxStock !== null && parseInt(data.maxStock.toString()) < 0) {
alert("max stock can't negative numbers");
return false;
}
if (data.minPrice !== null && parseInt(data.minPrice.toString()) < 0) {
alert("min price can't negative numbers");
return false;
}
if (data.maxPrice !== null && parseInt(data.maxPrice.toString()) < 0) {
alert("max price can't negative numbers");
return false;
}
if (
data.maxPrice !== null &&
data.minPrice !== null &&
parseInt(data.maxPrice.toString()) < parseInt(data.minPrice.toString())
) {
alert("min price can't bigger than max value");
return false;
}
if (
data.maxStock !== null &&
data.minStock !== null &&
parseInt(data.maxStock.toString()) < parseInt(data.minStock.toString())
) {
alert("min stock can't bigger than max value");
return false;
}
if (isEmpty(data.maxPrice) != isEmpty(data.minPrice)) {
alert('max and min price should both have values or both have no values at the same time');
return false;
}
if (isEmpty(data.maxStock) != isEmpty(data.minStock)) {
alert('max and min stock should both have values or both have no values at the same time');
return false;
}
return true;
};

@kryr34 kryr34 added frontend refactor Something is messed up, or fixing something from mess. labels Jan 9, 2024
@kryr34 kryr34 mentioned this issue Jan 9, 2024
3 tasks
@kryr34 kryr34 changed the title improve readable improve readablility isDataValid(data: FilterProps) Jan 9, 2024
@kryr34 kryr34 changed the title improve readablility isDataValid(data: FilterProps) improve readablility of isDataValid(data: FilterProps) Jan 9, 2024
@kryr34 kryr34 changed the title improve readablility of isDataValid(data: FilterProps) Improve readablility of isDataValid(data: FilterProps) Jan 9, 2024
@kryr34 kryr34 added this to the Final Delivery milestone Jan 9, 2024
@KatoToyoki KatoToyoki mentioned this issue Jan 10, 2024
8 tasks
@KatoToyoki KatoToyoki linked a pull request Jan 10, 2024 that will close this issue
8 tasks
@KatoToyoki KatoToyoki removed a link to a pull request Jan 10, 2024
8 tasks
@UGisBusy UGisBusy closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
frontend refactor Something is messed up, or fixing something from mess.
Projects
Status: ✅ Done
Development

No branches or pull requests

3 participants