common/xhr for ui/lobby

This commit is contained in:
Thibault Duplessis 2020-09-06 16:31:21 +02:00
parent dc8629a305
commit 2ef5c8d858
5 changed files with 56 additions and 80 deletions

View file

@ -1,6 +1,7 @@
import { LobbyOpts } from './interfaces'; import { LobbyOpts } from './interfaces';
import main from './main'; import main from './main';
import modal from 'common/modal'; import modal from 'common/modal';
import * as xhr from 'common/xhr';
export default function LichessLobby(opts: LobbyOpts) { export default function LichessLobby(opts: LobbyOpts) {
@ -40,12 +41,9 @@ export default function LichessLobby(opts: LobbyOpts) {
setTimeout(() => nbRoundSpread(msg.r), li.socket.pingInterval() / 2); setTimeout(() => nbRoundSpread(msg.r), li.socket.pingInterval() / 2);
}, },
reload_timeline() { reload_timeline() {
$.ajax({ xhr.text('/timeline').then(html => {
url: '/timeline', $('.timeline').html(html);
success: function(html) { li.pubsub.emit('content_loaded');
$('.timeline').html(html);
li.pubsub.emit('content_loaded');
}
}); });
}, },
featured(o: { html: string }) { featured(o: { html: string }) {
@ -63,9 +61,11 @@ export default function LichessLobby(opts: LobbyOpts) {
} }
}); });
li.StrongSocket.firstConnect.then(() => { li.StrongSocket.firstConnect.then(() => {
var gameId = getParameterByName('hook_like'); const gameId = getParameterByName('hook_like');
if (!gameId) return; if (!gameId) return;
$.post(`/setup/hook/${li.sri}/like/${gameId}?rr=${lobby.setup.ratingRange() || ''}`); xhr.text(
`/setup/hook/${li.sri}/like/${gameId}?rr=${lobby.setup.ratingRange() || ''}`,
{ method: 'post' });
lobby.setTab('real_time'); lobby.setTab('real_time');
history.replaceState(null, '', '/'); history.replaceState(null, '', '/');
}); });
@ -78,27 +78,20 @@ export default function LichessLobby(opts: LobbyOpts) {
const $startButtons = $('.lobby__start'), const $startButtons = $('.lobby__start'),
clickEvent = opts.blindMode ? 'click' : 'mousedown'; clickEvent = opts.blindMode ? 'click' : 'mousedown';
$startButtons.find('a:not(.disabled)').on(clickEvent, function(this: HTMLElement) { $startButtons.find('a:not(.disabled)').on(clickEvent, function(this: HTMLAnchorElement) {
$(this).addClass('active').siblings().removeClass('active'); $(this).addClass('active').siblings().removeClass('active');
li.loadCssPath('lobby.setup'); li.loadCssPath('lobby.setup');
lobby.leavePool(); lobby.leavePool();
$.ajax({ xhr.text(this.href)
url: $(this).attr('href'), .then(html => {
success: function(html) {
lobby.setup.prepareForm(modal(html, 'game-setup', () => { lobby.setup.prepareForm(modal(html, 'game-setup', () => {
$startButtons.find('.active').removeClass('active'); $startButtons.find('.active').removeClass('active');
})); }));
li.pubsub.emit('content_loaded'); li.pubsub.emit('content_loaded');
}, })
error: function(res) { .catch(li.reload);
if (res.status == 400) alert(res.responseText);
li.reload();
}
});
return false; return false;
}).on('click', function() { }).on('click', () => false);
return false;
});
if (['#ai', '#friend', '#hook'].includes(location.hash)) { if (['#ai', '#friend', '#hook'].includes(location.hash)) {
$startButtons $startButtons

View file

@ -153,9 +153,7 @@ export default class LobbyController {
clickPool = (id: string) => { clickPool = (id: string) => {
if (!this.data.me) { if (!this.data.me) {
xhr.anonPoolSeek(this.pools.find(function(p) { xhr.anonPoolSeek(this.pools.find(p => p.id == id)!);
return p.id === id;
}));
this.setTab('real_time'); this.setTab('real_time');
} else if (this.poolMember && this.poolMember.id === id) this.leavePool(); } else if (this.poolMember && this.poolMember.id === id) this.leavePool();
else { else {

View file

@ -1,6 +1,7 @@
import { FormStore, toFormLines, makeStore } from './form'; import { FormStore, toFormLines, makeStore } from './form';
import modal from 'common/modal'; import modal from 'common/modal';
import debounce from 'common/debounce'; import debounce from 'common/debounce';
import * as xhr from 'common/xhr';
import LobbyController from './ctrl'; import LobbyController from './ctrl';
const li = window.lichess; const li = window.lichess;
@ -206,11 +207,17 @@ export default class Setup {
this.root.redraw(); this.root.redraw();
} else { } else {
this.root.setTab($timeModeSelect.val() === '1' ? 'real_time' : 'seeks'); this.root.setTab($timeModeSelect.val() === '1' ? 'real_time' : 'seeks');
$.ajax({ xhr.text(
url: $form.attr('action').replace(/sri-placeholder/, li.sri), $form.attr('action').replace(/sri-placeholder/, li.sri),
data: $form.serialize() + "&color=" + color, {
type: 'post' method: 'post',
}); body: (() => {
const data = new FormData($form[0] as HTMLFormElement)
data.append('color', color);
console.log(data);
return data;
})()
});
} }
return false; return false;
}; };
@ -310,28 +317,21 @@ export default class Setup {
var validateFen = debounce(() => { var validateFen = debounce(() => {
$fenInput.removeClass("success failure"); $fenInput.removeClass("success failure");
var fen = $fenInput.val(); var fen = $fenInput.val();
if (fen) { if (fen) xhr.text(xhr.url($fenInput.parent().data('validate-url'), { fen }))
$.ajax({ .then(data => {
url: $fenInput.parent().data('validate-url'), $fenInput.addClass("success");
data: { $fenPosition.find('.preview').html(data);
fen: fen $fenPosition.find('a.board_editor').each(function(this: HTMLElement) {
}, $(this).attr('href', $(this).attr('href').replace(/editor\/.+$/, "editor/" + fen));
success: function(data) { });
$fenInput.addClass("success"); $submits.removeClass('nope');
$fenPosition.find('.preview').html(data); li.pubsub.emit('content_loaded');
$fenPosition.find('a.board_editor').each(function(this: HTMLElement) { })
$(this).attr('href', $(this).attr('href').replace(/editor\/.+$/, "editor/" + fen)); .catch(() => {
}); $fenInput.addClass("failure");
$submits.removeClass('nope'); $fenPosition.find('.preview').html("");
li.pubsub.emit('content_loaded'); $submits.addClass('nope');
}, })
error: function() {
$fenInput.addClass("failure");
$fenPosition.find('.preview').html("");
$submits.addClass('nope');
}
});
}
}, 200); }, 200);
$fenInput.on('keyup', validateFen); $fenInput.on('keyup', validateFen);

View file

@ -1,4 +1,5 @@
import { h } from 'snabbdom'; import { h } from 'snabbdom';
import * as xhr from 'common/xhr';
import { bind } from '../util'; import { bind } from '../util';
import LobbyController from '../../ctrl'; import LobbyController from '../../ctrl';
@ -71,21 +72,18 @@ export interface FilterNode extends HTMLElement {
filterLoaded?: boolean; filterLoaded?: boolean;
} }
export function render(ctrl: LobbyController) { export const render = (ctrl: LobbyController) =>
return h('div.hook__filters', { h('div.hook__filters', {
hook: { hook: {
insert(vnode) { insert(vnode) {
const el = vnode.elm as FilterNode; const el = vnode.elm as FilterNode;
if (el.filterLoaded) return; if (el.filterLoaded) return;
$.ajax({ xhr.text('/setup/filter')
url: '/setup/filter', .then(html => {
success(html) {
el.innerHTML = html; el.innerHTML = html;
el.filterLoaded = true; el.filterLoaded = true;
initialize(ctrl, el); initialize(ctrl, el);
} });
});
} }
} }
}); });
}

View file

@ -1,32 +1,19 @@
const headers = { import * as xhr from 'common/xhr';
'Accept': 'application/vnd.lichess.v3+json' import { Pool } from './interfaces';
};
export function seeks() { export const seeks = () => xhr.json('/lobby/seeks');
return $.ajax({
url: '/lobby/seeks',
headers: headers
});
}
export function nowPlaying() { export const nowPlaying = () => xhr.json('/account/now-playing').then(o => o.nowPlaying);
return $.ajax({
url: '/account/now-playing',
headers: headers
}).then(o => o.nowPlaying);
}
export function anonPoolSeek(pool) { export const anonPoolSeek = (pool: Pool) =>
return $.ajax({ xhr.json('/setup/hook/' + window.lichess.sri, {
method: 'POST', method: 'POST',
url: '/setup/hook/' + window.lichess.sri, body: xhr.form({
data: {
variant: 1, variant: 1,
timeMode: 1, timeMode: 1,
time: pool.lim, time: pool.lim,
increment: pool.inc, increment: pool.inc,
days: 1, days: 1,
color: 'random' color: 'random'
} })
}); });
}