-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfile.cs
More file actions
190 lines (177 loc) · 7.45 KB
/
Profile.cs
File metadata and controls
190 lines (177 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using Abies.Conduit.Main;
using Abies.Conduit.Routing;
using Abies.DOM;
using System.Collections.Generic;
using static Abies.Html.Attributes;
using static Abies.Html.Events;
using Abies.Conduit;
namespace Abies.Conduit.Page.Profile;
public interface Message : Abies.Message
{
public record ProfileLoaded(Home.Profile Profile) : Message;
public record ArticlesLoaded(List<Home.Article> Articles, int ArticlesCount) : Message;
public record FavoritedArticlesLoaded(List<Home.Article> Articles, int ArticlesCount) : Message;
public record ToggleTab(ProfileTab Tab) : Message;
public record ToggleFollow : Message;
}
public enum ProfileTab
{
MyArticles,
FavoritedArticles
}
public record Model(
UserName UserName,
bool IsLoading = true,
Home.Profile? Profile = null,
List<Home.Article>? Articles = null,
int ArticlesCount = 0,
ProfileTab ActiveTab = ProfileTab.MyArticles
);
public class Page : Element<Model, Message>
{
public static Model Initialize(Message argument)
{
return new Model(new UserName(""));
}
public static Subscription Subscriptions(Model model)
{
return new Subscription();
}
public static (Model model, IEnumerable<Command> commands) Update(Abies.Message message, Model model)
=> message switch
{
Message.ProfileLoaded profileLoaded => (
model with
{
Profile = profileLoaded.Profile,
IsLoading = false
},
[]
),
Message.ArticlesLoaded articlesLoaded => (
model with
{
Articles = articlesLoaded.Articles,
ArticlesCount = articlesLoaded.ArticlesCount
},
[]
),
Message.FavoritedArticlesLoaded favoritedArticlesLoaded => (
model with
{
Articles = favoritedArticlesLoaded.Articles,
ArticlesCount = favoritedArticlesLoaded.ArticlesCount
},
[]
), Message.ToggleTab toggleTab => (
model with { ActiveTab = toggleTab.Tab },
toggleTab.Tab == ProfileTab.FavoritedArticles
? [new LoadArticlesCommand(favoritedBy: model.UserName.Value)]
: [new LoadArticlesCommand(author: model.UserName.Value)]
),
Message.ToggleFollow => (
model,
model.Profile != null ? [ new ToggleFollowCommand(model.UserName.Value, model.Profile.Following) ] : []
),
_ => (model, [])
};
private static Node UserInfo(Model model, bool isCurrentUser) =>
div([class_("user-info")], [
div([class_("container")], [
div([class_("row")], [
div([class_("col-xs-12 col-md-10 offset-md-1")], [
img([class_("user-img"), src(model.Profile?.Image ?? "")]),
h4([], [text(model.UserName.Value)]),
p([], [text(model.Profile?.Bio ?? "")]),
isCurrentUser
? a([class_("btn btn-sm btn-outline-secondary action-btn"),
href("/settings")],[
i([class_("ion-gear-a")], []),
text(" Edit Profile Settings")
])
: button([class_(model.Profile?.Following ?? false
? "btn btn-sm btn-secondary action-btn"
: "btn btn-sm btn-outline-secondary action-btn"),
onclick(new Message.ToggleFollow())],[
i([class_("ion-plus-round")], []),
text(model.Profile?.Following ?? false
? $" Unfollow {model.UserName.Value}"
: $" Follow {model.UserName.Value}")
])
])
])
])
]); private static Node ArticleToggle(Model model) =>
div([class_("articles-toggle")], [
ul([class_("nav nav-pills outline-active")], [
li([class_("nav-item")], [
a([class_(model.ActiveTab == ProfileTab.MyArticles
? "nav-link active"
: "nav-link"),
href($"/profile/{model.UserName.Value}")],
[text("My Articles")])
]),
li([class_("nav-item")], [
a([class_(model.ActiveTab == ProfileTab.FavoritedArticles
? "nav-link active"
: "nav-link"),
href($"/profile/{model.UserName.Value}/favorites")],
[text("Favorited Articles")])
])
])
]);
private static Node ArticlePreview(Home.Article article) =>
div([class_("article-preview")], [
div([class_("article-meta")], [
a([href($"/profile/{article.Author.Username}")], [
img([src(article.Author.Image)])
]),
div([class_("info")], [
a([class_("author"), href($"/profile/{article.Author.Username}")], [
text(article.Author.Username)
]),
span([class_("date")], [text(article.CreatedAt)])
]),
div([class_("pull-xs-right")], [
button([class_(article.Favorited
? "btn btn-primary btn-sm pull-xs-right"
: "btn btn-outline-primary btn-sm pull-xs-right")],
[
i([class_("ion-heart")], []),
text($" {article.FavoritesCount}")
])
])
]),
a([class_("preview-link"), href($"/article/{article.Slug}")], [
h1([], [text(article.Title)]),
p([], [text(article.Description)]),
span([], [text("Read more...")])
])
]);
private static Node ArticleList(Model model) =>
model.Articles == null || model.Articles.Count == 0
? div([class_("article-preview")], [text("No articles are here... yet.")])
: div([], [ ..model.Articles.ConvertAll(article => ArticlePreview(article)) ]);
public static Node View(Model model) =>
model.IsLoading
? div([class_("profile-page")], [
div([class_("container")], [
div([class_("row")], [
div([class_("col-xs-12 col-md-10 offset-md-1")], [
text("Loading profile...")
])
])
])
])
: div([class_("profile-page")], [
UserInfo(model, false),
div([class_("container")], [
div([class_("row")], [
div([class_("col-xs-12 col-md-10 offset-md-1")], [
ArticleToggle(model),
ArticleList(model)
])
])
])
]);
}