import React from 'react' import { useSelector, useDispatch } from 'react-redux' import { setStyle } from '../redux/reducers/styleSlice'; import { AiOutlineClose } from 'react-icons/ai'; import { setPlayer } from '../redux/reducers/playerSlice'; import axios from 'axios'; const ManageCredits = () => { const playerState = useSelector(state => state.player); const styleState = useSelector(state => state.style); const dispatch = useDispatch(); function close(action = '') { dispatch(setStyle({ ...styleState.style, displayManageCreditsScreen: false, displayDepositModal: false, displayWithdrawModal: false, depositModalInputs: { name: '', card: '', expire: '', ccv: '', amount: '', }, displayWithdrawModal: false, withdrawModalInputs: { citibank: '', iban: '', bic: '', beneficiary: '', address: '', amount: '', }, inlineAlertText: '', notification: action === 'deposit' ? { show: true, text: `Deposited $${styleState.style.depositModalInputs.amount} successfully`, status: 'success', } : action === 'withdraw' ? { show: true, text: `Withdrawed $${styleState.style.withdrawModalInputs.amount} successfully`, status: 'success', } : { show: false, text: '', status: '' }, })) } function openDepositModal() { dispatch(setStyle({ ...styleState.style, displayDepositModal: true, displayWithdrawModal: false, inlineAlertText: '', })) } function openWithdrawModal() { dispatch(setStyle({ ...styleState.style, displayDepositModal: false, displayWithdrawModal: true, inlineAlertText: '', })) } function buyCredits() { axios.post(`/api/postgre`, { action: 'deposit', session_id: localStorage.CAESSINO_SESSION_ID, data: styleState.style.depositModalInputs }) .then(res => { if (res.data?.success) { dispatch(setPlayer({ ...playerState.player, credits: res.data.credits, })) close('deposit'); } else { dispatch(setStyle({ ...styleState.style, displayManageCreditsScreen: true, inlineAlertText: res.data?.message, })); } }) } function sellCredits() { axios.post(`/api/postgre`, { action: 'withdraw', session_id: localStorage.CAESSINO_SESSION_ID, data: styleState.style.withdrawModalInputs }) .then(res => { if (res.data?.success) { dispatch(setPlayer({ ...playerState.player, credits: res.data.credits, })) close('withdraw'); } else { dispatch(setStyle({ ...styleState.style, displayManageCreditsScreen: true, inlineAlertText: res.data?.message, })); } }) } return (
You currently have: ${playerState.player.credits}