Skip to content

Commit

Permalink
fix: change widget parent and emit to dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
daurensky committed Feb 15, 2024
1 parent 30c0a93 commit c3bef15
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 80 deletions.
3 changes: 1 addition & 2 deletions resources/lang/en/localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'localization_files_saved' => 'Localization files saved successfully',
],

'translation_file' => 'translation file',
'count_keys' => 'has :count key|has :count keys',
'count_keys' => ':count key|:count keys',
'not_created' => 'not created',
];
3 changes: 1 addition & 2 deletions resources/lang/ru/localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'localization_files_saved' => 'Файлы локализаций успешно сохранены',
],

'translation_file' => 'файл перевода',
'count_keys' => 'имеет :count ключ|имеет :count ключа|имеет :count ключей',
'count_keys' => ':count ключ|:count ключа|:count ключей',
'not_created' => 'не создан',
];
44 changes: 26 additions & 18 deletions resources/views/widgets/localization-files.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<x-filament::widget>
<x-filament::card>
{{-- Widget content --}}
@foreach(AdminKit::locales() as $locale)
{{Str::upper($locale)}} - {{__('admin-kit-localizations::localizations.translation_file')}}
@if ($exists[$locale])
<span class="hover:underline cursor-pointer font-bold"
wire:click="downloadTranslationFile('{{$locale}}')">{{$locale}}.json</span>
({{$sizes[$locale]}}), {{$counts[$locale]}}.
@else
<b>{{$locale}}.json</b> {{__('admin-kit-localizations::localizations.not_created')}}
@endif
<br/>
@endforeach
</x-filament::card>
<x-filament::section>
<div class="flex gap-4">
@foreach(AdminKit::locales() as $locale)
<div
class="flex flex-col gap-2 items-center w-full cursor-pointer dark:hover:bg-white/5 py-4 rounded-xl transition-colors"
wire:click="downloadTranslationFile('{{$locale}}')">
<x-filament::icon-button
icon="heroicon-o-arrow-down-tray"
size="xl"
>
<x-slot name="badge">
@if ($exists[$locale])
{{$counts[$locale]}}
@else
{{__('admin-kit-localizations::localizations.not_created')}}
@endif
</x-slot>
</x-filament::icon-button>
<span class="text-custom-400 text-xs" style="--c-400: var(--primary-400);">
{{$exists[$locale] ? $sizes[$locale] : '0.00 Kb'}}
</span>
<span class="font-bold text-sm">{{$locale}}.json</span>
</div>
@endforeach
</div>
</x-filament::section>
</x-filament::widget>

<style>
</style>
24 changes: 24 additions & 0 deletions src/Traits/LocalizationFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace AdminKit\Localizations\Traits;

use AdminKit\Core\Facades\AdminKit;
use Illuminate\Support\Facades\File;

trait LocalizationFiles
{
protected function addLocalization(string $key, array $values): void
{
foreach (AdminKit::locales() as $locale) {
$path = lang_path("$locale.json");

$jsonContent = file_exists($path)
? File::json($path)
: [];

$jsonContent[$key] = $values[$locale];

File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
}
}
13 changes: 7 additions & 6 deletions src/UI/Filament/Resources/LocalizationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace AdminKit\Localizations\UI\Filament\Resources;

use AdminKit\Core\Facades\AdminKit;
use AdminKit\Localizations\Models\Localization;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages;
use AdminKit\Localizations\UI\Filament\Resources\Widgets\LocalizationInformer;
use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Tables;
use AdminKit\Core\Facades\AdminKit;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use AdminKit\Localizations\Models\Localization;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages;
use AdminKit\Localizations\UI\Filament\Resources\Widgets\LocalizationInformer;

class LocalizationResource extends Resource
{
Expand Down Expand Up @@ -64,6 +64,7 @@ public static function table(Table $table): Table
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages;

use AdminKit\Core\Facades\AdminKit;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource;
use Filament\Resources\Pages\CreateRecord;
use AdminKit\Localizations\Traits\LocalizationFiles;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource;

class CreateLocalization extends CreateRecord
{
use LocalizationFiles;

protected static string $resource = LocalizationResource::class;

protected function getActions(): array
protected function getHeaderActions(): array
{
return [
//
Expand All @@ -24,23 +26,9 @@ protected function getRedirectUrl(): string

public function beforeCreate(): void
{
$this->updateLocalizationFile();
}

protected function updateLocalizationFile(): void
{
foreach (AdminKit::locales() as $locale) {
$key = $this->data['key'];
$value = $this->data['content'][$locale];
$path = lang_path($locale.'.json');

if (! file_exists($path)) {
file_put_contents($path, json_encode([$key => $value], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
} else {
$jsonContent = json_decode(file_get_contents($path), true);
$jsonContent[$key] = $value;
file_put_contents($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
}
$this->addLocalization(
$this->data['key'],
$this->data['content']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages;

use AdminKit\Core\Facades\AdminKit;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use AdminKit\Localizations\Traits\LocalizationFiles;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource;

class EditLocalization extends EditRecord
{
use LocalizationFiles;

protected static string $resource = LocalizationResource::class;

protected function getHeaderActions(): array
Expand All @@ -18,25 +20,11 @@ protected function getHeaderActions(): array
];
}

protected function beforeSave()
protected function beforeSave(): void
{
$this->updateLocalizationFile();
}

protected function updateLocalizationFile(): void
{
foreach (AdminKit::locales() as $locale) {
$key = $this->data['key'];
$value = $this->data['content'][$locale];
$path = lang_path("$locale.json");

if (! file_exists($path)) {
file_put_contents($path, json_encode([$key => $value], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
} else {
$jsonContent = json_decode(file_get_contents($path), true);
$jsonContent[$key] = $value;
file_put_contents($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
}
$this->addLocalization(
$this->data['key'],
$this->data['content']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages;

use Filament\Actions;
use Filament\Actions\Action;
use AdminKit\Core\Facades\AdminKit;
use Illuminate\Support\Facades\File;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use AdminKit\Localizations\Models\Localization;
use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource;
use AdminKit\Localizations\UI\Filament\Resources\Widgets\LocalizationInformer;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\File;

class ListLocalization extends ListRecords
{
Expand All @@ -33,16 +34,20 @@ protected function getHeaderActions(): array
];
}

public function publish()
public function publish(): void
{
$localizations = Localization::query()
->select('key', 'content')
->orderBy('id')
->get();

foreach (AdminKit::locales() as $locale) {
$path = lang_path("$locale.json");

$jsonContent = $localizations
->mapWithKeys(fn ($value, $key) => [$value->key => $value->getTranslation('content', $locale)])
->mapWithKeys(fn ($value, $key) => [
$value->key => $value->getTranslation('content', $locale),

Check failure on line 49 in src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Localizations\Models\Localization::$key.

Check failure on line 49 in src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Localizations\Models\Localization::$key.

Check failure on line 49 in src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Localizations\Models\Localization::$key.
])
->toArray();

File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
Expand All @@ -53,6 +58,6 @@ public function publish()
->success()
->send();

$this->emit('refreshLocalizationInformer'); // livewire event
$this->dispatch('refreshLocalizationInformer'); // livewire event
}
}
6 changes: 3 additions & 3 deletions src/UI/Filament/Resources/Widgets/LocalizationInformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace AdminKit\Localizations\UI\Filament\Resources\Widgets;

use Filament\Widgets\Widget;
use AdminKit\Core\Facades\AdminKit;
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class LocalizationInformer extends TableWidget
class LocalizationInformer extends Widget
{
protected int|string|array $columnSpan = 2;

Expand All @@ -22,7 +22,7 @@ public function render(): View
$exists[$locale] = File::exists($path);

if ($exists[$locale]) {
$sizes[$locale] = number_format(File::size($path) / 1024, 2).' Kb';
$sizes[$locale] = number_format(File::size($path) / 1024, 2) . ' Kb';
}

if ($exists[$locale]) {
Expand Down

0 comments on commit c3bef15

Please sign in to comment.