Skip to content

Commit

Permalink
Add type safety to all forms
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed May 7, 2024
1 parent 74cc3d4 commit d6634b0
Show file tree
Hide file tree
Showing 2 changed files with 350 additions and 343 deletions.
52 changes: 26 additions & 26 deletions src/app/(account)/account/idcard/page.client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import { useCallback, useState, useRef } from 'react'
import { ErrorMessage, SuccessMessage } from '@/components/alerts'

export default function PageClient() {
const [selectedFile, setSelectedFile] = useState(null)
const [isUploading, setIsUploading] = useState(false)
const [deliverAtEurofurence, setDeliverAtEurofurence] = useState(false)
const [error, setError] = useState(null)
const [success, setSuccess] = useState(null)
const displaynameRef = useRef('')
const nicknameRef = useRef('')
const pronounsRef = useRef('')
const speciesRef = useRef('')
const addressRef = useRef('')
const cityRef = useRef('')
const countryRef = useRef('')
const stateRef = useRef('')
const postalcodeRef = useRef('')
const [selectedFile, setSelectedFile] = useState<File | null>(null)

Check warning on line 6 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const [isUploading, setIsUploading] = useState<boolean>(false)

Check warning on line 7 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to false
const [deliverAtEurofurence, setDeliverAtEurofurence] = useState<boolean>(false)

Check warning on line 8 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to false
const [error, setError] = useState<string | null>(null)

Check warning on line 9 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const [success, setSuccess] = useState<string | null>(null)

Check warning on line 10 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const displaynameRef = useRef<HTMLInputElement>(null)

Check warning on line 11 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const nicknameRef = useRef<HTMLInputElement>(null)

Check warning on line 12 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const pronounsRef = useRef<HTMLInputElement>(null)

Check warning on line 13 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const speciesRef = useRef<HTMLInputElement>(null)

Check warning on line 14 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const addressRef = useRef<HTMLInputElement>(null)

Check warning on line 15 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const cityRef = useRef<HTMLInputElement>(null)

Check warning on line 16 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const countryRef = useRef<HTMLInputElement>(null)

Check warning on line 17 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const stateRef = useRef<HTMLInputElement>(null)

Check warning on line 18 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null
const postalcodeRef = useRef<HTMLInputElement>(null)

Check warning on line 19 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Pointless statement or boolean expression

Can be simplified to null

const handleCheckboxChange = useCallback((event) => {
const handleCheckboxChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {

Check failure on line 21 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Parsing error: Unexpected token, expected "," (21:49)
setDeliverAtEurofurence(event.target.checked)
}, [])

const handleFileChange = (event) => {
const selectedFile = event.target.files[0]
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const selectedFile = event.target.files ? event.target.files[0] : null
const validImageTypes = [
'image/jpeg',
'image/png',
Expand All @@ -32,7 +32,7 @@ export default function PageClient() {
'image/x-icon',
'image/vnd.djvu',
]
if (!validImageTypes.includes(selectedFile.type)) {
if (selectedFile && !validImageTypes.includes(selectedFile.type)) {
setError(
'Please select a valid image file type (JPEG, PNG, SVG, TIFF, ICO, DVU).'
)
Expand All @@ -41,7 +41,7 @@ export default function PageClient() {
}, 5000)
return
}
if (selectedFile.size > 1 * 1024 * 1024) {
if (selectedFile && selectedFile.size > 1 * 1024 * 1024) {
setError('File size must be less than 1 MB.')
setTimeout(() => {
setError(null)
Expand All @@ -51,10 +51,10 @@ export default function PageClient() {
const fileReader = new FileReader()
fileReader.readAsDataURL(selectedFile)
fileReader.onload = (event) => {
const imgElement = document.getElementById('selected-image')
imgElement.src = event.target.result
const imgElement = document.getElementById('selected-image') as HTMLImageElement

Check notice on line 54 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Expression statement which is not assignment or call

Expression statement is not assignment or call
imgElement.src = event.target.result as string
const img = new Image()
img.src = event.target.result
img.src = event.target.result as string
img.onload = () => {
if (img.width >= 128 && img.height >= 128) {
setSelectedFile(selectedFile)
Expand All @@ -68,13 +68,13 @@ export default function PageClient() {
}
}

const handleDrop = useCallback((event) => {
const handleDrop = useCallback((event: React.DragEvent<HTMLLabelElement>) => {
event.preventDefault()
const droppedFile = event.dataTransfer.files[0]
handleFileChange({ target: { files: [droppedFile] } })
handleFileChange({ target: { files: [droppedFile] } } as React.ChangeEvent<HTMLInputElement>)

Check notice on line 74 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Expression statement which is not assignment or call

Expression statement is not assignment or call

Check notice on line 74 in src/app/(account)/account/idcard/page.client.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Signature mismatch

Argument type { target: { files: File\[\] } } is not assignable to parameter type React.ChangeEvent\<HTMLInputElement\>
}, [])

const handleSubmit = async (event) => {
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()

const formData = new FormData()
Expand Down Expand Up @@ -119,7 +119,7 @@ export default function PageClient() {
country: countryRef.current?.value,
state: stateRef.current?.value,
postalcode: postalcodeRef.current?.value,
deliver_at_eurofurence: deliver_at_eurofurence.checked,
deliver_at_eurofurence: deliverAtEurofurence.checked,
},
})
)
Expand Down
Loading

0 comments on commit d6634b0

Please sign in to comment.