| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
-
- namespace App\Http\Controllers\Concerns;
-
- use App\Models\Store;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SectionEditor;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\ValidationException;
- use Inertia\Inertia;
-
- /**
- * Shared steps for Design tab boxes that add, edit or remove a section.
- */
- trait EditsDesignSections
- {
- private function storeWithDesign(Request $request, string $slug): Store
- {
- $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
-
- abort_if($store->plan === null, 404);
-
- return $store;
- }
-
- /**
- * 404 unless the design has a section with this id and type.
- */
- private function assertSection(Store $store, string $sectionId, string $type): void
- {
- abort_unless(SectionEditor::typeOf($store->plan ?? [], $sectionId) === $type, 404);
- }
-
- /**
- * Check the changed design with the plan checker and save it. A problem is shown under the box,
- * in the checker's own words, and nothing is saved.
- *
- * @param array<string, mixed> $plan
- */
- private function saveDesign(Store $store, PlanValidator $validator, array $plan, string $savedMessage): RedirectResponse
- {
- try {
- $store->plan = $validator->validate($plan);
- } catch (ValidationException $e) {
- throw ValidationException::withMessages(['section' => collect($e->errors())->flatten()->first() ?? __('This change could not be saved.')]);
- }
-
- $store->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
- ? __('Saved. Click "Publish changes" to show it on your live store.')
- : $savedMessage]);
-
- return to_route('stores.show', $store->slug);
- }
- }
|