Skip to content

Commit

Permalink
Improve type safety and input validation across frontend components
Browse files Browse the repository at this point in the history
- Enhanced type definitions in Layout, OsblDatasheet, and other components
- Updated input types for website and URL fields with placeholders
- Refined type annotations in MyAsyncSelect and other shared components
- Improved error handling and type safety in various form input methods
- Updated Inertia page resolution to throw an error for missing pages
- Cleaned up commented-out validation and code comments
  • Loading branch information
mpressen committed Feb 4, 2025
1 parent 10f66ea commit d74d20f
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/frontend/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const flashTypes = ['message', 'success', 'info', 'warning', 'error'] as const

interface LayoutProps {
showSidebar: boolean
flash: any
flash: { [key in typeof flashTypes[number]]?: string }
children?: ReactElement
}

export default function Layout ({
children,
showSidebar = false,
showSidebar,
flash = {}
}: LayoutProps): ReactElement {
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export default function OsblDatasheet ({ data, setData, errors, clearErrors }: O
<div className='flex flex-col gap-4'>
<Label>Réduction d'impôt * :</Label>
<RadioGroup
required // doesn't work as expected
value={String(data.tax_reduction)}
required // doesn't work. Comment kept for reference
value={data.tax_reduction}
onValueChange={(value) => {
setData('tax_reduction', value)
clearErrors('tax_reduction')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function FundManagementSection ({
errors,
clearErrors
}: FundManagementSectionProps): ReactElement {
function handleFundChange (index: number, field: keyof FundRecord, value: any): void {
function handleFundChange (index: number, field: keyof FundRecord, value: FundRecord[keyof FundRecord]): void {
const updatedItems = items.map((item: FundRecord, i: number) =>
i === index ? { ...item, [field]: value } : item
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ interface Props extends Omit<FormProps, 'setData'> {
onUpdate: (finance: AnnualFinance) => void
}

// Fix the return type and validation
const financeValidation = (data: AnnualFinance[], currentIndex: number): z.ZodType<any> => z.object({
year: z.string().refine(
(year) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export default function OsblHeader ({ data, setData, errors, clearErrors }: Omit

<MyInput
id='website'
type='text'
type='url'
labelText='Site internet :'
placeholder='https://'
value={data.website ?? ''}
onChange={(e) => {
setData('website', e.target.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function OsblLocationSheet ({
return checks.some(check => check)
})

function updateSheetLocation (field: keyof Location, value: any): void {
function updateSheetLocation<K extends keyof Location> (field: K, value: Location[K]): void {
setSheetLocation(prev => ({
...prev,
[field]: value
Expand All @@ -79,13 +79,15 @@ export default function OsblLocationSheet ({
onUpdate(deepCleanData(sheetLocation))
}

function getLabel (): string {
return [
function getLabel (): string | undefined {
const label = [
sheetLocation.address_attributes?.street_number,
sheetLocation.address_attributes?.street_name,
sheetLocation.address_attributes?.postal_code,
sheetLocation.address_attributes?.city
].filter(Boolean).join(' ')

return label.length > 0 ? label : undefined
}

return (
Expand Down Expand Up @@ -174,6 +176,7 @@ export default function OsblLocationSheet ({
<MyInput
labelText='Site web'
type='url'
placeholder='https://'
id={`location-website-${index}`}
value={sheetLocation.website ?? ''}
onChange={(e) => updateSheetLocation('website', e.target.value)}
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/components/shared/MyAsyncSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface MyAsyncCreatableSelectProps {
minInputLength?: number
placeholder?: string
required?: boolean
value: string
value: string | undefined
}

const createOption = (feature: AddressFeature): Option => ({
Expand Down
3 changes: 0 additions & 3 deletions app/frontend/components/shared/MyFileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export default function MyFileInput ({
if (inputRef.current != null) {
inputRef.current.value = ''
setHasFile(false)
// Trigger onChange with a fake event to notify parent component
const event = new Event('change', { bubbles: true }) as unknown as React.ChangeEvent<HTMLInputElement>
Object.defineProperty(event, 'target', { value: { value: '', files: null } })
}
onChange(undefined)
}
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/lib/inertiaConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const resolvePage: PageResolver = (name) => {
const page = pages[`../pages/${name}.tsx`]

if (page === undefined) {
console.error(`Missing Inertia page component: '${name}.tsx'`)
throw new Error(`Page ${name}.tsx not found`)
}

page.default.layout = (pageContent) =>
Expand Down
1 change: 0 additions & 1 deletion app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Location < ApplicationRecord

validates :address, presence: true
# validates :type, presence: true
# validates :name, exclusion: {in: [""]}, allow_nil: true
# validates :name, presence: true, if: -> { type.in?(%i[antenne_locale lieu_d_activite autre]) }
# validates :osbl, uniqueness: {scope: :type, if: -> { type == "siege_social" }}

Expand Down
1 change: 0 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
abort e.to_s.strip
end
RSpec.configure do |config|
# config.include ActionDispatch::TestProcess # for fixture_file_upload
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_paths = [
Rails.root.join("spec/fixtures")
Expand Down

0 comments on commit d74d20f

Please sign in to comment.