From ba0f12293813366db0351d7b4d7908172ac32d2f Mon Sep 17 00:00:00 2001 From: erik-sn Date: Thu, 24 Sep 2020 23:25:35 -0400 Subject: [PATCH] Working database interactions and NUI callbacks --- phone/src/Phone.js | 2 + .../apps/twitter/components/AddTweetModal.js | 159 ++++-------------- .../apps/twitter/components/AddTweetStatus.js | 23 +++ .../apps/twitter/components/ControlButtons.js | 59 +++++++ .../apps/twitter/components/IconButtons.js | 34 ++++ .../apps/twitter/components/MediaDisplay.js | 2 +- phone/src/apps/twitter/components/Tweet.js | 50 ++++++ .../src/apps/twitter/components/TweetList.js | 21 +++ .../src/apps/twitter/components/TwitterApp.js | 4 +- phone/src/apps/twitter/hooks/state.js | 16 ++ .../src/apps/twitter/hooks/useTweetStatus.js | 9 + phone/src/apps/twitter/hooks/useTweets.js | 7 + .../apps/twitter/hooks/useTwitterService.js | 23 +++ phone/src/apps/twitter/types.js | 2 + phone/src/apps/twitter/utils/media.js | 9 + phone/src/locale/en.json | 3 +- resources/client/cl_main.lua | 1 + resources/client/cl_twitter.lua | 43 +++++ resources/server/sv_twitter.lua | 32 ++++ 19 files changed, 372 insertions(+), 127 deletions(-) create mode 100644 phone/src/apps/twitter/components/AddTweetStatus.js create mode 100644 phone/src/apps/twitter/components/ControlButtons.js create mode 100644 phone/src/apps/twitter/components/IconButtons.js create mode 100644 phone/src/apps/twitter/components/Tweet.js create mode 100644 phone/src/apps/twitter/components/TweetList.js create mode 100644 phone/src/apps/twitter/hooks/state.js create mode 100644 phone/src/apps/twitter/hooks/useTweetStatus.js create mode 100644 phone/src/apps/twitter/hooks/useTweets.js create mode 100644 phone/src/apps/twitter/hooks/useTwitterService.js create mode 100644 phone/src/apps/twitter/utils/media.js create mode 100644 resources/client/cl_twitter.lua create mode 100644 resources/server/sv_twitter.lua diff --git a/phone/src/Phone.js b/phone/src/Phone.js index 335036e..0b77e0c 100644 --- a/phone/src/Phone.js +++ b/phone/src/Phone.js @@ -17,6 +17,7 @@ import { usePhoneService } from "./os/phone/hooks/usePhoneService"; import { useApps } from "./os/apps/hooks/useApps"; import { useContactsService } from "./apps/contacts/hooks/useContactsService"; +import { useTwitterService } from "./apps/twitter/hooks/useTwitterService"; // These events are just for testing. Comment it out before building. //setTimeout(() => { @@ -73,6 +74,7 @@ function Phone() { const { allApps } = useApps(); useSimcardService(); useContactsService(); + useTwitterService(); useInitKeyboard(); if (visibility === false) { diff --git a/phone/src/apps/twitter/components/AddTweetModal.js b/phone/src/apps/twitter/components/AddTweetModal.js index 49a6b48..59dd419 100644 --- a/phone/src/apps/twitter/components/AddTweetModal.js +++ b/phone/src/apps/twitter/components/AddTweetModal.js @@ -1,19 +1,21 @@ import React, { useState, useCallback, useEffect } from "react"; import { useTranslation } from "react-i18next"; -import Picker from "emoji-picker-react"; -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"; import CloseIcon from "@material-ui/icons/Close"; -import EmojiIcon from "@material-ui/icons/SentimentSatisfied"; +import { v4 as uuidv4 } from "uuid"; +import Nui from "../../../os/nui-events/utils/Nui"; import types from "../types"; +import { withValidMedia } from "../utils/media"; import "./emoji.css"; import EmojiSelect from "./EmojiSelect"; import MediaDisplay from "./MediaDisplay"; import MediaPrompt from "./MediaPrompt"; import TweetText from "./TweetText"; +import ControlButtons from "./ControlButtons"; +import IconButtons from "./IconButtons"; +import AddTweetStatus from "./AddTweetStatus"; const useStyles = makeStyles((theme) => ({ button: { @@ -41,29 +43,6 @@ const useStyles = makeStyles((theme) => ({ justifyContent: "space-between", flex: "1 0 45px", }, - leftButtons: { - display: "flex", - flexFlow: "row nowrap", - justifyContent: "flex-start", - paddingLeft: "5px", // re-align left buttons after overriding material-ui spacing - }, - rightButtons: { - display: "flex", - flexFlow: "row nowrap", - justifyContent: "flex-end", - alignItems: "justify-content", - marginRight: "15px", - }, - button: { - background: "transparent", - minWidth: "45px", // override default material-ui spacing between buttons - }, - buttonHidden: { - display: "none", - }, - closeMedia: { - marginLeft: "8px", - }, displayBlock: { /*Sets modal to center*/ display: "flex", @@ -82,44 +61,11 @@ 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 [showEmoji, setShowEmoji] = useState(false); const [text, setText] = useState(""); - const [media, setMedia] = useState(TEMP_MEDIA); + const [media, setMedia] = useState([]); const [mediaType, setMediaType] = useState(null); const [mediaLink, setMediaLink] = useState(null); const classes = useStyles(); @@ -157,57 +103,41 @@ export const AddTweetModal = ({ visible, handleClose }) => { : classes.displayNone; const submitTweet = () => { - const data = { text, media }; - - console.log("submitting tweet"); - console.log(data); + const data = { message: text, media, realUser: "testUser" }; + Nui.send("phone:createTweet", data); }; - const handleTextChange = useCallback((e) => setText(e.target.value), []); + const handleTextChange = (e) => setText(e.target.value); - const addMedia = useCallback(() => { + const addMedia = () => { setMediaType(null); setMediaLink(null); - // only add the media if it's a valid URL - const image = new Image(); - image.onload = () => { - setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }])); - }; - image.onerror = () => { - console.warn("Invalid image: ", mediaLink); - }; - image.src = mediaLink; - }, [mediaType, mediaLink]); - const _removeMedia = (id) => setMedia(media.filter((m) => m.id !== id)); - const removeMedia = useCallback(_removeMedia, [media]); - const _handleMediaChange = (e) => setMediaLink(e.target.value); - const handleMediaChange = useCallback(_handleMediaChange, []); - const setMediaTypeImage = useCallback(() => { + withValidMedia(mediaLink, () => + setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }])) + ); + }; + const removeMedia = (id) => setMedia(media.filter((m) => m.id !== id)); + const handleMediaChange = (e) => setMediaLink(e.target.value); + const setMediaTypeImage = () => { setShowEmoji(false); // clear the emoji so we can switch between emoji/media setMediaType(types.IMAGE); - }); - const closeMediaPrompt = useCallback(() => { + }; + const closeMediaPrompt = () => { setMediaType(null); setMediaLink(null); - }, []); + }; - const toggleShowEmoji = useCallback(() => { + const toggleShowEmoji = () => { // clear the media items so we can seemlessly toggle between emoji/media setMediaType(null); setMediaLink(null); setShowEmoji(!showEmoji); - }); - const _handleEmojiClick = (emojiObject, e) => { + }; + const handleEmojiClick = (emojiObject, e) => { setText(text.concat(emojiObject.native)); }; - const handleEmojiClick = useCallback(_handleEmojiClick, [text]); - const mediaPromptVisible = mediaType && !showEmoji; - const primaryButtonText = mediaPromptVisible - ? t("APPS_TWITTER_SUBMIT_MEDIA") - : t("APPS_TWITTER_TWEET"); - const showCloseButton = mediaType || showEmoji; return (
@@ -216,6 +146,7 @@ export const AddTweetModal = ({ visible, handleClose }) => { + { removeMedia={removeMedia} />
-
- - -
-
- - {showCloseButton && ( - - )} -
+ +
diff --git a/phone/src/apps/twitter/components/AddTweetStatus.js b/phone/src/apps/twitter/components/AddTweetStatus.js new file mode 100644 index 0000000..501e24d --- /dev/null +++ b/phone/src/apps/twitter/components/AddTweetStatus.js @@ -0,0 +1,23 @@ +import React, { useEffect } from "react"; +import { useRecoilState } from "recoil"; +import { useTranslation } from "react-i18next"; + +import { useTweetStatus } from "../hooks/useTweetStatus"; + +const HIDE_SUCCESS_TIMEOUT = 5000; + +function AddTweetStatus() { + const { isSuccessful } = useTweetStatus(); + const { t } = useTranslation(); + + console.log(isSuccessful); + + // on the "true" case the tweet was created and the user should + // see their tweet at the top of the list, so there's no need + // to directly prompt them that it was successful. + if (isSuccessful === null || isSuccessful === true) return null; + + return
{t("APPS_TWITTER_CREATE_FAILED")}
; +} + +export default AddTweetStatus; diff --git a/phone/src/apps/twitter/components/ControlButtons.js b/phone/src/apps/twitter/components/ControlButtons.js new file mode 100644 index 0000000..9255299 --- /dev/null +++ b/phone/src/apps/twitter/components/ControlButtons.js @@ -0,0 +1,59 @@ +import React from "react"; +import { useTranslation } from "react-i18next"; +import { makeStyles } from "@material-ui/core/styles"; +import { Button } from "@material-ui/core"; + +import { useTweetStatus } from "../hooks/useTweetStatus"; + +const useStyles = makeStyles((theme) => ({ + buttons: { + display: "flex", + flexFlow: "row nowrap", + justifyContent: "flex-end", + alignItems: "justify-content", + marginRight: "15px", + }, + closeMedia: { + marginLeft: "8px", + }, +})); + +export const ControlButtons = ({ + mediaType, + showEmoji, + onCloseClick, + onPrimaryClick, +}) => { + const { isLoading } = useTweetStatus(); + + const classes = useStyles(); + const { t } = useTranslation(); + + const mediaPromptVisible = mediaType && !showEmoji; + const primaryButtonText = mediaPromptVisible + ? t("APPS_TWITTER_SUBMIT_MEDIA") + : t("APPS_TWITTER_TWEET"); + const showCloseButton = mediaType || showEmoji; + + if (isLoading) return

!!!

; + + return ( +
+ + {showCloseButton && ( + + )} +
+ ); +}; + +export default ControlButtons; diff --git a/phone/src/apps/twitter/components/IconButtons.js b/phone/src/apps/twitter/components/IconButtons.js new file mode 100644 index 0000000..90d1094 --- /dev/null +++ b/phone/src/apps/twitter/components/IconButtons.js @@ -0,0 +1,34 @@ +import React from "react"; +import { makeStyles } from "@material-ui/core/styles"; +import { Button } from "@material-ui/core"; +import InsertPhotoIcon from "@material-ui/icons/InsertPhoto"; +import EmojiIcon from "@material-ui/icons/SentimentSatisfied"; + +const useStyles = makeStyles((theme) => ({ + buttons: { + display: "flex", + flexFlow: "row nowrap", + justifyContent: "flex-start", + paddingLeft: "5px", // re-align left buttons after overriding material-ui spacing + }, + button: { + background: "transparent", + minWidth: "45px", // override default material-ui spacing between buttons + }, +})); + +export const IconButtons = ({ onMediaClick, onEmojiClick }) => { + const classes = useStyles(); + return ( +
+ + +
+ ); +}; + +export default IconButtons; diff --git a/phone/src/apps/twitter/components/MediaDisplay.js b/phone/src/apps/twitter/components/MediaDisplay.js index ae6c21d..f0a46f6 100644 --- a/phone/src/apps/twitter/components/MediaDisplay.js +++ b/phone/src/apps/twitter/components/MediaDisplay.js @@ -29,7 +29,7 @@ export const MediaDisplay = ({ visible, media, removeMedia }) => {
{media.map((mediaItem) => ( removeMedia(mediaItem.id)} {...mediaItem} /> diff --git a/phone/src/apps/twitter/components/Tweet.js b/phone/src/apps/twitter/components/Tweet.js new file mode 100644 index 0000000..184a2aa --- /dev/null +++ b/phone/src/apps/twitter/components/Tweet.js @@ -0,0 +1,50 @@ +import React from "react"; +import { makeStyles } from "@material-ui/core/styles"; +import ListItemText from "@material-ui/core/ListItemText"; +import { Button } from "@material-ui/core"; +import FavoriteIcon from "@material-ui/icons/Favorite"; +import FavoriteBorderIcon from "@material-ui/icons/FavoriteBorder"; +import ReplyIcon from "@material-ui/icons/Reply"; +import QuoteIcon from "@material-ui/icons/FormatQuote"; + +import { ListItem } from "../../../ui/components/ListItem"; + +const useStyles = makeStyles((theme) => ({ + root: { + overflowX: "hidden", + display: "flex", + flexFlow: "column nowrap", + alignItems: "flex-start", + width: "100%", + }, + text: { + width: "100%", + }, + buttonContainer: { + display: "flex", + flexFlow: "row nowrap", + justifyContent: "flex-start", + }, +})); + +export const Tweet = ({ message, media }) => { + const classes = useStyles(); + return ( + + +
+ + + +
+
+ ); +}; + +export default Tweet; diff --git a/phone/src/apps/twitter/components/TweetList.js b/phone/src/apps/twitter/components/TweetList.js new file mode 100644 index 0000000..709e196 --- /dev/null +++ b/phone/src/apps/twitter/components/TweetList.js @@ -0,0 +1,21 @@ +import React from "react"; + +import { List } from "../../../ui/components/List"; +import { useTweets } from "../hooks/useTweets"; +import Tweet from "./Tweet"; + +export function TweetList() { + const { tweets } = useTweets(); + console.log(tweets); + return ( + + {tweets + .filter((t) => t.visible) + .map((tweet) => ( + + ))} + + ); +} + +export default TweetList; diff --git a/phone/src/apps/twitter/components/TwitterApp.js b/phone/src/apps/twitter/components/TwitterApp.js index 7d1f54f..065af41 100644 --- a/phone/src/apps/twitter/components/TwitterApp.js +++ b/phone/src/apps/twitter/components/TwitterApp.js @@ -8,6 +8,7 @@ import { AppContent } from "../../../ui/components/AppContent"; import { useTranslation } from "react-i18next"; import { useApp } from "../../../os/apps/hooks/useApps"; +import TweetList from "./TweetList"; import AddTweetModal from "./AddTweetModal"; const useStyles = makeStyles((theme) => ({ @@ -26,7 +27,7 @@ const useStyles = makeStyles((theme) => ({ })); export const TwitterApp = () => { - const [modalVisible, setModalVisible] = useState(true); + const [modalVisible, setModalVisible] = useState(false); const { t } = useTranslation(); const classes = useStyles(); const twitter = useApp("TWITTER"); @@ -46,6 +47,7 @@ export const TwitterApp = () => { + ); diff --git a/phone/src/apps/twitter/hooks/state.js b/phone/src/apps/twitter/hooks/state.js new file mode 100644 index 0000000..e660759 --- /dev/null +++ b/phone/src/apps/twitter/hooks/state.js @@ -0,0 +1,16 @@ +import { atom } from "recoil"; + +export const twitterState = { + tweets: atom({ + key: "tweets", + default: [], + }), + createTweetLoading: atom({ + key: "createTweetLoading", + default: false, + }), + createTweetSuccess: atom({ + key: "createTweetSuccess", + default: null, + }), +}; diff --git a/phone/src/apps/twitter/hooks/useTweetStatus.js b/phone/src/apps/twitter/hooks/useTweetStatus.js new file mode 100644 index 0000000..323928a --- /dev/null +++ b/phone/src/apps/twitter/hooks/useTweetStatus.js @@ -0,0 +1,9 @@ +import { useRecoilValue } from "recoil"; + +import { twitterState } from "./state"; + +export const useTweetStatus = () => { + const isLoading = useRecoilValue(twitterState.createTweetLoading); + const isSuccessful = useRecoilValue(twitterState.createTweetSuccess); + return { isLoading, isSuccessful }; +}; diff --git a/phone/src/apps/twitter/hooks/useTweets.js b/phone/src/apps/twitter/hooks/useTweets.js new file mode 100644 index 0000000..7b24c1c --- /dev/null +++ b/phone/src/apps/twitter/hooks/useTweets.js @@ -0,0 +1,7 @@ +import { useRecoilValue, useSetRecoilState } from "recoil"; +import { twitterState } from "./state"; + +export const useTweets = () => { + const tweets = useRecoilValue(twitterState.tweets); + return { tweets }; +}; diff --git a/phone/src/apps/twitter/hooks/useTwitterService.js b/phone/src/apps/twitter/hooks/useTwitterService.js new file mode 100644 index 0000000..b5b847a --- /dev/null +++ b/phone/src/apps/twitter/hooks/useTwitterService.js @@ -0,0 +1,23 @@ +import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent"; +import { useSetRecoilState } from "recoil"; + +import types from "../types"; +import { twitterState } from "./state"; +import { useTweets } from "./useTweets"; + +export const useTwitterService = () => { + const setTweets = useSetRecoilState(twitterState.tweets); + const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading); + const setCreateSuccess = useSetRecoilState(twitterState.createTweetSuccess); + + const _setCreateSuccess = (isSuccessful) => { + setCreateSuccess(isSuccessful === 1); // numeric 1 is returned by MySQL on successful query + setCreateLoading(false); // on any result we should set loading to false + }; + + useNuiEvent(types.APP_TWITTER, "fetchTweets", setTweets); + useNuiEvent(types.APP_TWITTER, "createTweetLoading", setCreateLoading); + useNuiEvent(types.APP_TWITTER, "createTweetResult", _setCreateSuccess); + + return useTweets(); +}; diff --git a/phone/src/apps/twitter/types.js b/phone/src/apps/twitter/types.js index 77963c2..666ff85 100644 --- a/phone/src/apps/twitter/types.js +++ b/phone/src/apps/twitter/types.js @@ -2,4 +2,6 @@ export default { IMAGE: "image", GIF: "gif", VIDEO: "video", + + APP_TWITTER: "TWITTER", }; diff --git a/phone/src/apps/twitter/utils/media.js b/phone/src/apps/twitter/utils/media.js new file mode 100644 index 0000000..3ea36be --- /dev/null +++ b/phone/src/apps/twitter/utils/media.js @@ -0,0 +1,9 @@ +export function withValidMedia(link, onSuccess) { + // only add the media if it's a valid URL + const image = new Image(); + image.onload = onSuccess; + image.onerror = () => { + console.warn(`Invalid image submitted: ${link}`); + }; + image.src = link; +} diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 52629b1..f52ab85 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -18,6 +18,7 @@ "APPS_TWITTER_SUBMIT_MEDIA": "Add Media", "APPS_TWITTER_CLOSE_MEDIA": "Close", "APPS_TWITTER_TWEET": "Tweet", - "APPS_TWITTER_CLOSE": "Close" + "APPS_TWITTER_CLOSE": "Close", + "APPS_TWITTER_CREATE_FAILED": "Failed to send your tweet" } } diff --git a/resources/client/cl_main.lua b/resources/client/cl_main.lua index c898e8d..d42de4a 100644 --- a/resources/client/cl_main.lua +++ b/resources/client/cl_main.lua @@ -238,6 +238,7 @@ end, false) RegisterNetEvent('phone:send') AddEventHandler('phone:send', function() TriggerEvent('phone:sendContacts') + TriggerEvent('phone:fetchTweets') end) RegisterNUICallback('phone:close', function() -- Called for when the phone is closed via the UI. diff --git a/resources/client/cl_twitter.lua b/resources/client/cl_twitter.lua new file mode 100644 index 0000000..ec7793f --- /dev/null +++ b/resources/client/cl_twitter.lua @@ -0,0 +1,43 @@ +ESX = nil + +Citizen.CreateThread(function() + while ESX == nil do + TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) + Citizen.Wait(0) + end +end) + +local APP_NAME = 'TWITTER' + + +function sendMessage(method, data) + SendNUIMessage( + { + app = APP_NAME, + method = method, + data = data + } + ) +end + +RegisterNetEvent('phone:fetchTweets') +AddEventHandler('phone:fetchTweets', function() + print('RegisterNetEvent: phone:fetchTweets') + ESX.TriggerServerCallback('phone:fetchTweets', function(tweets) + sendMessage('fetchTweets', tweets) + end) +end) + + +RegisterNUICallback('phone:fetchTweets', function() + print('RegisterNUICallback: phone:fetchTweets') + TriggerEvent('phone:fetchTweets') + end) + +RegisterNUICallback('phone:createTweet', function(data) + sendMessage('createTweetLoading', true) + ESX.TriggerServerCallback('phone:createTweet', function(isSuccessful) + sendMessage('createTweetResult', isSuccessful) + TriggerEvent('phone:fetchTweets') + end, data) +end) diff --git a/resources/server/sv_twitter.lua b/resources/server/sv_twitter.lua new file mode 100644 index 0000000..5d215fb --- /dev/null +++ b/resources/server/sv_twitter.lua @@ -0,0 +1,32 @@ +ESX = nil + +TriggerEvent('esx:getSharedObject', function(obj) + ESX = obj +end) + +ESX.RegisterServerCallback('phone:fetchTweets', function(source, cb) + print('RegisterServerCallback: phone:fetchTweets') + local _source = source + local xPlayer = ESX.GetPlayerFromId(_source) + local _identifier = xPlayer.getIdentifier() + MySQL.Async.fetchAll('SELECT * FROM npwd_twitter_tweets WHERE visible = 1 ORDER BY updatedAt DESC', {}, function(tweets) + cb(tweets) + end) +end) + + +ESX.RegisterServerCallback('phone:createTweet', function(source, cb, data) + print('RegisterServerEvent: phone:createTweet') + local _source = source + local xPlayer = ESX.GetPlayerFromId(_source) + local _identifier = xPlayer.getIdentifier() + + MySQL.Async.execute('INSERT INTO npwd_twitter_tweets (`identifier`, `realUser`, `message`) VALUES (@identifier, @realUser, @message)', + { + ['identifier'] = _identifier, + ['realUser'] = data.realUser, + ['message'] = data.message + }, function(result) + cb(result) + end) +end)