| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
-
- namespace App\Http\Controllers\Concerns;
-
- use App\Commerce\Cart;
- use App\Models\Product;
- use App\Models\Store;
- use App\StorePlan\PlanSchema;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Inertia\Response;
-
- /**
- * Shared by the controllers that show storefront pages.
- * For now the storefront is a preview that only the store's owner can open. Step 5 makes it public.
- */
- trait RendersStorefront
- {
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
-
- private function cart(Request $request, Store $store): Cart
- {
- return new Cart($request->session(), $store);
- }
-
- /**
- * Every storefront page gets the same basics: the plan, the fonts, the product list and the cart.
- *
- * @param array<string, mixed> $view Which page to show.
- */
- private function renderStorefront(Request $request, 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()),
- 'cart' => $this->cart($request, $store)->toStorefront(),
- ]);
- }
- }
|