CEC-758 Add SMS send page and result (#173)

* Add SMS send and result pages

* Update snapshot

Co-authored-by: jwu-fisker <jwu@fiskerinc.com>
This commit is contained in:
arpanetus
2022-08-02 01:11:11 +06:00
committed by GitHub
parent b70afa5312
commit 00af90902e
13 changed files with 1307 additions and 72 deletions

View File

@@ -0,0 +1,63 @@
import React, { useContext, useState } from "react";
import api from "../../services/smsAPI";
const SMSContext = React.createContext();
export class SMS {
constructor(message, ICCID, isAwaited) {
/** @type {string} */
this.messageText = message;
/** @type {string} */
this.ICCID = ICCID;
/** @type {boolean} */
this.await = isAwaited;
}
}
/**
* @param {SMS} data
*/
const validateSend = (data) => {
if (!data.messageText) throw new Error("message is required");
if (!data.ICCID) throw new Error("ICCID is required");
};
export const SMSProvider = ({ children }) => {
const [busy, setBusy] = useState(false);
/**
* @param {SMS} data
* @param {*} token
* @returns
*/
const sendSMS = async (data, token) => {
try {
setBusy(true);
validateSend(data);
const result = await api.send(data, token);
if (result.error) {
throw new Error(`Send message error. ${result.message}`);
}
return result;
} finally {
setBusy(false);
}
};
return (
<SMSContext.Provider
value={{
busy,
sendSMS,
}}
>
{children}
</SMSContext.Provider>
);
};
export const useSMSContext = () => useContext(SMSContext);