diff --git a/src/renderer/components/Settings.tsx b/src/renderer/components/Settings.tsx index 8a2a3da..c51b683 100644 --- a/src/renderer/components/Settings.tsx +++ b/src/renderer/components/Settings.tsx @@ -85,82 +85,44 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => checkChanges(); }; - const handleCalcOptMethodChange = (event: any) => { - setCalcOptMethod(event.target.value as string); - - checkChanges(); + const calcOptSelectMap = { + method: setCalcOptMethod, + madhab: setCalcOptMadhab, + highLatitudeRule: setCalcOptHighLatRule, }; - const handleCalcOptMadhabChange = (event: any) => { - setCalcOptMadhab(event.target.value as string); - - checkChanges(); - }; - - const handleCalcOptHighLatRuleChange = (event: any) => { - setCalcOptHighLatRule(event.target.value as string); - - checkChanges(); - }; + const handleCalcOptSelectChange = (event: ChangeEvent, map: 'method' | 'madhab' | 'highLatitudeRule') => { + calcOptSelectMap[map](event.target.value as string); - const handleCalOptAdjustment_FajrChange = (event: ChangeEvent) => { - setCalcOptAdjustment_Fajr(Number(event.target.value) || 0); checkChanges(); }; - const handleBlur_Fajr = () => { - if (calcOptAdjustment_Fajr < -600) setCalcOptAdjustment_Fajr(-600); - else if (calcOptAdjustment_Fajr > 600) setCalcOptAdjustment_Fajr(600); - }; - - const handleCalOptAdjustment_SunriseChange = (event: ChangeEvent) => { - setCalcOptAdjustment_Sunrise(Number(event.target.value) || 0); - checkChanges(); - }; - - const handleBlur_Sunrise = () => { - if (calcOptAdjustment_Sunrise < -600) setCalcOptAdjustment_Sunrise(-600); - else if (calcOptAdjustment_Sunrise > 600) setCalcOptAdjustment_Sunrise(600); - }; - - const handleCalOptAdjustment_DhuhrChange = (event: ChangeEvent) => { - setCalcOptAdjustment_Dhuhr(Number(event.target.value) || 0); - checkChanges(); + const adjustChangeMap: any = { + fajr: setCalcOptAdjustment_Fajr, + sunrise: setCalcOptAdjustment_Sunrise, + dhuhr: setCalcOptAdjustment_Dhuhr, + asr: setCalcOptAdjustment_Asr, + maghrib: setCalcOptAdjustment_Maghrib, + isha: setCalcOptAdjustment_Isha, }; - const handleBlur_Dhuhr = () => { - if (calcOptAdjustment_Dhuhr < -600) setCalcOptAdjustment_Dhuhr(-600); - else if (calcOptAdjustment_Dhuhr > 600) setCalcOptAdjustment_Dhuhr(600); + const varAdjustChangeMap: any = { + fajr: calcOptAdjustment_Fajr, + sunrise: calcOptAdjustment_Sunrise, + dhuhr: calcOptAdjustment_Dhuhr, + asr: calcOptAdjustment_Asr, + maghrib: calcOptAdjustment_Maghrib, + isha: calcOptAdjustment_Isha, }; - const handleCalOptAdjustment_AsrChange = (event: ChangeEvent) => { - setCalcOptAdjustment_Asr(Number(event.target.value) || 0); + const handleCalcOptAdjustmentChange = (event: ChangeEvent, map: 'fajr' | 'sunrise' | 'dhuhr' | 'asr' | 'maghrib' | 'isha') => { + adjustChangeMap[map](Number(event.target.value) || 0); checkChanges(); }; - const handleBlur_Asr = () => { - if (calcOptAdjustment_Asr < -600) setCalcOptAdjustment_Asr(-600); - else if (calcOptAdjustment_Asr > 600) setCalcOptAdjustment_Asr(600); - }; - - const handleCalOptAdjustment_MaghribChange = (event: ChangeEvent) => { - setCalcOptAdjustment_Maghrib(Number(event.target.value) || 0); - checkChanges(); - }; - - const handleBlur_Maghrib = () => { - if (calcOptAdjustment_Maghrib < -600) setCalcOptAdjustment_Maghrib(-600); - else if (calcOptAdjustment_Maghrib > 600) setCalcOptAdjustment_Maghrib(600); - }; - - const handleCalOptAdjustment_IshaChange = (event: ChangeEvent) => { - setCalcOptAdjustment_Isha(Number(event.target.value) || 0); - checkChanges(); - }; - - const handleBlur_Isha = () => { - if (calcOptAdjustment_Isha < -600) setCalcOptAdjustment_Isha(-600); - else if (calcOptAdjustment_Isha > 600) setCalcOptAdjustment_Isha(600); + const handleBlurCalcOptAdjustmentChange = (map: 'fajr' | 'sunrise' | 'dhuhr' | 'asr' | 'maghrib' | 'isha') => { + if (varAdjustChangeMap[map] < -300) adjustChangeMap[map](-300); + else if (varAdjustChangeMap[map] > 300) adjustChangeMap[map](300); }; // -------------------------------------------------------------------------- @@ -769,7 +731,13 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => Calculation Method - handleCalcOptSelectChange(e as any, 'method')} + disabled={calcOptMode === 'default' ? true : false} + > {methodList.map((method) => ( {method} @@ -780,7 +748,13 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => Madhab - handleCalcOptSelectChange(e as any, 'madhab')} + disabled={calcOptMode === 'default' ? true : false} + > {madhabList.map((method) => ( {method} @@ -795,7 +769,7 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => labelId='highlat-select' value={calcOptHighLatRule} label='High Latitude Adjustment' - onChange={handleCalcOptHighLatRuleChange} + onChange={(e) => handleCalcOptSelectChange(e as any, 'highLatitudeRule')} disabled={calcOptMode === 'default' ? true : false} > {highLatRuleList.map((method) => ( @@ -818,12 +792,12 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => style={{ marginTop: '4px' }} sx={{ ml: 0.5 }} value={calcOptAdjustment_Fajr} - onChange={handleCalOptAdjustment_FajrChange} - onBlur={handleBlur_Fajr} + onChange={(e) => handleCalcOptAdjustmentChange(e as any, 'fajr')} + onBlur={() => handleBlurCalcOptAdjustmentChange('fajr')} inputProps={{ step: 1, - min: -600, - max: 600, + min: -300, + max: 300, type: 'number', 'aria-labelledby': 'input-slider', }} @@ -835,12 +809,12 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => handleCalcOptAdjustmentChange(e as any, 'sunrise')} + onBlur={() => handleBlurCalcOptAdjustmentChange('sunrise')} inputProps={{ step: 1, - min: -600, - max: 600, + min: -300, + max: 300, type: 'number', 'aria-labelledby': 'input-slider', }} @@ -852,12 +826,12 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => handleCalcOptAdjustmentChange(e as any, 'dhuhr')} + onBlur={() => handleBlurCalcOptAdjustmentChange('dhuhr')} inputProps={{ step: 1, - min: -600, - max: 600, + min: -300, + max: 300, type: 'number', 'aria-labelledby': 'input-slider', }} @@ -869,12 +843,12 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => handleCalcOptAdjustmentChange(e as any, 'asr')} + onBlur={() => handleBlurCalcOptAdjustmentChange('asr')} inputProps={{ step: 1, - min: -600, - max: 600, + min: -300, + max: 300, type: 'number', 'aria-labelledby': 'input-slider', }} @@ -886,12 +860,12 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => handleCalcOptAdjustmentChange(e as any, 'maghrib')} + onBlur={() => handleBlurCalcOptAdjustmentChange('maghrib')} inputProps={{ step: 1, - min: -600, - max: 600, + min: -300, + max: 300, type: 'number', 'aria-labelledby': 'input-slider', }} @@ -903,12 +877,12 @@ export const Settings = ({ appTheme, ColorModeContext, setChangesMade }: any) => handleCalcOptAdjustmentChange(e as any, 'isha')} + onBlur={() => handleBlurCalcOptAdjustmentChange('isha')} inputProps={{ step: 1, - min: -600, - max: 600, + min: -300, + max: 300, type: 'number', 'aria-labelledby': 'input-slider', }}