Skip to content

Commit

Permalink
fix(ema): enhance for vote form
Browse files Browse the repository at this point in the history
  • Loading branch information
nanozuki committed Feb 8, 2024
1 parent 1c750ec commit 07efb94
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
1 change: 0 additions & 1 deletion service/ema/src/lib/domain/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function getStage(c: Ceremony, time: Date): Stage {

export function ensureStage(ceremony: Ceremony, stage: Stage, now: Date): void {
const stageNow = getStage(ceremony, now);
console.log(`stageNow: ${stageNow}, stage: ${stage}`);
if (stageNow === stage) {
// valid, do nothing
return;
Expand Down
2 changes: 0 additions & 2 deletions service/ema/src/lib/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { error, type HttpError } from '@sveltejs/kit';
import { match, P } from 'ts-pattern';

function Database(operation: string, err: Error): HttpError {
console.log('err.message = ', err.message);
const he = error(500, {
title: '数据库错误',
message: `Database error ${operation}: ${err.message}`,
stack: err.stack,
});
console.log('new database error: ', JSON.stringify(he));
return he;
}

Expand Down
1 change: 0 additions & 1 deletion service/ema/src/lib/server/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ export class Service {
} else {
const voteItems = await this.voteRepository.getVotes(year, dept);
const result = await this.calculator.calculate(voteItems);
console.log(`calculated for ${dept}, get result: ${JSON.stringify(result)}`);
await this.workRepository.setWorkRanking(result);
results.set(dept, 'ok');
}
Expand Down
33 changes: 22 additions & 11 deletions service/ema/src/routes/[year]/(authed)/votes/[dept]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
import { Button, Nomination, TabLine } from '$lib/comp';
import { dataRangeString, type Work } from '$lib/domain/entity';
import { departmentInfo } from '$lib/assets';
import { enhance } from '$app/forms';
export let data: PageData;
export let form: ActionData;
let inputed = false;
const inputValue = (work: Work) => {
if (form?.rankings.has(work.id)) {
return form?.rankings.get(work.id);
} else {
return work.ranking;
}
const getWork = (work: Work, f?: ActionData) => {
let formRanking = f?.rankings.get(work.id) || 0;
work.ranking = formRanking > 0 ? formRanking : work.ranking;
return work;
};
const resetForm = () => {
location.reload();
};
$: works = data.works.map((work) => getWork(work, form));
$: deptTotal = data.ceremony.departments.length;
$: deptIndex = data.ceremony.departments.indexOf(data.department);
$: deptInfo = departmentInfo(data.ceremony.year)[data.department];
Expand Down Expand Up @@ -62,16 +62,27 @@

<!-- Voting Form --->

<form method="POST" class="w-full flex flex-col gap-y-4">
<form
method="POST"
class="w-full flex flex-col gap-y-4"
use:enhance={() => {
return async () => {
inputed = false;
works = works.sort((a, b) => (a.ranking || Infinity) - (b.ranking || Infinity));
};
}}
>
<p class="text-text font-serif leading-normal">请在左侧写入作品的排名数字</p>
{#if form?.error} <p class="text-rose leading-normal">{form?.error}</p> {/if}
{#each data.works as work (work.id)}
{#if form?.errors.get(work.id)}<p class="text-rose leading-normal">{form?.errors.get(work.id)}</p>{/if}
{#if form?.error}
<p class="text-rose leading-normal">{form?.error}</p>
{/if}
{#each works as work (work.id)}
{#if form?.errors?.get(work.id)}<p class="text-rose leading-normal">{form?.errors?.get(work.id)}</p>{/if}
<div class="w-full grid grid-cols-ballot gap-x-2 gap-y-4">
<input
type="number"
name={work.id.toString()}
value={inputValue(work)}
bind:value={work.ranking}
min="1"
class="[appearance:textfield] h-8 w-8 text-center leading-7 bg-surface rounded border-2 border-pine self-center focus:border-rose focus-visible:border-rose outline-none shadow-none"
on:input={() => (inputed = true)}
Expand Down

0 comments on commit 07efb94

Please sign in to comment.