I'm new to React, what I'm trying to do is call my API with a GET request to change the status of an elevator to "Operational" and it's color status from red to green. The problem is that it doesnt seem to call the API and brings me back to my previous screen when I click CONFIRM.
the function for my call to the API looks like this
function ElevatorStatusScreen({navigation, route}) {
const { Elevator } = route.params;
const [elevatorStatus, setEleStatus] = React.useState(Elevator.status);
const [statusColor, setColorStatus] = React.useState('red');
// Call to the api to update the status of an elevator
const endtask = () => {
axios.get(`https://apilink/api/Elevators/update/${Elevator.id}/Operational`)
.then(response => {
if(response.status == 200){
Alert.alert('Status has been changed to Operational!')
setColorStatus('green')
setEleStatus('Operational');
}
})
}
return (
<ImageBackground
style={styles.background}
source={require('../assets/white.jpg')}
>
<Text style={styles.title}>Elevator: {Elevator.id}</Text>
<Text style={{ color: statusColor }}>{elevatorStatus}</Text>
{elevatorStatus === 'Stopped' ? (
<TouchableOpacity style={styles.endTask} onPress={() => endtask()}>
<Text style={styles.endText}>END TASK</Text>
</TouchableOpacity>
)
:
<TouchableOpacity style={styles.confirm} onPress={() => {
navigation.goBack()
}}>
<Text style={styles.endText}>CONFIRM</Text>
</TouchableOpacity>
}
</ImageBackground>
);
}
Not sure what I'm doing wrong. This is my endpoint in a C# project coming from my elevators controller. (I tested it and it works)
[HttpGet("update/{id}/{status}")]
public async Task<dynamic> test(string status, long id)
{
var elevator = await _context.elevators.FindAsync(id);
elevator.Status = status;
await _context.SaveChangesAsync();
return elevator;
}
the endpoint: //apilink/api/Elevators/update/1/Operational returns a JSON like this in my browser
{ "id":1, "status":"Operational", "columnId":1, "serialNumber":"666999990867" }
let me know if you need more code and thanks for your help.
The problem was that my elevatorStatus was 'offline' and NOT 'Stopped' in my database...sorry about that