Skip to content
Snippets Groups Projects
Topup.js 1.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useState } from "react";
    
    import { useParams, Navigate } from "react-router-dom";
    
    import MemberPicker from "./member/MemberPicker";
    
    meskio's avatar
    meskio committed
    import { Form, Col, Row, Button } from "react-bootstrap";
    
    import Sender from "./Sender";
    
    meskio's avatar
    meskio committed
    import PriceEditor from "./PriceEditor";
    
    meskio's avatar
    meskio committed
    
    
    function Topup() {
    
      const { num } = useParams();
    
      const [member, setMember] = useState(null);
      const [amount, setAmount] = useState(0);
      const [comment, setComment] = useState("");
      const [transaction, setTransaction] = useState(null);
    
    meskio's avatar
    meskio committed
    
    
      if (transaction !== null) {
    
        return <Navigate to={"/transaction/" + transaction.ID} />;
    
    meskio's avatar
    meskio committed
      }
    
    meskio's avatar
    meskio committed
    
    
      const body = () => {
        if (member === null) {
          return null;
    
    meskio's avatar
    meskio committed
        }
    
        return {
          member: member.num,
    
    meskio's avatar
    meskio committed
          amount: amount,
    
          comment,
        };
      };
    
    meskio's avatar
    meskio committed
    
    
      return (
    
        <Sender url="/api/topup" body={body} onSuccess={setTransaction}>
    
          <MemberPicker
            member={member}
            onChange={setMember}
            num={parseInt(num)}
            allowDisabled
          />
    
          <Form.Group as={Row}>
            <Form.Label as="legend" column sm={2}>
              Recarga
            </Form.Label>
            <Col sm={10}>
    
    meskio's avatar
    meskio committed
              <PriceEditor value={amount} onChange={setAmount} />
    
            </Col>
          </Form.Group>
          <Form.Group as={Row}>
            <Form.Label as="legend" column sm={2}>
              Comentario
            </Form.Label>
            <Col sm={10}>
              <Form.Control
                placeholder="..."
                value={comment}
                onChange={(e) => setComment(e.target.value)}
              />
            </Col>
          </Form.Group>
          <Form.Group as={Row}>
            <Col sm={{ span: 10, offset: 2 }}>
    
    meskio's avatar
    meskio committed
              <Button type="submit" disabled={member === null || isNaN(amount)}>
    
                Recarga
              </Button>
            </Col>
          </Form.Group>
        </Sender>
    
    meskio's avatar
    meskio committed
    }
    
    export default Topup;