Skip to content

Commit 40c2314

Browse files
added video filter, channel store
1 parent e8ea7ac commit 40c2314

File tree

6 files changed

+186
-90
lines changed

6 files changed

+186
-90
lines changed

components/Channel/ChannelAbout.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ import { supabase } from "../../SupabaseClient";
66

77
const ChannelAbout = () => {
88
const {
9-
activeChannel: { timestamp:joinedDate, views, description, location, uid },
9+
activeChannel: { timestamp:joinedDate, description, location, uid },
1010
} = useStateContext();
1111

1212
const [socialLinks, setSocialLinks] = useState([]);
13+
const [views, setViews] = useState(0);
1314
useEffect(() => {
1415
const myFunction = async () => {
1516
const {data} = await supabase.from('socialLinks').select().eq('channelRef', uid);
1617
setSocialLinks(data);
1718
}
19+
const fetchViews = async () => {
20+
const {data: videos} = await supabase.from('videos').select().eq('channelRef', uid);
21+
const views = [];
22+
videos.map(video => {
23+
video?.views?.map(view => views.push(view))
24+
})
25+
setViews(views?.length);
26+
}
1827
myFunction();
28+
fetchViews();
1929
}, [])
30+
2031
return (
2132
<div className="w-full flex items-start justify-between p-4 mt-10 dark:text-white h-screen">
2233
<div className="w-9/12 mr-8 flex flex-col">

components/Channel/ChannelChannels.jsx

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,112 @@
11
import { BellIcon } from "@heroicons/react/24/outline";
22
import { useRouter } from "next/router";
33
import { numify } from "numify";
4-
import React, { useState } from "react";
4+
import React, { useEffect, useState } from "react";
55
import { useChannelState } from "../../context/ChannelState";
66
import { useStateContext } from "../../context/StateContext";
7+
import { supabase } from "../../SupabaseClient";
78

89
const Channel = ({ channelRef }) => {
910
const router = useRouter();
10-
const { currentChannel, fetchChannelDetails } = useChannelState();
11-
const { channelImage, channelName, channelDisplayName, subscribers } =
12-
fetchChannelDetails(channelRef);
13-
const isSubscribed = (myChannelRef) => {
14-
const channels = currentChannel.subscriptions.filter(
15-
(channelRef) => channelRef === myChannelRef
16-
);
17-
return channels.length > 0;
18-
};
11+
const { currentChannel, fetchChannelDetails, Subscribe, UnSubscribe } =
12+
useChannelState();
13+
const [channelDetails, setChannelDetails] = useState();
14+
const [isSubscribed, setIsSubscribed] = useState();
15+
16+
const [updatedSubscribers, setUpdatedSubscribers] = useState(0);
17+
18+
useEffect(() => {
19+
const myFunction = async () => {
20+
const details = await fetchChannelDetails(channelRef);
21+
const { data } = await supabase
22+
.from("channelInfo")
23+
.select()
24+
.eq("channelRef", channelRef);
25+
const subscribers = data[0]?.subscribers;
26+
subscribers?.includes(currentChannel?.uid) && setIsSubscribed(true);
27+
setUpdatedSubscribers(subscribers?.length);
28+
setChannelDetails({ ...details, subscribers: subscribers });
29+
};
30+
myFunction();
31+
}, []);
1932

2033
return (
2134
<div className="flex flex-col justify-center items-center space-y-1">
2235
<img
23-
src={channelImage}
36+
src={channelDetails?.channelImage}
2437
alt="channel image"
2538
onClick={() => {
26-
router.push(`/@${channelName}`);
39+
router.push(`/@${channelDetails?.channelName}`);
2740
window.location.reload();
2841
}}
2942
className="rounded-full w-28 h-28 myb-2 cursor-pointer"
3043
/>
31-
<p className="font-bold text-center text-xl">{channelDisplayName}</p>
32-
<span className="text-gray text-xs">
33-
{numify(subscribers)} {subscribers <= 1 ? "Subscriber" : "Subscribers"}
34-
</span>
35-
<div>
36-
{isSubscribed(channelRef) ? (
37-
<div
38-
// onClick={Unsubscribe}
39-
className="mt-4 space-x-2 dark:hover:bg-neutral-700 text-neutral-900 dark:text-white dark:bg-neutral-800 flex items-center py-2 px-4 bg-gray-100 text-sm rounded-full cursor-pointer font-semibold hover:bg-gray-200"
40-
>
41-
<BellIcon className="icon mr-2 p-0 w-6 h-6" />
42-
<span>Subscribed</span>
44+
<p className="font-bold text-center text-xl">
45+
{channelDetails?.channelDisplayName}
46+
</p>
47+
48+
{channelDetails?.subscribers && (
49+
<>
50+
<span className="text-gray text-xs">
51+
{numify(updatedSubscribers)}{" "}
52+
{channelDetails?.subscribers?.length <= 1
53+
? "Subscriber"
54+
: "Subscribers"}
55+
</span>
56+
<div>
57+
{isSubscribed ? (
58+
<div
59+
// onClick={Unsubscribe}
60+
61+
onClick={() =>
62+
UnSubscribe(
63+
setUpdatedSubscribers,
64+
setIsSubscribed,
65+
channelRef
66+
)
67+
}
68+
className="mt-4 space-x-2 dark:hover:bg-neutral-700 text-neutral-900 dark:text-white dark:bg-neutral-800 flex items-center py-2 px-4 bg-gray-100 text-sm rounded-full cursor-pointer font-semibold hover:bg-gray-200"
69+
>
70+
<BellIcon className="icon mr-2 p-0 w-6 h-6" />
71+
<span>Subscribed</span>
72+
</div>
73+
) : (
74+
<button
75+
// onClick={Subscribe}
76+
onClick={() =>
77+
Subscribe(setUpdatedSubscribers, setIsSubscribed, channelRef)
78+
}
79+
className="subscribe mt-4"
80+
>
81+
Subscribe
82+
</button>
83+
)}
4384
</div>
44-
) : (
45-
<button
46-
// onClick={Subscribe}
47-
className="subscribe mt-4"
48-
>
49-
Subscribe
50-
</button>
51-
)}
52-
</div>
85+
</>
86+
)}
5387
</div>
5488
);
5589
};
5690

5791
const ChannelChannels = () => {
5892
const {
59-
activeChannel: { subscriptions },
93+
activeChannel: { uid },
6094
} = useStateContext();
95+
const [subscriptions, setSubscriptions] = useState([]);
96+
const { currentChannel } = useChannelState();
97+
useEffect(() => {
98+
const fetchSubscriptions = async () => {
99+
let currentSubscriptions = [];
100+
const { data: channels } = await supabase.from("channelInfo").select();
101+
channels.map(
102+
(channel) =>
103+
channel?.subscribers?.includes(uid) &&
104+
currentSubscriptions.push(channel?.channelRef)
105+
);
106+
setSubscriptions(currentSubscriptions);
107+
};
108+
fetchSubscriptions();
109+
}, []);
61110

62111
return (
63112
<div className="w-full mx-auto flex items-start flex-col space-y-2 p-4 mt-10 dark:text-white h-screen">

components/Channel/ChannelHome.jsx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React, { useState, useEffect } from "react";
22
import Masonry from "react-masonry-css";
33
import { useChannelState } from "../../context/ChannelState";
44
import { useStateContext } from "../../context/StateContext";
5+
import {supabase} from '../../SupabaseClient';
56
import Video from "../Video";
67

78
const ChannelHome = () => {
89
const {
9-
videos,
1010
setVideos,
1111
activeChannel: { channelName, uid },
1212
} = useStateContext();
13-
const { channelSearch, setChannelSearch } = useChannelState();
13+
const { channelSearch, setChannelSearch, fetchChannelVideos } = useChannelState();
1414
const [videoFilter, setVideoFilter] = useState("Recently Uploaded");
1515
const videoFilters = [
1616
{
@@ -33,21 +33,20 @@ const ChannelHome = () => {
3333
const [channelVideos, setChannelVideos] = useState([]);
3434
const {fetchChannelDetails} = useChannelState();
3535

36-
const handlePopularVideos = () => {
37-
const channelVideos = videos.filter(
38-
(video) => video?.channelRef === uid
39-
);
36+
const handlePopularVideos =async () => {
37+
const {data: videos} = await supabase.from('videos').select().eq('channelRef', uid);
4038
setChannelVideos(
41-
channelVideos.sort(
39+
videos.sort(
4240
(video1, video2) => parseFloat(video2.views) - parseFloat(video1.views)
4341
)
4442
);
43+
console.log( videos.sort(
44+
(video1, video2) => parseFloat(video2.views?.length) - parseFloat(video1.views?.length)
45+
))
4546
};
4647

47-
const handleRecentlyPosted = () => {
48-
const channelVideos = videos.filter(
49-
(video) => video?.channelName === channelName
50-
);
48+
const handleRecentlyPosted = async () => {
49+
const {data: channelVideos} = await supabase.from('videos').select().eq('channelRef', uid);
5150
channelVideos.length > 1 &&
5251
setChannelVideos(
5352
channelVideos.sort(
@@ -57,25 +56,26 @@ const ChannelHome = () => {
5756
);
5857
};
5958

60-
const searchChannelVideos = () => {
59+
const searchChannelVideos = async() => {
6160
if(channelSearch?.trim() === '') return;
62-
const channelVideos = videos.filter(
63-
(video) => video?.channelRef === uid
64-
);
65-
setChannelVideos(
66-
channelVideos.filter((video) => video.title.toLowerCase().includes(channelSearch.toLowerCase()))
61+
const myFunction = async () => {
62+
const channelVideos = await fetchChannelVideos(uid);
63+
await setChannelVideos(
64+
channelVideos?.filter((video) => video.title.toLowerCase().includes(channelSearch.toLowerCase()))
6765
);
66+
}
67+
myFunction();
6868
};
6969

70-
const fetchChannelVideos = () => {
71-
setChannelVideos(
72-
videos.filter((video) => video?.channelRef === uid)
73-
);
74-
}
75-
7670
useEffect(() => {
77-
channelSearch.trim() === '' ? fetchChannelVideos() : searchChannelVideos();
78-
}, [videos, channelSearch]);
71+
console.log(channelSearch, channelSearch?.trim()?.length)
72+
const fetchVideos = async () => {
73+
const channelVideos =await fetchChannelVideos(uid);
74+
console.log(channelVideos)
75+
await setChannelVideos(channelVideos);
76+
}
77+
channelSearch.trim()?.length ===0 ? fetchVideos(): searchChannelVideos();
78+
}, [channelSearch]);
7979

8080
return (
8181
<div className="w-full flex items-start flex-col p-4 mt-4 dark:text-white h-screen">

components/Channel/ChannelStore.jsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline";
2-
import React from "react";
2+
import React, { useEffect } from "react";
3+
import { useState } from "react";
34
import { NumericFormat } from "react-number-format";
45
import { useStateContext } from "../../context/StateContext";
6+
import { supabase } from "../../SupabaseClient";
57

68
const Item = ({ productImage, productPage, price, name, sponsor }) => {
79
return (
@@ -35,8 +37,20 @@ const Item = ({ productImage, productPage, price, name, sponsor }) => {
3537

3638
const ChannelStore = () => {
3739
const {
38-
activeChannel: { store },
40+
activeChannel: { uid },
3941
} = useStateContext();
42+
43+
const [store, setStore] = useState([]);
44+
45+
useEffect(() => {
46+
const myFunction = async () => {
47+
const {data} = await supabase.from('store').select().eq('channelRef', uid);
48+
setStore(data);
49+
console.log(data);
50+
console.log(uid);
51+
}
52+
myFunction();
53+
}, [])
4054
return (
4155
<div className="w-full flex flex-col items-start p-4 mt-10 dark:text-white h-screen">
4256
<p className="font-semibold text-lg mb-8">Featured</p>

components/Channel/ChannelVideos.jsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React, { useState, useEffect } from "react";
22
import Masonry from "react-masonry-css";
33
import { useChannelState } from "../../context/ChannelState";
44
import { useStateContext } from "../../context/StateContext";
5+
import {supabase} from '../../SupabaseClient';
56
import Video from "../Video";
67

78
const ChannelVideos = () => {
89
const {
9-
videos,
1010
setVideos,
1111
activeChannel: { channelName, uid },
1212
} = useStateContext();
13-
const { channelSearch, setChannelSearch } = useChannelState();
13+
const { channelSearch, setChannelSearch, fetchChannelVideos } = useChannelState();
1414
const [videoFilter, setVideoFilter] = useState("Recently Uploaded");
1515
const videoFilters = [
1616
{
@@ -31,20 +31,22 @@ const ChannelVideos = () => {
3131
};
3232

3333
const [channelVideos, setChannelVideos] = useState([]);
34+
const {fetchChannelDetails} = useChannelState();
3435

35-
const handlePopularVideos = () => {
36-
const channelVideos = videos.filter((video) => video?.channelRef === uid);
36+
const handlePopularVideos =async () => {
37+
const {data: videos} = await supabase.from('videos').select().eq('channelRef', uid);
3738
setChannelVideos(
38-
channelVideos.sort(
39+
videos.sort(
3940
(video1, video2) => parseFloat(video2.views) - parseFloat(video1.views)
4041
)
4142
);
43+
console.log( videos.sort(
44+
(video1, video2) => parseFloat(video2.views?.length) - parseFloat(video1.views?.length)
45+
))
4246
};
4347

44-
const handleRecentlyPosted = () => {
45-
const channelVideos = videos.filter(
46-
(video) => video?.channelName === channelName
47-
);
48+
const handleRecentlyPosted = async () => {
49+
const {data: channelVideos} = await supabase.from('videos').select().eq('channelRef', uid);
4850
channelVideos.length > 1 &&
4951
setChannelVideos(
5052
channelVideos.sort(
@@ -54,22 +56,26 @@ const ChannelVideos = () => {
5456
);
5557
};
5658

57-
const searchChannelVideos = () => {
58-
if (channelSearch?.trim() === "") return;
59-
const channelVideos = videos.filter((video) => video?.channelRef === uid);
60-
setChannelVideos(
61-
channelVideos.filter((video) =>
62-
video.title.toLowerCase().includes(channelSearch.toLowerCase())
63-
)
59+
const searchChannelVideos = async() => {
60+
if(channelSearch?.trim() === '') return;
61+
const myFunction = async () => {
62+
const channelVideos = await fetchChannelVideos(uid);
63+
await setChannelVideos(
64+
channelVideos?.filter((video) => video.title.toLowerCase().includes(channelSearch.toLowerCase()))
6465
);
66+
}
67+
myFunction();
6568
};
6669

67-
const fetchChannelVideos = () => {
68-
setChannelVideos(videos.filter((video) => video?.channelRef === uid));
69-
};
7070
useEffect(() => {
71-
channelSearch.trim() === "" ? fetchChannelVideos() : searchChannelVideos();
72-
}, [videos, channelSearch]);
71+
console.log(channelSearch, channelSearch?.trim()?.length)
72+
const fetchVideos = async () => {
73+
const channelVideos =await fetchChannelVideos(uid);
74+
console.log(channelVideos)
75+
await setChannelVideos(channelVideos);
76+
}
77+
channelSearch.trim()?.length ===0 ? fetchVideos(): searchChannelVideos();
78+
}, [channelSearch]);
7379

7480
return (
7581
<div className="w-full flex items-start flex-col p-4 mt-4 dark:text-white h-screen">

0 commit comments

Comments
 (0)