Files
g11-m3/Use this code.vue
2026-01-20 17:54:05 +03:00

332 lines
5.8 KiB
Vue

<template>
<div class="social-feed">
<!-- Header -->
<header class="header">
<h1>📱 Social Media Feed</h1>
<p class="subtitle">Simple feed using Async/Await</p>
</header>
<!-- Main Feed -->
<main class="feed">
<!-- Stats -->
<div class="stats">
<p>Total posts: {{ posts.length }}</p>
<button @click="loadPosts" :disabled="loading" class="refresh-btn">
{{ loading ? '🔄 Loading...' : '🔄 Refresh' }}
</button>
</div>
<!-- Loading -->
<div v-if="loading" class="loading">
<div class="spinner"></div>
<p>Loading posts...</p>
</div>
<!-- Error -->
<div v-else-if="error" class="error">
<p> Error: {{ error }}</p>
<button @click="loadPosts" class="retry-btn">Try Again</button>
</div>
<!-- Posts -->
<div v-else class="posts">
<div v-if="posts.length === 0" class="empty">
<p>No posts available.</p>
</div>
<div v-else class="posts-list">
<div
v-for="post in posts"
:key="post._id || post.id"
class="post"
>
<!-- User info -->
<div class="user-info">
<div class="avatar">U{{ post.userId }}</div>
<div>
<h3>User {{ post.userId }}</h3>
<small>Post ID: {{ (post._id || post.id).substring(0, 10) }}...</small>
</div>
</div>
<!-- Post content -->
<div class="post-content">
<h4>{{ post.title }}</h4>
<p>{{ post.body || post.content }}</p>
</div>
</div>
</div>
</div>
</main>
<!-- Footer -->
<footer class="footer">
<p>API: https://api.techshare.cc/api/posts</p>
</footer>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
// State
const posts = ref([])
const loading = ref(false)
const error = ref(null)
// Load posts function
async function loadPosts() {
loading.value = true
error.value = null
try {
console.log('Fetching posts...')
const response = await fetch('https://api.techshare.cc/api/posts')
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status}`)
}
posts.value = await response.json()
console.log(`Loaded ${posts.value.length} posts`)
} catch (err) {
console.error('Error:', err)
error.value = err.message || 'Something went wrong'
} finally {
loading.value = false
}
}
// Load on page load
onMounted(() => {
loadPosts()
})
</script>
<style scoped>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.social-feed {
min-height: 100vh;
background: #f5f7fa;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
/* Header */
.header {
background: white;
padding: 2rem;
text-align: center;
border-bottom: 1px solid #eaeaea;
}
.header h1 {
color: #333;
font-size: 2rem;
margin-bottom: 0.5rem;
}
.subtitle {
color: #666;
font-size: 1rem;
}
/* Feed */
.feed {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
}
/* Stats */
.stats {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2rem;
padding: 1rem;
background: white;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.stats p {
font-weight: 600;
color: #333;
}
.refresh-btn {
padding: 0.5rem 1rem;
background: #4361ee;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: 500;
}
.refresh-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Loading */
.loading {
text-align: center;
padding: 3rem;
}
.spinner {
width: 40px;
height: 40px;
border: 3px solid #ddd;
border-top: 3px solid #4361ee;
border-radius: 50%;
margin: 0 auto 1rem;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Error */
.error {
text-align: center;
padding: 2rem;
background: #ffebee;
color: #c62828;
border-radius: 8px;
margin-bottom: 1rem;
}
.retry-btn {
margin-top: 1rem;
padding: 0.5rem 1.5rem;
background: #c62828;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
/* Empty */
.empty {
text-align: center;
padding: 3rem;
color: #666;
}
/* Posts list */
.posts-list {
display: flex;
flex-direction: column;
gap: 1.5rem;
}
/* Single post */
.post {
background: white;
border-radius: 12px;
padding: 1.5rem;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
/* User info */
.user-info {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 1rem;
padding-bottom: 1rem;
border-bottom: 1px solid #f0f0f0;
}
.avatar {
width: 50px;
height: 50px;
background: #4361ee;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
font-size: 1.2rem;
}
.user-info h3 {
color: #333;
margin-bottom: 0.25rem;
}
.user-info small {
color: #888;
font-size: 0.85rem;
}
/* Post content */
.post-content h4 {
color: #333;
margin-bottom: 0.75rem;
font-size: 1.1rem;
line-height: 1.4;
}
.post-content p {
color: #555;
line-height: 1.6;
font-size: 0.95rem;
}
/* Footer */
.footer {
text-align: center;
padding: 2rem;
color: #666;
font-size: 0.9rem;
border-top: 1px solid #eaeaea;
margin-top: 3rem;
}
/* Responsive */
@media (max-width: 768px) {
.header {
padding: 1.5rem;
}
.header h1 {
font-size: 1.5rem;
}
.feed {
padding: 1rem;
}
.stats {
flex-direction: column;
gap: 1rem;
text-align: center;
}
.post {
padding: 1rem;
}
.user-info {
gap: 0.75rem;
}
.avatar {
width: 40px;
height: 40px;
font-size: 1rem;
}
}
</style>