From ad8589878606db25aaf8c22b125d7b5c65badd20 Mon Sep 17 00:00:00 2001 From: ZeKap Date: Thu, 22 Feb 2024 16:08:45 +0100 Subject: [PATCH 1/2] pages works --- src/Controller/SearchController.php | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index e335d78..928d848 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -8,13 +8,13 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Request; use Doctrine\ORM\EntityManagerInterface; -Use App\Utils; +use App\Utils; class SearchController extends AbstractController { private static function addFruitToAlbums(array $releases, string $fruit, EntityManagerInterface $entityManager): void - { + { foreach ($releases as $release) { $id = $release["id"]; $album = $entityManager->getRepository(Album::class)->findBy( @@ -37,7 +37,8 @@ class SearchController extends AbstractController } } - private static function getEmojiName(string | null $emoji) : string | null { + private static function getEmojiName(string | null $emoji): string | null + { return match ($emoji) { "🍎" => "apple", "🍐" => "pear", @@ -62,18 +63,18 @@ class SearchController extends AbstractController public function index(Request $request, EntityManagerInterface $entityManager): Response { $page = $request->query->get('page'); - if ($page== null) $page = 1; + if ($page == null) $page = 1; $fruit = $request->query->get('fruit'); $user_query = $request->query->get('q'); - $fruit_emoji = SearchController::getEmojiName($fruit); - if ($fruit_emoji == null) { + $fruit_name = SearchController::getEmojiName($fruit); + if ($fruit_name == null) { // We do not support requests without a fruit or with an invalid one // Redirect to the homepage in this case return $this->redirect('/'); } - $fruit_query = $user_query . " " . $fruit_emoji; + $fruit_query = $user_query . " " . $fruit_name; $page_str = strval($page); $response = Utils::makeRequest("GET", "/database/search", [ "q" => $fruit_query, @@ -86,30 +87,30 @@ class SearchController extends AbstractController SearchController::addFruitToAlbums($results, $fruit, $entityManager); $user = $this->getUser(); $array_id = array(); - foreach($user->getLiked() as $like){ + foreach ($user->getLiked() as $like) { array_push( $array_id, $like->getAlbumId() ); } $true_results = array(); - foreach($results as $result){ + foreach ($results as $result) { + + $isLiked = in_array($result["id"], $array_id); - $isLiked = in_array($result["id"],$array_id); - $result = array_merge( - $result, - ["isLiked" => $isLiked? "true": "false"] + $result, + ["isLiked" => $isLiked ? "true" : "false"] ); - array_push($true_results,$result); + array_push($true_results, $result); } - + $results = $true_results; - + return $this->render('search/search.html.twig', [ 'controller_name' => 'SearchController', 'query' => $user_query, - 'fruit_emoji' => $fruit_emoji, + 'fruit_emoji' => $fruit, 'fruit_name' => SearchController::getEmojiName($fruit), 'page' => $page, 'all_page' => $response["pagination"]["pages"], From 3229bdfb9176bb8482d8bb670d91d9e21292ac29 Mon Sep 17 00:00:00 2001 From: ZeKap Date: Thu, 22 Feb 2024 16:20:18 +0100 Subject: [PATCH 2/2] dynamic add and remove to favs thanks to css --- assets/styles/app.css | 16 ++++++++++++++++ templates/favorite/index.html.twig | 5 +++-- templates/search/search.html.twig | 8 ++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/assets/styles/app.css b/assets/styles/app.css index 295288f..842e444 100644 --- a/assets/styles/app.css +++ b/assets/styles/app.css @@ -155,6 +155,10 @@ main:is(#searchResults, #favorites) { } } +main#favorites > div.results > :not(section.fav) { + display: none; +} + section.album { display: flex; flex-direction: column; @@ -167,8 +171,20 @@ section.album { transition: all ease-out 0.15s; box-shadow: var(--shadow); border-radius: var(--radius); + & > button.toFav { + display: block; + } + & > p.alreadyFav { + display: none; + } &.fav { transform: rotate(-1deg); + & > p.alreadyFav { + display: block; + } + & > button.toFav { + display: none; + } } &:hover { transform: scale(1.01); diff --git a/templates/favorite/index.html.twig b/templates/favorite/index.html.twig index 9659a1e..b7d33f4 100644 --- a/templates/favorite/index.html.twig +++ b/templates/favorite/index.html.twig @@ -10,7 +10,6 @@ fetch("/favorite/remove/" + id).then((data) => { if (data.status == 200) { document.querySelector("#id" + id).classList.remove("fav"); - alert("Removed from favs"); } }); } @@ -21,8 +20,10 @@ {% set main_artist = album_liked["artists_sort"] %} {% set artists = album_liked["artists"] %} {% set album = album_liked["title"] %} -
+ {% set fruit = album_liked["fruit"] %} +
+

{{fruit}}

{{album}}

by {% for artist in artists %} diff --git a/templates/search/search.html.twig b/templates/search/search.html.twig index 8c5b5df..f7b47ff 100644 --- a/templates/search/search.html.twig +++ b/templates/search/search.html.twig @@ -7,7 +7,6 @@ endblock %} {% block body %} fetch("/favorite/add/" + id).then((data) => { if (data.status == 200) { document.querySelector("#id" + id).classList.add("fav"); - alert("Added to favs"); } }); } @@ -31,16 +30,13 @@ endblock %} {% block body %} src="{{result['cover_image']}}" alt="{{album ~ ' by ' ~ artist}}" /> - {% if result['isLiked'] == 'true' %} -

+

Already fav, go to favorites

- {% else %} - - {% endif %}
{% endfor %}