index.js ➔ Form   F
last analyzed

Complexity

Conditions 26

Size

Total Lines 110
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 110
c 0
b 0
f 0
rs 0
cc 26

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like index.js ➔ Form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import React from "react";
2
3
import bSecure from 'bsecure'
4
import { useToasts } from 'react-toast-notifications'
5
import {
6
  AppBar,
7
  TextField,
8
  Button,
9
} from "@mui/material";
10
import {
11
  HELPER
12
} from "../../util"
13
import './form.css'
14
15
const initialFormData = {
16
  name: "Default user",
17
  country_code: "92",
18
  phone: "3333333333",
19
  email: "[email protected]",
20
21
  order_id: "",
22
  sub_total: "500",
23
  discount: "50",
24
  total: "450",
25
  client_id: "",
26
  client_secret: "",
27
  client_env: "live",
28
}
29
30
let bsecure 
31
32
export default function Form() {
33
  const { addToast } = useToasts();
34
35
  const [formData, updateFormData] = React.useState(initialFormData);
36
  const handleChange = (e) => {
37
    updateFormData({
38
      ...formData,
39
      [e.target.name]: e.target.value.trim()
40
    });
41
  };
42
43
  
44
45
  const getAccesToken = async () => {
46
    let result = await bsecure.authorize();
47
    console.log(result)
48
    const { access_token } = result;
49
    if(HELPER.isEmpty(access_token)){
50
      addToast(result?.message, { appearance: 'error' });
51
    }else{
52
      processOrder()
53
    }
54
  };
55
56
  const handleSubmit = (e) => {
57
    e.preventDefault()
58
    bsecure = new bSecure({
59
      client_id: formData.client_id,
60
      client_secret: formData.client_secret,
61
      environment: formData.client_env,
62
    });
63
    getAccesToken();
64
  }; 
65
66
  const processOrder = async() => {
67
    const customer = {
68
      "name": formData?.name,
69
      "email": formData?.email,
70
      "country_code": formData?.country_code,
71
      "phone_number": formData?.phone_number,
72
    };
73
    const products = [
74
      {
75
      "id": "1",
76
      "name": "product-name",
77
      "sku": "product-sku",
78
      "quantity": 1,
79
      "price": formData?.sub_total,
80
      "sale_price": formData?.sub_total,
81
      "image": "product-image",
82
      "description": "product-description",
83
      "short_description": "product-short-description"
84
      }
85
    ];
86
    const charges = {
87
        "sub_total" : formData?.sub_total,
88
        "discount" : formData?.discount,
89
        "total" : formData?.total,
90
    }
91
    const order = bsecure.Order;
92
    order.setOrderId(formData?.order_id)
93
    order.setCharges(charges);
94
    order.setCustomer(customer);
95
    order.setCartItems(products);
96
    let result = await order.createOrder();
97
    const { checkout_url } = result.body;
98
    if(!HELPER.isEmpty(checkout_url)){
99
      window.open(checkout_url, '_blank');
100
    }else{
101
      addToast(result?.message, { appearance: 'error' });
102
    }
103
    return result;
104
  }
105
106
  return (
107
    <div className="Form">
108
      <AppBar>
109
        <h3>bSecure</h3>
110
      </AppBar>
111
      <form style={{ marginTop: 80, marginBottom: 20 }}>
112
        <div className="heading">Customer Detail</div>
113
        <div className="customer-info">
114
          <TextField onChange={handleChange} required defaultValue={formData?.name} label="Name" name="name" variant="outlined" />
115
          <TextField onChange={handleChange} required defaultValue={formData?.country_code} label="Country Code" name="country_code" variant="outlined" />
116
          <TextField onChange={handleChange} required defaultValue={formData?.phone} label="Phone" name="phone" variant="outlined" />
117
          <TextField onChange={handleChange} required defaultValue={formData?.email} label="Email" name="email" variant="outlined" />
118
        </div>
119
        <br />
120
        <div className="heading">Order Detail</div>
121
        <div className="order-info">
122
          <TextField onChange={handleChange} required defaultValue={formData?.order_id} label="Ordre id" name="order_id" variant="outlined" />
123
          <TextField onChange={handleChange} required defaultValue={formData?.sub_total} label="Subtotal amount" name="sub_total" variant="outlined" />
124
          <TextField onChange={handleChange} required defaultValue={formData?.discount} label="Discount" name="discount" variant="outlined" />
125
          <TextField onChange={handleChange} required defaultValue={formData?.total} label="Total amount" name="total" variant="outlined" />
126
        </div>
127
        <br />
128
        <div className="heading">Merchant Detail</div>
129
        <div className="order-info">
130
          <TextField onChange={handleChange} required defaultValue={formData?.client_id} label="Client ID" name="client_id" variant="outlined" />
131
          <TextField onChange={handleChange} required defaultValue={formData?.client_secret} label="Client Secret" name="client_secret" variant="outlined" />
132
          <TextField onChange={handleChange} required defaultValue={formData?.client_env} label="Client Environment" name="client_env" variant="outlined"  />
133
        </div>
134
135
        <Button type="button" variant="contained" color="primary" onClick={handleSubmit}>
136
          Pay via bsecure
137
        </Button>
138
      </form>
139
    </div>
140
  );
141
}
142