-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from MartinZikmund/feature/post-editor
- Loading branch information
Showing
63 changed files
with
658 additions
and
327 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
...eb/MZikmund.Web.Core/Dtos/PostListItem.cs → ...ikmund.DataContracts/Blog/PostListItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MZikmund.DataContracts; | ||
|
||
public class PagedResponse<T> | ||
{ | ||
public PagedResponse(IEnumerable<T> data, int pageNumber, int pageSize, int totalCount) | ||
{ | ||
Data = data; | ||
PageNumber = pageNumber; | ||
PageSize = pageSize; | ||
TotalCount = totalCount; | ||
} | ||
|
||
public IEnumerable<T> Data { get; } | ||
|
||
public int PageNumber { get; } | ||
|
||
public int PageSize { get; } | ||
|
||
public int TotalCount { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
src/app/MZikmund/ViewModels/Admin/BlogPostEditorViewModel.cs
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/app/MZikmund/ViewModels/Admin/BlogPostsManagerViewModel.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using MZikmund.Api.Client; | ||
using MZikmund.DataContracts.Blog; | ||
using MZikmund.Services.Loading; | ||
|
||
namespace MZikmund.ViewModels.Admin; | ||
|
||
public class PostEditorViewModel : PageViewModel | ||
{ | ||
private readonly IMZikmundApi _api; | ||
private readonly ILoadingIndicator _loadingIndicator; | ||
|
||
public PostEditorViewModel(IMZikmundApi api, ILoadingIndicator loadingIndicator) | ||
{ | ||
_api = api; | ||
_loadingIndicator = loadingIndicator; | ||
} | ||
|
||
public override string Title => Post?.Title ?? ""; | ||
|
||
public string Tags { get; set; } = ""; | ||
|
||
public Category[] Categories { get; set; } = Array.Empty<Category>(); | ||
|
||
public string CategoriesText => Categories is null ? "" : string.Join(", ", Categories.Select(c => c.DisplayName)); | ||
|
||
public Post? Post { get; set; } | ||
|
||
public ICommand SaveCommand => GetOrCreateAsyncCommand(SaveAsync); | ||
|
||
private async Task SaveAsync() | ||
{ | ||
if (Post is null) | ||
{ | ||
return; | ||
} | ||
|
||
var tags = Tags.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) | ||
.Select(t => new Tag { DisplayName = t.Trim() }) | ||
.ToArray(); | ||
|
||
Post.Tags = tags; | ||
Post.Categories = Categories; | ||
|
||
if (Post.Id == Guid.Empty) | ||
{ | ||
await _api.AddPostAsync(Post); | ||
} | ||
else | ||
{ | ||
await _api.UpdatePostAsync(Post.Id, Post); | ||
} | ||
} | ||
|
||
public override void ViewAppeared() => base.ViewAppeared(); | ||
|
||
public override async void ViewNavigatedTo(object parameter) | ||
{ | ||
using var _ = _loadingIndicator.BeginLoading(); | ||
var postId = (Guid)parameter; | ||
if (postId == Guid.Empty) | ||
{ | ||
Post = new Post(); | ||
} | ||
else | ||
{ | ||
Post = (await _api.GetPostAsync(postId)).Content; | ||
} | ||
|
||
PopulateInfo(Post!); | ||
} | ||
|
||
private void PopulateInfo(Post post) | ||
{ | ||
if (post is null) | ||
{ | ||
throw new ArgumentNullException(nameof(post)); | ||
} | ||
|
||
Tags = string.Join(", ", post.Tags.Select(t => t.DisplayName)); | ||
Categories = post.Categories.ToArray(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/app/MZikmund/ViewModels/Admin/PostsManagerViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections.ObjectModel; | ||
using MZikmund.Api.Client; | ||
using MZikmund.DataContracts.Blog; | ||
using MZikmund.Extensions; | ||
using MZikmund.Models.Dialogs; | ||
using MZikmund.Services.Dialogs; | ||
using MZikmund.Services.Loading; | ||
using MZikmund.Services.Localization; | ||
using MZikmund.Services.Navigation; | ||
using Newtonsoft.Json; | ||
using Windows.Storage.Pickers; | ||
|
||
namespace MZikmund.ViewModels.Admin; | ||
|
||
public class PostsManagerViewModel : PageViewModel | ||
{ | ||
private readonly IDialogService _dialogService; | ||
private readonly ILoadingIndicator _loadingIndicator; | ||
private readonly INavigationService _navigationService; | ||
private readonly IMZikmundApi _api; | ||
|
||
public PostsManagerViewModel( | ||
IMZikmundApi api, | ||
IDialogService dialogService, | ||
ILoadingIndicator loadingIndicator, | ||
INavigationService navigationService) | ||
{ | ||
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService)); | ||
_loadingIndicator = loadingIndicator ?? throw new ArgumentNullException(nameof(loadingIndicator)); | ||
_navigationService = navigationService; | ||
_api = api ?? throw new ArgumentNullException(nameof(api)); | ||
} | ||
|
||
public override string Title => Localizer.Instance.GetString("Posts"); | ||
|
||
public ObservableCollection<PostListItem> Posts { get; } = new ObservableCollection<PostListItem>(); | ||
|
||
public override async void ViewAppeared() | ||
{ | ||
await RefreshListAsync(); | ||
} | ||
|
||
private async Task RefreshListAsync() | ||
{ | ||
using var loadingScope = _loadingIndicator.BeginLoading(); | ||
try | ||
{ | ||
//TODO: Refresh collection based on IDs | ||
var posts = await _api.GetPostsAsync(); | ||
Posts.Clear(); | ||
Posts.AddRange(posts.Content!.Data.OrderByDescending(t => t.LastModifiedDate)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
await _dialogService.ShowStatusMessageAsync( | ||
StatusMessageDialogType.Error, | ||
"Could not load data", | ||
$"Error occurred loading data from server. {ex}"); | ||
} | ||
} | ||
|
||
public ICommand AddPostCommand => GetOrCreateCommand(AddPost); | ||
|
||
public ICommand UpdatePostCommand => GetOrCreateCommand<PostListItem>(UpdatePost); | ||
|
||
private void AddPost() | ||
{ | ||
_navigationService.Navigate<PostEditorViewModel>(Guid.Empty); | ||
} | ||
|
||
private void UpdatePost(PostListItem? post) | ||
{ | ||
if (post is null) | ||
{ | ||
return; | ||
} | ||
|
||
_navigationService.Navigate<PostEditorViewModel>(post.Id); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.