Skip to content

Commit

Permalink
Merge pull request #224 from Lapkipomoshi/dev-payment
Browse files Browse the repository at this point in the history
Dev payment
  • Loading branch information
Notsmartname committed May 10, 2024
2 parents a4cb500 + d8dffbd commit 8a8303b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
8 changes: 7 additions & 1 deletion src/hooks/useValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const useValidation = (value, validations, errorValidateMessage) => {
const [minLengthError, setMinLengthError] = useState(false);
const [maxLengthError, setMaxLengthError] = useState(false);
const [regexError, setRegexError] = useState(false);
const [isZeroError, setIsZeroError] = useState(false);

useEffect(() => {
if (!validations.notEmpty && !value) {
Expand All @@ -28,6 +29,9 @@ const useValidation = (value, validations, errorValidateMessage) => {
case 'regex':
validations[validation].test(value) ? setRegexError(false) : setRegexError(true);
break;
case 'isZero':
parseInt(value, 10) === 0 ? setIsZeroError(true) : setIsZeroError(false);
break;
default:
break;
}
Expand All @@ -43,10 +47,12 @@ const useValidation = (value, validations, errorValidateMessage) => {
setErrorText(errorValidateMessage.TOO_LONG);
} else if (regexError) {
setErrorText(errorValidateMessage.INVALID);
} else if (isZeroError) {
setErrorText(errorValidateMessage.IS_ZERO);
} else {
setErrorText('');
}
}, [emptyError, minLengthError, maxLengthError, regexError]);
}, [emptyError, minLengthError, maxLengthError, regexError, isZeroError]);

return errorText;
};
Expand Down
2 changes: 1 addition & 1 deletion src/modules/HelpToShelter/ApiHelpToShelter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const donateToShelter = async (id, amount) => {
const response = await fetch(`${baseUrl}/v1/payments/shelters/${id}/donate/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({
amount
Expand Down
32 changes: 15 additions & 17 deletions src/modules/HelpToShelter/HelpToShelter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import * as errorMessage from '../../utils/errorMessage';
import useInput from '../../hooks/useInput';
import { donateToShelter } from './ApiHelpToShelter';


const HelpToShelter = () => {
const { id } = useParams();
const materialAid = useInput('', { notEmpty: true, maxLength: 12, regex: regex.NUMBER }, errorMessage.DONATION_AMOUNT);
const { isOwner, isAuth, isShelterOwner } = useOutletContext();

const materialAid = useInput('', { notEmpty: true, maxLength: 12, regex: regex.NUMBER, isZero: true }, errorMessage.DONATION_AMOUNT);
const { shelter } = useOutletContext();
const handleDonate = async () => {
if (materialAid.value !== '') {
const donationAmount = parseInt(materialAid.value, 10);
const donationAmount = parseInt(materialAid.value, 10);
if ((donationAmount > 0) && (materialAid.value.length < 12)) {
try {
const paymentConfirmUrl = await donateToShelter(id, donationAmount);
window.open(paymentConfirmUrl, '_blank');
Expand All @@ -27,23 +27,21 @@ const HelpToShelter = () => {

return (
<section className='shelter-section help-to-shelter'>
{ !isOwner && (
{shelter.is_partner && (
<div className='help-to-shelter__mat'>
<h2 className='shelter-section__title help-to-shelter__title'>Материальная помощь</h2>
{ (isShelterOwner || isAuth) && (isAuth || isShelterOwner) && (
<DeclarationInput
caption='Какую сумму вы хотите пожертвовать?'
inputState={materialAid}
type='number'
name='salaryInput'
required
placeholder='1000'
/>
)}
<DeclarationInput
caption='Какую сумму вы хотите пожертвовать?'
inputState={materialAid}
type='number'
name='salaryInput'
required
placeholder='1000'
/>
<Button
className='help-to-shelter__button'
onClick={handleDonate}
disabled={(!isAuth && !isShelterOwner) || materialAid.value === ''}
disabled={materialAid.value === ''}
>
Пожертвовать деньги
</Button>
Expand Down
5 changes: 3 additions & 2 deletions src/ui/DeclarationInput/DeclarationInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react';
import './DeclarationInput.scss';

// TODO убрать li, это форма, а не список. Выше, где используется, заменить ul на form
// li изменено на div
const DeclarationInput = ({ caption, inputState, type, name, placeholder, required, showError = true }) => {
return (
<li className='declaration-input'>
<div className='declaration-input'>
<label className='declaration-input__caption' htmlFor={name}>
{caption}
</label>
Expand All @@ -20,7 +21,7 @@ const DeclarationInput = ({ caption, inputState, type, name, placeholder, requir
required={required}
/>
{showError && <p className='declaration-input__error'>{inputState.dirty && inputState.invalidText}</p>}
</li>
</div>
);
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/errorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ export const VACANCY_DESCRIPTION = {
export const DONATION_AMOUNT = {
TOO_LONG: tooLong,
NOT_FOUND: 'Введите сумму пожертвования',
IS_ZERO: 'Cумма должно превышать нулевое значение',
};

0 comments on commit 8a8303b

Please sign in to comment.