Skip to content

Commit 4d4f902

Browse files
authored
Adding local storage support for vote form (#1431)
* Adding local storage support for vote form
1 parent 2de484a commit 4d4f902

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

app/views/papers/_vote_summary.html.erb

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<div class="form-group">
1010
<div class="row">
1111
<div class="col">
12-
<%= f.text_area :comment, class: "form-control", placeholder: "Include a comment with your vote (required)" %>
12+
<%= f.text_area :comment, class: "form-control", placeholder: "Include a comment with your vote (required)",
13+
data: { paper_id: @paper.id }, id: "vote-comment" %>
1314
</div>
1415
</div>
1516
</div>
@@ -28,6 +29,72 @@
2829
</div>
2930
</div>
3031

32+
<script>
33+
(function() {
34+
try {
35+
if (!window.localStorage) {
36+
console.error('localStorage is not available in this browser');
37+
return;
38+
}
39+
40+
function initializeStorage() {
41+
const commentField = document.getElementById('vote-comment');
42+
if (!commentField) {
43+
console.error('Could not find comment field with ID vote-comment');
44+
return;
45+
}
46+
47+
const paperId = commentField.dataset.paperId;
48+
if (!paperId) {
49+
console.error('No paper ID found in data attributes');
50+
return;
51+
}
52+
53+
const storageKey = `vote_comment_${paperId}`;
54+
55+
try {
56+
const savedComment = localStorage.getItem(storageKey);
57+
if (savedComment) {
58+
commentField.value = savedComment;
59+
}
60+
} catch (e) {
61+
console.error('Error accessing localStorage:', e);
62+
}
63+
64+
commentField.addEventListener('input', function() {
65+
try {
66+
localStorage.setItem(storageKey, this.value);
67+
} catch (e) {
68+
console.error('Error saving to localStorage:', e);
69+
}
70+
});
71+
72+
const form = commentField.closest('form');
73+
if (form) {
74+
form.addEventListener('submit', function() {
75+
try {
76+
localStorage.removeItem(storageKey);
77+
} catch (e) {
78+
console.error('Error clearing localStorage:', e);
79+
}
80+
});
81+
} else {
82+
console.error('Could not find parent form element');
83+
}
84+
}
85+
86+
if (document.readyState === 'complete' || document.readyState === 'interactive') {
87+
initializeStorage();
88+
}
89+
90+
document.addEventListener('DOMContentLoaded', initializeStorage);
91+
92+
} catch (e) {
93+
console.error('Error in vote comment storage script:', e);
94+
}
95+
})();
96+
</script>
97+
3198
<div class="form-group">
3299
<%= link_to "View vote summary &raquo;".html_safe, "#voteSummary", class: 'tooltips', title: 'View vote summary', data: { toggle: 'collapse'} %>
33100

0 commit comments

Comments
 (0)