-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy.js
187 lines (159 loc) · 4.52 KB
/
my.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { config } from './config.js';
let login = false;
//즐겨찾기 뭐했는지 조회함수
async function getData() {
try {
const response = await fetch(config.serverURL + 'mypage', {
method: 'GET',
credentials: 'include',
});
const likeData = await response.json();
if (response.status == 401) {
alert(`${likeData.message}`);
window.location.href = 'login.html';
return;
}
if (response.ok) {
login = true;
}
const businessGroupMap = {
sojoong: 'sojoong',
inhyuck: 'inhyuck',
chahyuck: 'chahyuck',
EAI: 'EAI',
potal: 'potal',
haksa: 'haksa',
janghack: 'janghack',
chjin: 'chjin',
};
console.log(likeData); // 삭제!!!
const userId = likeData.accountId;
const usernameDiv = document.querySelector('.username_div');
usernameDiv.innerHTML = `<span style="font-weight: bold;">${userId}</span> 님의 즐겨찾기`;
likeData.message.forEach((item) => {
const starId = businessGroupMap[item.businessGroupName];
if (starId) {
const star = document.getElementById(starId);
if (star) {
const liked = item.liked;
const path = star.querySelector('path');
path.setAttribute('fill', liked ? 'yellow' : 'none');
favoriteStatus[starId] = liked ? 1 : 0;
} else {
console.log(`${starId} 찾을 수 없음`);
}
}
});
} catch (error) {
console.log('데이터 오류');
}
}
document.addEventListener('DOMContentLoaded', async function () {
await getData();
changeUI();
});
//
//별 눌렀을때 ui 변경, 데이터 수정
const favoriteStatus = {};
const starIds = [
'sojoong',
'inhyuck',
'chahyuck',
'EAI',
'potal',
'haksa',
'janghack',
'chjin',
];
starIds.forEach((starId) => {
const star = document.getElementById(starId);
if (star) {
star.addEventListener('click', function () {
let path = this.querySelector('path');
let currentFill = path.getAttribute('fill');
let newFill = currentFill === 'yellow' ? 'none' : 'yellow';
path.setAttribute('fill', newFill);
favoriteStatus[starId] = newFill === 'yellow' ? 1 : 0;
});
} else {
console.log(`ID가 '${starId}'인 요소를 찾을 수 없습니다.`);
}
});
//저장하기 버튼 눌렀을 때 즐겨찾기 수정 보내기
document
.getElementById('save_btn')
.addEventListener('click', async function () {
try {
// 서버 형식
const favoriteList = Object.keys(favoriteStatus).map((key) => ({
businessGroupName: key,
liked: favoriteStatus[key],
}));
const request = {
message: favoriteList,
};
const response = await fetch(config.serverURL + 'mypage', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request),
});
const result = await response.json();
if (response.ok) {
alert('즐겨찾기가 저장되었습니다.');
console.log(result);
window.location.reload;
} else {
alert(`저장 실패: ${result.message}`);
}
} catch (error) {
console.error('저장 중 오류 발생:', error);
alert('저장 중 오류가 발생하였습니다.');
}
});
//
//로그인버튼 로그아웃버튼으로 바뀌는 함수
function changeUI() {
const loginBtn = document.getElementById('head_log');
if (!login) {
loginBtn.textContent = '로그인';
loginBtn.onclick = () => (window.location.href = 'login.html'); //화살표함수
} else {
loginBtn.textContent = '로그아웃';
loginBtn.onclick = logout;
}
}
//로그아웃함수(로그인페이지 제외)
async function logout() {
if (!login) {
window.location.href = 'login.html';
return;
}
try {
const response = await fetch(config.serverURL + 'users/logout', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
});
if (!response.ok) {
console.error('로그아웃 실패');
throw new Error('로그아웃 실패');
}
login = false;
changeUI();
alert('로그아웃 되었습니다');
window.location.href = 'main.html';
} catch (error) {
console.log('오류가 발생했습니다.', error);
}
}
//코드 실행
const loginBtn = document.getElementById('head_log');
loginBtn.addEventListener('click', async function () {
if (login) {
await logout();
} else {
window.location.href = 'login.html';
}
});