I am working on a project which is done in Rails Hotwire where devise gem has been used for authentication and, in frontend Stimulus.js has been used. Instead of using link_to , I want to write an API with delete method, to refresh like: cookies, token , cause time delay is taking some time to remove all which is creating an issue:
My initial Code:
<%= link_to '/api/v2/iam/users/sign_out', method: :delete, data: { action: 'click->biz-account#logout' } do %>
<button class="hidden lg:block btn--outlined py-[16px]" data-cy="logout"><%= I18n.t('biz.log_out') %></button>
<% end %>
biz-account.js
logout() {
setTimeout(() => {
location.href = '/'
}, 300);
}
Now, I want to call the delete API from the view part, done the whole delete thing in javascript api. How to do this?
I tried to do this, which is not working:
app/javascript/services/api_calls.js
export const logout =
function () {
call(
'/api/v2/iam/users/sign_out',
{},
function (value) {
if (value.message) {
window.alert =
openSnackbar(value.message, 'success')
} else {
window.alert =
openSnackbar(value.errors, 'error')
}
callback(value);
},
{verb: 'DELETE'})
}