• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1590815032

16 Dec 2024 01:53PM UTC coverage: 73.522% (+0.7%) from 72.839%
1590815032

Pull #253

gitlab-ci

mineralsfree
feat: updated billing section in My Organization settings

Ticket: MEN-7466
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #253: MEN-7466-feat: updated billing section in My Organization settings

4257 of 6186 branches covered (68.82%)

Branch coverage included in aggregate %.

57 of 89 new or added lines in 11 files covered. (64.04%)

1 existing line in 1 file now uncovered.

40090 of 54132 relevant lines covered (74.06%)

22.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.33
/frontend/src/js/components/settings/CardSection.tsx
1
// Copyright 2020 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useState } from 'react';
15

16
import { Button } from '@mui/material';
17

18
import InfoText from '@northern.tech/common-ui/infotext';
19
import Loader from '@northern.tech/common-ui/loader';
20
import storeActions from '@northern.tech/store/actions';
21
import { Organization } from '@northern.tech/store/organizationSlice/types';
22
import { useAppDispatch } from '@northern.tech/store/store';
23
import { cancelUpgrade, startUpgrade } from '@northern.tech/store/thunks';
24
import { CardElement, useElements, useStripe } from '@stripe/react-stripe-js';
25

26
import stripeImage from '../../../assets/img/powered_by_stripe.png';
27

28
const { setSnackbar } = storeActions;
9✔
29

30
interface CardSectionProps {
31
  isSignUp: boolean;
32
  onClose?: () => void;
33
  organization: Organization;
34
  beforeCardSubmit?: () => Promise<void>;
35
  isValid?: boolean;
36
  onCardConfirmed: () => void;
37
}
38
const CardSection = ({ isSignUp, onClose, organization, onCardConfirmed, beforeCardSubmit, isValid = true }: CardSectionProps) => {
9✔
39
  const stripe = useStripe();
1✔
40
  const elements = useElements();
1✔
41
  const [errors, setErrors] = useState(false);
1✔
42
  const [loading, setLoading] = useState(false);
1✔
43
  const [empty, setEmpty] = useState(true);
1✔
44
  const dispatch = useAppDispatch();
1✔
45
  const setSnackbarMessage = (message: string) => dispatch(setSnackbar(message));
1✔
46
  const onCancel = () => {
1✔
NEW
47
    dispatch(cancelUpgrade(organization.id));
×
NEW
48
    if (onClose) {
×
NEW
49
      onClose();
×
50
    }
51
  };
52

53
  const confirmCard = async secret => {
1✔
54
    // Use elements.getElement to get a reference to the mounted Element.
55
    const cardElement = elements.getElement(CardElement);
×
56

57
    // Use your card Element with other Stripe.js APIs
58
    try {
×
59
      const result = await stripe.confirmCardSetup(secret, {
×
60
        payment_method: {
61
          card: cardElement
62
        }
63
      });
64

65
      if (result.error) {
×
NEW
66
        setSnackbarMessage(`Error while confirming card: ${result.error.message}`);
×
67
        onCancel();
×
68
      } else {
NEW
69
        setSnackbarMessage(`Card confirmed. Updating your account...`);
×
NEW
70
        onCardConfirmed();
×
71
      }
72
    } catch (error) {
73
      console.error(error);
×
NEW
74
      setSnackbarMessage(`Something went wrong while submitting the form. Please contact support.`);
×
75
      onCancel();
×
76
    }
77
  };
78

79
  const handleSubmit = async event => {
1✔
80
    event.preventDefault();
×
81
    setLoading(true);
×
NEW
82
    return (beforeCardSubmit ? beforeCardSubmit() : dispatch(startUpgrade(organization.id)).unwrap()).then(confirmCard).finally(() => setLoading(false));
×
83
  };
84

85
  const stripeElementChange = event => {
1✔
86
    setEmpty(false);
×
87
    if (event.complete) {
×
88
      // enable payment button
89
      setErrors(false);
×
90
    } else if (event.error) {
×
91
      // show validation to customer
92
      setErrors(true);
×
93
    }
94
  };
95

96
  return (
1✔
97
    <form className="margin-top-small" onSubmit={handleSubmit} onReset={onCancel}>
98
      <CardElement className="warning" onChange={event => stripeElementChange(event)} />
×
99
      {!!errors && <p className="warning">There is an error in the form. Please check that your details are correct</p>}
1!
100

101
      <div id="poweredByStripe">
102
        <div>All standard credit card fees apply (e.g. foreign transaction fee, if your card has one)</div>
103
        <img src={stripeImage} />
104
      </div>
105

106
      {isSignUp && <InfoText>Billing will be scheduled monthly, starting from today. You can cancel at any time.</InfoText>}
2✔
107

108
      <div className="flexbox center-aligned margin-top-small">
109
        {!isSignUp && (
1!
110
          <Button type="reset" disabled={loading} style={{ marginRight: 15 }}>
111
            Cancel
112
          </Button>
113
        )}
114
        <Button variant="contained" color="secondary" type="submit" disabled={errors || loading || empty || !isValid}>
3!
115
          {isSignUp ? 'Sign up' : 'Save'}
1!
116
        </Button>
117
      </div>
118
      <Loader show={loading} />
119
    </form>
120
  );
121
};
122

123
export default CardSection;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc