Compare commits
4 Commits
40abf65d06
...
movies
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ab403b3b3 | |||
| 2751b4181d | |||
| 5a561f9fff | |||
| 1cce273057 |
11
Components/Pages/Movie.razor
Normal file
11
Components/Pages/Movie.razor
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
@page "/movie/{id}"
|
||||||
|
|
||||||
|
<PageTitle>Movie Details</PageTitle>
|
||||||
|
|
||||||
|
<h1>TODO</h1>
|
||||||
|
<p>Movie ID: @id</p>
|
||||||
|
|
||||||
|
@code {
|
||||||
|
[Parameter]
|
||||||
|
public string id { get; set; }
|
||||||
|
}
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
@page "/movie/{id}"
|
|
||||||
|
|
||||||
<PageTitle>Movie Details</PageTitle>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
.my-grid {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 1fr 2fr;
|
|
||||||
grid-template-rows: auto auto;
|
|
||||||
gap: 20px;
|
|
||||||
max-width: 800px;
|
|
||||||
margin-right: auto;
|
|
||||||
}
|
|
||||||
.favorite {
|
|
||||||
background-color: gold;
|
|
||||||
color: black;
|
|
||||||
}
|
|
||||||
.not-favorite {
|
|
||||||
background-color: gray;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<div class="my-grid">
|
|
||||||
@if (movie is not null)
|
|
||||||
{
|
|
||||||
<h1>@movie.Title</h1>
|
|
||||||
<div></div>
|
|
||||||
<img src="@movie.Poster" alt="@movie.Title Poster" />
|
|
||||||
<div class="details">
|
|
||||||
<p>@movie.Runtime</p>
|
|
||||||
<p>@movie.Plot</p>
|
|
||||||
@if(ManageFavorite.IsFavorite(id))
|
|
||||||
{
|
|
||||||
<button class="btn-secondary favorite" @onclick="ToggleFavoriteMovie">Remove Favorite</button>
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
<button class="btn-secondary not-favorite" @onclick="ToggleFavoriteMovie">Add to Favorites</button>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
<p>Loading movie details...</p>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
@inject OmdbService OmdbService
|
|
||||||
@inject ManageFavorite ManageFavorite
|
|
||||||
|
|
||||||
@code {
|
|
||||||
[Parameter]
|
|
||||||
public required string id { get; set; }
|
|
||||||
private Movie? movie;
|
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
|
||||||
{
|
|
||||||
movie = await OmdbService.FetchMovieDetail(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
||||||
{
|
|
||||||
if (firstRender)
|
|
||||||
{
|
|
||||||
await ManageFavorite.LoadFavorites();
|
|
||||||
StateHasChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task ToggleFavoriteMovie()
|
|
||||||
{
|
|
||||||
await ManageFavorite.ToggleFavoriteMovie(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,13 @@
|
|||||||
using test_alebro.Components;
|
using test_alebro.Components;
|
||||||
using Blazored.LocalStorage;
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
builder.Services.AddBlazoredLocalStorage();
|
|
||||||
|
|
||||||
// Mine services
|
// Mine services
|
||||||
builder.Services.AddSingleton<OmdbService>();
|
builder.Services.AddSingleton<OmdbService>();
|
||||||
builder.Services.AddScoped<ManageFavorite>();
|
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Service to manage favorite movies using local storage
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="localStorage">The local storage service</param>
|
|
||||||
class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage)
|
|
||||||
{
|
|
||||||
// Inject the local storage service
|
|
||||||
private readonly Blazored.LocalStorage.ILocalStorageService LocalStorage = localStorage;
|
|
||||||
private readonly List<string> MoviesIDs = [];
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Load favorite movies from local storage
|
|
||||||
/// Use Immediately after service instantiation otherwise the list will be empty
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task LoadFavorites()
|
|
||||||
{
|
|
||||||
var favorites = await LocalStorage.GetItemAsync<List<string>>("favorite") ?? [];
|
|
||||||
MoviesIDs.Clear();
|
|
||||||
MoviesIDs.AddRange(favorites);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Check if a movie is favorite or not
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="movieID">The movie ID to check</param>
|
|
||||||
/// <returns>True if the movie is favorite, otherwise false</returns>
|
|
||||||
public bool IsFavorite(string movieID)
|
|
||||||
{
|
|
||||||
return MoviesIDs.Contains(movieID);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Toggle favorite status of a movie (by ID)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="movieID">The movie ID to toggle</param>
|
|
||||||
/// <returns>The new favorite status</returns>
|
|
||||||
public async Task<bool> ToggleFavoriteMovie(string movieID)
|
|
||||||
{
|
|
||||||
if (!IsFavorite(movieID))
|
|
||||||
{
|
|
||||||
MoviesIDs.Add(movieID);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MoviesIDs.RemoveAll(id => id == movieID);
|
|
||||||
}
|
|
||||||
|
|
||||||
await LocalStorage.SetItemAsync("favorite", MoviesIDs);
|
|
||||||
return IsFavorite(movieID);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the list of favorite movie IDs
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The list of favorite movie IDs</returns>
|
|
||||||
public List<string> GetFavoriteMovies()
|
|
||||||
{
|
|
||||||
return MoviesIDs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,8 +9,4 @@
|
|||||||
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
|
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user