From 624953a22156727af0d0db8b79f93721dde3bab2 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 29 Apr 2022 15:34:49 -0700 Subject: [PATCH] adding and removing to asset carousel --- net/web/src/Carousel/Carousel.jsx | 11 +++-- net/web/src/Carousel/Carousel.styled.js | 17 +++++++- .../User/Conversation/AddTopic/AddTopic.jsx | 43 ++++++++++++++++--- .../Conversation/AddTopic/useAddTopic.hook.js | 21 ++++++--- 4 files changed, 74 insertions(+), 18 deletions(-) diff --git a/net/web/src/Carousel/Carousel.jsx b/net/web/src/Carousel/Carousel.jsx index 37265118..0d991a27 100644 --- a/net/web/src/Carousel/Carousel.jsx +++ b/net/web/src/Carousel/Carousel.jsx @@ -1,12 +1,12 @@ import React, { useState, useEffect, useRef } from 'react'; import { CarouselWrapper } from './Carousel.styled'; -import { RightOutlined, LeftOutlined } from '@ant-design/icons'; +import { RightOutlined, LeftOutlined, CloseOutlined } from '@ant-design/icons'; import ReactResizeDetector from 'react-resize-detector'; import login from '../login.png'; import test from '../test.png'; -export function Carousel({ items, itemRenderer }) { +export function Carousel({ items, itemRenderer, itemRemove }) { const [slots, setSlots] = useState([]); const [itemIndex, setItemIndex] = useState(0); const [scrollLeft, setScrollLeft] = useState('hidden'); @@ -74,7 +74,12 @@ export function Carousel({ items, itemRenderer }) { {({ width, height }) => { itemWidth.current.set(i, width); - return
{ itemRenderer(items[i]) }
+ return ( +
+
{ itemRenderer(items[i]) }
+
itemRemove(i)}>
+
+ ); }}
)); diff --git a/net/web/src/Carousel/Carousel.styled.js b/net/web/src/Carousel/Carousel.styled.js index e46b53f0..ff418cb6 100644 --- a/net/web/src/Carousel/Carousel.styled.js +++ b/net/web/src/Carousel/Carousel.styled.js @@ -47,8 +47,23 @@ export const CarouselWrapper = styled.div` } .item { - height: 128px; margin-right: 32px; + position: relative; + } + + .delitem { + position: absolute; + top: 0; + right: 0; + background-color: white; + border-bottom-left-radius: 2px; + padding-left: 2px; + padding-right: 2px; + cursor: pointer; + } + + .asset { + height: 128px; } .space { diff --git a/net/web/src/User/Conversation/AddTopic/AddTopic.jsx b/net/web/src/User/Conversation/AddTopic/AddTopic.jsx index 47ee7e37..7de8928f 100644 --- a/net/web/src/User/Conversation/AddTopic/AddTopic.jsx +++ b/net/web/src/User/Conversation/AddTopic/AddTopic.jsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useRef } from 'react'; import { Button, Dropdown, Input, Tooltip, Menu } from 'antd'; import { AddTopicWrapper, BusySpin } from './AddTopic.styled'; import { Carousel } from '../../../Carousel/Carousel'; @@ -13,17 +13,31 @@ export function AddTopic() { let [ items, setItems] = useState([]); const { state, actions } = useAddTopic(); + const attachImage = useRef(null); + const attachAudio = useRef(null); + const attachVideo = useRef(null); + + const onSelect = (e, action) => { + var reader = new FileReader(); + reader.onload = () => { + action(reader.result); + } + reader.readAsDataURL(e.target.files[0]); + } const menu = ( -
setItems([test, login, login, test, test, login])}>Attach Image
+ onSelect(e, actions.addImage)} style={{display: 'none'}}/> +
attachImage.current.click()}>Attach Image
-
setItems([test, login, login, test, test, login])}>Attach Video
+ onSelect(e, actions.addAudio)} style={{display: 'none'}}/> +
attachAudio.current.click()}>Attach Audio
-
setItems([test, login, login, test, test, login])}>Attach Audio
+ onSelect(e, actions.addVideo)} style={{display: 'none'}}/> +
attachVideo.current.click()}>Attach Video
); @@ -41,12 +55,27 @@ export function AddTopic() { } } + const renderItem = (item) => { + if (item.image) { + return + } + if (item.audio) { + return + } + if (item.video) { + return + } + return <> + } + + const removeItem = (index) => { + actions.removeAsset(index); + } + return (
- { - return - }} /> +
actions.setMessageText(e.target.value)} value={state.messageText} /> diff --git a/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js b/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js index 0a97f86d..d98f434e 100644 --- a/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js +++ b/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js @@ -26,17 +26,24 @@ export function useAddTopic() { const addAsset = (value) => { setState((s) => { - s.assets.push(value); - return { ...s }; + let assets = [...s.assets, value]; + return { ...s, assets }; + }); + } + + const removeAsset = (index) => { + setState((s) => { + s.assets.splice(index, 1); + let assets = [...s.assets]; + return { ...s, assets }; }); } const actions = { - addImage: (image) => { addAsset(image) }, - addVideo: (video) => { addAsset(video) }, - addAudio: (audio) => { addAsset(audio) }, - addIframe: (iframe) => { addAsset(iframe) }, - removeAsset: (idx) => {}, + addImage: (image) => { addAsset({ image }) }, + addVideo: (video) => { addAsset({ video }) }, + addAudio: (audio) => { addAsset({ audio }) }, + removeAsset: (idx) => { removeAsset(idx) }, setMessageText: (value) => { updateState({ messageText: value }); },