AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditsDesignSections.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Controllers\Concerns;
  3. use App\Models\Store;
  4. use App\StorePlan\PlanValidator;
  5. use App\StorePlan\SectionEditor;
  6. use Illuminate\Http\RedirectResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Validation\ValidationException;
  9. use Inertia\Inertia;
  10. /**
  11. * Shared steps for Design tab boxes that add, edit or remove a section.
  12. */
  13. trait EditsDesignSections
  14. {
  15. private function storeWithDesign(Request $request, string $slug): Store
  16. {
  17. $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
  18. abort_if($store->plan === null, 404);
  19. return $store;
  20. }
  21. /**
  22. * 404 unless the design has a section with this id and type.
  23. */
  24. private function assertSection(Store $store, string $sectionId, string $type): void
  25. {
  26. abort_unless(SectionEditor::typeOf($store->plan ?? [], $sectionId) === $type, 404);
  27. }
  28. /**
  29. * Check the changed design with the plan checker and save it. A problem is shown under the box,
  30. * in the checker's own words, and nothing is saved.
  31. *
  32. * @param array<string, mixed> $plan
  33. */
  34. private function saveDesign(Store $store, PlanValidator $validator, array $plan, string $savedMessage): RedirectResponse
  35. {
  36. try {
  37. $store->plan = $validator->validate($plan);
  38. } catch (ValidationException $e) {
  39. throw ValidationException::withMessages(['section' => collect($e->errors())->flatten()->first() ?? __('This change could not be saved.')]);
  40. }
  41. $store->save();
  42. Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
  43. ? __('Saved. Click "Publish changes" to show it on your live store.')
  44. : $savedMessage]);
  45. return to_route('stores.show', $store->slug);
  46. }
  47. }