From 5cace2cf824fbd193fa98fad5af5967598f8c9a1 Mon Sep 17 00:00:00 2001 From: K Muller Date: Wed, 19 Nov 2025 16:16:34 +0100 Subject: [PATCH] fix: add last correction to news crud --- .../Requests/Back/News/StoreNewsRequest.php | 50 +++---- app/Models/News.php | 32 ++--- ...63811_alter_news_fields_add_side_table.php | 34 +++-- .../ts/back/Components/GoToFrontView.tsx | 1 - .../back/Components/Inputs/SimpleSwitch.tsx | 32 +++-- resources/ts/back/Pages/DynamicPages/Form.tsx | 1 - resources/ts/back/Pages/News/Form.tsx | 125 +++++++++++------- resources/ts/back/Pages/News/Show.tsx | 2 - resources/ts/back/types/global.d.ts | 1 + 9 files changed, 168 insertions(+), 110 deletions(-) diff --git a/app/Http/Requests/Back/News/StoreNewsRequest.php b/app/Http/Requests/Back/News/StoreNewsRequest.php index 62a49aa1..1ecada43 100644 --- a/app/Http/Requests/Back/News/StoreNewsRequest.php +++ b/app/Http/Requests/Back/News/StoreNewsRequest.php @@ -6,11 +6,11 @@ use App\Enums\News\NewsCategoryEnum; use App\Enums\News\NewsStatusEnum; use App\Models\News; use App\Traits\Requests\HasInertiaPicture; -use GuzzleHttp\Psr7\UploadedFile; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Gate; use Illuminate\Validation\Rules\Enum; use Uwil\LaravelCommon\Rules\IsValidVimeoId; +use Uwil\LaravelCommon\Rules\WysiwygLength; class StoreNewsRequest extends FormRequest { @@ -51,33 +51,37 @@ class StoreNewsRequest extends FormRequest 'slug' => 'required|string|between:3,255', 'picture' => [ function ($attribute, $value, $fail) { - if (request()->file($attribute) && request()->file($attribute)->getClientOriginalExtension() === 'gif') { - $validator = \Validator::make( - [$attribute => $value], - [$attribute => 'nullable|file|mimes:gif|max:5120'] - ); - } else { - $validator = \Validator::make( - [$attribute => $value], - [$attribute => 'nullable|file|mimes:jpg,jpeg,png,webp|max:5120|dimensions:width=525,height=295'] - ); + $extension = null; + if ($value instanceof \Illuminate\Http\UploadedFile) { + $extension = strtolower($value->getClientOriginalExtension()); + } elseif (is_string($value)) { + $extension = strtolower(pathinfo($value, PATHINFO_EXTENSION)); } + if ($extension === 'gif') { + $rules = 'required|file|mimes:gif|max:5120'; + } else { + $rules = 'required|file|mimes:jpg,jpeg,png,webp|max:5120|dimensions:width=525,height=295'; + } + /** @var callable[] $validatorClosures */ + $validatorClosures = $this->pictureRules($rules); - if ($validator->fails()) { - $fail($validator->errors()->first($attribute)); + foreach ($validatorClosures as $validatorClosure) { + $validatorClosure($attribute, $value, $fail); } } ], - 'picture_alt' => 'nullable|string|between:3,60', - 'picture_title' => 'nullable|string|between:3,60', - 'content' => 'required|string|max:5000', - 'status' => ['required', new Enum(NewsStatusEnum::class)], - 'categories' => 'nullable|array', - 'categories.*' => ['integer', new Enum(NewsCategoryEnum::class)], - 'published_at' => 'nullable|date', - 'unpublished_at' => 'nullable|date|after:published_at', - 'inverted' => 'boolean', - 'vimeo_id' => ['string', 'nullable', 'min:8', 'max:10', new IsValidVimeoId()] + + 'picture_alt' => 'nullable|string|between:3,60', + 'picture_title' => 'nullable|string|between:3,60', + 'content' => ['required', 'string', new WysiwygLength(10, 5000)], + 'status' => ['required', new Enum(NewsStatusEnum::class)], + 'categories' => 'nullable|array', + 'categories.*' => ['integer', new Enum(NewsCategoryEnum::class)], + 'published_at' => 'nullable|date', + 'unpublished_at' => 'nullable|date|after:published_at', + 'inverted' => 'boolean', + 'has_show_picture' => 'boolean', + 'vimeo_id' => ['string', 'nullable', 'min:8', 'max:10', new IsValidVimeoId()] ]; } diff --git a/app/Models/News.php b/app/Models/News.php index 8110c721..0f38d55d 100644 --- a/app/Models/News.php +++ b/app/Models/News.php @@ -17,20 +17,21 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; /** - * @property string $seo_title SEO title of the news. - * @property string $seo_description SEO description of the news. - * @property string $title Title of the news. - * @property string $slug Slug of the news. - * @property string $picture Main img of the news. - * @property string $picture_alt Alt text for the picture. - * @property string $picture_title Title for the picture. - * @property string $content Content of the news. - * @property boolean $inverted Are image and content inverted in front - * @property string $vimeo_id Optionnal vimeo_id - * @property \App\Enums\News\NewsStatusEnum $status Status of the news. - * @property-read \Illuminate\Support\Carbon $created_at Date on which the news was created. - * @property-read \Illuminate\Support\Carbon $updated_at Date on which the news was updated. - * @property-read \Illuminate\Support\Carbon $deleted_at Date on which the news was updated. + * @property string $seo_title SEO title of the news. + * @property string $seo_description SEO description of the news. + * @property string $title Title of the news. + * @property string $slug Slug of the news. + * @property string $picture Main img of the news. + * @property string $picture_alt Alt text for the picture. + * @property string $picture_title Title for the picture. + * @property string $content Content of the news. + * @property boolean $inverted Are image and content inverted in front + * @property string $vimeo_id Optionnal vimeo_id + * @property boolean $has_show_picture Show the picture in the show page + * @property \App\Enums\News\NewsStatusEnum $status Status of the news. + * @property-read \Illuminate\Support\Carbon $created_at Date on which the news was created. + * @property-read \Illuminate\Support\Carbon $updated_at Date on which the news was updated. + * @property-read \Illuminate\Support\Carbon $deleted_at Date on which the news was updated. */ class News extends Model implements TranslatableModel { @@ -78,7 +79,8 @@ class News extends Model implements TranslatableModel 'published_at', 'unpublished_at', 'inverted', - 'vimeo_id' + 'vimeo_id', + 'has_show_picture' ]; /** diff --git a/database/migrations/2025_11_17_163811_alter_news_fields_add_side_table.php b/database/migrations/2025_11_17_163811_alter_news_fields_add_side_table.php index 4d50d0d3..8bd3e2a1 100644 --- a/database/migrations/2025_11_17_163811_alter_news_fields_add_side_table.php +++ b/database/migrations/2025_11_17_163811_alter_news_fields_add_side_table.php @@ -8,28 +8,35 @@ return new class extends Migration { /** * Run the migrations. + * + * @return void */ public function up(): void { Schema::table('news', function (Blueprint $table) { - $table->mediumText('content')->comment('Body of the news, wysiwyg to be more specific')->change(); - $table->text('seo_title')->nullable(false)->comment('Seo title for the news')->change(); - $table->text('seo_description')->nullable(false)->comment('Seo description of the news')->change(); - $table->text('picture_alt')->nullable()->comment('Alt of the picture of the news')->change(); - $table->text('picture_title')->nullable()->comment('Title of the picture of the news')->change(); - $table->text('title')->nullable()->comment('Title of the news')->change(); - $table->text('slug')->nullable()->comment('Slug picture of the news')->change(); - $table->boolean('inverted')->default(false)->comment('Side of the text/image in the front display'); - $table->text('vimeo_id')->nullable()->comment('Vimeo id optionnal'); - }); - Schema::table('news', function (Blueprint $table) { - $table->text('slug')->nullable()->comment('Slug picture of the news')->change(); $table->dropUnique(['slug']); + $table->text('slug')->nullable()->comment('Slug picture of the news.')->change(); + }); + + Schema::table('news', function (Blueprint $table) { + $table->text('seo_title')->nullable(false)->comment('News seo title.')->change(); + $table->text('seo_description')->nullable(false)->comment('News seo description.')->change(); + $table->text('title')->nullable(false)->comment('News title.')->change(); + $table->string('picture')->nullable(false)->comment('News picture.')->change(); + $table->text('picture_alt')->nullable(false)->comment('Picture alt attribute.')->change(); + $table->text('picture_title')->nullable(false)->comment('Picture title attribute.')->change(); + $table->mediumText('content')->nullable(false)->comment('News content.')->change(); + $table->boolean('inverted')->default(false)->comment('Content position.')->after('status'); + $table->boolean('has_show_picture')->default(false)->comment('Show the picture in the show.')->after('status'); + $table->text('vimeo_id')->nullable()->comment('Vimeo id associated.')->after('content'); + $table->unsignedTinyInteger('status')->comment('News status.')->change(); }); } /** * Reverse the migrations. + * + * @return void */ public function down(): void { @@ -39,13 +46,14 @@ return new class extends Migration }); Schema::table('news', function (Blueprint $table) { - $table->text('content')->change(); $table->string('seo_title')->nullable(false)->comment('Seo title for the news')->change(); $table->string('seo_description')->nullable(false)->comment('Seo description of the news')->change(); $table->string('picture_alt')->nullable()->comment('Alt of the picture of the news')->change(); $table->string('picture_title')->nullable()->comment('Title of the picture of the news')->change(); $table->string('title')->nullable()->comment('Title of the news')->change(); + $table->text('content')->change(); $table->dropColumn('inverted'); + $table->dropColumn('has_show_picture'); $table->dropColumn('vimeo_id'); }); } diff --git a/resources/ts/back/Components/GoToFrontView.tsx b/resources/ts/back/Components/GoToFrontView.tsx index 36b9d291..81069715 100644 --- a/resources/ts/back/Components/GoToFrontView.tsx +++ b/resources/ts/back/Components/GoToFrontView.tsx @@ -75,5 +75,4 @@ const GoToFrontView = (props: Props) => { ) } - export default GoToFrontView; diff --git a/resources/ts/back/Components/Inputs/SimpleSwitch.tsx b/resources/ts/back/Components/Inputs/SimpleSwitch.tsx index 9ea537cc..a41e2d72 100644 --- a/resources/ts/back/Components/Inputs/SimpleSwitch.tsx +++ b/resources/ts/back/Components/Inputs/SimpleSwitch.tsx @@ -5,10 +5,14 @@ import { Switch } from "@/components/ui/switch"; // * Other import InputError from "@/back/Components/InputError"; import { cn } from "@/lib/utils"; +import { Label } from "@/components/ui/label"; type SimpleSwitchProps = { name: string; checked?: boolean; + label: string; + leftHelper?: string; + rightHelper?: string; required?: boolean; disabled?: boolean; parentErrors?: Record | null, @@ -19,6 +23,9 @@ type SimpleSwitchProps = { export default function SimpleSwitch({ name, checked, + label, + leftHelper, + rightHelper, required = false, disabled = false, parentErrors = null, @@ -29,14 +36,23 @@ export default function SimpleSwitch({ return (
- setData(name, value)} - required={required} - checked={checked} - disabled={disabled} - className={cn({ "border-destructive/50": localErrors[name] })} - /> + +
+ {leftHelper && ( + {leftHelper} + )} + setData(name, value)} + required={required} + checked={checked} + disabled={disabled} + className={cn({ "border-destructive/50": localErrors[name] })} + /> + {rightHelper && ( + {rightHelper} + )} +
); diff --git a/resources/ts/back/Pages/DynamicPages/Form.tsx b/resources/ts/back/Pages/DynamicPages/Form.tsx index bd1e205c..1a0d9eff 100644 --- a/resources/ts/back/Pages/DynamicPages/Form.tsx +++ b/resources/ts/back/Pages/DynamicPages/Form.tsx @@ -219,7 +219,6 @@ export default function Form({ return [buildPath(parent), currentTranslatedModelSlug].filter(Boolean).join("/"); }; - console.log(buildPath(current)); return buildPath(current); } diff --git a/resources/ts/back/Pages/News/Form.tsx b/resources/ts/back/Pages/News/Form.tsx index 71be1592..5045274e 100644 --- a/resources/ts/back/Pages/News/Form.tsx +++ b/resources/ts/back/Pages/News/Form.tsx @@ -2,7 +2,7 @@ import { useForm, usePage } from "@inertiajs/react"; import { BookText, Cog, RefreshCw } from "lucide-react"; import { route } from "ziggy-js"; -import { FormEvent, useEffect, useState } from "react"; +import { FormEvent, useState } from "react"; // * Other import SimpleString from "@/back/Components/Inputs/SimpleString"; import FieldsetWrapper from "@/back/Components/FieldsetWrapper"; @@ -26,6 +26,7 @@ import { Skeleton } from "@/components/ui/skeleton"; import { cn } from "@/lib/utils"; import { Card, CardContent } from "@/components/ui/card"; import SimpleVimeoId from "@/back/Components/Inputs/SimpleVimeoId"; +import { Separator } from "@/components/ui/separator"; export default function Form({ model, @@ -42,7 +43,16 @@ export default function Form({ }) { const { locale } = usePage().props.appEnv as AppEnv; const { toast } = useToast(); - const [isGif, setIsGif] = useState(false); + const [isGif, setIsGif] = useState(() => { + if (model?.picture && typeof model.picture === "string") { + return model.picture.toLowerCase().endsWith(".gif"); + } + if (isFile(data.picture)) { + return data.picture.name.toLowerCase().endsWith(".gif"); + } + return false; + }); + const [dynamicKey, setDynamicKey] = useState(null); const enumDynamicFieldTypes = usePage().props.enumDynamicFieldTypes as DynamicFieldTypes; const enumNewsStatus = usePage().props.enumNewsStatus as LaravelEnum; @@ -69,25 +79,8 @@ export default function Form({ ), ).ucFirst().value(); - useEffect(() => { - if (model && model.picture) { - const isGif = typeof model.picture === "string" && model.picture.toLowerCase().endsWith(".gif"); - setIsGif(isGif); - } - }, [model]); - - useEffect(() => { - setData('picture', null) - data.picture = null; - }, [isGif]) - - const refreshFileFromDb = () => { - if (model && model.picture) { - const isGif = typeof model.picture === "string" && model.picture.toLowerCase().endsWith(".gif"); - setIsGif(isGif); - setData('picture', model.picture) - setDynamicKey(Date.now()) - } + function isFile(value: unknown): value is File { + return value instanceof File; } /** Form section */ @@ -99,6 +92,7 @@ export default function Form({ slug: model?.slug ? model?.slug[locale] : "", content: model?.content ? model?.content[locale] : "", inverted: model?.inverted ?? false, + has_show_picture: model?.has_show_picture ?? true, vimeo_id: model?.vimeo_id ?? "", status: model?.status ?? enumNewsStatus.unpublished.value, picture: model?.picture, @@ -109,6 +103,16 @@ export default function Form({ unpublished_at: model?.unpublished_at ? model?.unpublished_at : null }); + + const refreshFileFromDb = () => { + if (model && model.picture) { + const isGif = typeof model.picture === "string" && model.picture.toLowerCase().endsWith(".gif"); + setIsGif(isGif); + setData('picture', model.picture) + setDynamicKey(Date.now()) + } + } + const submit = (e: FormEvent) => { e.preventDefault(); const routeUrl = route(`back.${modelRoute}.${action}`, model?.id); @@ -306,21 +310,43 @@ export default function Form({ description={useTrans("back.form_subtitle_display")} className="sm:grid-cols-2 gap-4" > - - +
+ +
+ + +
+
+ -
- -
+
+ +
+ + + + + +
+ + {/*
-
+
*/}
@@ -333,19 +359,24 @@ export default function Form({ >
-
- Image - { - setIsGif(value) - reset('picture') - setDynamicKey(Date.now()) - }} - /> - Gif -
+ { + setIsGif(value); + if (data.picture) { + setData(prev => ({ + ...prev, + picture: null + })); + } + setDynamicKey(Date.now()); + }} + /> + {model && model.picture && (