Skip to content

Commit

Permalink
feat: set menu items product to header
Browse files Browse the repository at this point in the history
  • Loading branch information
phojie committed Dec 31, 2022
1 parent ab90721 commit e2c0678
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 68 deletions.
13 changes: 13 additions & 0 deletions app/Http/Controllers/Components/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Resources\ProductResource;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController
{
Expand All @@ -24,4 +25,16 @@ public function show($id)

return response()->json($product, 200);
}

public function random(Request $request)
{
$limit = $request->limit ?? 3;

$query = Product::query()
->inRandomOrder()
->limit($limit)
->get();

return response()->json($query, 200);
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/Customer/ProductController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers\Customer;

use App\Http\Resources\ProductResource;
use App\Models\Product;

class ProductController
{
public function show(string $slug)
{
$category = Product::where('slug', $slug)->firstOrFail();

return new ProductResource($category);
}
}
82 changes: 16 additions & 66 deletions resources/js/Components/App/AppHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,11 @@ const navigation = {
categories: [
{
name: 'Category',
featured: [
{
name: 'New Arrivals',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-category-01.jpg',
imageAlt: 'Models sitting back to back, wearing Basic Tee in black and bone.',
},
{
name: 'Basic Tees',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-category-02.jpg',
imageAlt:
'Close up of Basic Tee fall bundle with off-white, ochre, olive, and black tees.',
},
{
name: 'Accessories',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-category-03.jpg',
imageAlt:
'Model wearing minimalist watch with black wristband and white watch face.',
},
{
name: 'Carry',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-category-04.jpg',
imageAlt:
'Model opening tan leather long wallet with credit card pockets and cash pouch.',
},
],
featured: [],
},
{
name: 'Products',
featured: [
{
name: 'New Arrivals',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-01-men-category-01.jpg',
imageAlt:
'Hats and sweaters on wood shelves next to various colors of t-shirts on hangers.',
},
{
name: 'Basic Tees',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-01-men-category-02.jpg',
imageAlt: 'Model wearing light heather gray t-shirt.',
},
{
name: 'Accessories',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-01-men-category-03.jpg',
imageAlt:
'Grey 6-panel baseball hat with black brim, black mountain graphic on front, and light heather gray body.',
},
{
name: 'Carry',
href: '#',
imageSrc:
'https://tailwindui.com/img/ecommerce-images/mega-menu-01-men-category-04.jpg',
imageAlt:
'Model putting folded cash into slim card holder olive leather wallet with hand stitching.',
},
],
featured: [],
},
],
pages: [
Expand All @@ -96,6 +32,20 @@ onMounted(async () => {
}
})
})
await useFetch(route('components.products.random')).get().json().then(({ data }) => {
// map data
navigation.categories[1].featured = _.map(data.value, (product) => {
return {
name: product.name,
href: route('products.show', {
slug: product.slug,
}),
imageSrc: product.image,
imageAlt: product.name,
}
})
})
})
</script>

Expand Down
2 changes: 1 addition & 1 deletion resources/js/ziggy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion routes/components/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use App\Http\Controllers\Components\TemporaryFileController;
use App\Http\Controllers\Components\UserController;


// unauthorized
Route::get('components/categories/random', [CategoryController::class, 'random'])->name('components.categories.random');
Route::get('components/products/random', [ProductController::class, 'random'])->name('components.products.random');

// authorized
Route::prefix('components')->middleware('auth')->group(function () {
Expand Down
11 changes: 11 additions & 0 deletions routes/public/product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use App\Http\Controllers\Customer\ProductController;
use Illuminate\Support\Facades\Route;

Route::prefix('products')->group(function () {
Route::controller(ProductController::class)->group(function () {
// show
Route::get('/{slug}', 'show')->name('products.show');
});
});
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Route::middleware(['auth', 'customer'])->group(function () {
require __DIR__.'/customer/index.php';
require __DIR__.'/public/category.php';
require __DIR__.'/public/product.php';
});

// admin routes
Expand Down

0 comments on commit e2c0678

Please sign in to comment.