From 2c05cb0b423ef0551e374c867cfa8ad0d7f8f2ba Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Tue, 13 Feb 2024 02:31:44 +0500 Subject: [PATCH 1/2] fix(validate-content): call /validate_content when adding content --- src/components/AddContentModal/index.tsx | 54 ++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/components/AddContentModal/index.tsx b/src/components/AddContentModal/index.tsx index 5b8827280..decc80ecb 100644 --- a/src/components/AddContentModal/index.tsx +++ b/src/components/AddContentModal/index.tsx @@ -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, @@ -148,6 +159,8 @@ export const AddContentModal = () => { const form = useForm({ mode: 'onChange' }) const { watch, setValue, reset } = form const [loading, setLoading] = useState(false) + const [isSourceRes, setIsSourceRes] = useState(false) + const [isContentRes, setIsContentRes] = useState(false) useEffect( () => () => { @@ -175,8 +188,40 @@ 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 + + console.log('valid api data', response) + + 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 = () => { @@ -202,9 +247,10 @@ export const AddContentModal = () => { {currentStep === 0 && } {currentStep === 1 && ( <> - {!isSource(type) ? ( + {isSourceRes && ( - ) : ( + )} + {isContentRes && ( )} From b3898da64e0c020db48b64b123627c235243e22a Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Tue, 13 Feb 2024 23:40:34 +0500 Subject: [PATCH 2/2] fix(validate-content): remove console log --- src/components/AddContentModal/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/AddContentModal/index.tsx b/src/components/AddContentModal/index.tsx index decc80ecb..ca382c86f 100644 --- a/src/components/AddContentModal/index.tsx +++ b/src/components/AddContentModal/index.tsx @@ -195,8 +195,6 @@ export const AddContentModal = () => { const response = (await api.post('/validate_content', JSON.stringify(data))) as ApiResponse - console.log('valid api data', response) - if (response.status === 404 || response.status === 400) { notify('Please enter a valid URL') reset()