Skip to content

Commit 5d3b520

Browse files
authored
Merge pull request #3399 from metacpan/revert-3380-leo/disable_favs
Revert "Remove favourite function - causing traffic"
2 parents aa9554a + fea1e11 commit 5d3b520

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

lib/MetaCPAN/Web/Model/ReleaseInfo.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ sub _fetch {
7575
sub {
7676
my ($dist) = @_;
7777
return (
78+
[ favorites => $self->_favorite->by_dist($dist) ],
7879
[ plussers => $self->_favorite->find_plussers($dist) ],
7980
[ versions => $self->_release->versions($dist) ],
8081
[ distribution => $self->_distribution->get($dist) ],

root/inc/favorite.tx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
1+
<div id="[% $release.distribution %]-fav" class="show-logged-in">
2+
<form action="/account/favorite/add" style="display: inline" method="POST">
3+
<input type="hidden" name="remove" value="0">
4+
<input type="hidden" name="release" value="[% $release.name %]">
5+
<input type="hidden" name="author" value="[% $release.author %]">
6+
<input type="hidden" name="distribution" value="[% $release.distribution %]">
7+
<button type="submit" class="favorite[% if $favorites { %] highlight[% } %]"><span>[% $favorites %]</span> ++</button>
8+
</form>
9+
</div>
10+
<div class="show-logged-out">
11+
<button class="fav-not-logged-in favorite[% if $favorites { %] highlight[% } %]"><span>[% $favorites %]</span> ++</button>
12+
</div>
213
%% if $github && $repository.url {
314
<div class="star-rating">
415
<a href="[% $repository.url %]" target="_blank" class="btn btn-primary" title="View on GitHub">

root/static/js/cpan.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ require('bootstrap/js/dropdown.js');
1313
require('bootstrap/js/modal.js');
1414
require('bootstrap/js/tooltip.js');
1515

16+
function setFavTitle(button) {
17+
button.setAttribute('title', button.classList.contains('active') ? 'Remove from favorites' : 'Add to favorites');
18+
}
1619

1720
async function processUserData() {
1821
let user_data;
@@ -49,6 +52,20 @@ async function processUserData() {
4952
document.querySelector('.logged-in-icon').replaceWith(avatar);
5053
}
5154

55+
// process users current favs
56+
for (const fav of user_data.faves) {
57+
const distribution = fav.distribution;
58+
59+
// On the page... make it deltable and styled as 'active'
60+
const fav_display = document.querySelector(`#${distribution}-fav`);
61+
62+
if (fav_display) {
63+
fav_display.querySelector('input[name="remove"]').value = 1;
64+
var button = fav_display.querySelector('button');
65+
button.classList.add('active');
66+
setFavTitle(button);
67+
}
68+
}
5269
}
5370

5471
function set_page_size(selector, storage_name) {
@@ -155,6 +172,10 @@ for (const el of document.querySelectorAll('.ellipsis')) {
155172

156173
createAnchors(document.querySelectorAll('.anchors'));
157174

175+
for (const favButton of document.querySelectorAll('.breadcrumbs .favorite')) {
176+
setFavTitle(favButton);
177+
}
178+
158179
jQuery('.dropdown-toggle').dropdown(); // bootstrap
159180

160181
const toc = document.querySelector(".content .toc")
@@ -203,6 +224,54 @@ if (changes) {
203224
});
204225
}
205226

227+
for (const favForm of document.querySelectorAll('form[action="/account/favorite/add"]')) {
228+
favForm.addEventListener('submit', async e => {
229+
e.preventDefault();
230+
const formData = new FormData(favForm);
231+
const response = await fetch(favForm.action, {
232+
method: favForm.method,
233+
headers: {
234+
'Accept': 'application/json',
235+
},
236+
body: formData,
237+
});
238+
if (!response.ok) {
239+
alert("Error adding favorite!");
240+
}
241+
242+
const button = favForm.querySelector('button');
243+
button.classList.toggle('active');
244+
setFavTitle(button);
245+
const counter = button.querySelector('span');
246+
const count = counter.innerText;
247+
if (button.classList.contains('active')) {
248+
counter.innerText = count ? parseInt(count, 10) + 1 : 1;
249+
// now added let users remove
250+
favForm.querySelector('input[name="remove"]').value = 1;
251+
if (!count)
252+
button.classList.toggle('highlight');
253+
}
254+
else {
255+
// can't delete what's already deleted
256+
favForm.querySelector('input[name="remove"]').value = 0;
257+
258+
counter.textContent = parseInt(count, 10) - 1;
259+
260+
if (counter.textContent == 0) {
261+
counter.textContent = '';
262+
button.classList.toggle('highlight');
263+
}
264+
}
265+
});
266+
}
267+
268+
for (const favButton of document.querySelectorAll('.fav-not-logged-in')) {
269+
favButton.addEventListener('click', e => {
270+
e.preventDefault();
271+
alert('Please sign in to add favorites');
272+
});
273+
}
274+
206275
for (const sel of document.querySelectorAll('.select-navigator')) {
207276
sel.addEventListener('change', () => {
208277
document.location.href = sel.value;

t/controller/shared/release-info.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ my $rt_prefix = $model->RT_URL_PREFIX;
1616

1717
# Not all tests apply to all releases.
1818
my @optional = qw(
19+
favorited
1920
home_page
2021
repository
2122
issues
@@ -69,6 +70,7 @@ test_psgi app, sub {
6970
home_page => 0,
7071
reviews => 0,
7172
repository => 0,
73+
favorited => 0
7274
},
7375
);
7476

@@ -125,6 +127,18 @@ test_psgi app, sub {
125127
# TODO: latest version (should be where we already are)
126128
# TODO: author
127129

130+
# not in release-info.html but should be shown on both:
131+
132+
my $favs = '//*[contains-token(@class, "favorite")]';
133+
$tx->like( $favs, qr/\+\+$/, 'tag for favorites (++)' );
134+
135+
optional_test favorited => sub {
136+
ok(
137+
$tx->find_value("$favs/span") > 0,
138+
"$req_uri has been marked as favorite"
139+
);
140+
};
141+
128142
# Info about a release (either the one we're looking at or the one the module belongs to)
129143

130144
# TODO: Download

0 commit comments

Comments
 (0)