2020-08-23 12:28:21 +09:00
|
|
|
import * as Cookies from 'js-cookie';
|
2020-08-06 09:53:50 +09:00
|
|
|
import { fetchPostJson, fetchDeleteJson, ResponseError } from './fetch';
|
2020-08-07 00:53:36 +09:00
|
|
|
import { linkCard, pageSelector, deleteCheckinModal } from './tissue';
|
2019-03-05 00:18:27 +09:00
|
|
|
|
2017-08-27 04:44:53 +09:00
|
|
|
require('./bootstrap');
|
2019-03-05 00:18:27 +09:00
|
|
|
|
|
|
|
$(() => {
|
|
|
|
if (Cookies.get('agechecked')) {
|
|
|
|
$('body').removeClass('tis-need-agecheck');
|
|
|
|
} else {
|
|
|
|
$('#ageCheckModal')
|
2020-06-06 18:01:56 +09:00
|
|
|
.modal({ backdrop: 'static' })
|
2019-03-05 00:18:27 +09:00
|
|
|
.on('hide.bs.modal', function () {
|
|
|
|
$('body').removeClass('tis-need-agecheck');
|
2020-06-06 18:01:56 +09:00
|
|
|
Cookies.set('agechecked', '1', { expires: 365 });
|
2019-03-05 00:18:27 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (navigator.serviceWorker) {
|
|
|
|
navigator.serviceWorker.register('/sw.js');
|
|
|
|
}
|
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
$('.alert').alert();
|
2020-08-06 01:09:25 +09:00
|
|
|
document.querySelectorAll('.tis-page-selector').forEach(pageSelector);
|
2019-03-05 00:18:27 +09:00
|
|
|
|
2020-08-07 00:25:43 +09:00
|
|
|
document.querySelectorAll('.link-card').forEach(linkCard);
|
2020-08-07 00:53:36 +09:00
|
|
|
|
|
|
|
const elDeleteCheckinModal = document.getElementById('deleteCheckinModal');
|
|
|
|
if (elDeleteCheckinModal) {
|
|
|
|
const $deleteCheckinModal = deleteCheckinModal(elDeleteCheckinModal);
|
|
|
|
$(document).on('click', '[data-target="#deleteCheckinModal"]', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
$deleteCheckinModal.modal('show', this);
|
|
|
|
});
|
|
|
|
}
|
2019-04-05 23:10:26 +09:00
|
|
|
|
2020-06-06 18:40:49 +09:00
|
|
|
$(document).on('click', '[data-href]', function (_event) {
|
2019-04-05 23:10:26 +09:00
|
|
|
location.href = $(this).data('href');
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', '.like-button', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const $this = $(this);
|
|
|
|
const targetId = $this.data('id');
|
|
|
|
const isLiked = $this.data('liked');
|
|
|
|
|
|
|
|
if (isLiked) {
|
2020-08-06 09:53:50 +09:00
|
|
|
fetchDeleteJson(`/api/likes/${encodeURIComponent(targetId)}`)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.status === 200 || response.status === 404) {
|
|
|
|
return response.json();
|
2019-04-05 23:10:26 +09:00
|
|
|
}
|
2020-08-06 09:53:50 +09:00
|
|
|
throw new ResponseError(response);
|
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
$this.data('liked', false);
|
|
|
|
$this.find('.oi-heart').removeClass('text-danger');
|
2019-04-05 23:10:26 +09:00
|
|
|
|
2020-08-06 09:53:50 +09:00
|
|
|
const count = data.ejaculation ? data.ejaculation.likes_count : 0;
|
|
|
|
$this.find('.like-count').text(count ? count : '');
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
2019-04-05 23:10:26 +09:00
|
|
|
alert('いいねを解除できませんでした。');
|
|
|
|
});
|
|
|
|
} else {
|
2020-08-06 09:53:50 +09:00
|
|
|
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');
|
2019-04-05 23:10:26 +09:00
|
|
|
|
2020-08-06 09:53:50 +09:00
|
|
|
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) {
|
2019-09-10 23:10:19 +09:00
|
|
|
alert('いいねするためにはログインしてください。');
|
|
|
|
return;
|
2019-04-05 23:10:26 +09:00
|
|
|
}
|
|
|
|
|
2020-08-06 09:53:50 +09:00
|
|
|
console.error(e);
|
2019-04-05 23:10:26 +09:00
|
|
|
alert('いいねできませんでした。');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-09-07 19:57:44 +09:00
|
|
|
|
2020-06-06 18:40:49 +09:00
|
|
|
$(document).on('click', '.card-spoiler-overlay', function (_event) {
|
2019-09-07 19:57:44 +09:00
|
|
|
const $this = $(this);
|
2020-06-06 18:01:56 +09:00
|
|
|
$this.siblings('.card-link').removeClass('card-spoiler');
|
2019-09-07 19:57:44 +09:00
|
|
|
$this.remove();
|
|
|
|
});
|
2019-09-10 23:10:19 +09:00
|
|
|
});
|