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 { useQueryClient, useMutation } from "react-query";
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) {
const [stock, setStock] = useState(product.quantity);
const [valueChanged, setValueChanged] = useState(false);
const dispatch = useDispatch();
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: UpdateProduct,
@ -23,6 +29,15 @@ export default function StockRenderer(product: Product) {
name: product.name,
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);
}
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
export interface IconProps {

View file

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

View file

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