Skip to content

Commit 2e7ae99

Browse files
authored
fix: Improved Help section (#400)
* fix: Search in HelpController * fix: Style for .container * fix: Improved design for Help section * fix: Working on the Referer and tests
1 parent 17b9cb0 commit 2e7ae99

File tree

7 files changed

+56
-33
lines changed

7 files changed

+56
-33
lines changed

app/Controllers/HelpController.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,19 @@ public function index()
1414
$markdownPages = service('markdownpages', ROOTPATH . 'help');
1515

1616
if ($this->request->is('post')) {
17+
$validReferer = $this->request->header('REFERER')?->getValueLine() === url_to('pages');
18+
$searchQuery = trim((string) $this->request->getPost('search'));
1719
$rules = [
18-
'search' => ['permit_empty', 'string', 'alpha_numeric_space', 'min_length[4]', 'max_length[32]'],
20+
'search' => ['string', 'alpha_numeric_space', 'min_length[4]', 'max_length[32]'],
1921
];
2022

21-
if (! $this->validateData($this->request->getPost(), $rules)) {
23+
if (! $this->validateData(['search' => $searchQuery], $rules)) {
2224
alerts()->set('error', $this->validator->getError('search'));
2325

24-
if ($this->request->header('REFERER')?->getValueLine() !== url_to('pages')) {
25-
return '';
26-
}
27-
28-
return $this->render('help/_index', ['pages' => $markdownPages]);
29-
}
30-
31-
$searchQuery = trim((string) $this->request->getPost('search'));
32-
33-
if ($searchQuery === '') {
34-
if ($this->request->header('REFERER')?->getValueLine() !== url_to('pages')) {
35-
return '';
36-
}
37-
38-
return $this->render('help/_index', ['pages' => $markdownPages]);
26+
return $validReferer ? $this->render('help/_index', ['pages' => $markdownPages]) : '';
3927
}
4028

41-
return $this->render('help/_search_results', ['search' => $markdownPages->search($searchQuery)]);
29+
return $validReferer ? $this->render('help/_search_results', ['search' => $markdownPages->search($searchQuery)]) : '';
4230
}
4331

4432
return $this->render('help/index', ['pages' => $markdownPages]);

tests/Controllers/HelpControllerTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ public function testIndex()
1515
$response = $this->get('help');
1616

1717
$response->assertOK();
18-
$response->assertSee('Help Section', 'h1');
18+
$response->assertSee('Help', 'h1');
1919
}
2020

2121
public function testIndexSearch()
2222
{
2323
$response = $this
2424
->withHeaders([
2525
csrf_header() => csrf_hash(),
26+
'REFERER' => url_to('pages'),
2627
])
2728
->post('help', [
2829
'search' => 'sample',
@@ -32,6 +33,22 @@ public function testIndexSearch()
3233
$response->assertSee('Search Results', 'h3');
3334
}
3435

36+
public function testIndexSearchWithoutReferrer()
37+
{
38+
$response = $this
39+
->withHeaders([
40+
csrf_header() => csrf_hash(),
41+
'REFERER' => '',
42+
'HX-Request' => 'true',
43+
])
44+
->post('help', [
45+
'search' => 'sample',
46+
]);
47+
48+
$this->assertSame(200, $response->response()->getStatusCode());
49+
$this->assertEmpty($response->response()->getBody());
50+
}
51+
3552
public function testIndexSearchEmpty()
3653
{
3754
$response = $this
@@ -45,6 +62,7 @@ public function testIndexSearchEmpty()
4562
]);
4663

4764
$response->assertStatus(200);
65+
$response->assertSee('The search field may only contain alphanumeric and space characters.', 'span');
4866
}
4967

5068
public function testIndexSearchValidationError()
@@ -56,7 +74,7 @@ public function testIndexSearchValidationError()
5674
csrf_header() => csrf_hash(),
5775
])
5876
->post('help', [
59-
'search' => 'sam',
77+
'search' => ' sam ',
6078
]);
6179

6280
$response->assertOK();
@@ -68,6 +86,7 @@ public function testIndexSearchNoResults()
6886
$response = $this
6987
->withHeaders([
7088
csrf_header() => csrf_hash(),
89+
'REFERER' => url_to('pages'),
7190
])
7291
->post('help', [
7392
'search' => 'invalid',

themes/default/css/app.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,14 @@ label.error {
199199
0% { transform: translateX(-100%); }
200200
100% { transform: translateX(100%); }
201201
}
202+
203+
@media (max-width: 640px) {
204+
.container {
205+
max-width:640px;
206+
padding-inline: 1rem;
207+
}
208+
209+
.navbar > .container {
210+
padding-inline: inherit !important;
211+
}
212+
}

themes/default/help/_header.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
<div class="hero-content text-center">
33
<div class="max-w-max">
44
<?php if (empty($page)): ?>
5-
<h1 class="text-5xl font-bold">Help Section</h1>
6-
<p class="py-6">Provident cupiditate voluptatem et in. Quaerat fugiat ut assumenda excepturi exercitationem quasi.</p>
5+
<h1 class="text-5xl font-bold">Help</h1>
6+
<p class="py-6">Do you have any questions? We have tried to answer them in this section.</p>
77
<?= form_open('', [
8-
'hx-post' => current_url(),
8+
'hx-post' => current_url(),
99
'hx-target' => '#help-content-container',
1010
]); ?>
1111
<div class="form-control">
12-
<div class="input-group flex-auto">
12+
<div class="input-group flex">
1313
<input type="text" name="search" value="<?= set_value('search', $search ?? ''); ?>" maxlength="32"
1414
placeholder="Search..." class="input input-bordered w-full">
15-
<button class="btn btn-square border border-base-300" type="submit" data-loading-disable>
15+
<button class="btn btn-square border border-base-300 mx-1 btn-primary" type="submit" data-loading-disable>
1616
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>
1717
</button>
1818
</div>

themes/default/help/_index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 w-full">
2+
13
<?php foreach ($pages->dirs(null, [1])->items() as $category): ?>
24

35
<div class="inline-block" hx-boost="true">
4-
<ul class="menu bg-base-200 w-64 rounded-box">
6+
<ul class="menu bg-base-200 rounded-box">
57
<li class="menu-title"><?= $category->getName(); ?></li>
68
<?php foreach ($category->getFiles()->slice(0, 4)->items() as $file): ?>
79
<li><a href="<?= url_to('page', $file->getPath()); ?>"><?= $file->getName(); ?></a></li>
@@ -10,3 +12,5 @@
1012
</div>
1113

1214
<?php endforeach; ?>
15+
16+
</div>

themes/default/help/_search_results.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php if ($search->getResults()->isEmpty()): ?>
22

3-
<h3>No results, sorry.</h3>
3+
<h3 class="font-bold">No results, sorry.</h3>
44

55
<?php else: ?>
66

7-
<h3>Search Results (<?= $search->getResults()->count(); ?>)</h3>
7+
<h3 class="font-bold">Search Results (<?= $search->getResults()->count(); ?>)</h3>
88

99
<div hx-boost="true">
1010
<?php foreach ($search->getResults()->items() as $key => $result): ?>
1111

12-
<div class="card card-compact bg-base-100 shadow-xl my-6">
12+
<div class="card card-compact bg-base-100 shadow-lg my-6">
1313
<div class="card-body">
1414
<p>
1515
<strong><?= ++$key; ?>.</strong>

themes/default/help/_sidebar.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
<h2>Help Section</h2>
1+
<h2>Search in Help Section</h2>
22

33
<?= form_open(route_to('pages'), [
4-
'hx-post' => route_to('pages'),
4+
'hx-post' => route_to('pages'),
55
'hx-target' => '#help-content-container',
6+
'class' => 'mt-2'
67
]); ?>
78
<div class="form-control">
8-
<div class="input-group flex-auto">
9+
<div class="input-group flex">
910
<input type="text" name="search" value="<?= set_value('search', $search ?? ''); ?>" maxlength="32"
1011
placeholder="Search..." class="input input-bordered w-full">
11-
<button class="btn btn-square border border-base-300" type="submit" data-loading-disable>
12+
<button class="btn btn-square border border-base-300 mx-1 btn-primary" type="submit" data-loading-disable>
1213
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>
1314
</button>
1415
</div>

0 commit comments

Comments
 (0)