Skip to content

Commit

Permalink
Merge pull request #66 from Minn-Choi/feature/#6
Browse files Browse the repository at this point in the history
fix: api ์—ฐ๊ฒฐ ์ˆ˜์ •
  • Loading branch information
Minn-Choi authored Dec 2, 2024
2 parents 4e3e7ea + e0a0a91 commit c5c313e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 132 deletions.
12 changes: 8 additions & 4 deletions src/components/common/feed/FollowStats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@ const formattedDate = computed(() => {
const fetchFollowCounts = async () => {
try {
const followerResponse = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/followers`, {
const followerResponse = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/followers`, {
method: 'GET',
credentials: 'include',
});
const followerData = await followerResponse.json();
followerCount.value = Array.isArray(followerData) ? followerData.length : 0;
followerCount.value = followerData.count || 0;
console.log('ํŒ”๋กœ์›Œ ๋ฐ์ดํ„ฐ:', followerData);
const followingResponse = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/following`, {
const followingResponse = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/following`, {
method: 'GET',
credentials: 'include',
});
const followingData = await followingResponse.json();
followingCount.value = Array.isArray(followingData) ? followingData.length : 0;
followingCount.value = followingData.count || 0;
console.log('ํŒ”๋กœ์ž‰ ๋ฐ์ดํ„ฐ:', followingData);
} catch (error) {
console.error('ํŒ”๋กœ์›Œ ๋ฐ ํŒ”๋กœ์ž‰ ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ:', error);
followerCount.value = 0;
followingCount.value = 0;
}
};
const router = useRouter()
const goToFollowPage = () => {
Expand Down
133 changes: 22 additions & 111 deletions src/components/common/feed/SearchBar.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
<template>
<div class="search-modal">
<div class="search-bar">
<input class="textinput"
<input
class="textinput"
type="text"
v-model="email"
placeholder="์นœ๊ตฌ์˜ ์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”"
/>
<img src="../../../assets/images/plusBtn2.svg" class="search-icon" @click="searchFriend" />
</div>

<div class="search-results" v-if="searched && searchResults.length > 0">
<div v-for="user in searchResults" :key="user.id" class="user-item">
<span>{{ user.email }}</span>
<button
:class="{'following': user.isFollowing}"
:disabled="user.isFollowing"
@click="followUser(user)"
>
{{ user.isFollowing ? 'ํŒ”๋กœ์šฐ' : 'ํŒ”๋กœ์šฐ' }}
</button>
</div>
</div>

<div v-if="searched && searchResults.length === 0" class="no-results">
๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
<img
src="../../../assets/images/plusBtn2.svg"
class="search-icon"
@click="followUser"
/>
</div>
</div>
</template>
Expand All @@ -32,66 +20,37 @@
import { ref } from 'vue'
const email = ref('')
const searchResults = ref([])
const searched = ref(false)
const searchFriend = async () => {
searched.value = false
if (!email.value) {
searchResults.value = []
searched.value = true
const followUser = async () => {
if (!email.value.trim()) {
alert("์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”.")
return
}
try {
const response = await fetch(
`${process.env.VUE_APP_BE_API_URL}/api/follow`,
`${process.env.VUE_APP_BE_API_URL}/api/follows`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: email.value }),
credentials: 'include'
body: JSON.stringify({ email: email.value.trim() }),
credentials: 'include',
}
)
if (response.ok) {
const data = await response.json()
searchResults.value = data.users
searched.value = true
alert("ํŒ”๋กœ์šฐ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
email.value = ''
} else if (response.status === 404) {
alert("์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
} else {
alert("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
searchResults.value = []
searched.value = true
alert("ํŒ”๋กœ์šฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.")
}
} catch (error) {
alert("API ์˜ค๋ฅ˜")
console.error('API ์š”์ฒญ ์˜ค๋ฅ˜:', error)
searched.value = true
}
}
const followUser = async (user) => {
try {
const response = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follow`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ userId: user.id }),
credentials: 'include'
})
if (response.ok) {
user.isFollowing = true
} else {
alert('ํŒ”๋กœ์šฐ ์‹คํŒจ')
}
} catch (error) {
console.error('ํŒ”๋กœ์šฐ ์š”์ฒญ ์˜ค๋ฅ˜:', error)
alert('ํŒ”๋กœ์šฐ ์š”์ฒญ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.')
console.error("API ์š”์ฒญ ์˜ค๋ฅ˜:", error)
alert("API ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.")
}
}
</script>
Expand All @@ -112,8 +71,8 @@ const followUser = async (user) => {
.textinput {
background-color: white;
outline: none;
border: none;
box-shadow: none;
border: none;
box-shadow: none;
}
.search-bar {
Expand Down Expand Up @@ -141,52 +100,4 @@ const followUser = async (user) => {
height: 24px;
cursor: pointer;
}
.search-results {
width: 100%;
margin-top: 10px;
font-family: 'NaR';
}
.user-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 12px;
font-family: 'NaR';
}
.user-item span {
font-size: 14px;
color: #333;
}
.user-item button {
background-color: #ff9100;
color: #fff;
font-family: 'NaR';
border: none;
border-radius: 15px;
padding: 5px 10px;
font-size: 12px;
cursor: pointer;
transition: background-color 0.3s;
}
.user-item button.following {
background-color: #d9d9d9;
color: #666;
cursor: default;
}
.user-item button:not(.following):hover {
background-color: #ff7300;
}
.no-results {
color: #878282;
font-size: 14px;
margin-top: 10px;
margin-bottom: 10px;
}
</style>
81 changes: 64 additions & 17 deletions src/views/feed/AkoStampFollow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

<div class="bg-white rounded-2xl mb-4" style="height: 300px; display: flex; justify-content: center;">
<div class="follower font-NaL" style="width:50%; display: flex; flex-direction: column; align-items: center; margin-top:10px;">
<div @click="showFollowers = true; showFollowings = false" style="cursor: pointer;">
<div @click="showFollowers = true; showFollowings = false" style="cursor: pointer; z-index: 200;">
ํŒ”๋กœ์›Œ {{ followerCount }}
</div>
<div class="followerline" :style="{ width: '100%', height: '2px', background: showFollowers ? '#FF7F00' : '#B3B3B3', marginBottom: '15px' }"></div>
<ul v-show="showFollowers && followers.length > 0" style="padding: 20; width: 240%; z-index: 200;">
<ul v-show="showFollowers && followers.length > 0" style="margin-left: 155px; padding: 20; width: 150%; z-index: 200;">
<li v-for="follower in followers" :key="follower.followingId" class="flex justify-between items-center mb-4">
<span class='font-NaR'>{{ follower.nickname }}</span>
<button @click="blockFollower(follower.followingId)" class="followerDelete-btn">์‚ญ์ œ</button>
Expand Down Expand Up @@ -62,60 +62,107 @@ const showFollowings = ref(false);
async function fetchFollowerCount() {
try {
const response = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/followers`, {
const response = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/followers`, {
method: 'GET',
credentials: 'include'
});
const data = await response.json();
followers.value = data;
followerCount.value = data.length > 0 ? data.length : 0; // ๋ฐฐ์—ด์ด ๋น„์–ด์žˆ์œผ๋ฉด 0
// API ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋กœ๊ทธ ์ถœ๋ ฅ
console.log("ํŒ”๋กœ์›Œ ์‘๋‹ต ๋ฐ์ดํ„ฐ:", data);
if (data && Array.isArray(data.follows) && data.follows.length > 0) {
followers.value = data.follows.map(follower => ({
followingId: follower.followingId,
nickname: follower.nickname, // nickname์ด ์ œ๋Œ€๋กœ ๋“ค์–ด์˜ค๋Š”์ง€ ํ™•์ธ
email: follower.email,
})) || [];
} else {
followers.value = []; // ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด ๋นˆ ๋ฐฐ์—ด
}
followerCount.value = data.count || 0; // ํŒ”๋กœ์›Œ ์ˆ˜
} catch (error) {
console.error('ํŒ”๋กœ์›Œ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ:', error);
followerCount.value = 0; // ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ 0 ์„ค์ •
followerCount.value = 0;
}
}
// ํŒ”๋กœ์ž‰ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ
async function fetchFollowingCount() {
try {
const response = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/following`, {
const response = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/following`, {
method: 'GET',
credentials: 'include'
});
const data = await response.json();
followings.value = data;
followingCount.value = data.length > 0 ? data.length : 0;
// API ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋กœ๊ทธ ์ถœ๋ ฅ
console.log("ํŒ”๋กœ์ž‰ ์‘๋‹ต ๋ฐ์ดํ„ฐ:", data);
if (data && Array.isArray(data.follows) && data.follows.length > 0) {
followings.value = data.follows.map(following => ({
followingId: following.followingId,
nickname: following.nickname, // nickname์ด ์ œ๋Œ€๋กœ ๋“ค์–ด์˜ค๋Š”์ง€ ํ™•์ธ
email: following.email,
})) || [];
} else {
followings.value = []; // ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด ๋นˆ ๋ฐฐ์—ด
}
followingCount.value = data.count || 0; // ํŒ”๋กœ์ž‰ ์ˆ˜
} catch (error) {
console.error('ํŒ”๋กœ์ž‰ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ:', error);
followingCount.value = 0;
followingCount.value = 0;
}
}
async function blockFollower(id) {
try {
await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/block/${id}`, {
const response = await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/block`, {
method: 'DELETE',
credentials: 'include'
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
body: JSON.stringify({
followerId: id,
}),
});
followers.value = followers.value.filter(follower => follower.followingId !== id);
followerCount.value = followers.value.length;
if (response.ok) {
followers.value = followers.value.filter(follower => follower.followingId !== id);
followerCount.value = followers.value.length;
} else {
console.error('ํŒ”๋กœ์›Œ ์ฐจ๋‹จ ์š”์ฒญ ์‹คํŒจ:', response.status, response.statusText);
}
} catch (error) {
console.error('ํŒ”๋กœ์›Œ ์ฐจ๋‹จ ์‹คํŒจ:', error);
}
}
async function unfollow(id) {
try {
await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows/${id}`, {
await fetch(`${process.env.VUE_APP_BE_API_URL}/api/follows`, {
method: 'DELETE',
credentials: 'include'
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
body: JSON.stringify({
followingId: id
}),
});
followings.value = followings.value.filter(following => following.followingId !== id);
followingCount.value = followings.value.length;
} catch (error) {
console.error('์–ธํŒ”๋กœ์šฐ ์‹คํŒจ:', error);
}
}
onMounted(() => {
fetchFollowerCount();
fetchFollowingCount();
Expand All @@ -124,4 +171,4 @@ onMounted(() => {
const goBack = () => {
window.history.back();
};
</script>
</script>

0 comments on commit c5c313e

Please sign in to comment.