diff --git a/config.json b/config.json index 6b01a9c..c0c10cd 100644 --- a/config.json +++ b/config.json @@ -9,7 +9,7 @@ "DestroyPhoneReCheck": 3, "notificationPosition": { "horizontal": "right", - "vertical": "bottom" + "vertical": "top" }, "twitter": { "showNotifications": true, @@ -22,5 +22,8 @@ "enableEmojis": true, "enableImages": true, "maxImages": 3 + }, + "bank": { + "showNotifications": true } } \ No newline at end of file diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 0865345..0e442ba 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -59,6 +59,31 @@ setTimeout(() => { ) }, 1000) +setTimeout(() => { + new MessageEvent("messsage", { + data: { + app: "BANK", + method: "setNotification", + data: { + id: 1, + message: "ahhaha" + } + } + }) +}, 1000) + +setTimeout(() => { + new MessageEvent("messsage", { + data: { + app: "TWITTER", + method: "setNotification", + data: { + id: 1, + message: "ahhaha" + } + } + }) +}, 1000) setTimeout(() => { window.dispatchEvent( @@ -155,18 +180,6 @@ setTimeout(() => { ); }, 1000); -setTimeout(() => { - window.dispatchEvent( - new MessageEvent("message", { - data: { - app: "PHONE", - method: "setVisibility", - data: true, - }, - }) - ); -}, 1000); - setTimeout(() => { window.dispatchEvent( new MessageEvent("message", { diff --git a/phone/src/apps/bank/components/BankApp.css b/phone/src/apps/bank/components/BankApp.css index 64993ad..ad19ba4 100644 --- a/phone/src/apps/bank/components/BankApp.css +++ b/phone/src/apps/bank/components/BankApp.css @@ -31,6 +31,11 @@ color: #388CF8 !important; } + +#bank-app .MuiSnackbar-root { + position: absolute; +} + #bank-title { font-family: 'Bebas Neue', cursive; font-size: 50px; diff --git a/phone/src/apps/bank/components/BankApp.tsx b/phone/src/apps/bank/components/BankApp.tsx index cfc38c9..55415f5 100644 --- a/phone/src/apps/bank/components/BankApp.tsx +++ b/phone/src/apps/bank/components/BankApp.tsx @@ -21,6 +21,7 @@ import { NavigationBar } from "./navigation/NavigationBar"; import { BankHome } from './home/BankHome'; import { BankAccount } from './account/BankAccount'; import { TransactionList } from "./transactions/TransactionList"; +import { BankAlert } from "./alert/BankAlert"; const useStyles = makeStyles((theme) => ({ root: { @@ -67,6 +68,7 @@ export const BankApp = () => { + diff --git a/phone/src/apps/bank/components/alert/Alert.tsx b/phone/src/apps/bank/components/alert/Alert.tsx new file mode 100644 index 0000000..2ba1e67 --- /dev/null +++ b/phone/src/apps/bank/components/alert/Alert.tsx @@ -0,0 +1,8 @@ +import React from "react"; +import MuiAlert from "@material-ui/lab/Alert"; + +export function Alert(props) { + return ; +} + +export default Alert; diff --git a/phone/src/apps/bank/components/alert/BankAlert.tsx b/phone/src/apps/bank/components/alert/BankAlert.tsx new file mode 100644 index 0000000..cda9c51 --- /dev/null +++ b/phone/src/apps/bank/components/alert/BankAlert.tsx @@ -0,0 +1,43 @@ +import React, { useState, useEffect } from "react"; +import { useBankAlert } from "../../hooks/useBankAlert"; +import Snackbar from "@material-ui/core/Snackbar"; +import Alert from "./Alert"; +import { useTranslation } from "react-i18next"; + +export const BankAlert = () => { + const { t } = useTranslation(); + const [open, setOpen] = useState(false); + const { bankAlert, setBankAlert } = useBankAlert(); + const [ message, setMessage ] = useState(null); + const [severity, setSeverity] = useState(""); + + const _handleClose = () => { + setOpen(false); + setBankAlert(null); + }; + + useEffect(() => { + if (bankAlert === true ) { + setOpen(true); + setMessage(t("APPS_BANK_ALERT_TRANSFER_SUCCESS")); + setSeverity("success") + } else if (bankAlert === false) { + setOpen(true); + setMessage(t("APPS_BANK_ALERT_TRANSFER_FAILURE")); + setSeverity("error") + } + }, [bankAlert]) + + return ( + + + {message} + + + ); +}; \ No newline at end of file diff --git a/phone/src/apps/bank/components/notification/BankNotification.tsx b/phone/src/apps/bank/components/notification/BankNotification.tsx index 6f64552..122bff0 100644 --- a/phone/src/apps/bank/components/notification/BankNotification.tsx +++ b/phone/src/apps/bank/components/notification/BankNotification.tsx @@ -1,16 +1,45 @@ -import React from 'react' -import Alert from '@material-ui/lab/Alert'; -import Notification from '../../../../ui/components/Notification'; -import { useBankAlert } from './../../hooks/useBankAlert'; +import React, { useEffect, useState } from "react"; +import Notification from "../../../../ui/components/Notification"; +import { usePhone } from "../../../../os/phone/hooks/usePhone"; +import { useTranslation } from "react-i18next"; +import useStyles from './notification.style'; +import { useBankNotification } from "../../hooks/useBankNotification"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faWonSign } from "@fortawesome/free-solid-svg-icons"; -export const BankNotification = ({ alert }) => { - const { setBankAlert } = useBankAlert(); +export const BankNotification = () => { + const { notification } = useBankNotification() + const classes = useStyles() + const { t } = useTranslation(); + const [visible, setVisible ] = useState(false); + const { config } = usePhone() - const handleClose = () => { - setBankAlert(false) - } + useEffect(() => { + if (notification) { + setVisible(true); + } + }, [notification?.id]) + + if (!config?.bank.showNotifications || !notification) return null; return ( -
- ) -} + setVisible(false)} + > +
+
+
+
+ +
+
{notification.type}
+
+
{t("APPS_BANK_TIME_JUST_NOW")}
+
+
{notification.source} sent you ${notification.transferAmount}.

{notification.message}
+
+
+ ) +} \ No newline at end of file diff --git a/phone/src/apps/bank/components/notification/notification.style.ts b/phone/src/apps/bank/components/notification/notification.style.ts new file mode 100644 index 0000000..7d80b71 --- /dev/null +++ b/phone/src/apps/bank/components/notification/notification.style.ts @@ -0,0 +1,43 @@ +import { makeStyles, createStyles, Theme } from "@material-ui/core/styles"; + +const useStyles = makeStyles((theme: Theme) => + createStyles({ + content: { + padding: theme.spacing(2), + 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", + }, + heading: { + marginBottom: "3px", + }, + justNow: { + fontSize: "18px", + fontStyle: "italic", + paddingRight: "3px", + }, + message: { + fontSize: "16px", + overflow: "hidden", + display: "-webkit-box", + WebkitLineClamp: 2, + WebkitBoxOrient: "vertical", + }, + }) +) + +export default useStyles; \ No newline at end of file diff --git a/phone/src/apps/bank/components/transfers/TransferModal.tsx b/phone/src/apps/bank/components/transfers/TransferModal.tsx index fe4050b..6759bf0 100644 --- a/phone/src/apps/bank/components/transfers/TransferModal.tsx +++ b/phone/src/apps/bank/components/transfers/TransferModal.tsx @@ -6,6 +6,7 @@ import CloseIcon from '@material-ui/icons/Close'; import Nui from '../../../../os/nui-events/utils/Nui'; import { useBankModal} from '../../hooks/useBankModal'; +import { Message } from '@material-ui/icons'; const useStyles = makeStyles((theme) => ({ input: { @@ -50,15 +51,13 @@ export const TransferModal = () => { } const addTransfer = () => { + const transferAmount = parseInt(amount); Nui.send('phone:addTransfer', { targetID, - amount, + transferAmount, message }) - setTimeout(() => { - setShowBankModal(false) - }, 200) - + setShowBankModal(false) } return ( diff --git a/phone/src/apps/bank/hooks/state.ts b/phone/src/apps/bank/hooks/state.ts index 2f17396..70ad8d4 100644 --- a/phone/src/apps/bank/hooks/state.ts +++ b/phone/src/apps/bank/hooks/state.ts @@ -9,12 +9,16 @@ export const bankState = { key: 'modalVisibility', default: false, }), - bankAlert: atom({ + transferSuccessful: atom({ key: 'bankAlert', - default: false + default: null }), bankCredentials: atom({ key: 'bankCredentails', default: null + }), + notification: atom({ + key: 'bankNotification', + default: null }) } \ No newline at end of file diff --git a/phone/src/apps/bank/hooks/useBankAlert.ts b/phone/src/apps/bank/hooks/useBankAlert.ts index 2fb828a..da6deb0 100644 --- a/phone/src/apps/bank/hooks/useBankAlert.ts +++ b/phone/src/apps/bank/hooks/useBankAlert.ts @@ -1,7 +1,7 @@ import { useRecoilState } from 'recoil'; import { bankState } from './state'; -export const useBankAlert = () => { - const [ bankAlert, setBankAlert ] = useRecoilState(bankState.bankAlert); +export const useBankAlert = (): any => { + const [ bankAlert, setBankAlert ] = useRecoilState(bankState.transferSuccessful); return { bankAlert, setBankAlert }; } \ No newline at end of file diff --git a/phone/src/apps/bank/hooks/useBankNotification.ts b/phone/src/apps/bank/hooks/useBankNotification.ts new file mode 100644 index 0000000..e67087f --- /dev/null +++ b/phone/src/apps/bank/hooks/useBankNotification.ts @@ -0,0 +1,13 @@ +import { useRecoilValue } from 'recoil'; + +import { Transfer } from '../../../common/interfaces/bank'; +import { bankState } from './state'; + +interface IBankNotification { + notification: Transfer | null; +} + +export const useBankNotification = (): IBankNotification => { + const notification = useRecoilValue(bankState.notification); + return { notification } +} \ No newline at end of file diff --git a/phone/src/apps/bank/hooks/useBankService.ts b/phone/src/apps/bank/hooks/useBankService.ts index 168b3f8..f853886 100644 --- a/phone/src/apps/bank/hooks/useBankService.ts +++ b/phone/src/apps/bank/hooks/useBankService.ts @@ -2,12 +2,21 @@ import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; import { bankState } from './state'; import { useTransactions } from './useTransactions' import { useCredentials } from './useCredentials'; +import { useBankAlert } from './useBankAlert'; import { useSetRecoilState } from 'recoil' +import { useBankNotification } from './useBankNotification'; export const useBankService = () => { const setTransaction = useSetRecoilState(bankState.transactions) const setCredentials = useSetRecoilState(bankState.bankCredentials) + const setAlertMessage = useSetRecoilState(bankState.transferSuccessful) + const setNotification = useSetRecoilState(bankState.notification); + + /// + useNuiEvent('BANK', 'setTransaction', setTransaction); - useNuiEvent('BANK', 'setCredentials', setCredentials) - return {useTransactions, useCredentials}; + useNuiEvent('BANK', 'setCredentials', setCredentials); + useNuiEvent('BANK', 'setAlert', setAlertMessage); + useNuiEvent('BANK', 'setNotification', setNotification); + return { useTransactions, useCredentials, useBankAlert, useBankNotification }; } \ No newline at end of file diff --git a/phone/src/common/interfaces/bank.ts b/phone/src/common/interfaces/bank.ts new file mode 100644 index 0000000..b11a21e --- /dev/null +++ b/phone/src/common/interfaces/bank.ts @@ -0,0 +1,14 @@ +export interface Credentials { + name: string; + balance: number; +} + +export interface Transfer { + id: number; + targetID: number; + source: string; + type: string; + transferAmount: number; + message: string; +} + diff --git a/phone/src/common/interfaces/config.ts b/phone/src/common/interfaces/config.ts index 85715eb..c5f43bf 100644 --- a/phone/src/common/interfaces/config.ts +++ b/phone/src/common/interfaces/config.ts @@ -16,6 +16,10 @@ interface NotificationConfig { vertical: "bottom" | "top"; } +interface BankConfig { + showNotifications: boolean; +} + export interface ServerConfig { Locale: string; KeyTogglePhone: number; @@ -26,5 +30,6 @@ export interface ServerConfig { DestoryChance: number; DestroyPhoneReCheck: number; twitter: TwitterConfig; + bank: BankConfig; notificationPosition: NotificationConfig; } diff --git a/phone/src/index.tsx b/phone/src/index.tsx index c7fc224..eb1d652 100644 --- a/phone/src/index.tsx +++ b/phone/src/index.tsx @@ -8,11 +8,13 @@ import "./main.css"; import Phone from "./Phone"; import * as serviceWorker from "./serviceWorker"; import TwitterNotification from "./apps/twitter/components/notification/TwitterNotification"; +import { BankNotification } from "./apps/bank/components/notification/BankNotification"; ReactDOM.render( + , diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 4094108..644289d 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -41,10 +41,13 @@ "APPS_BANK_HOME_TITLE": "Welcome", "APPS_BANK_ACCOUNT_LINK": "Account", + "APPS_BANK_TIME_JUST_NOW": "Just now", "APPS_BANK_ACCOUNT_TITLE": "Account Summary", "APPS_BANK_ACCOUNT_TRANSACTIONS": "Transactions", "APPS_BANK_ACCOUNT_SEE_TRANSACTION": "See all", "APPS_BANK_ACTIONS_TRANSFER": "Transfer", + "APPS_BANK_ALERT_TRANSFER_SUCCESS": "Transfer done successfully!", + "APPS_BANK_ALERT_TRANSFER_FAILURE": "Transfer failed!", "APPS_CONTACT_FORM_NAME": "Name", "APPS_CONTACT_FORM_NUMBER": "Number", diff --git a/phone/src/ui/components/Notification.tsx b/phone/src/ui/components/Notification.tsx index 851880d..03254f1 100644 --- a/phone/src/ui/components/Notification.tsx +++ b/phone/src/ui/components/Notification.tsx @@ -32,7 +32,7 @@ function Notification({ children, handleClose, open }) { null }} + ClickAwayListenerProps={{ onClickAway: () => setTimeout(() => { handleClose() }, 5000) }} onClose={handleClose} open={open} TransitionComponent={Fade} diff --git a/resources/client/cl_bank.ts b/resources/client/cl_bank.ts index c5681c1..c29703c 100644 --- a/resources/client/cl_bank.ts +++ b/resources/client/cl_bank.ts @@ -12,7 +12,6 @@ onNet(events.BANK_SEND_TRANSFERS, (transfer: any) => { }) onNet(events.BANK_SEND_CREDENTIALS, (credentials: any) => { - console.log("Client:", credentials.balance, credentials.name) SendNuiMessage( JSON.stringify({ app: "BANK", @@ -26,8 +25,28 @@ RegisterNuiCallbackType(events.BANK_ADD_TRANSFER); on(`__cfx_nui:${events.BANK_ADD_TRANSFER}`, (data: any) => { const transfer = data; emitNet(events.BANK_ADD_TRANSFER, transfer) +}) - setTimeout(() => { - emitNet(events.BANK_FETCH_TRANSACTIONS); - }, 500) +onNet(events.BANK_ADD_TRANSFER_SUCCESS, () => { + emitNet(events.BANK_FETCH_TRANSACTIONS); +}) + +onNet(events.BANK_TRANSACTION_ALERT, (bool: boolean) => { + SendNuiMessage( + JSON.stringify({ + app: 'BANK', + method: 'setAlert', + data: bool + }) + ) +}) + +onNet(events.BANK_TRANSACTION_NOTIFICATION, (transfer: any) => { + SendNuiMessage( + JSON.stringify({ + app: "BANK", + method: 'setNotification', + data: transfer + }) + ) }) diff --git a/resources/server/sv_bank.ts b/resources/server/sv_bank.ts index a005995..97941f8 100644 --- a/resources/server/sv_bank.ts +++ b/resources/server/sv_bank.ts @@ -1,18 +1,9 @@ import events from '../utils/events'; import { ESX, getSource } from './server'; import { pool } from './db'; -import { resourceLimits } from 'worker_threads'; - -interface Transfer { - targetID: string; - amount: number; - message: string; -} - -interface Credentials { - name: string; - balance: number; -} +import { Transfer, Credentials } from '../../phone/src/common/interfaces/bank'; +import { useIdentifier } from './functions'; +import { type } from 'os'; async function fetchAllTransactions(identifier: string): Promise { @@ -32,63 +23,84 @@ function fetchCredentials(): Credentials { balance } - console.log(result.name, result.balance); - return result; } - async function addTransfer(identifier: string, transfer: Transfer): Promise { - const xPlayer = ESX.GetPlayerFromId(getSource()) const bankBalance = xPlayer.getAccount('bank').money; - console.log(bankBalance) - if (transfer.amount > bankBalance) { - console.log("poor man") - emitNet(events.BANK_TRANSACTION_ALERT, getSource, 'You do not have enough money on your account', 'error') - } - else if (transfer.amount <= bankBalance) { - const query = "INSERT INTO npwd_bank_transfers (identifier, target, amount, message) VALUES (?, ?, ?, ?)" + const sourceName = xPlayer.getName() + + const xTarget = ESX.GetPlayerFromId(transfer.targetID); - await pool.query(query, [ + + if (transfer.transferAmount > bankBalance) { + emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false) + } + else if (transfer.transferAmount < bankBalance) { + emitNet(events.BANK_TRANSACTION_ALERT, getSource(), true); + + xTarget.addAccountMoney('bank', transfer.transferAmount); + xPlayer.removeAccountMoney('bank', transfer.transferAmount); + + const query = "INSERT INTO npwd_bank_transfers (identifier, target, amount, message, type, source) VALUES (?, ?, ?, ?, ?, ?)" + + const [results] = await pool.query(query, [ identifier, transfer.targetID, - transfer.amount, - transfer.message + transfer.transferAmount, + transfer.message, + "Transfer", + sourceName, ]) - emitNet(events.BANK_TRANSACTION_ALERT, getSource, 'You succesfully transferred money to someone', 'success') + const insertData = results; + return await getTransfer(insertData.insertId) } } +/** + * Gets a transfer from its ID + * @param transferId - transfer id + */ +async function getTransfer(transferId: number): Promise { + const query = "SELECT * FROM npwd_bank_transfers WHERE id = ?" + const [ results ] = await pool.query(query, [transferId]); + const transfers = results; + const transfer = transfers[0]; + return {...transfer}; +} + onNet(events.BANK_FETCH_TRANSACTIONS, async () => { try { const _source = (global as any).source; - const _identifier = ESX.GetPlayerFromId(getSource()).getIdentifier() + const _identifier = await useIdentifier() const transfer = await fetchAllTransactions(_identifier); emitNet(events.BANK_SEND_TRANSFERS, _source, transfer) - console.log("WOORK") } catch (error) { - console.log('Failed to fetch transactions', error) + console.log("Failed to fetch transactions"); } }) -onNet(events.BANK_ADD_TRANSFER, (transfer: Transfer) => { +onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => { + const xTarget = ESX.GetPlayerFromId(transfer.targetID); + const targetSource = xTarget.source; try { - const _identifier = ESX.GetPlayerFromId(getSource()).getIdentifier() - addTransfer(_identifier, transfer) + const _identifier = await useIdentifier() + const transferNotify = await addTransfer(_identifier, transfer) + emitNet(events.BANK_TRANSACTION_NOTIFICATION, targetSource, transferNotify) + emitNet(events.BANK_ADD_TRANSFER_SUCCESS, getSource()); } catch (error) { - console.log('Failed to add transfer', error) + emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false) } }) onNet(events.BANK_GET_CREDENTIALS, () => { try { const credentials = fetchCredentials() - console.log("New log: ", credentials.balance, credentials.name) emitNet(events.BANK_SEND_CREDENTIALS, getSource(), credentials) } catch (error) { - console.log(error) + console.log("Failed to fetch credentials"); } }) \ No newline at end of file diff --git a/resources/utils/events.ts b/resources/utils/events.ts index 0dc62a1..de34896 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -45,9 +45,11 @@ export default { // Bank BANK_ADD_TRANSFER: 'phone:addTransfer', + BANK_ADD_TRANSFER_SUCCESS: 'phone:addTransferSuccess', BANK_FETCH_TRANSACTIONS: 'phone:fetchAllTransactions', BANK_SEND_TRANSFERS: 'phone:sendTransfers', - BANK_TRANSACTION_ALERT: 'phone:transactionAlert', + BANK_TRANSACTION_ALERT: 'phone:bankTransactionAlert', + BANK_TRANSACTION_NOTIFICATION: 'phone:bankTransactionNotification', BANK_SEND_CREDENTIALS: 'phone:sendBankCredentials', BANK_GET_CREDENTIALS: 'phone:getBankCredentials',