Merge branch 'fixnewsfileinput' into 'develop'

fix: add last correction to news crud

See merge request ldc-web/projets/ldc!60
This commit is contained in:
karl.muller 2025-11-19 16:14:38 +00:00
commit d5563c1bd5
9 changed files with 168 additions and 110 deletions

View file

@ -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()]
];
}

View file

@ -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'
];
/**

View file

@ -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');
});
}

View file

@ -75,5 +75,4 @@ const GoToFrontView = (props: Props) => {
)
}
export default GoToFrontView;

View file

@ -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<string, string> | 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 (
<div className="flex flex-col items-start w-fit gap-1.5">
<Switch
name={name}
onCheckedChange={(value) => setData(name, value)}
required={required}
checked={checked}
disabled={disabled}
className={cn({ "border-destructive/50": localErrors[name] })}
/>
<Label htmlFor={name}>{label + (required ? " *" : "")}</Label>
<div className="flex items-center gap-3">
{leftHelper && (
<small>{leftHelper}</small>
)}
<Switch
name={name}
onCheckedChange={(value) => setData(name, value)}
required={required}
checked={checked}
disabled={disabled}
className={cn({ "border-destructive/50": localErrors[name] })}
/>
{rightHelper && (
<small>{rightHelper}</small>
)}
</div>
<InputError message={localErrors[name]} />
</div>
);

View file

@ -219,7 +219,6 @@ export default function Form({
return [buildPath(parent), currentTranslatedModelSlug].filter(Boolean).join("/");
};
console.log(buildPath(current));
return buildPath(current);
}

View file

@ -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<boolean>(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<number | null>(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<HTMLFormElement>) => {
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"
>
<SimpleSwitch
name={"inverted"}
checked={Boolean(data.inverted)}
setData={setData}
/>
<Card className="col-span-2 w-fit">
<div className="flex items-center gap-4">
<SimpleSwitch
label="Afficher l'image sur la page ?"
name={"has_show_picture"}
checked={data.has_show_picture}
setData={setData}
/>
<div className={cn(!data.has_show_picture ? 'hidden' : "flex items-center gap-4")}>
<Separator orientation="vertical" className="h-5" />
<SimpleSwitch
label="Ou placer l'image ?"
leftHelper="Gauche"
rightHelper="Droite"
name="inverted"
checked={data.inverted}
setData={setData}
/>
</div>
</div>
<Card className="w-full">
<CardContent className="pt-6 ">
<div className="flex items-center gap-4">
<Skeleton className={cn('h-[125px] w-[250px] rounded-xl', data.inverted ? 'order-2' : 'order-1')} />
<div className={cn('space-y-2', data.inverted ? 'order-1' : 'order-2')}>
<div className={cn("grid grid-cols-2 gap-3")}>
<Skeleton className={cn('h-[125px] w-full rounded-xl', { 'hidden': !data.has_show_picture }, data.inverted ? 'order-2' : 'order-1')} />
<div className={cn('space-y-2', { 'col-span-2': !data.has_show_picture }, data.inverted ? 'order-1' : 'order-2')}>
<Skeleton className="h-5 w-[80%]" />
<Skeleton className="h-5 w-[75%]" />
<Skeleton className="h-5 w-[50%]" />
<Skeleton className="h-5 w-[95%]" />
<Skeleton className="h-5 w-[79%]" />
</div>
{/* <div className={cn('space-y-2', data.inverted ? 'order-1' : 'order-2')}>
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
<Skeleton className="h-4 w-[300px]" />
<Skeleton className="h-4 w-[290px]" />
</div>
</div> */}
</div>
</CardContent>
</Card>
@ -333,19 +359,24 @@ export default function Form({
>
<div className="grid sm:grid-cols-2 gap-4 col-span-2 gap-4">
<div className="flex items-center justify-between gap-2 col-span-1">
<div className="flex items-center gap-2">
<small>Image</small>
<SimpleSwitch
name={"inputType"}
checked={isGif}
setData={(_, value) => {
setIsGif(value)
reset('picture')
setDynamicKey(Date.now())
}}
/>
<small>Gif</small>
</div>
<SimpleSwitch
name={"inputType"}
label="Type d'image"
leftHelper="Image"
rightHelper="GIF"
checked={isGif}
setData={(_, value) => {
setIsGif(value);
if (data.picture) {
setData(prev => ({
...prev,
picture: null
}));
}
setDynamicKey(Date.now());
}}
/>
{model && model.picture && (
<BtnOptionnalLinkWithTooltip
variant={'secondary'}
@ -375,7 +406,7 @@ export default function Form({
setData={setData}
reset={reset}
value={data.picture}
required={false}
required={true}
maxFileSize={"5120"}
allowedFormats={isGif ? 'gif' : "jpg,jpeg,png,webp"}
validationRules={isGif ? null : {
@ -390,7 +421,7 @@ export default function Form({
<div className="grid sm:grid-cols-2 col-span-2 gap-4">
<SimpleString
name="picture_alt"
required={false}
required={true}
label={str(
useTrans("back.form_associate_to", {
params: {
@ -405,7 +436,7 @@ export default function Form({
/>
<SimpleString
name="picture_title"
required={false}
required={true}
label={str(
useTrans("back.form_associate_to", {
params: {

View file

@ -32,13 +32,11 @@ export default function Show({ data }: { data: SingleResource }) {
};
const { viewAny } = auth.policies.news;
const newsModel = data.data as NewsModel;
console.log({ newsModel })
const actionModels = [
Views.EDITION,
Views.DUPLICATION,
Views.ARCHIVATION,
];
console.log(useLocale.use.timezone());
const columns = [
{
accessorKey: "seo_title",

View file

@ -115,6 +115,7 @@ type NewsModel = {
inverted: boolean;
status: number;
vimeo_id: string | null;
has_show_picture: boolean;
picture: string | null;
picture_alt: TranslationField | null;
picture_title: TranslationField | null;