tissue/resources/assets/js/app.ts

95 lines
3.4 KiB
TypeScript
Raw Normal View History

import * as Cookies from 'js-cookie';
2020-08-06 09:53:50 +09:00
import { fetchPostJson, fetchDeleteJson, ResponseError } from './fetch';
2017-08-27 04:44:53 +09:00
require('./bootstrap');
$(() => {
if (Cookies.get('agechecked')) {
$('body').removeClass('tis-need-agecheck');
} else {
$('#ageCheckModal')
.modal({ backdrop: 'static' })
.on('hide.bs.modal', function () {
$('body').removeClass('tis-need-agecheck');
Cookies.set('agechecked', '1', { expires: 365 });
});
}
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/sw.js');
}
$('[data-toggle="tooltip"]').tooltip();
$('.alert').alert();
$('.tis-page-selector').pageSelector();
$('.link-card').linkCard();
const $deleteCheckinModal = $('#deleteCheckinModal').deleteCheckinModal();
$(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('いいねできませんでした。');
});
}
});
2020-06-06 18:40:49 +09:00
$(document).on('click', '.card-spoiler-overlay', function (_event) {
const $this = $(this);
$this.siblings('.card-link').removeClass('card-spoiler');
$this.remove();
});
2019-09-10 23:10:19 +09:00
});