|
| 1 | +import React, { ReactElement, useState } from 'react' |
| 2 | +import style from './account.module.css' |
| 3 | +import { DEFAULT_CSV_COLLAPSE_THRESHOLD } from 'constants/index' |
| 4 | + |
| 5 | +interface IProps { |
| 6 | + csvCollapseThreshold: number |
| 7 | +} |
| 8 | + |
| 9 | +export default function ChangeCsvCollapseThreshold ({ csvCollapseThreshold }: IProps): ReactElement { |
| 10 | + const [threshold, setThreshold] = useState(csvCollapseThreshold ?? DEFAULT_CSV_COLLAPSE_THRESHOLD) |
| 11 | + const [inputValue, setInputValue] = useState(String(csvCollapseThreshold ?? DEFAULT_CSV_COLLAPSE_THRESHOLD)) |
| 12 | + const [error, setError] = useState('') |
| 13 | + const [success, setSuccess] = useState('') |
| 14 | + const [disabled, setDisabled] = useState(true) |
| 15 | + |
| 16 | + const onSubmit = async (e: React.FormEvent): Promise<void> => { |
| 17 | + e.preventDefault() |
| 18 | + const newThreshold = parseInt(inputValue, 10) |
| 19 | + if (isNaN(newThreshold) || newThreshold < 0) { |
| 20 | + setError('Please enter a valid non-negative number') |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + const oldThreshold = threshold |
| 25 | + setThreshold(newThreshold) |
| 26 | + setDisabled(true) |
| 27 | + |
| 28 | + try { |
| 29 | + const res = await fetch('/api/user/csvCollapseThreshold', { |
| 30 | + method: 'PUT', |
| 31 | + headers: { |
| 32 | + 'Content-Type': 'application/json' |
| 33 | + }, |
| 34 | + body: JSON.stringify({ csvCollapseThreshold: newThreshold }) |
| 35 | + }) |
| 36 | + if (res.status === 200) { |
| 37 | + setError('') |
| 38 | + setSuccess('Updated successfully.') |
| 39 | + setTimeout(() => { |
| 40 | + setSuccess('') |
| 41 | + }, 3000) |
| 42 | + } else { |
| 43 | + throw new Error('Failed to update threshold') |
| 44 | + } |
| 45 | + } catch (err: any) { |
| 46 | + setSuccess('') |
| 47 | + setError(err.message ?? 'Failed to update threshold') |
| 48 | + setThreshold(oldThreshold) |
| 49 | + setInputValue(String(oldThreshold)) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>): void => { |
| 54 | + const value = e.target.value |
| 55 | + setInputValue(value) |
| 56 | + const numValue = parseInt(value, 10) |
| 57 | + if (!isNaN(numValue) && numValue >= 0 && numValue !== threshold) { |
| 58 | + setDisabled(false) |
| 59 | + setError('') |
| 60 | + } else if (numValue === threshold) { |
| 61 | + setDisabled(true) |
| 62 | + } else { |
| 63 | + setDisabled(true) |
| 64 | + if (value !== '') { |
| 65 | + setError('Please enter a valid non-negative number') |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className={style.threshold_ctn}> |
| 72 | + <form onSubmit={(e) => { void onSubmit(e) }}> |
| 73 | + <div className={style.threshold_row}> |
| 74 | + <input |
| 75 | + id="csvCollapseThreshold" |
| 76 | + type="text" |
| 77 | + min="0" |
| 78 | + required |
| 79 | + value={inputValue} |
| 80 | + onChange={handleInputChange} |
| 81 | + className={style.threshold_input} |
| 82 | + /> |
| 83 | + <button disabled={disabled} className='button_main' type='submit'>Update</button> |
| 84 | + </div> |
| 85 | + {error !== '' && <div className={style.error_message}>{error}</div>} |
| 86 | + {success !== '' && <div className={style.success_message}>{success}</div>} |
| 87 | + </form> |
| 88 | + </div> |
| 89 | + ) |
| 90 | +} |
0 commit comments