0

when user wants to to POST somthing he must be singed in(without username & pass). Problem is i'm trying to make when CreatePost() invoked it will call SingUser() and based on SingUser() fetch request it will call CreatePost() again to let user post after he sign in.

this is in createpost component

CreatePost(){
    fetch(url ,{
      method :'POST',
      headers:{
        Accept:'application/json',
        'Content-Type' :'application/json',
      },
      body: JSON.stringify(post)
   }).then((response) => response.json())
  .then((responseJson)=>{
      if(responseJson.status =='inactive'){
               //SignUser
      }else{
             //post
      }
  }).catch((error)=>{ //later
  });
}

here is SingUser() in other file

async function SignUser() {
           try{
          User.vtoken = await AsyncStorage.getItem('vtoken');
          var userTemp={
            vtoken: User.vtoken,
            ntoken : User.ntoken
            }
                fetch(url,{
                    method :'POST',
                    headers:{
                      Accep : 'application/json',
                      'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(userTemp)
                  }).then((response)=> response.json()).
                then((responseJson)=>{
                        if(responseJson.path == 2){
                            Save(responseJson, userTemp);}
                            else return;
                  }).catch((error)=>{
                     });
            
       }catch(error){}
}

async function Save(result , userTemp){
    try{
        await AsyncStorage.setItem('vtoken', result.vtoken);
            User.vtoken = result.vtoken;
            userTemp.vtoken = result.vtoken;

          fetch(url,{
            method :'POST',
            headers:{
              Accep : 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(userTemp)
          }).then((response)=>response.json()).
          then((responseJson)=>{
              return 'done';
          }).catch((error)=>{})
      }
      catch(error){}
}
export {SignUser}

i hope u understand what im trying to do if there is better way to do it thnx:(

2 Answers 2

1

You can do something like this:

const errorCodeMap = {
  USER_INACTIVE: 10,
}

const statusMap = {
  INACTIVE: `inactive`
}

const METHOD = `POST`
const APPLICATION_JSON = `application/json`

const headerDefault = {
  Accept: APPLICATION_JSON,
  'Content-Type': APPLICATION_JSON,
}

const who = `post`

async function createPost(payload, options) {
  try {
    const {
      url = ``,
      fetchOptions = {
        method: METHOD,
        headers: headerDefault,
      },
    } = options
    const {
      post,
    } = payload

    const response = await fetch(url, {
      ...fetchOptions,
      body: JSON.stringify(post)
    })

    const {
      status,
      someUsefulData,
    } = await response.json()

    if (status === statusMap.INACTIVE) {
      return {
        data: null,
        errors: [{
          type: who,
          code: errorCodeMap.USER_INACTIVE,
          message: `User inactive`
        }]
      }
    } else {
      const data = someNormalizeFunction(someUsefulData)

      return {
        data,
        errors: [],
      }
    }
  } catch (err) {

  }
}

async function createPostRepeatOnInactive(payload, options) {
  try {
    const {
      repeat = 1,
    } = options

    let index = repeat
    while (index--) {
      const { data, errors } = createPost(payload, options)

      if (errors.length) {
        await signUser()
      } else {
        return {
          data,
          errors,
        }
      }
    }
  } catch (err) {

  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

solve it, I did little adjustments

async CreatePost(){
   try{
     var response = await fetch(url ,{
      method :'POST',
      headers:{
        Accept:'application/json',
        'Content-Type' :'application/json',
      },
      body: JSON.stringify(post)});
      var responseJson = await response.json();
      if(responseJson.status =='inactive' && postRepeat == true){
               postRepeat == false;
                await SignUser();
                this.CreatePost();
      }
       else{
        //posted
      }
}catch(err){}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.