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: `Withdrew $${Math.min(styleState.style.withdrawModalInputs.amount, playerState.player.credits)} 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 (
close()} style={{position: 'absolute', top: '20px', right: '20px'}}/>

Manage (In-Game) Credits:

You currently have: ${playerState.player.credits}

{ styleState.style.displayDepositModal && styleState.style.inlineAlertText.length > 0 && {styleState.style.inlineAlertText}} { !styleState.style.displayDepositModal && } { styleState.style.displayDepositModal && (
{onChangeDeposit(e, 'name')}} value={styleState.style.depositModalInputs.name} placeholder="John Doe"/> {onChangeDeposit(e, 'card')}} value={styleState.style.depositModalInputs.card} placeholder="2333 9298 9821 1111"/> {onChangeDeposit(e, 'expire')}} value={styleState.style.depositModalInputs.expire} placeholder="07/24"/> {onChangeDeposit(e, 'ccv')}} value={styleState.style.depositModalInputs.ccv} placeholder="337"/> {onChangeDeposit(e, 'amount')}} value={styleState.style.depositModalInputs.amount} placeholder="500"/>
)}
{ styleState.style.displayWithdrawModal && styleState.style.inlineAlertText.length > 0 && {styleState.style.inlineAlertText}} { !styleState.style.displayWithdrawModal && } { styleState.style.displayWithdrawModal && (
{onChangeWithdraw(e, 'citibank')}} value={styleState.style.withdrawModalInputs.citibank} placeholder="Swedbank"/> {onChangeWithdraw(e, 'iban')}} value={styleState.style.withdrawModalInputs.iban} placeholder="SE45 5000 0000 0583 9825 7466"/> {onChangeWithdraw(e, 'bic')}} value={styleState.style.withdrawModalInputs.bic} placeholder="SWEDSESSXXX"/> {onChangeWithdraw(e, 'beneficiary')}} value={styleState.style.withdrawModalInputs.beneficiary} placeholder="John Doe"/> {onChangeWithdraw(e, 'address')}} value={styleState.style.withdrawModalInputs.address} placeholder="Landsvägen 40, Sundbyberg"/> {onChangeWithdraw(e, 'amount')}} value={styleState.style.withdrawModalInputs.amount} placeholder="500"/>
)}
) function onChangeDeposit(e, what) { dispatch(setStyle({ ...styleState.style, depositModalInputs: { name: what === 'name' ? e.target.value : styleState.style.depositModalInputs.name, card: what === 'card' ? e.target.value : styleState.style.depositModalInputs.card, expire: what === 'expire' ? e.target.value : styleState.style.depositModalInputs.expire, ccv: what === 'ccv' ? e.target.value : styleState.style.depositModalInputs.ccv, amount: what === 'amount' ? e.target.value : styleState.style.depositModalInputs.amount, } })) } function onChangeWithdraw(e, what) { dispatch(setStyle({ ...styleState.style, withdrawModalInputs: { citibank: what === 'citibank' ? e.target.value : styleState.style.depositModalInputs.citibank, iban: what === 'iban' ? e.target.value : styleState.style.depositModalInputs.iban, bic: what === 'bic' ? e.target.value : styleState.style.depositModalInputs.bic, beneficiary: what === 'beneficiary' ? e.target.value : styleState.style.depositModalInputs.beneficiary, address: what === 'address' ? e.target.value : styleState.style.depositModalInputs.address, amount: what === 'amount' ? e.target.value : styleState.style.depositModalInputs.amount, } })) } } export default ManageCredits