Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit b4e27a3

Browse files
committed
feat(challenge): Fill previous answer & disallow repeated submit
1 parent 5bfaddb commit b4e27a3

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

assets/controllers/challenge_executor_controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export default class extends Controller<HTMLElement> {
2525
throw new Error(`Element not found: ${editorSelector}`);
2626
}
2727

28+
const lastQuery = this.element.dataset["lastQuery"];
29+
2830
this.view = new EditorView({
31+
doc: lastQuery,
2932
extensions: [
3033
basicSetup,
3134
sql(),

src/Twig/Components/Challenge/Executor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Entity\SolutionEvent;
99
use App\Entity\SolutionEventStatus;
1010
use App\Entity\User;
11+
use App\Repository\SolutionEventRepository;
1112
use App\Service\QuestionDbRunnerService;
1213
use Doctrine\ORM\EntityManagerInterface;
1314
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -26,6 +27,7 @@ final class Executor
2627

2728
public function __construct(
2829
private readonly QuestionDbRunnerService $questionDbRunnerService,
30+
private readonly SolutionEventRepository $solutionEventRepository,
2931
private readonly EntityManagerInterface $entityManager,
3032
) {
3133
}
@@ -42,6 +44,16 @@ public function __construct(
4244
#[LiveProp(writable: true)]
4345
public string $query;
4446

47+
public function getPreviousQuery(): string
48+
{
49+
$se = $this->solutionEventRepository->findOneBy([
50+
'question' => $this->question,
51+
'submitter' => $this->user,
52+
], orderBy: ['id' => 'DESC']);
53+
54+
return $se?->getQuery() ?? '';
55+
}
56+
4557
#[LiveAction]
4658
public function execute(SerializerInterface $serializer): void
4759
{

templates/components/Challenge/Executor.html.twig

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1+
{% set previousQuery = this.previousQuery %}
2+
13
<section {{ attributes.defaults({class: 'challenge-sql-executor'}|merge(stimulus_controller('challenge-executor', {
24
modelName: 'query',
35
editorSelector: '.challenge-sql-executor__editor',
4-
}))) }}>
6+
}))) }} data-last-query="{{ previousQuery }}">
57
<section class="challenge-sql-executor__actions hstack">
68
<div class="challenge-sql-executor__actions__left me-auto">
79
{% block actions %}{% endblock %}
810
</div>
911

1012
<div class="challenge-sql-executor__actions__right">
11-
<button type="button" class="btn btn-primary"
12-
data-loading="action(execute)|addAttribute(disabled)"
13-
data-action="live#action"
14-
data-live-action-param="execute">
15-
<i class="bi bi-send"></i>
16-
提交
17-
</button>
13+
{% if query is not empty and query is not same as(previousQuery) %}
14+
<button type="button" class="btn btn-primary"
15+
data-loading="action(execute)|addAttribute(disabled)"
16+
data-action="live#action"
17+
data-live-action-param="execute">
18+
<i class="bi bi-send"></i>
19+
提交
20+
</button>
21+
{% else %}
22+
<button type="button" class="btn btn-primary" disabled>
23+
<i class="bi bi-send"></i>
24+
提交
25+
</button>
26+
{% endif %}
1827
</div>
1928
</section>
2029
<hr>

0 commit comments

Comments
 (0)