Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/guide/upgrading/9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Be sure to [register this new Service Provider](https://laravel.com/docs/provide

## Middleware updates (legacy config only)

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

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

return [
'middleware' => [
// ...
'common' => [
// ...
\Code16\Sharp\Http\Middleware\HandleGlobalFilters::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <- be sure to place this one after HandleGlobalFilters
],
'web' => [
// ...
\Code16\Sharp\Http\Middleware\HandleSharpErrors::class,
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Show/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
</template>
<template v-if="show.authorizations.update">
<Button class="h-8 pointer-events-auto" size="sm" :disabled="isReordering" as-child>
<Link :as="isReordering ? 'button' : 'a'" :href="show.formUrl">
<Link :as="isReordering ? 'button' : 'a'" :href="show.config.formEditUrl">
{{ props.show.config.editButtonLabel || __('sharp::action_bar.show.edit_button') }}
</Link>
</Button>
Expand Down
4 changes: 2 additions & 2 deletions resources/js/entity-list/components/EntityList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { computed, ref, watch } from "vue";
import { showAlert, showDeleteConfirm } from "@/utils/dialogs";
import { EntityListInstance, InstanceId } from "../types";
import { getAppendableParentUri, route } from "@/utils/url";
import { route } from "@/utils/url";
import { Button } from '@/components/ui/button';
import { api } from "@/api/api";
import EntityListPagination from "@/entity-list/components/EntityListPagination.vue";
Expand Down Expand Up @@ -500,7 +500,7 @@
class="h-8 gap-1"
size="sm"
:disabled="reordering || selecting"
:href="route('code16.sharp.form.create', { parentUri: getAppendableParentUri(), entityKey })"
:href="entityList.config.formCreateUrl"
@click="onCreate"
>
{{ props.entityList.config.createButtonLabel || __('sharp::action_bar.list.create_button') }}
Expand Down
54 changes: 27 additions & 27 deletions resources/js/filters/components/GlobalFilters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@
import { useFilters } from "../useFilters";
import { route } from "@/utils/url";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { useTemplateRef } from "vue";
import { getCsrfToken } from "@/utils/request";

const props = defineProps<{
globalFilters: GlobalFiltersData,
}>();

const filters = useFilters(props.globalFilters.config.filters, props.globalFilters.filterValues);

function onChanged(filter: FilterData, value: FilterData['value']) {
router.post(
route('code16.sharp.filters.update', { filterKey: filter.key }),
{ value },
{ preserveState: false, preserveScroll: false }
);
}
const form = useTemplateRef<HTMLFormElement>('form');
</script>

<template>
<div class="grid gap-2">
<template v-for="filter in filters.rootFilters" :key="filter.key">
<template v-if="filter.type === 'select'">
<Select
:model-value="String(filters.currentValues[filter.key])"
@update:model-value="onChanged(filter, $event as string)"
>
<SelectTrigger class="h-8">
<SelectValue />
</SelectTrigger>
<SelectContent>
<template v-for="value in filter.values">
<SelectItem :value="String(value.id)">
{{ value.label ?? value.id }}
</SelectItem>
</template>
</SelectContent>
</Select>
<form :action="route('code16.sharp.filters.update')" method="post" ref="form">
<input name="_token" :value="getCsrfToken()" type="hidden">
<div class="grid gap-2">
<template v-for="filter in filters.rootFilters" :key="filter.key">
<template v-if="filter.type === 'select'">
<Select
:name="`filterValues[${filter.key}]`"
v-model="filters.currentValues[filter.key]"
@update:model-value="$nextTick(() => form.submit())"
>
<SelectTrigger class="h-8">
<SelectValue />
</SelectTrigger>
<SelectContent>
<template v-for="value in filter.values">
<SelectItem :value="value.id">
{{ value.label ?? value.id }}
</SelectItem>
</template>
</SelectContent>
</Select>
</template>
</template>
</template>
</div>
</div>
</form>
</template>
16 changes: 0 additions & 16 deletions resources/js/show/Show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ShowListFieldData,
ShowTextFieldData
} from "@/types";
import { getAppendableParentUri, route } from "@/utils/url";
import { config } from "@/utils/config";


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

get formUrl(): string {
if(route().params.instanceId) {
return route('code16.sharp.form.edit', {
parentUri: getAppendableParentUri(),
entityKey: this.entityKey,
instanceId: this.instanceId,
});
}

return route('code16.sharp.form.create', {
parentUri: getAppendableParentUri(),
entityKey: this.entityKey,
});
}

get instanceState(): string | number | null {
return this.config.state
? this.data[this.config.state.attribute] as any
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ export type SessionData = {
export type SessionStatusLevel = "error" | "success";
export type ShowConfigData = {
deleteConfirmationText: string;
formEditUrl: string;
isSingle: boolean;
commands: ConfigCommandsData | null;
titleAttribute: string | null;
Expand Down
44 changes: 40 additions & 4 deletions resources/js/types/routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ declare module 'ziggy-js' {
"code16.sharp.home": [],
"code16.sharp.dashboard": [
{
"name": "dashboardKey",
"name": "filterKey",
"required": true
},
{
"name": "dashboardKey",
"required": true,
"binding": "key"
}
],
"code16.sharp.dashboard.filters.store": [
Expand All @@ -16,8 +21,13 @@ declare module 'ziggy-js' {
],
"code16.sharp.list": [
{
"name": "entityKey",
"name": "filterKey",
"required": true
},
{
"name": "entityKey",
"required": true,
"binding": "key"
}
],
"code16.sharp.list.filters.store": [
Expand All @@ -27,6 +37,10 @@ declare module 'ziggy-js' {
}
],
"code16.sharp.single-show": [
{
"name": "filterKey",
"required": true
},
{
"name": "entityKey",
"required": true,
Expand All @@ -44,6 +58,10 @@ declare module 'ziggy-js' {
}
],
"code16.sharp.show.show": [
{
"name": "filterKey",
"required": true
},
{
"name": "parentUri",
"required": true
Expand Down Expand Up @@ -73,6 +91,10 @@ declare module 'ziggy-js' {
}
],
"code16.sharp.form.create": [
{
"name": "filterKey",
"required": true
},
{
"name": "parentUri",
"required": true
Expand All @@ -95,6 +117,10 @@ declare module 'ziggy-js' {
}
],
"code16.sharp.form.edit": [
{
"name": "filterKey",
"required": true
},
{
"name": "parentUri",
"required": true
Expand Down Expand Up @@ -177,8 +203,13 @@ declare module 'ziggy-js' {
],
"code16.sharp.api.list": [
{
"name": "entityKey",
"name": "filterKey",
"required": true
},
{
"name": "entityKey",
"required": true,
"binding": "key"
}
],
"code16.sharp.api.list.filters.store": [
Expand Down Expand Up @@ -263,8 +294,13 @@ declare module 'ziggy-js' {
],
"code16.sharp.api.dashboard": [
{
"name": "dashboardKey",
"name": "filterKey",
"required": true
},
{
"name": "dashboardKey",
"required": true,
"binding": "key"
}
],
"code16.sharp.api.dashboard.filters.store": [
Expand Down
3 changes: 0 additions & 3 deletions resources/js/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { route as routeFn } from 'ziggy-js';
import { config } from "@/utils/config";

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

export function isSharpLink(url: string) {
return (
Expand Down
1 change: 1 addition & 0 deletions src/Config/SharpConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SharpConfigBuilder
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
\Code16\Sharp\Http\Middleware\HandleGlobalFilters::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'web' => [
Expand Down
1 change: 1 addition & 0 deletions src/Data/Show/ShowConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ShowConfigData extends Data
{
public function __construct(
public string $deleteConfirmationText,
public string $formEditUrl,
public bool $isSingle = false,
public ?ConfigCommandsData $commands = null,
public ?string $titleAttribute = null,
Expand Down
23 changes: 14 additions & 9 deletions src/Filters/GlobalFilters/GlobalFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
use Code16\Sharp\Filters\Filter;
use Code16\Sharp\Filters\GlobalRequiredFilter;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;

final class GlobalFilters implements Arrayable
{
use HasFilters;

public static string $defaultKey = 'root';
public static string $valuesUrlSeparator = '~';
private ?Collection $globalFilters = null;

public function getFilters(): array
{
return collect(sharp()->config()->get('global_filters'))
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize())
->all();
if ($this->globalFilters === null) {
$this->globalFilters = collect(sharp()->config()->get('global_filters'))
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize())
->values();
}

return $this->globalFilters->all();
}

public function isEnabled(): bool
Expand All @@ -32,15 +41,11 @@ public function toArray(): array
'filterValues' => [
'default' => $this->filterContainer()->getFilterHandlers()
->flatten()
->mapWithKeys(function (Filter $handler) {
return [$handler->getKey() => $handler->defaultValue()];
})
->mapWithKeys(fn (Filter $handler) => [$handler->getKey() => $handler->defaultValue()])
->toArray(),
'current' => $this->filterContainer()->getFilterHandlers()
->flatten()
->mapWithKeys(function (Filter $handler) {
return [$handler->getKey() => $handler->currentValue()];
})
->mapWithKeys(fn (Filter $handler) => [$handler->getKey() => $handler->currentValue()])
->toArray(),
'valuated' => [], // not needed here
],
Expand Down
23 changes: 7 additions & 16 deletions src/Filters/GlobalRequiredFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@

abstract class GlobalRequiredFilter extends SelectRequiredFilter
{
private mixed $currentValue = null;

final public function currentValue(): mixed
{
$value = session()->get($this->getSessionKey());

if ($value === null) {
if (($value = $this->defaultValue()) !== null) {
session()->put($this->getSessionKey(), $value);
}
}

return $value;
return $this->currentValue !== null
? $this->currentValue
: $this->defaultValue();
}

final public function setCurrentValue(mixed $value): void
{
if ($value === null) {
session()->forget($this->getSessionKey());
$this->currentValue = null;

return;
}
Expand All @@ -29,12 +25,7 @@ final public function setCurrentValue(mixed $value): void
->where('id', $value)
->first();

session()->put($this->getSessionKey(), $formattedValue['id'] ?? null);
}

private function getSessionKey(): string
{
return '_sharp_retained_global_filter_'.$this->getKey();
$this->currentValue = $formattedValue['id'] ?? null;
}

public function authorize(): bool
Expand Down
4 changes: 1 addition & 3 deletions src/Filters/SelectFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ protected function formattedValues(): array

if (! is_array(collect($values)->first())) {
return collect($values)
->map(function ($label, $id) {
return compact('id', 'label');
})
->map(fn ($label, $id) => compact('id', 'label'))
->values()
->all();
}
Expand Down
Loading
Loading