1import { store, getContext } from '@wordpress/interactivity'; 2 3store('star-rate', { 4 state: { 5 get formattedAverage() { 6 const state = store('star-rate').state; 7 if (state.count === 0) { 8 return 'â'; 9 } 10 return Math.round(state.average) + '/5'; 11 }, 12 13 get formattedCount() { 14 const state = store('star-rate').state; 15 const i18n = state.i18n || {}; 16 17 if (state.count === 0) { 18 return i18n.noVotes || 'No votes yet'; 19 } 20 if (state.count === 1) { 21 return i18n.oneVote || '1 vote'; 22 } 23 24 const template = i18n.votes || '%d votes'; 25 return template.replace('%d', state.count); 26 }, 27 }, 28 29 actions: { 30 async vote(event) { 31 const context = getContext(); 32 const state = store('star-rate').state; 33 const i18n = state.i18n || {}; 34 35 if (state.hasVoted || state.isLoading) { 36 return; 37 } 38 39 const rating = context.rating; 40 state.isLoading = true; 41 42 try { 43 const response = await fetch(state.restUrl, { 44 method: 'POST', 45 headers: { 46 'Content-Type': 'application/json', 47 'X-WP-Nonce': state.nonce, 48 }, 49 credentials: 'same-origin', 50 body: JSON.stringify({ 51 post_id: state.postId, 52 rating: rating, 53 nonce: state.nonce, 54 }), 55 }); 56 57 const data = await response.json(); 58 59 if (response.ok && data.success) { 60 state.average = data.data.average; 61 state.count = data.data.count; 62 state.hasVoted = true; 63 } else { 64 console.error('Star Rate: Vote failed -', data.message); 65 alert(data.message || i18n.errorVote || 'Failed to record vote.'); 66 } 67 } catch (error) { 68 console.error('Star Rate: Network error -', error); 69 alert(i18n.errorNetwork || 'Network error. Please try again.'); 70 } finally { 71 state.isLoading = false; 72 } 73 }, 74 75 hoverStar() { 76 const context = getContext(); 77 const state = store('star-rate').state; 78 79 if (!state.hasVoted) { 80 context.hoverRating = context.rating; 81 } 82 }, 83 84 resetHover() { 85 const context = getContext(); 86 const state = store('star-rate').state; 87 88 if (!state.hasVoted) { 89 context.hoverRating = 0; 90 } 91 }, 92 }, 93 94 callbacks: { 95 isStarFilled() { 96 const context = getContext(); 97 const state = store('star-rate').state; 98 99 if (state.hasVoted) { 100 return context.rating <= Math.round(state.average); 101 } 102 103 if (context.hoverRating > 0) { 104 return context.rating <= context.hoverRating; 105 } 106 107 return context.rating <= Math.round(state.average); 108 }, 109 }, 110});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.