mohebifar / mohebifar/react-native-loader

Stop Activity indicator animation when server is not connected

Open
#21 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
392
Forks
63
PR merge metrics
No merged PRs in 30d

Description

Hi,

You can find my Login screen API code below. I am using Activity indicator animation which is working fine, whenever the button is pressed. Once the user inputs(username & password) match with the API data correctly, the screen will be navigated to Home screen. Problem with the code is, i just want to stop the loader animation when server(API) is not connected. Or i want to show some alert message about server connection, whenever the app launches.

I am new React native. Kindly share your ideas.

        {
            this.setState({
                errorUsername:'',
                errorPassword:'',
                loader:true,
                loggedIn:this.props.receiveState //RECEIVE VALUE FROM REDUCER
            })
            await fetch(LOG_IN_URL,{
                method: 'POST',
                headers: new Headers({'content-type': 'application/json'}),
                body: JSON.stringify({"userId": this.state.username, "password": this.state.password})
                })
                .then(function (response) {  
                    return response.json();
                })
                .then((responseJson) => {
                    console.log("LOGGED IN SUCCESS OR FAILURE:::::::::"+JSON.stringify(responseJson));
                    if(responseJson.status === 'success')
                    {
                        const userToken = responseJson.data.userSession;
                        AsyncStorage.setItem('userSession', userToken)
                        console.log('ASYNC STORAGE VALUE IS SET::::::'+JSON.stringify(userToken));
                        this.setState({ 
                            loggedIn: true,
                            errorPassword:'',
                            errorUsername:'',
                            username:'',
                            password:'',
                            loader:false
                        })
                        this.props.sendState(this.state.loggedIn); //SEND VALUE TO REDUCER
                    }
                    else
                    {
                        this.refs.customToast.show('Invalid Details', DURATION.LENGTH_LONG);
                    }
                    })
                .catch(function (error) {
                    this.setState({loader:false})
                    this.refs.customToast.show('Check Network Connection', DURATION.LENGTH_LONG);
                    console.log("ERROR CAUGHT::::::::"+error);
                });
            this.state.loggedIn === true ? this.props.navigation.navigate('Home') : ''
        }
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pasted login fetch flow, especially the loader state and the catch handler. Check how react-native-loader is used in the repository and whether a network failure can reset the indicator or display an alert; the issue is done when the requested disconnected-server behavior is defined and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react-native
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.