|
| 1 | +import { motion } from 'framer-motion' |
| 2 | +import useBudgetPlannerSection from './hooks/useBudgetPlannerSecion' |
| 3 | +import InputField from '../DepositSection/DepositCalculation/InputField' |
| 4 | +import { formatNumberWithCommas, parseAmountInputFromCommas } from '@/services/inputServices' |
| 5 | +import { useEffect, useState } from 'react' |
| 6 | +import NeedsSection from './NeedsSection/NeedsSection' |
| 7 | +import DepositDropdownSection from './DepositDropdownSection/DepositDropdownSection' |
| 8 | +import RecommendationSection from './RecommendationSection/RecommendationSection' |
| 9 | + |
| 10 | +const BudgetPlannerSection = () => { |
| 11 | + const { |
| 12 | + needInput, |
| 13 | + setNeedInput, |
| 14 | + priceInput, |
| 15 | + setPriceInput, |
| 16 | + handleAddNeed, |
| 17 | + needs, |
| 18 | + depositInput, |
| 19 | + setDepositInput, |
| 20 | + filteredBanks, |
| 21 | + handleBankSelection, |
| 22 | + handleRemoveNeed, |
| 23 | + recommendation |
| 24 | + } = useBudgetPlannerSection() |
| 25 | + const EMPTY_STRING = '' |
| 26 | + const [errors, setErrors] = useState({ |
| 27 | + needInput: EMPTY_STRING, |
| 28 | + priceInput: EMPTY_STRING, |
| 29 | + depositInput: EMPTY_STRING |
| 30 | + }) |
| 31 | + const [, setIsFormValid] = useState(false) |
| 32 | + |
| 33 | + const newErrors = { |
| 34 | + needInput: EMPTY_STRING, |
| 35 | + priceInput: EMPTY_STRING, |
| 36 | + depositInput: EMPTY_STRING |
| 37 | + } |
| 38 | + |
| 39 | + const isNumberAndDecimalRegex = /^\d+(\.\d+)?$/ |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + setIsFormValid( |
| 43 | + needInput !== EMPTY_STRING && priceInput !== EMPTY_STRING && depositInput !== EMPTY_STRING |
| 44 | + ) |
| 45 | + }, [needInput, priceInput, depositInput]) |
| 46 | + |
| 47 | + const containerVariants = { |
| 48 | + hidden: { opacity: 0, y: 20 }, |
| 49 | + visible: { |
| 50 | + opacity: 1, |
| 51 | + y: 0, |
| 52 | + transition: { duration: 0.8, ease: 'easeOut', staggerChildren: 0.2 } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const childVariants = { |
| 57 | + hidden: { opacity: 0, y: 20 }, |
| 58 | + visible: { opacity: 1, y: 0, transition: { duration: 0.6, ease: 'easeOut' } } |
| 59 | + } |
| 60 | + |
| 61 | + const validateDepositAmount = () => { |
| 62 | + if (!depositInput.trim()) { |
| 63 | + return 'Deposit amount is required.' |
| 64 | + } |
| 65 | + if (isNaN(Number(depositInput.replace(/,/g, '')))) { |
| 66 | + return 'Deposit amount must be numeric.' |
| 67 | + } |
| 68 | + return null |
| 69 | + } |
| 70 | + |
| 71 | + const handleInputChange = ( |
| 72 | + e: React.ChangeEvent<HTMLInputElement>, |
| 73 | + setField: (value: string) => void, |
| 74 | + fieldName: string |
| 75 | + ) => { |
| 76 | + const value = e.target.value |
| 77 | + if (fieldName === 'priceInput') { |
| 78 | + const formattedValue = formatNumberWithCommas(value) |
| 79 | + setField(formattedValue) |
| 80 | + } else if (fieldName == 'depositInput') { |
| 81 | + if (!depositInput) { |
| 82 | + newErrors.depositInput = 'Deposit is required' |
| 83 | + } else if (!isNumberAndDecimalRegex.test(parseAmountInputFromCommas(depositInput))) { |
| 84 | + newErrors.depositInput = 'Deposit should be a valid number' |
| 85 | + } |
| 86 | + const formattedValue = formatNumberWithCommas(value) |
| 87 | + setField(formattedValue) |
| 88 | + } else { |
| 89 | + setField(value) |
| 90 | + } |
| 91 | + |
| 92 | + setErrors((prev) => ({ ...prev, [fieldName]: EMPTY_STRING })) |
| 93 | + } |
| 94 | + |
| 95 | + const validateField = () => { |
| 96 | + let isValid = true |
| 97 | + |
| 98 | + if (!needInput) { |
| 99 | + newErrors.needInput = 'Need is required' |
| 100 | + isValid = false |
| 101 | + } |
| 102 | + |
| 103 | + if (!priceInput) { |
| 104 | + newErrors.priceInput = 'Price is required' |
| 105 | + isValid = false |
| 106 | + } else if (!isNumberAndDecimalRegex.test(parseAmountInputFromCommas(priceInput))) { |
| 107 | + newErrors.priceInput = 'Price should be a valid number' |
| 108 | + isValid = false |
| 109 | + } |
| 110 | + |
| 111 | + setErrors(newErrors) |
| 112 | + return isValid |
| 113 | + } |
| 114 | + |
| 115 | + const handleAddNeedWithValidation = () => { |
| 116 | + const isValid = validateField() |
| 117 | + if (isValid) { |
| 118 | + handleAddNeed(needInput, parseAmountInputFromCommas(priceInput)) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return ( |
| 123 | + <motion.div |
| 124 | + className='pt-10 pb-20 w-full' |
| 125 | + initial='hidden' |
| 126 | + whileInView='visible' |
| 127 | + viewport={{ once: true, amount: 0.2 }} |
| 128 | + variants={containerVariants} |
| 129 | + > |
| 130 | + <motion.h1 |
| 131 | + className='text-center text-2xl md:text-3xl font-bold text-charter-blue-600 mt-8 mb-4' |
| 132 | + variants={childVariants} |
| 133 | + > |
| 134 | + Budget Planner |
| 135 | + </motion.h1> |
| 136 | + |
| 137 | + <motion.p |
| 138 | + className='text-center text-charter-blue text-lg md:text-xl mx-4 md:mx-10 mb-8' |
| 139 | + variants={childVariants} |
| 140 | + > |
| 141 | + Plan your monthly expenses and see how deposit returns can help cover your needs. |
| 142 | + </motion.p> |
| 143 | + |
| 144 | + <motion.div |
| 145 | + className='card shadow-xl border-s-8 border-charter-blue mx-4 md:mx-20 lg:mx-36' |
| 146 | + variants={childVariants} |
| 147 | + > |
| 148 | + <div className='card-body'> |
| 149 | + <motion.div |
| 150 | + className='grid grid-cols-1 md:grid-cols-2 gap-4 mt-10' |
| 151 | + variants={childVariants} |
| 152 | + > |
| 153 | + <InputField |
| 154 | + label='Need' |
| 155 | + placeholder='Enter need (e.g., Rent)' |
| 156 | + type='text' |
| 157 | + value={needInput} |
| 158 | + onChange={(e) => handleInputChange(e, setNeedInput, 'needInput')} |
| 159 | + error={errors.needInput} |
| 160 | + /> |
| 161 | + |
| 162 | + <InputField |
| 163 | + label='Price' |
| 164 | + placeholder='Enter price (e.g., 1200000)' |
| 165 | + type='text' |
| 166 | + value={priceInput} |
| 167 | + onChange={(e) => handleInputChange(e, setPriceInput, 'priceInput')} |
| 168 | + error={errors.priceInput} |
| 169 | + /> |
| 170 | + |
| 171 | + <div className='col-span-1 md:col-span-2'> |
| 172 | + <button |
| 173 | + onClick={handleAddNeedWithValidation} |
| 174 | + className='btn btn-primary w-full text-[#ffffff]' |
| 175 | + > |
| 176 | + Add Need |
| 177 | + </button> |
| 178 | + </div> |
| 179 | + </motion.div> |
| 180 | + |
| 181 | + {needs.length > 0 && <NeedsSection needs={needs} onRemoveNeed={handleRemoveNeed} />} |
| 182 | + |
| 183 | + {needs.length > 0 && ( |
| 184 | + <div className='mt-8'> |
| 185 | + <h3 className='text-lg font-semibold text-charter-blue-600 mb-2'> |
| 186 | + Enter Deposit Amount: |
| 187 | + </h3> |
| 188 | + <InputField |
| 189 | + label='Deposit Amount' |
| 190 | + placeholder='Enter deposit amount (e.g., 5000000)' |
| 191 | + type='text' |
| 192 | + value={depositInput} |
| 193 | + onChange={(e) => handleInputChange(e, setDepositInput, 'depositInput')} |
| 194 | + error={errors.depositInput} |
| 195 | + /> |
| 196 | + </div> |
| 197 | + )} |
| 198 | + |
| 199 | + {depositInput.trim() && !validateDepositAmount() && ( |
| 200 | + <DepositDropdownSection |
| 201 | + filteredBanks={filteredBanks} |
| 202 | + handleBankSelection={handleBankSelection} |
| 203 | + /> |
| 204 | + )} |
| 205 | + |
| 206 | + {recommendation && <RecommendationSection recommendation={recommendation} />} |
| 207 | + </div> |
| 208 | + </motion.div> |
| 209 | + </motion.div> |
| 210 | + ) |
| 211 | +} |
| 212 | + |
| 213 | +export default BudgetPlannerSection |
0 commit comments