Skip to content

Commit 8c90d00

Browse files
Added Change Banner Image Feature βœ…πŸŒŸ
1 parent 2785469 commit 8c90d00

File tree

2 files changed

+60
-73
lines changed

2 files changed

+60
-73
lines changed

β€Žcomponents/ChannelHeader.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ const ChannelHeader = () => {
3535
setActiveChannel,
3636
activeChannel
3737
} = useStateContext();
38-
const { Subscribe, UnSubscribe, currentChannel,changeChanneImage } = useChannelState();
38+
const { Subscribe, UnSubscribe, currentChannel,changeChannelImage } = useChannelState();
3939
const [updatedSubscribers, setUpdatedSubscribers] = useState(
4040
channelInfo?.subscribers?.length
4141
);
4242
const [isSubscribed, setIsSubscribed] = useState(false);
4343
const scrollRef = useRef();
4444
const channelImageRef = useRef();
45+
const bannerImageRef = useRef();
4546

4647
useEffect(() => {
4748
const getData = async () => {
@@ -93,20 +94,28 @@ const ChannelHeader = () => {
9394
setChannelTab(tab);
9495
}, 1000);
9596
};
96-
const { channelSearch, setChannelSearch } = useChannelState();
97+
const { channelSearch, setChannelSearch, changeChannelBannerImage } = useChannelState();
9798
const inputRef = useRef();
9899
const [isInput, setIsInput] = useState(false);
99100
const [newChannelImage, setNewChannelImage] = useState(channelImage);
100-
console.log(channelImage, newChannelImage)
101+
const [newBannerImage, setNewBannerImage] = useState(channelBannerImage);
101102

102103
return (
103104
<div className="scrollbar overflow-x-hidden pl-10">
104-
<div className="w-full h-[35vh] relative">
105+
<div className="w-full h-[35vh] relative group">
106+
105107
<img
106-
src={channelBannerImage}
108+
src={newBannerImage}
107109
className="w-full h-full object-cover object-left-bottom"
108110
alt="channel banner"
109111
/>
112+
113+
{currentChannel?.uid === uid &&
114+
<div className='absolute right-20 top-20'>
115+
<CameraIcon onClick={() => bannerImageRef?.current?.click()} className='icon hidden z-[100000] group-hover:block absolute flex-1 p-4 w-16 h-16 bg-neutral-900/30 dark:bg-white/10 text-white'/>
116+
<input type='file' accept='image/*' className='w-0' ref={bannerImageRef} onChange={(e) => changeChannelBannerImage(e, setNewBannerImage)}/>
117+
</div>
118+
}
110119
<div className="absolute right-2 cursor-pointer bottom-2 gap-4 bg-white dark:bg-black/90 p-2 rounded-lg flex justify-between items-center">
111120
<div className="flex items-center gap-1">
112121
<img
@@ -147,7 +156,7 @@ const ChannelHeader = () => {
147156
{currentChannel?.uid === uid &&
148157
<>
149158
<CameraIcon onClick={() => channelImageRef?.current?.click()} className='icon hidden z-[100000] group-hover:block absolute flex-1 p-8 w-24 h-24 bg-neutral-900/30 dark:bg-white/10 text-white'/>
150-
<input type='file' accept='images/*' className='w-0' ref={channelImageRef} onChange={(e) => changeChanneImage(setNewChannelImage, e)}/>
159+
<input type='file' accept='image/*' className='w-0' ref={channelImageRef} onChange={(e) => changeChannelImage(e, setNewChannelImage)}/>
151160
</>
152161
}
153162
</div>

β€Žcontext/ChannelState.jsx

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useContext, createContext, useState } from "react";
22
import { async } from "regenerator-runtime";
3+
import { uid } from "uid";
34
import UIDGenerator from "uid-generator";
45
import { supabase } from "../SupabaseClient";
56
export const ChannelState = createContext();
@@ -11,53 +12,6 @@ export const ChannelStateProvider = ({ children }) => {
1112
const [commentOption, setCommentOption] = useState("");
1213
const [likedVideos, setLikedVideos] = useState([]);
1314

14-
// deffault channel
15-
/*
16-
{
17-
channelName: "Gaurav",
18-
channelImage: "https://avatars.githubusercontent.com/u/88154142?v=4",
19-
channelBannerImage:
20-
"https://cdn.pixabay.com/photo/2017/10/31/19/05/web-design-2906159__480.jpg",
21-
subscribers: 1301310301,
22-
uid: "a00c3e26-aa9b-11fa-afa1-0242ac120003",
23-
channelDisplayName: "Gaurav",
24-
socialLinks: [
25-
{
26-
name: "facebook",
27-
logo: "https://cdn-icons-png.flaticon.com/128/5968/5968764.png",
28-
url: "http://www.facebook.com",
29-
},
30-
{
31-
name: "instagram",
32-
logo: "https://cdn-icons-png.flaticon.com/128/174/174855.png",
33-
url: "http://www.instagram.com",
34-
},
35-
{
36-
name: "twitter",
37-
logo: "https://cdn-icons-png.flaticon.com/128/733/733579.png",
38-
url: "http://www.twitter.com",
39-
},
40-
],
41-
views: 23242323232323,
42-
joinedDate: new Date(),
43-
description: `This is my brand new Channel`,
44-
location: "United States",
45-
subscriptions: [
46-
"a20c3a26-aa9b-11wa-afa1-0442ac120009",
47-
"a00c3e26-aa9b-11ed-afa1-0242ac120002",
48-
],
49-
email: "gaurav@gmail.com",
50-
playlists: [
51-
{
52-
name: "MyPlaylist",
53-
videos: [],
54-
}, {
55-
name: "My Playlist 2 ",
56-
videos: ["449112141"],
57-
},
58-
],
59-
}*/
60-
6115
const [News, setNews] = useState([
6216
{
6317
image: "https://avatars.githubusercontent.com/u/88154142?v=4",
@@ -289,25 +243,48 @@ export const ChannelStateProvider = ({ children }) => {
289243
video.setAttribute("muted", "true");
290244
};
291245

292-
const changeChanneImage = async (setNewChannelImage,e) => {
293-
try {
294-
const avatarFile = e.target.files[0]
295-
const location = GetUid();
296-
const { data, error } = await supabase
297-
.storage
298-
.from('channel-images')
299-
.upload(location, avatarFile, {
300-
cacheControl: '3600',
301-
upsert: true
302-
})
303-
const {path} = data;
304-
const channelImage = `https://lumsrpmlumtfpbbafpug.supabase.co/storage/v1/object/public/channel-images/${path}`;
305-
setNewChannelImage(channelImage);
306-
setCurrentChanne({...currentChannel, channelImage: channelImage})
307-
await supabase.from('channels').update({channelImage: channelImage}).eq('uid', currentChannel?.uid)
308-
e.target.value= '';
309-
} catch{err => console.log(err)}
310-
}
246+
const changeChannelImage = async (e, setNewChannelImage) => {
247+
const avatarImage = e.target.files[0];
248+
const location = uid();
249+
const { data, error } = await supabase.storage
250+
.from("channel-images")
251+
.upload(location, avatarImage, {
252+
cacheControl: "3600",
253+
upsert: false,
254+
});
255+
const path = data?.path;
256+
const channelImage = `https://lumsrpmlumtfpbbafpug.supabase.co/storage/v1/object/public/channel-images/${path}`;
257+
setNewChannelImage(channelImage);
258+
await supabase
259+
.from("channels")
260+
.update({ channelImage: channelImage })
261+
.eq('uid', currentChannel?.uid)
262+
setCurrentChannel({ ...currentChannel, channelImage: channelImage });
263+
e.target.value = "";
264+
};
265+
266+
267+
268+
const changeChannelBannerImage = async (e, setNewBannerImage) => {
269+
console.log('uploading....')
270+
const avatarImage = e.target.files[0];
271+
const location = uid();
272+
const { data, error } = await supabase.storage
273+
.from("channel-banner-images")
274+
.upload(location, avatarImage, {
275+
cacheControl: "3600",
276+
upsert: false,
277+
});
278+
const path = data?.path;
279+
const bannerImage = `https://lumsrpmlumtfpbbafpug.supabase.co/storage/v1/object/public/channel-banner-images/${path}`;
280+
setNewBannerImage(bannerImage);
281+
await supabase
282+
.from("channels")
283+
.update({ channelBannerImage: bannerImage })
284+
.eq('uid', currentChannel?.uid)
285+
setCurrentChannel({ ...currentChannel, channelBannerImage: bannerImage });
286+
e.target.value = "";
287+
};
311288

312289
const channelSearches = [
313290
"hello world",
@@ -613,7 +590,8 @@ export const ChannelStateProvider = ({ children }) => {
613590
setThumbnailDialog,
614591
handleLogin,
615592
getVideoThumbnail,
616-
changeChanneImage
593+
changeChannelImage,
594+
changeChannelBannerImage
617595
}}
618596
>
619597
{children}

0 commit comments

Comments
Β (0)