Made current session stats widget in dashboard functional

This commit is contained in:
keannu125 2023-03-07 19:52:40 +08:00
parent fd7891359c
commit 6619163abe
5 changed files with 60 additions and 3 deletions

View file

@ -5,10 +5,16 @@ import IsNumber from "../IsNumber/IsNumber";
import { UpdateProduct } from "../../Api/Api"; import { UpdateProduct } from "../../Api/Api";
import { useQueryClient, useMutation } from "react-query"; import { useQueryClient, useMutation } from "react-query";
import { Product } from "../../../Interfaces/Interfaces"; import { Product } from "../../../Interfaces/Interfaces";
import { useDispatch } from "react-redux";
import {
this_session_increment_removed,
this_session_increment_added,
} from "../../../Features/Redux/Slices/TransactionsThisSessionSlice/TransactionsThisSessionSlice";
export default function StockRenderer(product: Product) { export default function StockRenderer(product: Product) {
const [stock, setStock] = useState(product.quantity); const [stock, setStock] = useState(product.quantity);
const [valueChanged, setValueChanged] = useState(false); const [valueChanged, setValueChanged] = useState(false);
const dispatch = useDispatch();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const mutation = useMutation({ const mutation = useMutation({
mutationFn: UpdateProduct, mutationFn: UpdateProduct,
@ -23,6 +29,15 @@ export default function StockRenderer(product: Product) {
name: product.name, name: product.name,
quantity: stock, quantity: stock,
}); });
if (stock > product.quantity) {
dispatch(
this_session_increment_added(Math.abs(stock - product.quantity))
);
} else {
dispatch(
this_session_increment_removed(Math.abs(stock - product.quantity))
);
}
setValueChanged(false); setValueChanged(false);
} }
useEffect(() => { useEffect(() => {

View file

@ -0,0 +1,22 @@
import { createSlice } from "@reduxjs/toolkit";
export const TransactionsThisSessionSlice = createSlice({
name: "Transactions",
initialState: {
added: 0,
removed: 0,
},
reducers: {
this_session_increment_added: (state, action) => {
state.added = action.payload;
},
this_session_increment_removed: (state, action) => {
state.removed = action.payload;
},
},
});
export const { this_session_increment_added, this_session_increment_removed } =
TransactionsThisSessionSlice.actions;
export default TransactionsThisSessionSlice.reducer;

View file

@ -43,6 +43,13 @@ export interface LoggedInUserState {
}; };
} }
export interface SessionTransactions {
session_transactions: {
added: number;
removed: number;
};
}
// Component Props Interfaces // Component Props Interfaces
export interface IconProps { export interface IconProps {

View file

@ -2,11 +2,13 @@ import { configureStore } from "@reduxjs/toolkit";
import LoginReducer from "../../../Features/Redux/Slices/Login/LoginSlice"; import LoginReducer from "../../../Features/Redux/Slices/Login/LoginSlice";
import LoggedInUserReducer from "../../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice"; import LoggedInUserReducer from "../../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import OldSessionReducer from "../../../Features/Redux/Slices/OldSession/OldSessionSlice"; import OldSessionReducer from "../../../Features/Redux/Slices/OldSession/OldSessionSlice";
import TransactionsThisSessionReducer from "../../../Features/Redux/Slices/TransactionsThisSessionSlice/TransactionsThisSessionSlice";
export default configureStore({ export default configureStore({
reducer: { reducer: {
logged_in: LoginReducer, logged_in: LoginReducer,
logged_in_user: LoggedInUserReducer, logged_in_user: LoggedInUserReducer,
old_session_checked: OldSessionReducer, old_session_checked: OldSessionReducer,
session_transactions: TransactionsThisSessionReducer,
}, },
}); });

View file

@ -15,7 +15,8 @@ import {
GetLowestStockedProduct, GetLowestStockedProduct,
GetProducts, GetProducts,
} from "../../Components/Api/Api"; } from "../../Components/Api/Api";
import { ProductLog } from "../../Interfaces/Interfaces"; import { ProductLog, SessionTransactions } from "../../Interfaces/Interfaces";
import { useSelector } from "react-redux";
export default function Dashboard() { export default function Dashboard() {
const logs = useQuery("logs", GetLogs, { retry: 0 }); const logs = useQuery("logs", GetLogs, { retry: 0 });
@ -27,6 +28,12 @@ export default function Dashboard() {
retry: 0, retry: 0,
} }
); );
const session_added = useSelector(
(state: SessionTransactions) => state.session_transactions.added
);
const session_removed = useSelector(
(state: SessionTransactions) => state.session_transactions.removed
);
if (logs.isLoading || products.isLoading || lowest_stock_product.isLoading) { if (logs.isLoading || products.isLoading || lowest_stock_product.isLoading) {
return ( return (
<div> <div>
@ -110,14 +117,18 @@ export default function Dashboard() {
Added Added
</p> </p>
</div> </div>
<p style={{ ...styles.text_white, ...styles.text_L }}>254</p> <p style={{ ...styles.text_white, ...styles.text_L }}>
{session_added}
</p>
<div style={styles.content_row}> <div style={styles.content_row}>
<ColoredCube size={32} color="#a44141" /> <ColoredCube size={32} color="#a44141" />
<p style={{ ...styles.text_white, ...styles.text_L }}> <p style={{ ...styles.text_white, ...styles.text_L }}>
Removed Removed
</p> </p>
</div> </div>
<p style={{ ...styles.text_white, ...styles.text_XL }}>118</p> <p style={{ ...styles.text_white, ...styles.text_XL }}>
{session_removed}
</p>
</div> </div>
<div <div
style={{ style={{