import React, { useEffect, useState } from 'react'; import { Button, Form, Grid, Header, Segment, Statistic } from 'semantic-ui-react'; import { API, showError, showInfo, showSuccess } from '../../helpers'; import { renderQuota } from '../../helpers/render'; const TopUp = () => { const [redemptionCode, setRedemptionCode] = useState(''); const [topUpLink, setTopUpLink] = useState(''); const [userQuota, setUserQuota] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); const [user, setUser] = useState({}); const topUp = async () => { if (redemptionCode === '') { showInfo('Please enter the recharge code!') return; } setIsSubmitting(true); try { const res = await API.post('/api/user/topup', { key: redemptionCode }); const { success, message, data } = res.data; if (success) { showSuccess('Recharge successful!'); setUserQuota((quota) => { return quota + data; }); setRedemptionCode(''); } else { showError(message); } } catch (err) { showError('Request failed'); } finally { setIsSubmitting(false); } }; const openTopUpLink = () => { if (!topUpLink) { showError('The super administrator did not set a recharge link!'); return; } let url = new URL(topUpLink); let username = user.username; let user_id = user.id; // add username and user_id to the topup link url.searchParams.append('username', username); url.searchParams.append('user_id', user_id); url.searchParams.append('transaction_id', crypto.randomUUID()); window.open(url.toString(), '_blank'); }; const getUserQuota = async ()=>{ let res = await API.get(`/api/user/self`); const {success, message, data} = res.data; if (success) { setUserQuota(data.quota); setUser(data); } else { showError(message); } } useEffect(() => { let status = localStorage.getItem('status'); if (status) { status = JSON.parse(status); if (status.top_up_link) { setTopUpLink(status.top_up_link); } } getUserQuota().then(); }, []); return (
Recharge quota
{ setRedemptionCode(e.target.value); }} />
{renderQuota(userQuota)} Remaining quota
); }; export default TopUp;