fix: 🐛 save sessionUsername in localStorage

This commit is contained in:
Juan Rodriguez
2021-06-16 10:24:33 -05:00
parent e3ab670eac
commit 38ce72618a
4 changed files with 9 additions and 9 deletions
@@ -6,11 +6,7 @@ export default class extends Controller {
initialize() {
this.lastLink = null
const selector = document.querySelector('meta[name="logged-in"]')
this.loggedIn = selector.getAttribute('content')
selector.parentNode.removeChild(selector)
this.sessionUsername = localStorage.getItem("session-username")
}
onCreateLinkSuccess(event) {
@@ -18,7 +14,8 @@ export default class extends Controller {
this.outputTarget.innerHTML = xhr.response
if (this.loggedIn && this.lastLink && this.lastLink.includes(this.loggedIn) && !this.userLinksTarget.innerHTML.includes(this.lastLink)) {
if (this.sessionUsername && this.lastLink && this.lastLink.includes(this.sessionUsername)
&& !this.userLinksTarget.innerHTML.includes(this.lastLink)) {
this.userLinksTarget.innerHTML = this.lastLink + this.userLinksTarget.innerHTML
}
@@ -55,6 +55,7 @@ export default class extends Controller {
}
onSuccessLogout() {
localStorage.removeItem("session-username")
Turbolinks.visit('/')
}
}
-2
View File
@@ -11,8 +11,6 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<meta name="logged-in" content="<%= current_user_username %>" />
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
+5 -1
View File
@@ -14,4 +14,8 @@
<div data-controller="links">
<%= render partial: "links/new" %>
<%= render partial: "links/index" %>
</div>
</div>
<% unless @current_user.nil? %>
<script>localStorage.setItem("session-username", "<%= current_user_username %>");</script>
<% end %>