-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix route parsing in form filler view. (#141)
- Loading branch information
1 parent
8f0509d
commit 765a74c
Showing
14 changed files
with
150 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
import { | ||
type RouteData, | ||
getRouteDataFromQueryString, | ||
} from '@atj/forms/src/route-data'; | ||
|
||
export const useRouteParams = (): RouteData => { | ||
const location = useLocation(); | ||
const queryString = location.search.startsWith('?') | ||
? location.search.substring(1) | ||
: location.search; | ||
return { | ||
routeParams: getRouteDataFromQueryString(queryString), | ||
pathname: location.pathname, | ||
}; | ||
}; | ||
|
||
export const useQueryString = (): string => { | ||
const location = useLocation(); | ||
const queryString = location.search.startsWith('?') | ||
? location.search.substring(1) | ||
: location.search; | ||
return queryString; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { z } from 'zod'; | ||
|
||
import { en as message } from '@atj/common/src/locales/en/app'; | ||
|
||
import { ParsePatternConfigData, Pattern } from '../../pattern'; | ||
import { safeZodParseFormErrors } from '../../util/zod'; | ||
|
||
const configSchema = z.object({ | ||
label: z.string().min(1, message.patterns.input.fieldLabelRequired), | ||
initial: z.string().optional(), | ||
required: z.boolean(), | ||
maxLength: z.coerce.number(), | ||
}); | ||
export type InputConfigSchema = z.infer<typeof configSchema>; | ||
|
||
export const parseConfigData: ParsePatternConfigData< | ||
InputConfigSchema | ||
> = obj => { | ||
return safeZodParseFormErrors(configSchema, obj); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { en as message } from '@atj/common/src/locales/en/app'; | ||
|
||
import { Pattern, type PatternConfig } from '../../pattern'; | ||
|
||
import { parseConfigData, type InputConfigSchema } from './config'; | ||
import { createPrompt } from './prompt'; | ||
import { type InputPatternOutput, parseUserInput } from './response'; | ||
|
||
export type InputPattern = Pattern<InputConfigSchema>; | ||
|
||
export const inputConfig: PatternConfig<InputPattern, InputPatternOutput> = { | ||
displayName: message.patterns.input.displayName, | ||
initial: { | ||
label: message.patterns.input.fieldLabel, | ||
initial: '', | ||
required: true, | ||
maxLength: 128, | ||
}, | ||
parseUserInput, | ||
parseConfigData, | ||
getChildren() { | ||
return []; | ||
}, | ||
createPrompt, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type InputPattern, inputConfig } from '.'; | ||
import { | ||
type CreatePrompt, | ||
type TextInputProps, | ||
getFormSessionValue, | ||
validatePattern, | ||
} from '../..'; | ||
|
||
export const createPrompt: CreatePrompt<InputPattern> = ( | ||
_, | ||
session, | ||
pattern, | ||
options | ||
) => { | ||
const extraAttributes: Record<string, any> = {}; | ||
const sessionValue = getFormSessionValue(session, pattern.id); | ||
if (options.validate) { | ||
const isValidResult = validatePattern(inputConfig, pattern, sessionValue); | ||
if (!isValidResult.success) { | ||
extraAttributes['error'] = isValidResult.error; | ||
} | ||
} | ||
return { | ||
props: { | ||
_patternId: pattern.id, | ||
type: 'input', | ||
inputId: pattern.id, | ||
value: sessionValue, | ||
label: pattern.data.label, | ||
required: pattern.data.required, | ||
...extraAttributes, | ||
} as TextInputProps, | ||
children: [], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { z } from 'zod'; | ||
|
||
import { ParseUserInput } from '../../pattern'; | ||
import { safeZodParseToFormError } from '../../util/zod'; | ||
|
||
import { type InputPattern } from '.'; | ||
|
||
const createSchema = (data: InputPattern['data']) => { | ||
const schema = z.string().max(data.maxLength); | ||
if (!data.required) { | ||
return schema; | ||
} | ||
return schema.min(1, { message: 'This field is required' }); | ||
}; | ||
|
||
export type InputPatternOutput = z.infer<ReturnType<typeof createSchema>>; | ||
|
||
export const parseUserInput: ParseUserInput< | ||
InputPattern, | ||
InputPatternOutput | ||
> = (pattern, obj) => { | ||
return safeZodParseToFormError(createSchema(pattern['data']), obj); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters