Skip to content

Commit

Permalink
Merge pull request #916 from aliraza556/call-validate-content
Browse files Browse the repository at this point in the history
Implement /validate_content Endpoint Integration for Enhanced URL Validation
  • Loading branch information
Rassl authored Feb 13, 2024
2 parents c1be527 + b3898da commit bfedae9
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/components/AddContentModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export type FormData = {
latitude: string
}

interface ApiResponse {
status: number
source?: string
content?: string
}

interface ApiError {
status: number
message?: string
}

const handleSubmitForm = async (
data: FieldValues,
close: () => void,
Expand Down Expand Up @@ -148,6 +159,8 @@ export const AddContentModal = () => {
const form = useForm<FormData>({ mode: 'onChange' })
const { watch, setValue, reset } = form
const [loading, setLoading] = useState(false)
const [isSourceRes, setIsSourceRes] = useState(false)
const [isContentRes, setIsContentRes] = useState(false)

useEffect(
() => () => {
Expand Down Expand Up @@ -175,8 +188,38 @@ export const AddContentModal = () => {
close()
}

const onNextStep = () => {
setCurrentStep(currentStep + 1)
const onNextStep = async () => {
if (currentStep === 0) {
try {
const data = { source }

const response = (await api.post('/validate_content', JSON.stringify(data))) as ApiResponse

if (response.status === 404 || response.status === 400) {
notify('Please enter a valid URL')
reset()

return
}

if (response.source) {
setIsSourceRes(true)
setCurrentStep(currentStep + 1)
}

if (response.content) {
setIsContentRes(true)
setCurrentStep(currentStep + 1)
}
} catch (e) {
const error = e as ApiError

if (error.status === 404 || error.status === 400) {
notify('Please enter a valid URL')
reset()
}
}
}
}

const onPrevStep = () => {
Expand All @@ -202,9 +245,10 @@ export const AddContentModal = () => {
{currentStep === 0 && <SourceStep allowNextStep={isValidSource} onNextStep={onNextStep} type={type} />}
{currentStep === 1 && (
<>
{!isSource(type) ? (
{isSourceRes && (
<LocationStep form={form} latitude={latitude} longitude={longitude} onNextStep={onNextStep} />
) : (
)}
{isContentRes && (
<SourceTypeStep onNextStep={onNextStep} onPrevStep={onPrevStep} type={type} value={sourceValue} />
)}
</>
Expand Down

0 comments on commit bfedae9

Please sign in to comment.