Skip to content

Commit

Permalink
Hookup channel icon upload / preview. Doesn't actually upload file yet
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Jan 30, 2024
1 parent 6ddbba6 commit 7f40883
Showing 1 changed file with 73 additions and 18 deletions.
91 changes: 73 additions & 18 deletions web2/src/components/channel_config/ChannelPropertiesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@mui/material';
import { usePrevious } from '@uidotdev/usehooks';
import { isEmpty } from 'lodash-es';
import { useEffect } from 'react';
import { ChangeEvent, useEffect, useRef, useState } from 'react';
import useDebouncedState from '../../hooks/useDebouncedState.ts';
import { updateCurrentChannel } from '../../store/channelEditor/actions.ts';
import useStore from '../../store/index.ts';
Expand All @@ -29,6 +29,7 @@ const VisuallyHiddenInput = styled('input')({
});

export default function ChannelPropertiesEditor() {
const imgRef = useRef<HTMLImageElement | null>(null);
const channel = useStore((s) => s.channelEditor.currentEntity);
const prevChannel = usePrevious(channel);

Expand All @@ -40,12 +41,31 @@ export default function ChannelPropertiesEditor() {
const [channelNumber, debounceChannelNumber, setChannelNumber] =
useDebouncedState(channel?.number ?? 1, 250);

const [channelIcon, setChannelIcon] = useState(channel?.icon.path);

const [, channelIconPreview, setChannelIconPreview] = useDebouncedState(
channel?.icon.path,
250,
);

useEffect(() => {
if (!prevChannel && channel) {
setChannelName(channel.name);
setChannelNumber(channel.number);
const url = isEmpty(channel.icon.path)
? `/dizquetv.png`
: channel.icon.path;
setChannelIcon(url);
setChannelIconPreview(url);
}
}, [prevChannel, channel, setChannelName, setChannelNumber]);
}, [
prevChannel,
channel,
setChannelName,
setChannelNumber,
setChannelIcon,
setChannelIconPreview,
]);

useEffect(() => {
if (
Expand All @@ -63,6 +83,29 @@ export default function ChannelPropertiesEditor() {
}
}, [channel, debounceChannelNumber]);

const onloadstart = () => {
console.log('on load start');
};

useEffect(() => {
if (imgRef.current) {
const current = imgRef.current;
current.onloadstart = onloadstart;
return () => {
current.removeEventListener('onloadstart', onloadstart);
};
}
}, [imgRef]);

const handleFileUpload = (e: ChangeEvent<HTMLInputElement>) => {
console.log(e.target.files);
if (e.target.files && e.target.files.length > 0) {
const file = e.target.files[0];
setChannelIcon(`http://localhost:8000/images/uploads/${file.name}`); // Placeholder
setChannelIconPreview(URL.createObjectURL(file));
}
};

return (
channel && (
<Box>
Expand All @@ -85,23 +128,35 @@ export default function ChannelPropertiesEditor() {
value={channel.groupTitle}
margin="normal"
/>
<FormControl fullWidth margin="normal">
<InputLabel>Thumbnail URL</InputLabel>
<OutlinedInput
label="Thumbnail URL"
value={
isEmpty(channel.icon.path) ? `/dizquetv.png` : channel.icon.path
}
endAdornment={
<InputAdornment position="end">
<IconButton component="label">
<CloudUploadIcon />
<VisuallyHiddenInput type="file" />
</IconButton>
</InputAdornment>
}
<Box sx={{ display: 'flex', alignItems: 'end' }}>
<Box
component="img"
width="10%"
src={channelIconPreview}
sx={{ mr: 1 }}
ref={imgRef}
/>
</FormControl>
<FormControl fullWidth margin="normal">
<InputLabel>Thumbnail URL</InputLabel>
<OutlinedInput
label="Thumbnail URL"
value={channelIcon}
type="url"
onChange={(e) => setChannelIcon(e.target.value)}
endAdornment={
<InputAdornment position="end">
<IconButton component="label">
<CloudUploadIcon />
<VisuallyHiddenInput
onChange={(e) => handleFileUpload(e)}
type="file"
/>
</IconButton>
</InputAdornment>
}
/>
</FormControl>
</Box>
</Box>
)
);
Expand Down

0 comments on commit 7f40883

Please sign in to comment.