Skip to content

Commit

Permalink
Merge pull request #197 from amosproj/feature/194-set-minimum-messages
Browse files Browse the repository at this point in the history
#194: set minimum value of shown messages to 1 and set to absolute value
  • Loading branch information
Shahraz98 authored Jul 6, 2023
2 parents ba6b2f0 + 4a25d40 commit 8beaed9
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions frontend/src/components/modals/MessageModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface MessageResponse {
const MessageModal: React.FC<MessageModalProps> = ({ topic }) => {
const [open, setOpen] = useState(false)
const [amount, setAmount] = useState<number>(10)
const [inputValue, setInputValue] = useState<string>('10')
const [data, setData] = useState<MessageInfo[]>([])
const [loading, setLoading] = useState<boolean>(true)
const [error, setError] = useState<string | null>(null)
Expand Down Expand Up @@ -76,7 +77,28 @@ const MessageModal: React.FC<MessageModalProps> = ({ topic }) => {
})
}
const handleAmountChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setAmount(Number(event.target.value))
const value = event.target.value
setInputValue(value) // set the inputValue state

if (value === '') {
return // allow empty input but don't update amount
}

// Convert input value to a number
let newAmount = Number(value)

// Take the absolute value
newAmount = Math.abs(newAmount)

// Round it down to nearest integer
newAmount = Math.floor(newAmount)

// Set the minimum possible value to 1
newAmount = Math.max(1, newAmount)

// Update state
setAmount(newAmount)
setInputValue(newAmount.toString())
}

return (
Expand Down Expand Up @@ -107,7 +129,7 @@ const MessageModal: React.FC<MessageModalProps> = ({ topic }) => {
type="number"
label="Nr. of messages requested"
variant="outlined"
defaultValue={amount}
value={inputValue}
onChange={handleAmountChange}
size="small"
/>
Expand Down

0 comments on commit 8beaed9

Please sign in to comment.