Skip to content

Commit

Permalink
Salva usuário no localstorage em vez da sessão
Browse files Browse the repository at this point in the history
  • Loading branch information
naaando committed Nov 29, 2024
1 parent 26999a5 commit 657bb5a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion resources/js/Components/Nav.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup>
import user from "@/user";
import { ref, watch } from "vue";
const props = defineProps({
Expand All @@ -21,7 +22,7 @@ const emit = defineEmits(["name-updated"]);
const setUsername = () => {
console.log(`Setting name: ${finalName.value}`);
sessionStorage.setItem("username", finalName.value);
user.saveUsername(finalName.value)
emit("name-updated");
};
</script>
Expand Down
3 changes: 2 additions & 1 deletion resources/js/Components/ProfileModal.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup>
import user from "@/user";
import { ref } from "vue";
import Modal from "./Modal.vue";
Expand All @@ -15,7 +16,7 @@ const emit = defineEmits(["name-updated"]);
const setUsername = () => {
console.log(`Setting name: ${finalName.value}`);
sessionStorage.setItem("username", finalName.value);
user.saveUsername(finalName.value);
emit("name-updated");
};
</script>
Expand Down
5 changes: 3 additions & 2 deletions resources/js/Layouts/MainLayout.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script setup>
import Nav from "@/Components/Nav.vue";
import ProfileModal from "@/Components/ProfileModal.vue";
import { ref, onMounted } from "vue";
import user from "@/user";
import { onMounted, ref } from "vue";
const username = ref("");
const fetchUsername = () => {
username.value = sessionStorage.getItem("username") ?? "";
username.value = user.retrieveUsername() ?? "";
console.log(`Name on session storage: ${username.value}`);
};
Expand Down
8 changes: 3 additions & 5 deletions resources/js/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import user from "./user";

export function createRoom(name) {
return axios.post("/api/rooms", { name: name });
Expand Down Expand Up @@ -45,7 +46,7 @@ export const votes = (roundId) => ({
}

const response = await axios.post(`/api/rounds/${roundId}/votes`, {
name: this.retrieveUsername(),
name: user.retrieveUsername(),
});

this.saveId(response.data.data.id);
Expand All @@ -54,7 +55,7 @@ export const votes = (roundId) => ({
set(value) {
const id = this.retrieveId();
axios.put(`/api/rounds/${roundId}/votes/${id}`, {
name: this.retrieveUsername(),
name: user.retrieveUsername(),
vote: value,
});
},
Expand All @@ -64,7 +65,4 @@ export const votes = (roundId) => ({
saveId(id) {
sessionStorage.setItem(`vote.${roundId}`, id);
},
retrieveUsername() {
return sessionStorage.getItem(`username`);
},
});
8 changes: 8 additions & 0 deletions resources/js/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
saveUsername(username) {
localStorage.setItem(`username`, username);
},
retrieveUsername() {
return localStorage.getItem(`username`);
}
}

0 comments on commit 657bb5a

Please sign in to comment.