-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Autocomplete validation (#2811)
- Loading branch information
Showing
21 changed files
with
479 additions
and
190 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/app/services/context/validation-context/ValidationContext.tsx
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,12 @@ | ||
import { createContext } from 'react'; | ||
|
||
interface ValidationContext { | ||
isValid: boolean; | ||
validate: (queryUrl: string) => void; | ||
query: string; | ||
error: string; | ||
} | ||
|
||
export const ValidationContext = createContext<ValidationContext>( | ||
{} as ValidationContext | ||
); |
66 changes: 66 additions & 0 deletions
66
src/app/services/context/validation-context/ValidationProvider.tsx
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,66 @@ | ||
import { ReactNode, useEffect, useMemo, useState } from 'react'; | ||
|
||
import { ValidationService } from '../../../../modules/validation/validation-service'; | ||
import { useAppSelector } from '../../../../store'; | ||
import { IResource } from '../../../../types/resources'; | ||
import { ValidationError } from '../../../utils/error-utils/ValidationError'; | ||
import { getResourcesSupportedByVersion } from '../../../utils/resources/resources-filter'; | ||
import { parseSampleUrl } from '../../../utils/sample-url-generation'; | ||
import { GRAPH_API_VERSIONS } from '../../graph-constants'; | ||
import { ValidationContext } from './ValidationContext'; | ||
|
||
interface ValidationProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const ValidationProvider = ({ children }: ValidationProviderProps) => { | ||
const { resources } = useAppSelector((state) => state); | ||
const base = getResourcesSupportedByVersion(resources.data.children, GRAPH_API_VERSIONS[0]); | ||
|
||
const [isValid, setIsValid] = useState<boolean>(false); | ||
const [query, setQuery] = useState<string>(''); | ||
const [validationError, setValidationError] = useState<string>(''); | ||
|
||
const [versionedResources, setVersionedResources] = | ||
useState<IResource[]>(resources.data.children.length > 0 ? base : []); | ||
const [version, setVersion] = useState<string>(GRAPH_API_VERSIONS[0]); | ||
|
||
const { queryVersion } = parseSampleUrl(query); | ||
|
||
useEffect(() => { | ||
if (resources.data.children.length > 0) { | ||
setVersionedResources(getResourcesSupportedByVersion(resources.data.children, GRAPH_API_VERSIONS[0])); | ||
} | ||
}, [resources]) | ||
|
||
useEffect(() => { | ||
if (version !== queryVersion && GRAPH_API_VERSIONS.includes(queryVersion) && resources.data.children.length > 0) { | ||
setVersionedResources(getResourcesSupportedByVersion(resources.data.children, queryVersion)); | ||
setVersion(queryVersion); | ||
} | ||
}, [query]); | ||
|
||
const validate = (queryToValidate: string) => { | ||
setQuery(queryToValidate); | ||
try { | ||
ValidationService.validate(queryToValidate, versionedResources); | ||
setIsValid(true); | ||
setValidationError(''); | ||
} catch (error: unknown) { | ||
const theError = error as ValidationError; | ||
setValidationError(theError.message); | ||
setIsValid(theError.type === 'warning'); | ||
} | ||
}; | ||
|
||
const contextValue = useMemo(() => { | ||
return { isValid, validate, query, error: validationError }; | ||
}, [isValid, validate, query, validationError]); | ||
|
||
return ( | ||
<ValidationContext.Provider value={contextValue}> | ||
{children} | ||
</ValidationContext.Provider> | ||
); | ||
|
||
}; |
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,14 @@ | ||
type ErrorType = 'warning' | 'error'; | ||
|
||
class ValidationError extends Error { | ||
type: ErrorType; | ||
|
||
constructor(message: string, type: ErrorType, name: string = 'ValidationError') { | ||
super(message); | ||
this.name = name; | ||
this.type = type; | ||
this.message = message; | ||
} | ||
} | ||
|
||
export { ValidationError }; |
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
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
Oops, something went wrong.