Skip to content

Commit

Permalink
fix: prevent delete fallback locale (#627)
Browse files Browse the repository at this point in the history
* fix: prevent delete fallback locale

* fix: prevent disable fallback locale
  • Loading branch information
andreiio authored Feb 21, 2024
1 parent a14610c commit acd6b41
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Admin/LanguageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Http\Controllers\Admin;

use App\Console\Commands\UpdateTranslationsCommand;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\LanguageRequest;
use App\Http\Resources\Collections\LanguageCollection;
use App\Http\Resources\LanguageResource;
Expand All @@ -18,7 +17,7 @@
use Inertia\Inertia;
use Inertia\Response;

class LanguageController extends Controller
class LanguageController extends AdminController
{
public function lines(?string $locale = null): JsonResponse
{
Expand Down Expand Up @@ -79,6 +78,7 @@ public function reset(): RedirectResponse
public function edit(Language $language): Response
{
return Inertia::render('Languages/Edit', [
'isFallback' => $language->isFallback(),
'resource' => LanguageResource::make($language),
'source' => LanguageLine::getTranslationsForGroup(auth()->user()->preferredLocale(), '*'),
'languages' => ISO_639_1::getCombinedLanguageOptions()
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public function getDirectionAttribute(): string
{
return ISO_639_1::getLanguageDirection($this->code);
}

public function isFallback(): bool
{
return app()->getFallbackLocale() === $this->code;
}
}
28 changes: 14 additions & 14 deletions app/Policies/LanguagePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LanguagePolicy
/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @param User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
Expand All @@ -26,8 +26,8 @@ public function viewAny(User $user)
/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $user
* @param \App\Models\Language $language
* @param User $user
* @param Language $language
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, Language $language)
Expand All @@ -38,7 +38,7 @@ public function view(User $user, Language $language)
/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @param User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
Expand All @@ -49,8 +49,8 @@ public function create(User $user)
/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\Language $language
* @param User $user
* @param Language $language
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update(User $user, Language $language)
Expand All @@ -61,20 +61,20 @@ public function update(User $user, Language $language)
/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Language $language
* @param User $user
* @param Language $language
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete(User $user, Language $language)
{
return $user->isAdmin();
return $user->isAdmin() && ! $language->isFallback();
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\Language $language
* @param User $user
* @param Language $language
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore(User $user, Language $language)
Expand All @@ -85,12 +85,12 @@ public function restore(User $user, Language $language)
/**
* Determine whether the user can permanently delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Language $language
* @param User $user
* @param Language $language
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete(User $user, Language $language)
{
return $user->isAdmin();
return $user->isAdmin() && ! $language->isFallback();
}
}
28 changes: 16 additions & 12 deletions resources/js/components/Form/Switch.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<template>
<switch-group as="div" class="flex items-center gap-3">
<switch-toggle
:disabled="disabled"
v-model="proxyChecked"
:class="[
proxyChecked ? 'bg-blue-600' : 'bg-gray-200',
'relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2',
]"
class="relative inline-flex flex-shrink-0 h-6 transition-colors duration-200 ease-in-out border-2 border-transparent rounded-full w-11 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"
:class="{
'bg-blue-600': proxyChecked && !disabled,
'bg-gray-200': !proxyChecked && !disabled,
'bg-blue-200': disabled,
'cursor-pointer': !disabled,
}"
>
<span
:class="[
Expand Down Expand Up @@ -39,7 +43,10 @@
<switch-label
v-if="label"
as="span"
class="text-sm font-medium text-gray-700 cursor-pointer"
class="text-sm font-medium text-gray-700"
:class="{
'cursor-pointer': !disabled,
}"
>
<span v-text="label" />

Expand All @@ -55,13 +62,10 @@
<switch-description
v-if="help"
as="span"
class="text-sm"
:class="{
'text-gray-300': $attrs.disabled,
'text-gray-500': !$attrs.disabled,
}"
v-text="help"
/>
class="text-sm text-gray-500"
>
{{ help }}
</switch-description>
</span>
</switch-group>
</template>
Expand Down
7 changes: 6 additions & 1 deletion resources/js/pages/Languages/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
required
/>

<form-switch :label="$t('field.enabled')" v-model="form.enabled" />
<form-switch
:label="$t('field.enabled')"
:disabled="isFallback"
v-model="form.enabled"
/>
</template>

<template #content="{ form }">
Expand Down Expand Up @@ -85,6 +89,7 @@
source: Object,
model: Object,
languages: Array,
isFallback: Boolean,
},
};
</script>

0 comments on commit acd6b41

Please sign in to comment.