From 2fc78df59b07a468a93dd793218c5f518b8f36c3 Mon Sep 17 00:00:00 2001 From: erik-sn Date: Sun, 20 Sep 2020 11:51:41 -0400 Subject: [PATCH] working media adding and removal --- phone/package.json | 3 +- .../apps/twitter/components/AddTweetModal.js | 56 ++++++++++++++++--- .../apps/twitter/components/MediaDisplay.js | 56 +++++++++++++++++-- phone/yarn.lock | 5 ++ resources/config.lua | 3 +- 5 files changed, 107 insertions(+), 16 deletions(-) diff --git a/phone/package.json b/phone/package.json index 0c5d0ed..dbb4c28 100644 --- a/phone/package.json +++ b/phone/package.json @@ -20,7 +20,8 @@ "react-router-dom": "^5.2.0", "react-scripts": "3.4.3", "recoil": "^0.0.10", - "styled-components": "^5.1.1" + "styled-components": "^5.1.1", + "uuid": "^8.3.0" }, "devDependencies": { "@babel/cli": "^7.4.4", diff --git a/phone/src/apps/twitter/components/AddTweetModal.js b/phone/src/apps/twitter/components/AddTweetModal.js index d146663..8c7c31a 100644 --- a/phone/src/apps/twitter/components/AddTweetModal.js +++ b/phone/src/apps/twitter/components/AddTweetModal.js @@ -1,5 +1,6 @@ import React, { useState, useCallback } from "react"; import { useTranslation } from "react-i18next"; +import { v4 as uuidv4 } from "uuid"; import { makeStyles } from "@material-ui/core/styles"; import { Button, Paper } from "@material-ui/core"; import InsertPhotoIcon from "@material-ui/icons/InsertPhoto"; @@ -42,6 +43,7 @@ const useStyles = makeStyles((theme) => ({ display: "flex", flexFlow: "row nowrap", justifyContent: "flex-start", + paddingLeft: "5px", // re-align left buttons after overriding material-ui spacing }, rightButtons: { display: "flex", @@ -52,6 +54,7 @@ const useStyles = makeStyles((theme) => ({ }, button: { background: "transparent", + minWidth: "45px", // override default material-ui spacing between buttons }, buttonHidden: { display: "none", @@ -72,9 +75,42 @@ const useStyles = makeStyles((theme) => ({ }, })); +const TEMP_MEDIA = [ + { + id: uuidv4(), + mediaType: "image", + mediaLink: + "http://www.freedigitalphotos.net/images/thumbs/city-10011901.jpg", + }, + { + id: uuidv4(), + mediaType: "image", + mediaLink: + "https://images.unsplash.com/photo-1541963463532-d68292c34b19?ixlib=rb-1.2.1&w=1000&q=80", + }, + { + id: uuidv4(), + mediaType: "image", + mediaLink: + "https://static01.nyt.com/images/2020/07/17/business/00virus-cities1/00virus-cities1-mediumSquareAt3X.jpg", + }, + { + id: uuidv4(), + mediaType: "image", + mediaLink: + "https://image.cnbcfm.com/api/v1/image/105066394-GettyImages-498350103_1.jpg?v=1591858410", + }, + { + id: uuidv4(), + mediaType: "image", + mediaLink: + "https://www.cbre.us/-/media/cbre/countryunitedkingdom/images/our-cities/231_what_makes_a_successful_city_image2_1024x576.jpg", + }, +]; + export const AddTweetModal = ({ visible, handleClose }) => { const [text, setText] = useState(""); - const [media, setMedia] = useState(null); + const [media, setMedia] = useState(TEMP_MEDIA); const [mediaType, setMediaType] = useState(null); const [mediaLink, setMediaLink] = useState(null); const classes = useStyles(); @@ -84,24 +120,30 @@ export const AddTweetModal = ({ visible, handleClose }) => { : classes.displayNone; const submitTweet = () => { - const data = { text, ...media }; + const data = { text, media }; console.log("submitting tweet"); - console.log(data); }; const addMedia = useCallback(() => { - setMedia({ mediaType, mediaLink }); + // we add an ID here so that we can have a distinct identifier on the + // client before it is added to the database. This ID is NOT what used + // or contained in the database. + setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }])); setMediaType(null); setMediaLink(null); }, [mediaType, mediaLink]); - const closeMedia = useCallback(() => { + const _removeMedia = (id) => setMedia(media.filter((m) => m.id !== id)); + const removeMedia = useCallback(_removeMedia, [media]); + + const closeMediaPrompt = useCallback(() => { setMediaType(null); setMediaLink(null); }, []); + console.log(media); return (
@@ -115,7 +157,7 @@ export const AddTweetModal = ({ visible, handleClose }) => { handleChange={(e) => setMediaLink(e.target.value)} /> )} - {media && } +
diff --git a/phone/src/apps/twitter/components/MediaDisplay.js b/phone/src/apps/twitter/components/MediaDisplay.js index 6b9e112..fea449f 100644 --- a/phone/src/apps/twitter/components/MediaDisplay.js +++ b/phone/src/apps/twitter/components/MediaDisplay.js @@ -1,32 +1,76 @@ -import React from "react"; +import React, { useCallback } from "react"; +import { IconButton } from "@material-ui/core"; import { makeStyles } from "@material-ui/core/styles"; +import CloseIcon from "@material-ui/icons/Close"; import types from "../types"; const useStyles = makeStyles((theme) => ({ root: { margin: "2px 15px", }, + mediaImage: { + // catch the close button here so it doesn't go all the way up to the modal + position: "relative", + display: "inline-block", + }, + imgButton: { + position: "absolute", // show the close button in the top right of the image + top: "2px", + right: "5px", + backgroundColor: "#000", + opacity: 0.7, + "&:hover": { + // fixes a bug where when the user closes one image material-ui puts + // the hover status on the previous image + backgroundColor: "#000", + }, + }, img: { - maxHeight: "125px", + maxHeight: "125px", // constrain images so we don't overwhelm the UI + maxWidth: "500px", + padding: "0px 3px 3px 0px", }, })); -export const Media = ({ mediaType, mediaLink }) => { +export const MediaImage = ({ mediaLink, handleClick }) => { const classes = useStyles(); + return ( +
+ + + + +
+ ); +}; + +export const Media = ({ id, mediaType, mediaLink, handleClose }) => { switch (mediaType) { case types.IMAGE: case types.GIF: - return ; + return ; default: return null; } }; -export const MediaDisplay = (props) => { +export const MediaDisplay = ({ media, removeMedia }) => { const classes = useStyles(); + if (media.length === 0) return null; + return (
- + {media.map((mediaItem) => ( + removeMedia(mediaItem.id)} + {...mediaItem} + /> + ))}
); }; diff --git a/phone/yarn.lock b/phone/yarn.lock index b127ba9..19465f2 100644 --- a/phone/yarn.lock +++ b/phone/yarn.lock @@ -11415,6 +11415,11 @@ uuid@^3.3.2, uuid@^3.4.0: resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== +uuid@^8.3.0: + version "8.3.0" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea" + integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ== + v8-compile-cache@^2.0.3, v8-compile-cache@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.1.tgz#54bc3cdd43317bca91e35dcaf305b1a7237de745" diff --git a/resources/config.lua b/resources/config.lua index 923f3ad..218ddb5 100644 --- a/resources/config.lua +++ b/resources/config.lua @@ -9,5 +9,4 @@ Config.PhoneAsItem = false Config.twitter = {} Config.twitter.characterLimit = 240 Config.twitter.renderImages = true -- whether or not links to images are automatically rendered on the UI -Config.twitter.renderGifs = true -- whether or not links to gifs are automatically rendered on the UI -Config.twitter.renderVideos = true -- whether or not links to videos are automatically rendered on the UI +Config.twitter.maxImages = 3 -- max amount of images that can be attached to a single tweet