From b2466f0e1535e7bcfa7dab8ea4fd85f471911a7b Mon Sep 17 00:00:00 2001 From: erik-sn Date: Sat, 17 Oct 2020 21:39:35 -0400 Subject: [PATCH] working notification --- .../notification/TwitterNotification.tsx | 75 +++++++++++++++++++ phone/src/apps/twitter/hooks/state.ts | 4 + .../twitter/hooks/useTwitterNotification.ts | 13 ++++ .../apps/twitter/hooks/useTwitterService.ts | 8 ++ phone/src/apps/twitter/interfaces.ts | 10 +++ phone/src/index.tsx | 8 +- phone/src/locale/en.json | 1 + phone/src/ui/components/Notification.tsx | 40 ++++++++++ resources/client/cl_twitter.ts | 4 + resources/html/index.html | 45 ++++++++++- resources/server/sv_twitter.ts | 40 +++++++--- resources/utils/events.ts | 1 + 12 files changed, 236 insertions(+), 13 deletions(-) create mode 100644 phone/src/apps/twitter/components/notification/TwitterNotification.tsx create mode 100644 phone/src/apps/twitter/hooks/useTwitterNotification.ts create mode 100644 phone/src/apps/twitter/interfaces.ts create mode 100644 phone/src/ui/components/Notification.tsx diff --git a/phone/src/apps/twitter/components/notification/TwitterNotification.tsx b/phone/src/apps/twitter/components/notification/TwitterNotification.tsx new file mode 100644 index 0000000..ad85817 --- /dev/null +++ b/phone/src/apps/twitter/components/notification/TwitterNotification.tsx @@ -0,0 +1,75 @@ +import React, { useEffect, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { makeStyles } from "@material-ui/core/styles"; +import TwitterIcon from '@material-ui/icons/Twitter'; +import { useTwitterNotification } from '../../hooks/useTwitterNotification'; +import Notification from '../../../../ui/components/Notification'; + +const useStyles = makeStyles({ + content: { + padding: '5px 8px', + overflowY: 'hidden' + }, + title: { + display: 'flex', + flexFlow: 'row nowrap', + justifyContent: 'space-between', + alignItems: 'center' + }, + titleLeft: { + display: 'flex', + flexFlow: 'row nowrap', + justifyContent: 'flex-start', + alignItems: 'center' + }, + icon: { + marginTop: '3px', + marginRight: '5px' + }, + profileName: { + marginBottom: '3px', + }, + justNow: { + fontSize: '18px', + fontStyle: 'italic', + paddingRight: '3px', + }, + message: { + fontSize: '16px', + overflow: 'hidden', + display: '-webkit-box', + WebkitLineClamp: 2, + WebkitBoxOrient: 'vertical', + } +}); + +function TwitterNotification() { + const classes = useStyles(); + const { t } = useTranslation(); + const [visible, setVisible] = useState(false); + const { notification } = useTwitterNotification(); + + useEffect(() => { + setVisible(true); + }, [notification?.id]) + + if (!notification) return null; + + return ( + setVisible(false)}> +
+
+
+
+
@{notification.profile_name}
+
+
{t("APPS_TWITTER_JUST_NOW")}
+
+
{notification.message}
+
+
+ ); +} + + +export default TwitterNotification; diff --git a/phone/src/apps/twitter/hooks/state.ts b/phone/src/apps/twitter/hooks/state.ts index a908080..10d8a65 100644 --- a/phone/src/apps/twitter/hooks/state.ts +++ b/phone/src/apps/twitter/hooks/state.ts @@ -37,4 +37,8 @@ export const twitterState = { key: "updateProfileSuccess", default: null, }), + notification: atom({ + key: 'notification', + default: null, + }) }; diff --git a/phone/src/apps/twitter/hooks/useTwitterNotification.ts b/phone/src/apps/twitter/hooks/useTwitterNotification.ts new file mode 100644 index 0000000..98b6ad7 --- /dev/null +++ b/phone/src/apps/twitter/hooks/useTwitterNotification.ts @@ -0,0 +1,13 @@ +import { useRecoilValue } from "recoil"; +import { Tweet } from "../interfaces"; + +import { twitterState } from "./state"; + +interface ITwitterNotification { + notification: Tweet | null; +} + +export const useTwitterNotification = (): ITwitterNotification => { + const notification = useRecoilValue(twitterState.notification); + return { notification }; +}; diff --git a/phone/src/apps/twitter/hooks/useTwitterService.ts b/phone/src/apps/twitter/hooks/useTwitterService.ts index fb60973..f580cf3 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.ts +++ b/phone/src/apps/twitter/hooks/useTwitterService.ts @@ -6,6 +6,8 @@ import { IMAGE_DELIMITER } from "../utils/images"; import { APP_TWITTER } from "../utils/constants"; import { twitterState } from "./state"; +const NOTIFICATION_TIMEOUT = 5000; + /** * Perform all necessary processing/transforms from the raw database * data to what the frontend expects @@ -41,6 +43,7 @@ export const useTwitterService = () => { const setCreateSuccess = useSetRecoilState( twitterState.createTweetSuccessful ); + const setNotification = useSetRecoilState(twitterState.notification); const _setProfile = (profile) => { setProfile(profile); @@ -55,6 +58,10 @@ export const useTwitterService = () => { setCreateLoading(false); }; + const _setNotification = (tweet) => { + setNotification(tweet); + } + const _setTweets = (tweets) => { setTweets(tweets.map(processTweet)); }; @@ -69,4 +76,5 @@ export const useTwitterService = () => { useNuiEvent(APP_TWITTER, "fetchTweetsFiltered", _setFilteredTweets); useNuiEvent(APP_TWITTER, "createTweetLoading", setCreateLoading); useNuiEvent(APP_TWITTER, "createTweetResult", _setCreateSuccess); + useNuiEvent(APP_TWITTER, 'createTweetBroadcast', _setNotification) }; diff --git a/phone/src/apps/twitter/interfaces.ts b/phone/src/apps/twitter/interfaces.ts new file mode 100644 index 0000000..5db7924 --- /dev/null +++ b/phone/src/apps/twitter/interfaces.ts @@ -0,0 +1,10 @@ +export interface Tweet { + id: number; + profile_name?: string; + avatar_url?: string; + message: string; + createdAt?: string; + updatedAt?: string; + images?: string; + likes?: number; + } \ No newline at end of file diff --git a/phone/src/index.tsx b/phone/src/index.tsx index 98e19ed..2835c84 100644 --- a/phone/src/index.tsx +++ b/phone/src/index.tsx @@ -1,16 +1,18 @@ import "@babel/polyfill"; - import React from "react"; import ReactDOM from "react-dom"; +import { HashRouter } from "react-router-dom"; +import { RecoilRoot } from "recoil"; + import "./main.css"; import Phone from "./Phone"; import * as serviceWorker from "./serviceWorker"; -import { HashRouter } from "react-router-dom"; -import { RecoilRoot } from "recoil"; +import TwitterNotification from './apps/twitter/components/notification/TwitterNotification'; ReactDOM.render( + , diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index a9fe2a9..dd19257 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -20,6 +20,7 @@ "APPS_TWITTER_SUBMIT_IMAGE": "Add Media", "APPS_TWITTER_CLOSE_IMAGE": "Close", "APPS_TWITTER_CLOSE": "Close", + "APPS_TWITTER_JUST_NOW": "Just now", "APPS_TWITTER_SEARCH_TWEETS": "Search for tweets", "APPS_TWITTER_SEARCH_TWEETS_PLACEHOLDER": "Search by profile name or message", "APPS_TWITTER_TWEET_MESSAGE_CHAR_LIMIT": "Message is over the character limit", diff --git a/phone/src/ui/components/Notification.tsx b/phone/src/ui/components/Notification.tsx new file mode 100644 index 0000000..c19970e --- /dev/null +++ b/phone/src/ui/components/Notification.tsx @@ -0,0 +1,40 @@ +import React from "react"; +import { ThemeProvider } from "@material-ui/core"; +import Paper from '@material-ui/core/Paper'; +import Snackbar from '@material-ui/core/Snackbar'; +import Fade from '@material-ui/core/Fade'; +import { makeStyles } from "@material-ui/core/styles"; +import { useSettings } from "../../apps/settings/hooks/useSettings"; + +const useStyles = makeStyles({ + paper: { + width: '350px', + height: '85px', + opacity: '0.93', + } +}); + +function Notification({ key, children, handleClose, open }) { + const classes = useStyles(); + const { currentTheme } = useSettings(); + return ( + + null }} + onClose={handleClose} + open={open} + TransitionComponent={Fade} + autoHideDuration={6000} + > + + {children} + + + + ); +} + + +export default Notification; diff --git a/resources/client/cl_twitter.ts b/resources/client/cl_twitter.ts index e08d010..5276c3e 100644 --- a/resources/client/cl_twitter.ts +++ b/resources/client/cl_twitter.ts @@ -84,6 +84,10 @@ onNet(events.TWITTER_CREATE_TWEET_FAILURE, () => { sendTwitterMessage(events.TWITTER_CREATE_TWEET_FAILURE); }); +onNet(events.TWITTER_CREATE_TWEET_BROADCAST, (tweet: any) => { + sendTwitterMessage(events.TWITTER_CREATE_TWEET_BROADCAST, tweet); +}); + /** * Twitter likes */ diff --git a/resources/html/index.html b/resources/html/index.html index 9c511ba..d196fe9 100644 --- a/resources/html/index.html +++ b/resources/html/index.html @@ -1 +1,44 @@ -React App
\ No newline at end of file + + + + + + + + + + + + + + React App + + + +
+ + + diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index 0106467..4abf5a7 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -93,12 +93,32 @@ async function fetchTweetsFiltered( * @param identifier - player identifier * @param tweet - tweet to be created */ -async function createTweet(identifier: string, tweet: Tweet): Promise { +async function createTweet(identifier: string, tweet: Tweet): Promise { const query = ` INSERT INTO npwd_twitter_tweets (identifier, message, images) VALUES (?, ?, ?) `; - await pool.query(query, [identifier, tweet.message, tweet.images]); + const [results] = await pool.query(query, [ + identifier, + tweet.message, + tweet.images, + ]); + const insertData = results; + const insertId = insertData.insertId; + + const resultQuery = ` + SELECT + npwd_twitter_profiles.profile_name, + npwd_twitter_profiles.avatar_url, + npwd_twitter_tweets.* + FROM npwd_twitter_tweets + LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier + WHERE npwd_twitter_tweets.id = ? + `; + const [createResults] = await pool.query(resultQuery, [insertId]); + const tweets = createResults; + + return tweets[0]; } /** @@ -266,13 +286,15 @@ onNet(events.TWITTER_FETCH_TWEETS_FILTERED, async (searchValue: string) => { }); onNet(events.TWITTER_CREATE_TWEET, async (tweet: Tweet) => { - try { - const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier(); - await createTweet(identifier, tweet); - emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), true); - } catch (e) { - emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), false); - } + // try { + const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier(); + const createdTweet = await createTweet(identifier, tweet); + + emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), true); + emitNet(events.TWITTER_CREATE_TWEET_BROADCAST, -1, createdTweet); + // } catch (e) { + // emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), false); + // } }); onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => { diff --git a/resources/utils/events.ts b/resources/utils/events.ts index 6421797..e13604c 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -15,6 +15,7 @@ export default { TWITTER_CREATE_TWEET: "phone:createTweet", TWITTER_CREATE_TWEET_LOADING: "createTweetLoading", TWITTER_CREATE_TWEET_RESULT: "createTweetResult", + TWITTER_CREATE_TWEET_BROADCAST: "createTweetBroadcast", TWITTER_CREATE_TWEET_FAILURE: "createTweetResultFailed", TWITTER_TOGGLE_LIKE: "phone:toggleLike", TWITTER_TOGGLE_LIKE_SUCCESS: "toggleLikeSuccess",