diff --git a/src/Modix.Web/Pages/Promotions.razor b/src/Modix.Web/Pages/Promotions.razor index 2f456b433..4a060df7c 100644 --- a/src/Modix.Web/Pages/Promotions.razor +++ b/src/Modix.Web/Pages/Promotions.razor @@ -25,7 +25,10 @@ Start One - @foreach (var campaign in Campaigns.Where(x => _showInactive ? true : (x.Outcome is null)).OrderByDescending(x => x.Outcome is null).ThenByDescending(x => x.CreateAction.Created)) + @foreach (var (roleColor, campaign) in Campaigns + .Where(x => _showInactive ? true : (x.Campaign.Outcome is null)) + .OrderByDescending(x => x.Campaign.Outcome is null) + .ThenByDescending(x => x.Campaign.CreateAction.Created)) { var isCurrentUserCampaign = CurrentUserId == campaign.Subject.Id; @@ -46,7 +49,7 @@ _ => Color.Error }; - +
@@ -57,7 +60,7 @@ @campaign.Subject.GetFullUsername() - @campaign.TargetRole.Name + @campaign.TargetRole.Name
@@ -102,13 +105,13 @@ Sorry, you aren't allowed to see comments on your own campaign. } - else if (!campaignCommentData.ContainsKey(campaign.Id)) + else if (!CampaignCommentData.ContainsKey(campaign.Id)) { } else { - foreach (var comment in campaignCommentData[campaign.Id].Values.OrderByDescending(x => x.CreatedAt)) + foreach (var comment in CampaignCommentData[campaign.Id].Values.OrderByDescending(x => x.CreatedAt)) { var sentimentIcon = comment.PromotionSentiment == PromotionSentiment.Approve ? Icons.Material.Filled.ThumbUp : Icons.Material.Filled.ThumbDown;
@@ -129,7 +132,7 @@ } - if (campaign.CloseAction is null && !campaignCommentData[campaign.Id].Any(x => x.Value.IsFromCurrentUser)) + if (campaign.CloseAction is null && !CampaignCommentData[campaign.Id].Any(x => x.Value.IsFromCurrentUser)) { } @@ -177,9 +180,8 @@ private ulong CurrentUserId { get; set; } - private IReadOnlyCollection Campaigns = Array.Empty(); - private Dictionary RoleColors = new Dictionary(); - private Dictionary> campaignCommentData = new Dictionary>(); + private IReadOnlyCollection<(string RoleColor, PromotionCampaignSummary Campaign)> Campaigns = Array.Empty<(string RoleColor, PromotionCampaignSummary Campaign)>(); + private Dictionary> CampaignCommentData = new Dictionary>(); private bool _showInactive; @@ -194,18 +196,31 @@ return; var currentUser = DiscordHelper.GetCurrentUser(); - RoleColors = currentUser!.Guild.Roles.ToDictionary(x => x.Id, x => x.Color.ToString()); + var roleColors = currentUser!.Guild.Roles.ToDictionary(x => x.Id, x => x.Color.ToString()); - Campaigns = await PromotionsService.SearchCampaignsAsync(new PromotionCampaignSearchCriteria + Campaigns = (await PromotionsService.SearchCampaignsAsync(new PromotionCampaignSearchCriteria { GuildId = currentUser.Guild.Id - }); + })) + .Select(campaign => (GetRoleColor(roleColors, campaign.TargetRole.Id), campaign)) + .ToArray(); CurrentUserId = currentUser.Id; StateHasChanged(); } + private string GetRoleColor(Dictionary roleColors, ulong roleId) + { + // In case the role has been deleted and we still have a campaign record for that role we serve a grey color. + if (!roleColors.TryGetValue(roleId, out var colorHex)) + { + return $"color: grey"; + } + + return $"color: {colorHex}"; + } + private async Task ShowInactiveChanged(bool showInactive) { _showInactive = showInactive; @@ -220,7 +235,7 @@ if (CurrentUserId == userId) return; - if (campaignCommentData.ContainsKey(campaignId)) + if (CampaignCommentData.ContainsKey(campaignId)) return; var result = await PromotionsService.GetCampaignDetailsAsync(campaignId); @@ -230,7 +245,7 @@ return; } - campaignCommentData[campaignId] = result.Comments + CampaignCommentData[campaignId] = result.Comments .Where(x => x.ModifyAction is null) .Select(c => new CampaignCommentData(c.Id, c.Sentiment, c.Content, c.CreateAction.Created, c.CreateAction.CreatedBy.Id == CurrentUserId)) .ToDictionary(x => x.Id, x => x); @@ -245,7 +260,7 @@ var promotionActionSummary = await PromotionsService.AddCommentAsync(campaignId, sentiment, content); var newComment = promotionActionSummary.NewComment; - campaignCommentData[campaignId][newComment!.Id] = new CampaignCommentData(newComment.Id, newComment.Sentiment, newComment.Content, promotionActionSummary.Created, true); + CampaignCommentData[campaignId][newComment!.Id] = new CampaignCommentData(newComment.Id, newComment.Sentiment, newComment.Content, promotionActionSummary.Created, true); } catch (InvalidOperationException ex) { @@ -278,8 +293,8 @@ var promotionActionSummary = await PromotionsService.UpdateCommentAsync(commentId, newPromotionSentiment, newContent); var newComment = promotionActionSummary.NewComment; - campaignCommentData[campaignId].Remove(commentId); - campaignCommentData[campaignId][newComment!.Id] = new CampaignCommentData(newComment.Id, newComment.Sentiment, newComment.Content, promotionActionSummary.Created, true); + CampaignCommentData[campaignId].Remove(commentId); + CampaignCommentData[campaignId][newComment!.Id] = new CampaignCommentData(newComment.Id, newComment.Sentiment, newComment.Content, promotionActionSummary.Created, true); } catch (InvalidOperationException ex) {