initial likes implementation

This adds the initial likes implementation. We take an approach of just
toggling the like in the database rather than adding a loading mechanic
and waiting on the UI.
This commit is contained in:
erik-sn committed 2020-10-05 21:37:19 -04:00
1 parent 7e690d0e92
commit bb23413533
6 files changed
+149 -12

No files matched your search

@@ -0,0 +1,23 @@
import React, { useState, useEffect } from "react";
import { Button } from "@material-ui/core";
import FavoriteBorderIcon from "@material-ui/icons/FavoriteBorder";
import FavoriteIcon from "@material-ui/icons/Favorite";
import Nui from "../../../../os/nui-events/utils/Nui";
function LikeButton({ tweetId, isLiked }) {
const [liked, setLiked] = useState(isLiked);
const handleClick = () => {
Nui.send("phone:toggleLike", tweetId);
setLiked(!liked);
};
return (
<Button onClick={handleClick}>
{liked ? <FavoriteIcon /> : <FavoriteBorderIcon />}
</Button>
);
}
export default LikeButton;
@@ -12,6 +12,7 @@ import MoreIcon from "@material-ui/icons/MoreVert";
import { ListItem } from "../../../../ui/components/ListItem";
import { secondsToHumanReadable } from "../../utils/time";
import LikeButton from "../buttons/LikeButton";
import ReplyButton from "../buttons/ReplyButton";
import ImageDisplay from "../images/ImageDisplay";
import Avatar from "../Avatar";
@@ -63,11 +64,13 @@ const useStyles = makeStyles(() => ({
export const Tweet = (tweet) => {
const {
id,
message,
images,
avatar_url,
profile_name,
seconds_since_tweet,
isLiked,
} = tweet;
const classes = useStyles();
const { t } = useTranslation();
@@ -110,9 +113,7 @@ export const Tweet = (tweet) => {
{enableImages && <ImageDisplay visible images={images} small />}
<div className={classes.buttonContainer}>
<ReplyButton profile_name={profile_name} />
<Button>
<FavoriteBorderIcon />
</Button>
<LikeButton tweetId={id} isLiked={isLiked} />
<QuoteButton message={message} />
<Button>
<MoreIcon />
+4
View File
@@ -77,3 +77,7 @@ RegisterNUICallback('phone:updateTwitterProfile', function(data)
TriggerEvent('phone:getOrCreateTwitterProfile')
end, data)
end)
RegisterNUICallback('phone:toggleLike', function(tweetId)
TriggerServerEvent('phone:toggleLike', tweetId)
end)
+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>
+3 -3
View File
@@ -2,7 +2,7 @@
-- or if it already exists in the users table
ALTER TABLE `users` ADD COLUMN `phone_number` VARCHAR(20) NULL DEFAULT NULL;
CREATE TABLE npwd_phone_contacts (
CREATE TABLE IF NOT EXISTS npwd_phone_contacts (
id int(11) NOT NULL AUTO_INCREMENT,
identifier varchar(60) DEFAULT NULL,
number varchar(10) DEFAULT NULL,
@@ -18,7 +18,7 @@ COLLATE utf8mb4_general_ci;
INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('phone', 'Phone', 1, 0, 1);
INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('dphone', 'Destroyed Phone', 1, 0, 1);
CREATE TABLE `npwd_twitter_tweets` (
CREATE TABLE IF NOT EXISTS `npwd_twitter_tweets` (
`id` int NOT NULL AUTO_INCREMENT,
`message` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -31,7 +31,7 @@ CREATE TABLE `npwd_twitter_tweets` (
) ENGINE=InnoDB AUTO_INCREMENT=190 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `npwd_twitter_profiles` (
CREATE TABLE IF NOT EXISTS `npwd_twitter_profiles` (
`id` int NOT NULL AUTO_INCREMENT,
`profile_name` varchar(90) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`identifier` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
+71 -5
View File
@@ -4,40 +4,45 @@ TriggerEvent('esx:getSharedObject', function(obj)
ESX = obj
end)
function fetchAllTweets(cb)
function fetchAllTweets(profileId, cb)
MySQL.Async.fetchAll([[
SELECT
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
FROM npwd_twitter_tweets
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = @profileId
WHERE visible = 1
ORDER BY npwd_twitter_tweets.createdAt DESC
LIMIT 100
]],
{},
{ profileId = profileId },
function(tweets)
cb(tweets)
end)
end
function fetchTweetsFiltered(searchValue, cb)
function fetchTweetsFiltered(profileId, searchValue, cb)
local searchValueParameterized = '%' .. searchValue .. '%'
MySQL.Async.fetchAll([[
SELECT
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
FROM npwd_twitter_tweets
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = @profileId
WHERE visible = 1 AND (npwd_twitter_profiles.profile_name LIKE @profile_name OR npwd_twitter_tweets.message LIKE @message)
ORDER BY npwd_twitter_tweets.createdAt DESC
LIMIT 100
]],
{
profileId = profileId,
profile_name = searchValueParameterized,
message = searchValueParameterized
},
@@ -47,12 +52,26 @@ function fetchTweetsFiltered(searchValue, cb)
end
ESX.RegisterServerCallback('phone:fetchTweets', function(source, cb)
fetchAllTweets(cb)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local _identifier = xPlayer.getIdentifier()
getProfile(_identifier, function(result)
local profile = result[1]
fetchAllTweets(profile.id, cb)
end)
end)
-- search value expected to be lower case
ESX.RegisterServerCallback('phone:fetchTweetsFiltered', function(source, cb, searchValue)
fetchTweetsFiltered(searchValue, cb)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local _identifier = xPlayer.getIdentifier()
getProfile(_identifier, function(result)
local profile = result[1]
fetchTweetsFiltered(profile.id, searchValue, cb)
end)
end)
@@ -181,3 +200,50 @@ ESX.RegisterServerCallback('phone:updateTwitterProfile', function(source, cb, da
end)
end)
function doesLikeExist(profileId, tweetId, cb)
MySQL.Async.fetchAll([[
SELECT * FROM npwd_twitter_likes
WHERE profile_id = @profileId AND tweet_id = @tweetId
LIMIT 1
]],
{ profileId = profileId, tweetId = tweetId },
function(result)
cb(result)
end)
end
function createLike(profileId, tweetId)
MySQL.Async.execute([[
INSERT INTO npwd_twitter_likes (`profile_id`, `tweet_id`)
VALUES (@profileId, @tweetId)
]],
{ profileId = profileId, tweetId = tweetId })
end
function deleteLike(profileId, tweetId)
MySQL.Async.execute([[
DELETE FROM npwd_twitter_likes
WHERE profile_id = @profileId AND tweet_id = @tweetId
]],
{ profileId = profileId, tweetId = tweetId })
end
RegisterServerEvent('phone:toggleLike')
AddEventHandler('phone:toggleLike', function(tweetId)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local _identifier = xPlayer.getIdentifier()
getProfile(_identifier, function(result)
local profile = result[1]
doesLikeExist(profile.id, tweetId, function(result)
if next(result) == nil then -- like doesn't exist
createLike(profile.id, tweetId)
else -- like does exist
deleteLike(profile.id, tweetId)
end
end)
end)
end)