x-csrf-tokenが送られねえ
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as Cookies from 'js-cookie';
|
||||
import jqXHR = JQuery.jqXHR;
|
||||
import { fetchPostJson, fetchDeleteJson, ResponseError } from './fetch';
|
||||
|
||||
require('./bootstrap');
|
||||
|
||||
@@ -41,57 +41,46 @@ $(() => {
|
||||
const isLiked = $this.data('liked');
|
||||
|
||||
if (isLiked) {
|
||||
const callback = (data: any) => {
|
||||
$this.data('liked', false);
|
||||
$this.find('.oi-heart').removeClass('text-danger');
|
||||
|
||||
const count = data.ejaculation ? data.ejaculation.likes_count : 0;
|
||||
$this.find('.like-count').text(count ? count : '');
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url: '/api/likes/' + encodeURIComponent(targetId),
|
||||
method: 'delete',
|
||||
type: 'json',
|
||||
})
|
||||
.then(callback)
|
||||
.catch(function (xhr: jqXHR) {
|
||||
if (xhr.status === 404) {
|
||||
callback(JSON.parse(xhr.responseText));
|
||||
return;
|
||||
fetchDeleteJson(`/api/likes/${encodeURIComponent(targetId)}`)
|
||||
.then((response) => {
|
||||
if (response.status === 200 || response.status === 404) {
|
||||
return response.json();
|
||||
}
|
||||
throw new ResponseError(response);
|
||||
})
|
||||
.then((data) => {
|
||||
$this.data('liked', false);
|
||||
$this.find('.oi-heart').removeClass('text-danger');
|
||||
|
||||
console.error(xhr);
|
||||
const count = data.ejaculation ? data.ejaculation.likes_count : 0;
|
||||
$this.find('.like-count').text(count ? count : '');
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
alert('いいねを解除できませんでした。');
|
||||
});
|
||||
} else {
|
||||
const callback = (data: any) => {
|
||||
$this.data('liked', true);
|
||||
$this.find('.oi-heart').addClass('text-danger');
|
||||
fetchPostJson('/api/likes', { id: targetId })
|
||||
.then((response) => {
|
||||
if (response.status === 200 || response.status === 409) {
|
||||
return response.json();
|
||||
}
|
||||
throw new ResponseError(response);
|
||||
})
|
||||
.then((data) => {
|
||||
$this.data('liked', true);
|
||||
$this.find('.oi-heart').addClass('text-danger');
|
||||
|
||||
const count = data.ejaculation ? data.ejaculation.likes_count : 0;
|
||||
$this.find('.like-count').text(count ? count : '');
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url: '/api/likes',
|
||||
method: 'post',
|
||||
type: 'json',
|
||||
data: {
|
||||
id: targetId,
|
||||
},
|
||||
})
|
||||
.then(callback)
|
||||
.catch(function (xhr: jqXHR) {
|
||||
if (xhr.status === 409) {
|
||||
callback(JSON.parse(xhr.responseText));
|
||||
return;
|
||||
} else if (xhr.status === 401) {
|
||||
const count = data.ejaculation ? data.ejaculation.likes_count : 0;
|
||||
$this.find('.like-count').text(count ? count : '');
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e instanceof ResponseError && e.response.status === 401) {
|
||||
alert('いいねするためにはログインしてください。');
|
||||
return;
|
||||
}
|
||||
|
||||
console.error(xhr);
|
||||
console.error(e);
|
||||
alert('いいねできませんでした。');
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user