Ensure item is updated when modified
This commit is contained in:
@@ -12,27 +12,38 @@ public class ConfirmItemHandler(IValueStore<Item> valueStore,
|
||||
ItemHeaderConfiguration? configuration = await mediator.Handle<ConfirmEventArgs<Item>,
|
||||
ItemHeaderConfiguration>(args);
|
||||
|
||||
if (configuration is not null)
|
||||
{
|
||||
bool success = false;
|
||||
if (valueStore?.Value is Item item)
|
||||
{
|
||||
(bool Success, int Id, string Name) = await mediator.Handle<EditEventArgs<(int, ItemConfiguration)>,
|
||||
(bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration?.Name })));
|
||||
(bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration.Name })));
|
||||
|
||||
if (Success)
|
||||
{
|
||||
Item newItem = new Item { Id = Id, Name = Name };
|
||||
Item newItem = new() { Id = Id, Name = Name };
|
||||
publisher.Publish(Modified.As(item, newItem));
|
||||
|
||||
valueStore.Set(newItem);
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
(bool Success, int Id, string Name) = await mediator.Handle<CreateEventArgs<ItemConfiguration>,
|
||||
(bool, int, string)>(new CreateEventArgs<ItemConfiguration>(new ItemConfiguration { Name = configuration?.Name }));
|
||||
(bool, int, string)>(new CreateEventArgs<ItemConfiguration>(new ItemConfiguration { Name = configuration.Name }));
|
||||
|
||||
if (Success)
|
||||
{
|
||||
publisher.Publish(Created.As(new Item { Id = Id, Name = Name }));
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
publisher.Publish(Notify.As(configuration));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
public record ItemHeaderConfiguration
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
|
||||
INotificationHandler<UnarchiveEventArgs<Item>>,
|
||||
INotificationHandler<FavouriteEventArgs<Item>>,
|
||||
INotificationHandler<UnfavouriteEventArgs<Item>>,
|
||||
INotificationHandler<NotifyEventArgs<ItemHeaderConfiguration>>,
|
||||
ISelectable,
|
||||
IRemovable
|
||||
{
|
||||
@@ -59,4 +60,13 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
|
||||
|
||||
public Task Handle(UnfavouriteEventArgs<Item> args) =>
|
||||
Task.FromResult(Favourite = false);
|
||||
public Task Handle(NotifyEventArgs<ItemHeaderConfiguration> args)
|
||||
{
|
||||
if (args.Value is ItemHeaderConfiguration configuration)
|
||||
{
|
||||
Name = configuration.Name;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,8 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider,
|
||||
if (cachedItem is not null)
|
||||
{
|
||||
IServiceScope serviceScope = serviceProvider.CreateScope();
|
||||
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
|
||||
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
|
||||
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(newItem.Id, newItem.Name, "Description", true)
|
||||
is ItemNavigationViewModel viewModel)
|
||||
{
|
||||
int oldIndex = cache.IndexOf(cachedItem);
|
||||
cache.Remove(cachedItem);
|
||||
|
||||
@@ -32,8 +28,7 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider,
|
||||
int newIndex = cache.IndexOf(newItem);
|
||||
valueStore.Set(newItem);
|
||||
|
||||
publisher.Publish(RemoveAndInsertAt.As(oldIndex, newIndex, viewModel), nameof(ContainerViewModel));
|
||||
}
|
||||
publisher.Publish(MoveTo.As<ItemNavigationViewModel>(oldIndex, newIndex), nameof(ContainerViewModel));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user