1 | import { defineStore } from 'pinia'
|
---|
2 |
|
---|
3 | export const userStore = defineStore('userStore', {
|
---|
4 |
|
---|
5 | state() {
|
---|
6 | return {
|
---|
7 | data: {
|
---|
8 | id: 0,
|
---|
9 | firstName: "",
|
---|
10 | lastName: "",
|
---|
11 | email: "",
|
---|
12 | phoneNumber: "",
|
---|
13 | role: "UN_AUTHENTICATED",
|
---|
14 | token: "",
|
---|
15 | logoUrl: null,
|
---|
16 | profilePictureBase64Encoded: null
|
---|
17 | }
|
---|
18 |
|
---|
19 | }
|
---|
20 | },
|
---|
21 | actions: {
|
---|
22 |
|
---|
23 | setLocalStorage(jsonObject) {
|
---|
24 |
|
---|
25 | this.data = jsonObject
|
---|
26 | //this.data.profilePictureBase64Encoded =
|
---|
27 |
|
---|
28 | if (this.data.logoUrl != null) {
|
---|
29 | fetch('http://localhost:8080' + this.data.logoUrl, {
|
---|
30 | headers: {
|
---|
31 | Authorization: this.getToken,
|
---|
32 | }
|
---|
33 | })
|
---|
34 | .then(response => response.blob())
|
---|
35 | .then(blob => {
|
---|
36 | const reader = new FileReader();
|
---|
37 | reader.onloadend = () => {
|
---|
38 | const base64data = reader.result;
|
---|
39 |
|
---|
40 | this.setProfilePicturePathToLocalStorage(this.data.logoUrl, base64data)
|
---|
41 | };
|
---|
42 | reader.readAsDataURL(blob);
|
---|
43 | })
|
---|
44 | .catch(err => console.log("Error parsing the blob", err))
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | localStorage.setItem('userData', JSON.stringify(jsonObject));
|
---|
50 |
|
---|
51 | },
|
---|
52 | removeProfilePictureFromLocaleStorage() {
|
---|
53 | this.data.profilePictureBase64Encoded = null;
|
---|
54 | this.data.logoUrl = null;
|
---|
55 | localStorage.setItem('userData', JSON.stringify(this.data));
|
---|
56 | },
|
---|
57 |
|
---|
58 | setNewEditedDataToLocalStorage(jsonObject) {
|
---|
59 | const {firstName, lastName, phoneNumber} = jsonObject;
|
---|
60 | this.data.firstName = firstName;
|
---|
61 | this.data.lastName = lastName;
|
---|
62 | this.data.phoneNumber = phoneNumber;
|
---|
63 |
|
---|
64 | localStorage.setItem('userData', JSON.stringify(this.data));
|
---|
65 | },
|
---|
66 | setNewEmailToLocalStorage(jsonObject) {
|
---|
67 | const {email, jwt} = jsonObject;
|
---|
68 | this.data.email = email;
|
---|
69 | this.data.token = jwt;
|
---|
70 |
|
---|
71 | localStorage.setItem('userData', JSON.stringify(this.data));
|
---|
72 | },
|
---|
73 | setProfilePicturePathToLocalStorage(profilePicturePath, base64Encoded) {
|
---|
74 | this.data.logoUrl = profilePicturePath;
|
---|
75 | this.data.profilePictureBase64Encoded = base64Encoded;
|
---|
76 | localStorage.setItem('userData', JSON.stringify(this.data));
|
---|
77 | },
|
---|
78 | getLocalStorage() {
|
---|
79 | let nonparsed = localStorage.getItem('userData');
|
---|
80 | if (nonparsed !== null) {
|
---|
81 | this.data = JSON.parse(nonparsed);
|
---|
82 | }
|
---|
83 | },
|
---|
84 | clearLocalStorage() {
|
---|
85 |
|
---|
86 | this.data = {
|
---|
87 | id: 0,
|
---|
88 | firstName: "",
|
---|
89 | lastName: "",
|
---|
90 | email: "",
|
---|
91 | phoneNumber: "",
|
---|
92 | role: "UN_AUTHENTICATED",
|
---|
93 | token: "",
|
---|
94 | logoUrl: null,
|
---|
95 | profilePictureBase64Encoded: null
|
---|
96 | }
|
---|
97 | localStorage.setItem('userData', JSON.stringify(this.data));
|
---|
98 |
|
---|
99 | }
|
---|
100 | },
|
---|
101 | getters: {
|
---|
102 | getToken(){
|
---|
103 | return 'Bearer ' + this.data.token;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | })
|
---|