mardi 2 mars 2021

and && operator in react native. not validating all conditions in react native

I am new to react native. I have created A screen from where I am sending some data to server. And I am uploading images also. but the problem is I want show alert when all images or any images are null. but when I run below code. They not validating all conditions. if any one image is upload then form is submitting. but I want that API will run if and only if all images are upload.

here is my code.

  const validateInputs = () => {

    if (singleFilePAN && singleFileADH && singleFileADH1 != null) 
      {
    if (!/[A-Z]{5}[0-9]{4}[A-Z]{1}/.test(PAN)){
      setPanError('Please Insert valid PAN Card Image \n And Valid Pan card number')
      return;
    }
    if (!/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/.test(GST))  {
      setGstError('Please Insert valid GST Number')
      return;
    }
    if (!/^\d{4}\s\d{4}\s\d{4}$/.test(ADH)) { 
      setAdhError('Please Insert valid Aadhar Card Image \n And Valid Adhar card number') 
      return;
    }
    else 
    {
{
  fetch('https://xyz/Abs/uploadt_data', {
    method: 'POST',
    headers: {'Accept': 'application/json, text/plain, */*',  "Content-Type" : "application/json"},
  body : JSON.stringify([{formData}])
})
  .then((returnValue) => returnValue.json())
  .then(function(response) {
    console.log(response)
    Alert.alert("File uploaded");
    return response.json();
  });
});
}
    Alert.alert("success")
    return;
}
    }
    Alert.alert("Please Upload All Images")
  };

  const takePicture = async (type) => {
    if (camera) {
      const data = await camera.takePictureAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.All,
      });

      console.log(data.uri);
      const newImageArr = imageArray;
      if (imageType === 'PAN') {
        newImageArr.PAN = data.uri;
      }else if (imageType === 'ADH' && evenTry) {
        newImageArr.ADH = data.uri;
        setEvenTry((val) => !val);
      } else if (imageType === 'ADH' && !evenTry) {
        newImageArr.ADH1 = data.uri;
        setEvenTry((val) => !val);
      }
      setImageArray({...newImageArr});
      
      setShowCamera(false);
      setImageType('');
      setSingleFilePAN({ singleFilePAN: newImageArr.PAN});
      setSingleFileADH({ singleFileADH: newImageArr.ADH});
      setSingleFileADH1({ singleFileADH1: newImageArr.ADH1});
    }
  };

  const pickImage = async (type) => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,

    });

    console.log(result.uri);
    if (!result.cancelled) {
      const newImageArr = imageArray;
      if (type === 'PAN') {
        newImageArr.PAN = result.uri;
      } else if (type === 'ADH' && evenTry) {
        newImageArr.ADH = result.uri;
        setEvenTry((val) => !val);
      } else if (type === 'ADH' && !evenTry) {
        newImageArr.ADH1 = result.uri;  
        setEvenTry((val) => !val);
      }
      setImageArray({ ...newImageArr });
     setSingleFilePAN({ singleFilePAN: newImageArr.PAN});
     setSingleFileADH({ singleFileADH: newImageArr.ADH});
     setSingleFileADH1({ singleFileADH1: newImageArr.ADH1});
    }

Aucun commentaire:

Enregistrer un commentaire