Skip to content

Commit eb2a342

Browse files
dvlppaguingand
andauthored
Handle global filter in routes (#661)
* WIP * WIP tests * WIP fix more tests * fix show edit form url * update e2e * Fix tests * Add redirects * Set the global filter value according to the URL + tests * Add tests * Specify route parameters * Fix route parameters * Try to change phpunit conf * Remove session stuff for global filters * "Fix" 404 / throw * Fix phpunit config * Fix tests * Fix migration guide * Fix URL * Add {filterKey} on all web routes, fix tests * Fix global filter update * CS * fix global filter update * Fix breadcrumb URLs * Cleanup demo * CS * Fix tests --------- Co-authored-by: antoine <[email protected]>
1 parent 629a40c commit eb2a342

File tree

62 files changed

+841
-514
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+841
-514
lines changed

docs/guide/upgrading/9.0.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Be sure to [register this new Service Provider](https://laravel.com/docs/provide
113113

114114
## Middleware updates (legacy config only)
115115

116-
Due to migration to inertia, two middleware must be added to the config. Also, `SetSharpLocale` must be removed from `api` group.
116+
Due to migration to inertia, three middleware must be added to the config. Also, `SetSharpLocale` must be removed from `api` group.
117117

118118
::: info
119119
If you migrated to the new config builder class, you should be ok unless you have explicitly overridden the whole middleware list.
@@ -126,7 +126,11 @@ Here is the impact on the deprecated config file:
126126

127127
return [
128128
'middleware' => [
129-
// ...
129+
'common' => [
130+
// ...
131+
\Code16\Sharp\Http\Middleware\HandleGlobalFilters::class,
132+
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <- be sure to place this one after HandleGlobalFilters
133+
],
130134
'web' => [
131135
// ...
132136
\Code16\Sharp\Http\Middleware\HandleSharpErrors::class,

resources/js/Pages/Show/Show.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340
</template>
341341
<template v-if="show.authorizations.update">
342342
<Button class="h-8 pointer-events-auto" size="sm" :disabled="isReordering" as-child>
343-
<Link :as="isReordering ? 'button' : 'a'" :href="show.formUrl">
343+
<Link :as="isReordering ? 'button' : 'a'" :href="show.config.formEditUrl">
344344
{{ props.show.config.editButtonLabel || __('sharp::action_bar.show.edit_button') }}
345345
</Link>
346346
</Button>

resources/js/entity-list/components/EntityList.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import { computed, ref, watch } from "vue";
1414
import { showAlert, showDeleteConfirm } from "@/utils/dialogs";
1515
import { EntityListInstance, InstanceId } from "../types";
16-
import { getAppendableParentUri, route } from "@/utils/url";
16+
import { route } from "@/utils/url";
1717
import { Button } from '@/components/ui/button';
1818
import { api } from "@/api/api";
1919
import EntityListPagination from "@/entity-list/components/EntityListPagination.vue";
@@ -500,7 +500,7 @@
500500
class="h-8 gap-1"
501501
size="sm"
502502
:disabled="reordering || selecting"
503-
:href="route('code16.sharp.form.create', { parentUri: getAppendableParentUri(), entityKey })"
503+
:href="entityList.config.formCreateUrl"
504504
@click="onCreate"
505505
>
506506
{{ props.entityList.config.createButtonLabel || __('sharp::action_bar.list.create_button') }}

resources/js/filters/components/GlobalFilters.vue

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,42 @@
44
import { useFilters } from "../useFilters";
55
import { route } from "@/utils/url";
66
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
7+
import { useTemplateRef } from "vue";
8+
import { getCsrfToken } from "@/utils/request";
79
810
const props = defineProps<{
911
globalFilters: GlobalFiltersData,
1012
}>();
1113
1214
const filters = useFilters(props.globalFilters.config.filters, props.globalFilters.filterValues);
1315
14-
function onChanged(filter: FilterData, value: FilterData['value']) {
15-
router.post(
16-
route('code16.sharp.filters.update', { filterKey: filter.key }),
17-
{ value },
18-
{ preserveState: false, preserveScroll: false }
19-
);
20-
}
16+
const form = useTemplateRef<HTMLFormElement>('form');
2117
</script>
2218

2319
<template>
24-
<div class="grid gap-2">
25-
<template v-for="filter in filters.rootFilters" :key="filter.key">
26-
<template v-if="filter.type === 'select'">
27-
<Select
28-
:model-value="String(filters.currentValues[filter.key])"
29-
@update:model-value="onChanged(filter, $event as string)"
30-
>
31-
<SelectTrigger class="h-8">
32-
<SelectValue />
33-
</SelectTrigger>
34-
<SelectContent>
35-
<template v-for="value in filter.values">
36-
<SelectItem :value="String(value.id)">
37-
{{ value.label ?? value.id }}
38-
</SelectItem>
39-
</template>
40-
</SelectContent>
41-
</Select>
20+
<form :action="route('code16.sharp.filters.update')" method="post" ref="form">
21+
<input name="_token" :value="getCsrfToken()" type="hidden">
22+
<div class="grid gap-2">
23+
<template v-for="filter in filters.rootFilters" :key="filter.key">
24+
<template v-if="filter.type === 'select'">
25+
<Select
26+
:name="`filterValues[${filter.key}]`"
27+
v-model="filters.currentValues[filter.key]"
28+
@update:model-value="$nextTick(() => form.submit())"
29+
>
30+
<SelectTrigger class="h-8">
31+
<SelectValue />
32+
</SelectTrigger>
33+
<SelectContent>
34+
<template v-for="value in filter.values">
35+
<SelectItem :value="value.id">
36+
{{ value.label ?? value.id }}
37+
</SelectItem>
38+
</template>
39+
</SelectContent>
40+
</Select>
41+
</template>
4242
</template>
43-
</template>
44-
</div>
43+
</div>
44+
</form>
4545
</template>

resources/js/show/Show.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ShowListFieldData,
99
ShowTextFieldData
1010
} from "@/types";
11-
import { getAppendableParentUri, route } from "@/utils/url";
1211
import { config } from "@/utils/config";
1312

1413

@@ -31,21 +30,6 @@ export class Show implements ShowData {
3130
this.instanceId = instanceId;
3231
}
3332

34-
get formUrl(): string {
35-
if(route().params.instanceId) {
36-
return route('code16.sharp.form.edit', {
37-
parentUri: getAppendableParentUri(),
38-
entityKey: this.entityKey,
39-
instanceId: this.instanceId,
40-
});
41-
}
42-
43-
return route('code16.sharp.form.create', {
44-
parentUri: getAppendableParentUri(),
45-
entityKey: this.entityKey,
46-
});
47-
}
48-
4933
get instanceState(): string | number | null {
5034
return this.config.state
5135
? this.data[this.config.state.attribute] as any

resources/js/types/generated.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ export type SessionData = {
789789
export type SessionStatusLevel = "error" | "success";
790790
export type ShowConfigData = {
791791
deleteConfirmationText: string;
792+
formEditUrl: string;
792793
isSingle: boolean;
793794
commands: ConfigCommandsData | null;
794795
titleAttribute: string | null;

resources/js/types/routes.d.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ declare module 'ziggy-js' {
44
"code16.sharp.home": [],
55
"code16.sharp.dashboard": [
66
{
7-
"name": "dashboardKey",
7+
"name": "filterKey",
88
"required": true
9+
},
10+
{
11+
"name": "dashboardKey",
12+
"required": true,
13+
"binding": "key"
914
}
1015
],
1116
"code16.sharp.dashboard.filters.store": [
@@ -16,8 +21,13 @@ declare module 'ziggy-js' {
1621
],
1722
"code16.sharp.list": [
1823
{
19-
"name": "entityKey",
24+
"name": "filterKey",
2025
"required": true
26+
},
27+
{
28+
"name": "entityKey",
29+
"required": true,
30+
"binding": "key"
2131
}
2232
],
2333
"code16.sharp.list.filters.store": [
@@ -27,6 +37,10 @@ declare module 'ziggy-js' {
2737
}
2838
],
2939
"code16.sharp.single-show": [
40+
{
41+
"name": "filterKey",
42+
"required": true
43+
},
3044
{
3145
"name": "entityKey",
3246
"required": true,
@@ -44,6 +58,10 @@ declare module 'ziggy-js' {
4458
}
4559
],
4660
"code16.sharp.show.show": [
61+
{
62+
"name": "filterKey",
63+
"required": true
64+
},
4765
{
4866
"name": "parentUri",
4967
"required": true
@@ -73,6 +91,10 @@ declare module 'ziggy-js' {
7391
}
7492
],
7593
"code16.sharp.form.create": [
94+
{
95+
"name": "filterKey",
96+
"required": true
97+
},
7698
{
7799
"name": "parentUri",
78100
"required": true
@@ -95,6 +117,10 @@ declare module 'ziggy-js' {
95117
}
96118
],
97119
"code16.sharp.form.edit": [
120+
{
121+
"name": "filterKey",
122+
"required": true
123+
},
98124
{
99125
"name": "parentUri",
100126
"required": true
@@ -177,8 +203,13 @@ declare module 'ziggy-js' {
177203
],
178204
"code16.sharp.api.list": [
179205
{
180-
"name": "entityKey",
206+
"name": "filterKey",
181207
"required": true
208+
},
209+
{
210+
"name": "entityKey",
211+
"required": true,
212+
"binding": "key"
182213
}
183214
],
184215
"code16.sharp.api.list.filters.store": [
@@ -263,8 +294,13 @@ declare module 'ziggy-js' {
263294
],
264295
"code16.sharp.api.dashboard": [
265296
{
266-
"name": "dashboardKey",
297+
"name": "filterKey",
267298
"required": true
299+
},
300+
{
301+
"name": "dashboardKey",
302+
"required": true,
303+
"binding": "key"
268304
}
269305
],
270306
"code16.sharp.api.dashboard.filters.store": [

resources/js/utils/url.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { route as routeFn } from 'ziggy-js';
22
import { config } from "@/utils/config";
33

4-
export function getAppendableParentUri() {
5-
return location.pathname.replace(new RegExp(`^/${config('sharp.custom_url_segment')}/`), '');
6-
}
74

85
export function isSharpLink(url: string) {
96
return (

src/Config/SharpConfigBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SharpConfigBuilder
4444
\Illuminate\Session\Middleware\StartSession::class,
4545
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
4646
\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
47+
\Code16\Sharp\Http\Middleware\HandleGlobalFilters::class,
4748
\Illuminate\Routing\Middleware\SubstituteBindings::class,
4849
],
4950
'web' => [

src/Data/Show/ShowConfigData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class ShowConfigData extends Data
1313
{
1414
public function __construct(
1515
public string $deleteConfirmationText,
16+
public string $formEditUrl,
1617
public bool $isSingle = false,
1718
public ?ConfigCommandsData $commands = null,
1819
public ?string $titleAttribute = null,

0 commit comments

Comments
 (0)