-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFavoriteButton.tsx
More file actions
36 lines (32 loc) · 1.48 KB
/
FavoriteButton.tsx
File metadata and controls
36 lines (32 loc) · 1.48 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
import { DataSignal } from 's-js';
import * as Surplus from 'surplus';
import { Article, Client } from '../app/client';
import { OptimisticToggle } from './OptimisticToggle';
export { FavoriteButton, CompactFavoriteButton };
const
FavoriteModel = (article : DataSignal<Article>, client : Client) =>
OptimisticToggle(
() => article().favorited,
() => article().favoritesCount,
fav => client.favorite[fav ? 'post' : 'del'](article().slug).then(a => article(a.article))
),
CompactFavoriteButton = (article : DataSignal<Article>, client : Client) => {
const { on, count, toggle, saving } = FavoriteModel(article, client);
return (
<button class={`btn ${on() ? 'btn-primary' : 'btn-outline-primary'} ${saving() ? 'disabled' : ''} btn-sm pull-xs-right`}
type="button" onClick={toggle}
>
<i class="ion-heart" /> {count()}
</button>
);
},
FavoriteButton = (article : DataSignal<Article>, client : Client) => {
const { on, count, toggle, saving } = FavoriteModel(article, client);
return (
<button class={`btn ${on() ? 'btn-primary' : 'btn-outline-primary'} ${saving() ? 'disabled' : ''} btn-sm`}
type="button" onClick={toggle}
>
<i class="ion-heart" /> {on() ? 'Unfavorite Article' : 'Favorite Article'} <span class="counter">({count()})</span>
</button>
);
};