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:
63
src/components/Contexts/SMSContext.jsx
Normal file
63
src/components/Contexts/SMSContext.jsx
Normal 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);
|
||||
Reference in New Issue
Block a user