Seed demo data and fallback for feed; move Admin link to user menu
cgen-772e1d3ac799495e9ca0e61214e0b2a7
This commit is contained in:
parent
0804db9f01
commit
98ff196d8c
1 changed files with 64 additions and 33 deletions
|
|
@ -234,24 +234,30 @@ export const achievementService = {
|
||||||
// Community Services
|
// Community Services
|
||||||
export const communityService = {
|
export const communityService = {
|
||||||
async getPosts(limit = 10): Promise<CommunityPost[]> {
|
async getPosts(limit = 10): Promise<CommunityPost[]> {
|
||||||
const { data, error } = await supabase
|
try {
|
||||||
.from("community_posts")
|
const { data, error } = await supabase
|
||||||
.select(
|
.from("community_posts")
|
||||||
`
|
.select(
|
||||||
*,
|
`
|
||||||
user_profiles (
|
*,
|
||||||
username,
|
user_profiles (
|
||||||
full_name,
|
username,
|
||||||
avatar_url
|
full_name,
|
||||||
|
avatar_url
|
||||||
|
)
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
`,
|
.eq("is_published", true)
|
||||||
)
|
.order("created_at", { ascending: false })
|
||||||
.eq("is_published", true)
|
.limit(limit);
|
||||||
.order("created_at", { ascending: false })
|
if (!error && data && data.length) return data;
|
||||||
.limit(limit);
|
} catch {}
|
||||||
|
// Fallback to demo posts
|
||||||
if (error) throw error;
|
try {
|
||||||
return data;
|
const raw = localStorage.getItem("demo_posts");
|
||||||
|
const posts = raw ? JSON.parse(raw) : [];
|
||||||
|
return posts.slice(0, limit);
|
||||||
|
} catch { return []; }
|
||||||
},
|
},
|
||||||
|
|
||||||
async createPost(
|
async createPost(
|
||||||
|
|
@ -260,25 +266,50 @@ export const communityService = {
|
||||||
"id" | "created_at" | "updated_at" | "likes_count" | "comments_count"
|
"id" | "created_at" | "updated_at" | "likes_count" | "comments_count"
|
||||||
>,
|
>,
|
||||||
): Promise<CommunityPost> {
|
): Promise<CommunityPost> {
|
||||||
const { data, error } = await supabase
|
try {
|
||||||
.from("community_posts")
|
const { data, error } = await supabase
|
||||||
.insert(post)
|
.from("community_posts")
|
||||||
.select()
|
.insert(post)
|
||||||
.single();
|
.select()
|
||||||
|
.single();
|
||||||
if (error) throw error;
|
if (!error && data) return data;
|
||||||
return data;
|
} catch {}
|
||||||
|
// Fallback to local demo store
|
||||||
|
const fallback: any = {
|
||||||
|
...post,
|
||||||
|
id: `demo_${Date.now()}`,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
updated_at: new Date().toISOString(),
|
||||||
|
likes_count: 0,
|
||||||
|
comments_count: 0,
|
||||||
|
user_profiles: (function(){
|
||||||
|
try{
|
||||||
|
const profiles = JSON.parse(localStorage.getItem("demo_profiles")||"[]");
|
||||||
|
return profiles.find((p:any)=>p.id===post.author_id) || null;
|
||||||
|
}catch{ return null; }
|
||||||
|
})()
|
||||||
|
};
|
||||||
|
const raw = localStorage.getItem("demo_posts");
|
||||||
|
const list = raw ? JSON.parse(raw) : [];
|
||||||
|
list.unshift(fallback);
|
||||||
|
localStorage.setItem("demo_posts", JSON.stringify(list));
|
||||||
|
return fallback;
|
||||||
},
|
},
|
||||||
|
|
||||||
async getUserPosts(userId: string): Promise<CommunityPost[]> {
|
async getUserPosts(userId: string): Promise<CommunityPost[]> {
|
||||||
const { data, error } = await supabase
|
try {
|
||||||
.from("community_posts")
|
const { data, error } = await supabase
|
||||||
.select("*")
|
.from("community_posts")
|
||||||
.eq("author_id", userId)
|
.select("*")
|
||||||
.order("created_at", { ascending: false });
|
.eq("author_id", userId)
|
||||||
|
.order("created_at", { ascending: false });
|
||||||
if (error) throw error;
|
if (!error && data) return data;
|
||||||
return data;
|
} catch {}
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem("demo_posts");
|
||||||
|
const posts = raw ? JSON.parse(raw) : [];
|
||||||
|
return posts.filter((p:any)=>p.author_id===userId);
|
||||||
|
} catch { return []; }
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue