Skip to content

Commit

Permalink
Merge pull request #122 from Qsilver97/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
Qsilver97 authored May 24, 2024
2 parents 2518d10 + 3084b64 commit b376e92
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 97 deletions.
11 changes: 9 additions & 2 deletions mobile/src/components/SocketComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import { useAuth } from "@app/context/AuthContext";

export const SocketCom: React.FC = () => {
const dispatch = useDispatch();
const { setBalances, setTokenBalances, allAddresses, setIsLoading } =
useAuth();
const {
setBalances,
setTokenBalances,
allAddresses,
balances,
setPrevBalances,
} = useAuth();
useEffect(() => {
eventEmitter.on("S2C/live", (res) => {
if (res.data.command == "CurrentTickInfo") {
Expand All @@ -34,6 +39,7 @@ export const SocketCom: React.FC = () => {

if (res.data.balance) {
setBalances((prev) => {
setPrevBalances(prev);
return {
...prev,
[res.data.address]: parseFloat(res.data.balance),
Expand All @@ -44,6 +50,7 @@ export const SocketCom: React.FC = () => {
res.data.balances.map((balance: [number, string]) => {
if (balance[0] < allAddresses.length)
setBalances((prev) => {
setPrevBalances(prev);
return {
...prev,
[allAddresses[balance[0]]]: parseFloat(balance[1]),
Expand Down
68 changes: 57 additions & 11 deletions mobile/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface AuthContextType {
}>
>;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
setPrevBalances: React.Dispatch<React.SetStateAction<Balances>>;
}

type TransactionItem = [string, string, string, string, string, string];
Expand All @@ -82,13 +83,18 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
>("Closed");
const [txId, setTxId] = useState<string>("");
const [expectedTick, setExpectedTick] = useState<number>(0);
const [txResult, setTxResult] = useState<string>("");
const [txResult, setTxResult] = useState<string>("Unknown");
const [tokenBalances, setTokenBalances] = useState<{
[name: string]: Balances;
}>({});
const [statusInterval, setStatusInterval] = useState<any>();
const [isLoading, setIsLoading] = useState<boolean>(false);

const [outTx, setOutTx] = useState(false);
const [prevBalances, setPrevBalances] = useState<Balances>({});
const [historyNum, setHistoryNum] = useState(histories.length);
const [expectingHistoryUpdate, setExpectingHistoryUpdate] = useState(false);

const lang = local.Toast;

const login = (userDetails: UserDetailType | null) => {
Expand All @@ -101,22 +107,40 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
const logout = () => {
dispatch(setIsAuthenticated(false));
dispatch(setPassword(""));
setTempPassword("");
setUser(null);
setBalances({});
setCurrentAddress("");
setAllAddresses([]);
setHistories([]);
setTxStatus("Closed");
setTxId("");
setExpectedTick(0);
setTxResult("");
setTokenBalances({});
setIsLoading(false);
};

useEffect(() => {
setTokens([]);
if (currentAddress != "") {
setIsLoading(true);
getHistory(currentAddress);
getToken();
}
}, [currentAddress]);

// If someone send qu to me
useEffect(() => {
if (currentAddress != "") {
getHistory(currentAddress);
}
}, [balances]);
if (currentAddress == "") return;
if (Object.is(prevBalances, {})) return;
if (!balances[currentAddress] || !prevBalances[currentAddress]) return;
if (balances[currentAddress] > prevBalances[currentAddress]) setOutTx(true);
else setOutTx(false);
setHistoryNum(histories.length + 1);
getHistory(currentAddress);
setExpectingHistoryUpdate(true);
}, [balances[currentAddress]]);

useEffect(() => {
if (user?.accountInfo) {
Expand All @@ -125,6 +149,30 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
}
}, [user]);

useEffect(() => {
if (expectingHistoryUpdate && currentAddress !== "") {
const interval = setInterval(() => {
if (histories.length !== historyNum) {
getHistory(currentAddress);
} else {
const newHistory = histories.reverse()[0];
if (outTx)
Toast.show({
type: "info",
text1: lang.ReceivedQUFrom.replace(
"{amount}",
`${Math.abs(parseInt(newHistory[3]))}`
).replace("{address}", newHistory[2]),
});
setExpectingHistoryUpdate(false); // Clear the flag when history is up-to-date
clearInterval(interval); // Clear the interval when done
}
}, 1500);

return () => clearInterval(interval);
}
}, [histories.length, historyNum, currentAddress, expectingHistoryUpdate]);

useEffect(() => {
const handleHistoryEvent = (res: any) => {
console.log("S2C/histories: ", res);
Expand Down Expand Up @@ -166,9 +214,11 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
if (res.data) {
if (res.data == "failed") {
setTxStatus("Rejected");
setTxResult("Failed");
Toast.show({ type: "error", text1: "E-23: " + lang.TransferFailed });
} else if (res.data.value.result == 0) {
setTxStatus("Pending");
setTxResult("Pending");
} else if (res.data.value.result == 1) {
setTxResult(res.data.value.display);
} else {
Expand All @@ -177,6 +227,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
} else {
Toast.show({ type: "error", text1: "E-24: " + lang.TransferFailed });
setTxStatus("Rejected");
setTxResult("Pending");
clearInterval(statusInterval);
}
};
Expand Down Expand Up @@ -229,12 +280,6 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
}
}, [txResult]);

// useEffect(() => {
// setTokenBalances((prev) => {
// return { ...prev, QU: balances };
// });
// }, [balances]);

return (
<AuthContext.Provider
value={{
Expand All @@ -259,6 +304,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
setExpectedTick,
setTokenBalances,
setIsLoading,
setPrevBalances,
}}
>
{children}
Expand Down
15 changes: 7 additions & 8 deletions mobile/src/screens/Main/Orderbook/components/Core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,19 @@ const Core: React.FC<IProps> = ({

const validateOrder = (orderList: IOrderUnit[], flag: string) => {
let isValidAddress = false;
let isValidPriceOrAmount = false;
for (const order of orderList) {
if (order[1] === currentAddress) {
isValidAddress = true;
if (order[3] !== price || parseInt(order[2]) < parseInt(amount)) {
showError(
"E-34: " +
(order[3] !== price
? lang.toast_NotExistOrder
: lang.toast_NoOrder)
);
return false;
if (order[3] == price && parseInt(order[2]) >= parseInt(amount)) {
isValidPriceOrAmount = true;
}
}
}
if (!isValidPriceOrAmount) {
showError("E-34: " + lang.toast_NotExistOrder);
return false;
}
if (!isValidAddress) {
showError("E-35: " + lang.toast_NoOrder);
return false;
Expand Down
9 changes: 6 additions & 3 deletions mobile/src/screens/Main/Orderbook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Orderlist from "./components/Orderlist";
import { useAuth } from "@app/context/AuthContext";
import { myOrders } from "@app/api/api";
import eventEmitter from "@app/api/eventEmitter";
import { useIsFocused } from "@react-navigation/native";

type IOrderUnit = [number, string, string, string]; // index, address, amount, price
interface IOrderData {
Expand All @@ -35,19 +36,21 @@ const Orderbook: React.FC = () => {
const [orderData, setOrderData] = useState<IOrderData>({});
const modal1 = useDisclose();
const lang = local.Main.Orderbook;
const { currentAddress, allAddresses, txResult, isLoading, setIsLoading } =
useAuth();
const { currentAddress, txResult, txStatus } = useAuth();

useEffect(() => {
myOrders();
}, [currentAddress, txResult, useIsFocused()]);

useEffect(() => {
const handleMyOrdersEvent = (res: any) => {
setOrderData(res.data);
};
eventEmitter.on("S2C/my-orders", handleMyOrdersEvent);
return () => {
eventEmitter.off("S2C/my-orders", handleMyOrdersEvent);
};
}, [currentAddress, txResult]);
}, []);

return (
<>
Expand Down
22 changes: 14 additions & 8 deletions mobile/src/screens/Main/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import {
VStack,
useColorMode,
} from "native-base";
import React, { useMemo, useState } from "react";
import React, { useEffect, useMemo, useState } from "react";
import ColorModeSetting from "./ColorModeSetting";
import LanguageSetting from "./LanguageSetting";
import NetworkSetting from "./NetworkSetting";
import SecuritySetting from "./SecuritySetting";
import local from "@app/utils/locales";
import { Image } from "react-native";
import About from "./About";
import { useIsFocused } from "@react-navigation/native";

const Settings: React.FC = () => {
const { bgColor, textColor } = useColors();
const [language, setLanguage] = useState("");
const lang = local.Main.Settings;
const isFocused = useIsFocused();

return (
<VStack
Expand All @@ -36,13 +38,17 @@ const Settings: React.FC = () => {
<Text fontSize="4xl">{lang.Settings}</Text>
</HStack>
</VStack>
<ScrollView>
<LanguageSetting setLanguage={setLanguage} />
<ColorModeSetting />
<SecuritySetting />
<NetworkSetting />
<About />
</ScrollView>
{isFocused ? (
<ScrollView>
<LanguageSetting setLanguage={setLanguage} />
<ColorModeSetting />
<SecuritySetting />
<NetworkSetting />
<About />
</ScrollView>
) : (
<></>
)}
</VStack>
);
};
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/screens/Main/Transaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Transaction: React.FC = () => {
<>
{histories.length ? (
<VStack flex={1}>
{histories?.map((tx, key) => (
{histories?.reverse().map((tx, key) => (
<Pressable
key={key}
onPress={() => {
Expand Down
2 changes: 2 additions & 0 deletions mobile/src/screens/Main/Wallet/TransferButtonFab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const TransferButtonFab: React.FC<IProps> = ({ onToggle }) => {
right: -1,
zIndex: 100,
}}
borderColor="white"
borderWidth="1"
></Box>
)}
</View>
Expand Down
Loading

0 comments on commit b376e92

Please sign in to comment.