working notification

This commit is contained in:
erik-sn committed 2020-10-17 21:40:53 -04:00
1 parent 7d27a6bddf
commit b2466f0e15
12 files changed
+236 -13

No files matched your search

@@ -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 (
<Notification key={notification.id} open={visible} handleClose={() => setVisible(false)}>
<div className={classes.content}>
<div className={classes.title}>
<div className={classes.titleLeft}>
<div className={classes.icon}><TwitterIcon /></div>
<div className={classes.profileName}>@{notification.profile_name}</div>
</div>
<div className={classes.justNow}>{t("APPS_TWITTER_JUST_NOW")}</div>
</div>
<div className={classes.message}>{notification.message}</div>
</div>
</Notification>
);
}
export default TwitterNotification;
+4
View File
@@ -37,4 +37,8 @@ export const twitterState = {
key: "updateProfileSuccess",
default: null,
}),
notification: atom({
key: 'notification',
default: null,
})
};
@@ -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<Tweet | null>(twitterState.notification);
return { notification };
};
@@ -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)
};
+10
View File
@@ -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;
}
+5 -3
View File
@@ -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(
<HashRouter>
<RecoilRoot>
<TwitterNotification />
<Phone />
</RecoilRoot>
</HashRouter>,
+1
View File
@@ -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",
+40
View File
@@ -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 (
<ThemeProvider theme={currentTheme()}>
<Snackbar
key={key}
anchorOrigin={{ vertical: 'top', horizontal: 'left' }}
ClickAwayListenerProps={{ onClickAway: () => null }}
onClose={handleClose}
open={open}
TransitionComponent={Fade}
autoHideDuration={6000}
>
<Paper className={classes.paper}>
{children}
</Paper>
</Snackbar>
</ThemeProvider>
);
}
export default Notification;
+4
View File
@@ -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
*/
+44 -1
View File
@@ -1 +1,44 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="%PUBLIC_URL%/favicon.ico"/><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"/><link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><title>React App</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="main.js"></script></body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<!--
Notice the use of in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script src="dev.js"></script></body>
</html>
+31 -9
View File
@@ -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<any> {
async function createTweet(identifier: string, tweet: Tweet): Promise<Tweet> {
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 = <any>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 = <Tweet[]>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) => {
+1
View File
@@ -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",