Skip to content

Commit

Permalink
fix edit gallery on edit
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 17, 2023
1 parent 9ad2205 commit a799704
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
20 changes: 19 additions & 1 deletion app/Services/ProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Notifications\Admin\ProjectCreated as ProjectCreatedAdmin;
use App\Notifications\Ngo\ProjectCreated;
use Illuminate\Support\Facades\Notification;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class ProjectService
{
Expand All @@ -25,6 +26,21 @@ public function __construct($projectClass = null)
}
}

private static function updateGallery(Project $project, ?array $value): void
{
$mediaIds = collect($value)
->filter(fn ($item) => \is_array($item))
->pluck('id');
$project->getMedia('gallery')
->map(function (Media $media) use ($mediaIds) {
if (! $mediaIds->contains($media->id)) {
$media->delete();
}
});
collect($value)->filter(fn ($image) => ! \is_array($image))
->map(fn ($image) => $project->addMedia($image)->toMediaCollection('gallery'));
}

public function create(array $data): Project|RegionalProject
{
$data['organization_id'] = auth()->user()->organization_id;
Expand Down Expand Up @@ -103,11 +119,13 @@ public static function update(Project $project, array $attributes)
$key = $attributes->keys()->first();
$value = $attributes->get($key);

// dd($attributes,$value);

return match ($key) {
'counties' => $project->counties()->sync($value),
'categories' => $project->categories()->sync($value),
'image' => $project->addMedia($value)->toMediaCollection('preview'),
'gallery' => collect($value)->map(fn ($image) => $project->addMedia($image)->toMediaCollection('gallery')),
'gallery' => self::updateGallery($project, $value),

default => ($project->status === ProjectStatus::approved && \in_array($key, $project->requiresApproval))
? $project->fill($attributes->all())->saveForApproval()
Expand Down
18 changes: 17 additions & 1 deletion resources/js/Pages/AdminOng/Projects/EditProject.vue
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,23 @@
</div>
</template>
</Field>
<FileGroup v-model="originalProject.gallery" sync :label="$t('photo_gallery')" @removeImage="removeGalleryImage(id)"/>
<Field :label="$t('main_image')" :errors="formChangeStatus.errors.preview">
<template #value>
<div class="flex items-center col-span-12 gap-6 text-base font-medium leading-6 text-gray-700">
<img class="object-contain w-32 h-32 shrink-0" v-for="image in originalProject.gallery" :src="image.url" alt="" />

<div>
<EditModal
@action="editField('gallery')"
@cancel="resetField('gallery')"
:text="$t('change_image_label')"
>
<FileGroup v-model="originalProject.gallery" :label="$t('photo_gallery')"/>
</EditModal>
</div>
</div>
</template>
</Field>

</dl>

Expand Down

0 comments on commit a799704

Please sign in to comment.