import $ from 'jquery';
import { ActionDialog } from 'vj/components/dialog';
import Notification from 'vj/components/notification';
import { NamedPage } from 'vj/misc/Page';
import { i18n, request, tpl } from 'vj/utils';
const page = new NamedPage('contest_manage', () => {
$(document).on('click', '[name="set_score"]', async (ev) => {
const pid = $(ev.currentTarget).data('pid');
const op = await new ActionDialog({
$body: tpl`
${i18n('Set Score for Contest')}
`,
onDispatch(action) {
if (action === 'ok' && $('[name="score"]').val() === null) {
$('[name="score"]').focus();
return false;
}
if (!Number.isFinite(+$('[name="score"]').val()) || +$('[name="score"]').val() <= 0) {
Notification.error('Invalid score');
return false;
}
return true;
},
}).open();
if (op !== 'ok') return;
try {
const res = await request.post('', {
operation: 'set_score',
pid,
score: $('[name="score"]').val(),
});
if (!res.error) {
Notification.success('Score Updated');
$(ev.currentTarget).text($('[name="score"]').val() as number);
$(ev.currentTarget).data('score', $('[name="score"]').val());
}
} catch (e) {
Notification.error(e.message);
}
});
});
export default page;