Do the search

This commit is contained in:
2024-06-22 16:54:14 +01:00
parent fadb1bc00a
commit 01002b31a7
10 changed files with 262 additions and 7 deletions

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Http\Controllers;
use Inertia\Inertia;
class DashboardController extends Controller
{
public function index()
{
return Inertia::render('Dashboard');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers;
use App\Http\Integrations\Spoonacular\Requests\FindRecipesByIngredients;
use App\Http\Integrations\Spoonacular\SpoonacularConnector;
use App\Http\Requests\RecipeSearchRequest;
use Inertia\Inertia;
class SearchController extends Controller
{
public function recipes(RecipeSearchRequest $request)
{
$data = $request->validated();
$connector = new SpoonacularConnector();
$request = new FindRecipesByIngredients(
ingredients: array_map("trim", explode(",", $data['ingredients'])),
ranking: 2
);
$response = $connector->send($request);
return Inertia::render('Search/Recipes', [
'recipes' => $response->json(),
'previousSearch' => $data['ingredients']
]);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RecipeSearchRequest extends FormRequest
{
public function rules(): array
{
return [
'ingredients' => ['required', 'string', 'min:3']
];
}
public function authorize(): bool
{
return true;
}
}

56
package-lock.json generated
View File

@@ -5,6 +5,8 @@
"packages": {
"": {
"devDependencies": {
"@headlessui/vue": "^1.7.22",
"@heroicons/vue": "^2.1.4",
"@inertiajs/vue3": "^1.0.0",
"@tailwindcss/forms": "^0.5.3",
"@vitejs/plugin-vue": "^5.0.0",
@@ -434,6 +436,32 @@
"node": ">=12"
}
},
"node_modules/@headlessui/vue": {
"version": "1.7.22",
"resolved": "https://registry.npmjs.org/@headlessui/vue/-/vue-1.7.22.tgz",
"integrity": "sha512-Hoffjoolq1rY+LOfJ+B/OvkhuBXXBFgd8oBlN+l1TApma2dB0En0ucFZrwQtb33SmcCqd32EQd0y07oziXWNYg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@tanstack/vue-virtual": "^3.0.0-beta.60"
},
"engines": {
"node": ">=10"
},
"peerDependencies": {
"vue": "^3.2.0"
}
},
"node_modules/@heroicons/vue": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/@heroicons/vue/-/vue-2.1.4.tgz",
"integrity": "sha512-wykVSZ/fqEG49lIeHgFGT9TCvBw9THuRTtA/sPp7FVk3iBob/HcmitMcLDwtXOW82TXb38HeLRl1/pcElPeSdg==",
"dev": true,
"license": "MIT",
"peerDependencies": {
"vue": ">= 3"
}
},
"node_modules/@inertiajs/core": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@inertiajs/core/-/core-1.2.0.tgz",
@@ -819,6 +847,34 @@
"tailwindcss": ">=3.0.0 || >= 3.0.0-alpha.1"
}
},
"node_modules/@tanstack/virtual-core": {
"version": "3.7.0",
"resolved": "https://registry.npmjs.org/@tanstack/virtual-core/-/virtual-core-3.7.0.tgz",
"integrity": "sha512-p0CWuqn+n8iZmsL7/l0Xg7kbyIKnHNqkEJkMDOkg4x3Ni3LohszmnJY8FPhTgG7Ad9ZFGcdKmn1R1mKUGEh9Xg==",
"dev": true,
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/vue-virtual": {
"version": "3.7.0",
"resolved": "https://registry.npmjs.org/@tanstack/vue-virtual/-/vue-virtual-3.7.0.tgz",
"integrity": "sha512-RkSrajvJpV1RdJKgZnPgzyzVVx76QjPAu+spgdAms+SZRcSbYMUKlcjusnHjhszck5ngHXSXbSBp45ycF1nlDw==",
"dev": true,
"license": "MIT",
"dependencies": {
"@tanstack/virtual-core": "3.7.0"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"vue": "^2.7.0 || ^3.0.0"
}
},
"node_modules/@types/estree": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz",

View File

@@ -6,6 +6,8 @@
"build": "vite build"
},
"devDependencies": {
"@headlessui/vue": "^1.7.22",
"@heroicons/vue": "^2.1.4",
"@inertiajs/vue3": "^1.0.0",
"@tailwindcss/forms": "^0.5.3",
"@vitejs/plugin-vue": "^5.0.0",

View File

@@ -0,0 +1,45 @@
<script setup>
import {useForm} from "@inertiajs/vue3";
const props = defineProps(['previousSearch'])
const form = useForm({
ingredients: props.previousSearch ?? "",
})
function search() {
if (form.ingredients.trim().length >= 3) {
form.post('/search/recipes', {
onSuccess: params => {
console.log(params)
}
})
}
}
</script>
<template>
<div class="bg-white rounded-md p-4">
<form class="space-y-4" action="" method="post" @submit.prevent="search">
<div>
<label for="ingredients" class="block text-sm font-medium leading-6 text-gray-900">Search Recipes (comma separated list of ingredients)</label>
<div class="mt-2">
<input v-model="form.ingredients" type="text" name="ingredients" id="ingredients" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" placeholder="salmon" />
</div>
</div>
<div class="flex justify-end">
<button type="submit" :disabled="form.processing" class="bg-green-500 px-4 py-2 text-white rounded-sm"> Search </button>
</div>
</form>
<progress v-if="form.progress" :value="form.progress.percentage" max="100">
{{ form.progress.percentage }}%
</progress>
</div>
</template>
<style scoped>
</style>

View File

@@ -0,0 +1,43 @@
<script setup>
const props = defineProps({
recipe: {
required: true
}
})
</script>
<template>
<div class="max-w-2xl overflow-hidden bg-white rounded-lg shadow-md dark:bg-gray-800">
<img class="object-cover w-full h-64" :src="recipe.image" :alt="recipe.title">
<div class="p-6 h-full">
<div>
<span class="text-xs font-medium text-blue-600 uppercase dark:text-blue-400">Recipe</span>
<a href="#" class="block mt-2 text-xl font-semibold text-gray-800 transition-colors duration-300 transform dark:text-white hover:text-gray-600 hover:underline" tabindex="0" role="link">{{ recipe.title }}</a>
</div>
<div class="mt-4 border-t border-gray-100 text-white">
<dl class="divide-y divide-gray-100">
<div class="px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0">
<dt class="text-sm font-medium leading-6">Used Ingredients ({{ recipe.usedIngredientCount}})</dt>
<dd class="mt-1 text-sm leading-6 sm:col-span-2 sm:mt-0">
{{ recipe.usedIngredients.map((item) => item.name).join(", ") }}
</dd>
</div>
<div class="px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0">
<dt class="text-sm font-medium leading-6 ">Missing Ingredients ({{ recipe.missedIngredientCount }})</dt>
<dd class="mt-1 text-sm leading-6 sm:col-span-2 sm:mt-0">
{{ recipe.missedIngredients.map((item) => item.name).join(", ") }}
</dd>
</div>
</dl>
</div>
</div>
</div>
</template>
<style scoped>
</style>

View File

@@ -1,6 +1,9 @@
<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';
import { Head } from '@inertiajs/vue3';
import {Head} from '@inertiajs/vue3';
import RecipeSearchForm from "@/Components/Search/RecipeSearchForm.vue";
</script>
<template>
@@ -13,9 +16,7 @@ import { Head } from '@inertiajs/vue3';
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900">You're logged in!</div>
</div>
<recipe-search-form />
</div>
</div>
</AuthenticatedLayout>

View File

@@ -0,0 +1,44 @@
<script setup>
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout.vue";
import {Head} from "@inertiajs/vue3";
import RecipeSearchForm from "@/Components/Search/RecipeSearchForm.vue";
import RecipeSearchResultCard from "@/Components/Search/RecipeSearchResultCard.vue";
const props = defineProps({
recipes: {
type: Array,
required: true
},
previousSearch: {
required: false
}
})
</script>
<template>
<Head title="Recipe Search"/>
<AuthenticatedLayout>
<template #header>
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Recipes ({{ recipes.length }})</h2>
</template>
<div class="py-12 space-y-4">
<div class="max-w-7xl mx-auto">
<recipe-search-form :previous-search="props.previousSearch" />
</div>
<div class="max-w-7xl mx-auto">
<ul role="list" class="grid grid-cols-1 gap-6 sm:grid-cols-2 md:grid-cols-3 ">
<li v-for="recipe in recipes" :key="recipe.id"
class="col-span-1 flex flex-col divide-y divide-gray-200 rounded-lg text-center">
<recipe-search-result-card :recipe="recipe"/>
</li>
</ul>
</div>
</div>
</AuthenticatedLayout>
</template>
<style scoped>
</style>

View File

@@ -1,6 +1,8 @@
<?php
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SearchController;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
@@ -14,9 +16,10 @@ Route::get('/', function () {
]);
});
Route::get('/dashboard', function () {
return Inertia::render('Dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::get('/dashboard', [DashboardController::class, 'index'])->middleware(['auth', 'verified'])->name('dashboard');
Route::post('/search/recipes', [SearchController::class, 'recipes'])
->middleware(['auth', 'verified'])
->name('search.recipes');
Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');