Я хочу изменить DoorState
при нажатии кнопки.
После публикации API для изменения DoorState
, вызовите get API для проверки состояния робота.
На самом деле DoorState
изменилось. Но не меняйте код console.log(after)
. Почему?
{
deliveryPost.Delivery.status === 'assigned' &&
(checkDoorState(deliveryPost.Delivery.postId) === 'closed' ? ( <
>
<
button className = "bg-white text-teal-500 py-2 px-4 ml-2 border border-teal-500 rounded hover:bg-teal-500 hover:text-white"
onClick = {
() => clickRobotAction(deliveryPost.Delivery.postId, 'open')
} >
Open Door <
/button> <
/>
) : ( <
>
<
button className = "bg-white text-teal-500 py-2 px-4 ml-2 border border-teal-500 rounded hover:bg-teal-500 hover:text-white"
onClick = {
() => clickRobotAction(deliveryPost.Delivery.postId, 'close')
} >
Close Door <
/button> <
/>
))
}
``
`
`
``
const clickRobotAction = async (postId: string, action: string) => {
const appliedRobot = robots.find(robot => robot.postId === postId);
const {
robotId
} = appliedRobot;
try {
await axios.post(`api/${robotId}/action/${action}`); // DoorState open <=> close
const after = await axios.get('api/robots');
console.log(after); // But not change in this log. Why?
} catch (error) {
console.error(error);
}
};