BlazorStart/Start/Client/Store/Features/CurrentContainer/CurrentContainerReducers.cs

140 lines
4.2 KiB
C#
Raw Normal View History

2021-12-05 23:50:48 +00:00
using System.Collections.Generic;
using System.Linq;
using Fluxor;
2021-12-04 00:44:02 +00:00
using Start.Client.Store.State;
2021-12-05 23:50:48 +00:00
using Start.Shared;
2021-12-04 00:44:02 +00:00
namespace Start.Client.Store.Features.CurrentContainer {
public static class CurrentContainerReducers {
[ReducerMethod(typeof(FetchCurrentContainerAction))]
public static RootState FetchCurrentContainer(RootState state) {
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = null,
IsLoadingCurrentContainer = true,
ErrorMessage = null
}
};
}
[ReducerMethod]
public static RootState ReceivedCurrentContainer(RootState state,
ReceivedCurrentContainerAction action) {
2022-04-19 20:04:38 +00:00
BookmarkContainerDto? container = action.BookmarkContainer;
container.BookmarkGroups = container.BookmarkGroups
?.SortGroups()
.ToList();
2021-12-04 00:44:02 +00:00
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = action.BookmarkContainer,
IsLoadingCurrentContainer = false,
ErrorMessage = null
}
};
}
[ReducerMethod]
public static RootState ErrorFetchingCurrentContainer(RootState state,
ErrorFetchingCurrentContainerAction action) {
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = null,
IsLoadingCurrentContainer = false,
ErrorMessage = action.ErrorMessage
}
};
}
2021-12-05 23:50:48 +00:00
[ReducerMethod]
public static RootState AddBookmarkGroup(RootState state, AddBookmarkGroupAction action) {
BookmarkContainerDto? container = state.CurrentContainerState.Container;
if (container == null)
return state;
if (action.BookmarkGroup.BookmarkContainerId != container.BookmarkContainerId)
return state;
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = new BookmarkContainerDto(container.BookmarkContainerId,
2022-04-19 20:04:38 +00:00
container.Title, container.SortOrder, container.BookmarkGroups?
2021-12-05 23:50:48 +00:00
.Concat(new List<BookmarkGroupDto> { action.BookmarkGroup })
2022-04-19 20:04:38 +00:00
.SortGroups()
2021-12-05 23:50:48 +00:00
.ToList())
}
};
}
[ReducerMethod]
public static RootState RemoveBookmarkGroup(RootState state,
RemoveBookmarkGroupAction action) {
BookmarkContainerDto? container = state.CurrentContainerState.Container;
if (container == null)
return state;
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = new BookmarkContainerDto(container.BookmarkContainerId,
2022-04-19 20:04:38 +00:00
container.Title, container.SortOrder, container.BookmarkGroups?
2021-12-05 23:50:48 +00:00
.Where(g => g.BookmarkGroupId != action.BookmarkGroupId)
.ToList())
}
};
}
2021-12-14 00:27:13 +00:00
[ReducerMethod]
public static RootState AddBookmark(RootState state, AddBookmarkAction action) {
BookmarkContainerDto? container = state.CurrentContainerState.Container;
if (container == null)
return state;
List<BookmarkGroupDto>? groups = container.BookmarkGroups
?.Select(bg => {
if (bg.BookmarkGroupId == action.Bookmark.BookmarkGroupId) {
return new BookmarkGroupDto(bg.BookmarkGroupId, bg.Title, bg.Color,
2022-04-19 20:04:38 +00:00
bg.SortOrder, bg.BookmarkContainerId, bg.Bookmarks?
2021-12-14 00:27:13 +00:00
.Concat(new List<BookmarkDto> { action.Bookmark })
2022-04-19 20:04:38 +00:00
.SortBookmarks()
2021-12-14 00:27:13 +00:00
.ToList());
}
return bg;
})
.ToList();
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = new BookmarkContainerDto(container.BookmarkContainerId,
2022-04-19 20:04:38 +00:00
container.Title, container.SortOrder, groups)
2021-12-14 00:27:13 +00:00
}
};
}
[ReducerMethod]
public static RootState RemoveBookmark(RootState state, RemoveBookmarkAction action) {
BookmarkContainerDto? container = state.CurrentContainerState.Container;
if (container == null)
return state;
List<BookmarkGroupDto>? groups = container.BookmarkGroups
?.Select(bg => new BookmarkGroupDto(bg.BookmarkGroupId, bg.Title, bg.Color,
2022-04-19 20:04:38 +00:00
bg.SortOrder, bg.BookmarkContainerId, bg.Bookmarks
2021-12-14 00:27:13 +00:00
?.Where(b => b.BookmarkId != action.BookmarkId)
.ToList()))
.ToList();
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = new BookmarkContainerDto(container.BookmarkContainerId,
2022-04-19 20:04:38 +00:00
container.Title, container.SortOrder, groups)
2021-12-14 00:27:13 +00:00
}
};
}
2021-12-04 00:44:02 +00:00
}
}