| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Models\Product;
- use App\Models\Store;
- use App\StorePlan\PlanSchema;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Inertia\Response;
-
- class StoreDesignController extends Controller
- {
- /**
- * The home page of the storefront, drawn from the store's saved plan.
- */
- public function preview(Request $request, string $slug): Response
- {
- return $this->render($this->ownedStore($request, $slug), ['type' => 'page', 'pageId' => PlanSchema::HOME_PAGE]);
- }
-
- /**
- * The shop page: every product shoppers may see.
- */
- public function shop(Request $request, string $slug): Response
- {
- return $this->render($this->ownedStore($request, $slug), ['type' => 'shop']);
- }
-
- /**
- * One product's own page.
- */
- public function product(Request $request, string $slug, string $productSlug): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- $product = $store->products()->active()->with('images')->where('slug', $productSlug)->firstOrFail();
-
- return $this->render($store, ['type' => 'product', 'product' => $product->toStorefront()]);
- }
-
- /**
- * Every storefront page gets the same basics: the plan, the fonts and the product list.
- *
- * @param array<string, mixed> $view Which page to show.
- */
- private function render(Store $store, array $view): Response
- {
- abort_if($store->plan === null, 404);
-
- return Inertia::render('stores/preview', [
- 'storeName' => $store->name,
- 'language' => $store->language->value,
- 'plan' => $store->plan,
- 'fonts' => PlanSchema::FONTS,
- // Links inside the storefront are built on top of this address.
- 'baseUrl' => route('stores.preview', $store->slug, false),
- 'view' => $view,
- 'products' => $store->products()
- ->active()
- ->with('images')
- ->latest('id')
- ->limit(60)
- ->get()
- ->map(fn (Product $product) => $product->toStorefront()),
- ]);
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
- }
|