forked from adamhaile/surplus-realworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfile.tsx
More file actions
77 lines (68 loc) · 3.1 KB
/
Profile.tsx
File metadata and controls
77 lines (68 loc) · 3.1 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
import S, { DataSignal } from 's-js';
import * as Surplus from 'surplus';
import { App } from '../app/app';
import { ArticlesQuery, UserProfile, Client } from '../app/client';
import { Page, MenuSection } from './Page';
import { PaginatedArticleFeed, ArticlesToggle } from './ArticlesFeed';
import { FollowButton } from './FollowButton';
import { Loading } from './Loading';
export { ProfileRoute };
type ProfileModel = ReturnType<typeof ProfileModel>;
const
ProfileRoute = async (app : App, user : string, favorites : boolean) => {
const model = ProfileModel(app, user, favorites);
S(() => app.location(`#/@${user}${model.tab() === 'favorites' ? '/favorites' : ''}`));
return () => <ProfilePage {...model} />;
},
ProfileModel = (app : App, username : string, favorites : boolean) => {
const
tab = S.value(favorites ? 'favorites' : 'my'),
profile = S.data({} as UserProfile),
isUser = () => app.user() && app.user()!.username === username,
request = app.client.profiles.get(username),
feed = () => {
const filter = tab() === 'my' ? { author: username} : { favorited : username };
return (q : ArticlesQuery) => app.client.articles.get({ ...q, ...filter });
};
request.then(resp => profile(resp.profile));
return { app, tab, username, isUser, profile, feed };
},
ProfilePage = (props: ProfileModel) => (
<Page app={props.app} title={`@${props.username} - Conduit`} section={props.isUser() ? MenuSection.Profile : MenuSection.None}>
<Profile {...props} />
</Page>
),
Profile = (props : ProfileModel) => (
<div class="profile-page">
<UserInfo {...props} />
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-10 offset-md-1">
<ArticlesToggle tab={props.tab}>{{
my: 'My Articles',
favorites: 'Favorited Articles'
}}</ArticlesToggle>
<PaginatedArticleFeed size={10} feed={props.feed()} client={props.app.client} />
</div>
</div>
</div>
</div>
),
UserInfo = ({ profile, isUser, app: { client, user } } : ProfileModel) => (
<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={profile().image || '//:0'} />
<h4>{profile().username}</h4>
<p>{profile().bio}</p>
{isUser()
? <a class="btn btn-sm btn-outline-secondary action-btn" href="#/settings">
<i class="ion-gear-a"></i> Edit Profile Settings
</a>
: FollowButton(profile, client)}
</div>
</div>
</div>
</div>
);