Skip to content

Commit

Permalink
Update emailService.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
niccolopaganini authored Nov 5, 2023
1 parent 6d3f976 commit 841d3ef
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions www/js/control/emailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ import { logInfo, logDebug, displayError } from "../plugin/logger";
import 'cordova-plugin-email-composer';

function getEmailConfig() {
return async () => {
return new Promise(async (resolve, reject) => {
try {
logInfo("About to get email config");
let url = "json/emailConfig.json";
let response = await fetch(url);
let emailConfigData = await response.json();
logDebug("emailConfigString = " + JSON.stringify(emailConfigData.address));
return emailConfigData.address;
resolve(emailConfigData.address);
} catch (err) {
try {
let url = "json/emailConfig.json.sample";
let response = await fetch(url);
let emailConfigData = await response.json();
logDebug("default emailConfigString = " + JSON.stringify(emailConfigData.address));
return emailConfigData.address;
resolve(emailConfigData.address);
} catch (err) {
displayError(err, "Error while reading default email config");
reject(err);
}
}
};
});
}

function hasAccount() {
async function hasAccount() {
return new Promise<boolean>((resolve, reject) => {
window['cordova'].plugins['email'].hasAccount(hasAct => {
resolve(hasAct);
Expand All @@ -37,7 +38,7 @@ function hasAccount() {
function sendEmail(database: string, emailConfig: string | null) {
let parentDir = "unknown";

if (window['ionic'].Platform.isIOS() && !hasAct) {
if (window['ionic'].Platform.isIOS() && !(await hasAccount())) {
alert(i18next.t('email-service.email-account-not-configured'));
return;
}
Expand Down Expand Up @@ -76,13 +77,14 @@ function sendEmail(database: string, emailConfig: string | null) {
function EmailHelper() {
const [emailConfig, setEmailConfig] = useState<string | null>(null);

// Fetch email config when the component mounts
useEffect(() => {
getEmailConfig().then((config) => {
setEmailConfig(config);
}).catch((err) => {
displayError(err, "Error while reading email config");
});
getEmailConfig()
.then((config) => {
setEmailConfig(config);
})
.catch((err) => {
displayError(err, "Error while reading email config");
});
}, []);

return (
Expand All @@ -93,3 +95,5 @@ function EmailHelper() {
</div>
);
}

export default EmailHelper;

0 comments on commit 841d3ef

Please sign in to comment.