Skip to content

Commit

Permalink
changes made to emailService and other files as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
niccolopaganini committed Nov 13, 2023
1 parent f297691 commit c39b368
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 9 deletions.
3 changes: 2 additions & 1 deletion www/js/control/LogPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useTranslation } from 'react-i18next';
import { FlashList } from '@shopify/flash-list';
import moment from 'moment';
import AlertBar from './AlertBar';
import { sendEmail } from './emailService';

type loadStats = { currentStart: number; gotMaxIndex: boolean; reachedEnd: boolean };

Expand Down Expand Up @@ -96,7 +97,7 @@ const LogPage = ({ pageVis, setPageVis }) => {
};

const emailLog = function () {
EmailHelper.sendEmail('loggerDB');
sendEmail('loggerDB');
};

const separator = () => <View style={{ height: 8 }} />;
Expand Down
6 changes: 3 additions & 3 deletions www/js/control/ProfileSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import useAppConfig from '../useAppConfig';
import AlertBar from './AlertBar';
import DataDatePicker from './DataDatePicker';
import PrivacyPolicyModal from './PrivacyPolicyModal';

import { sendEmail } from './emailService';
import { uploadFile } from './uploadService';
import ActionMenu from '../components/ActionMenu';
import SensedPage from './SensedPage';
Expand Down Expand Up @@ -44,7 +44,7 @@ const ProfileSettings = () => {

//angular services needed
const CarbonDatasetHelper = getAngularService('CarbonDatasetHelper');
const EmailHelper = getAngularService('EmailHelper');
//const EmailHelper = getAngularService('EmailHelper');
const NotificationScheduler = getAngularService('NotificationScheduler');
const ControlHelper = getAngularService('ControlHelper');

Expand Down Expand Up @@ -256,7 +256,7 @@ const ProfileSettings = () => {

const emailLog = function () {
// Passing true, we want to send logs
EmailHelper.sendEmail('loggerDB');
sendEmail('loggerDB');
};

async function updatePrefReminderTime(storeNewVal = true, newTime) {
Expand Down
4 changes: 2 additions & 2 deletions www/js/control/SensedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getAngularService } from '../angular-react-helper';
import { useTranslation } from 'react-i18next';
import { FlashList } from '@shopify/flash-list';
import moment from 'moment';
import EmailHelper from './emailService';
import { sendEmail } from './emailService';

const SensedPage = ({ pageVis, setPageVis }) => {
const { t } = useTranslation();
Expand All @@ -17,7 +17,7 @@ const SensedPage = ({ pageVis, setPageVis }) => {
const [entries, setEntries] = useState([]);

const emailCache = function () {
EmailHelper.sendEmail('userCacheDB');
sendEmail('userCacheDB');
};

async function updateEntries() {
Expand Down
105 changes: 105 additions & 0 deletions www/js/control/emailService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React, { useEffect, useState } from 'react';
import i18next from "i18next";
import { logInfo, logDebug, displayError } from "../plugin/logger";
//import 'cordova-plugin-email-composer';


// Separated functions here

/*
function getEmailConfig() {
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));
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));
resolve(emailConfigData.address);
} catch (err) {
displayError(err, "Error while reading default email config");
reject(err);
}
}
});
}
*/

async function hasAccount(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
window['cordova'].plugins['email'].hasAccount(hasAct => {
resolve(hasAct);
});
});
}

export async function sendEmail(database: string) {
let parentDir = "unknown";

if (window['ionic'].Platform.isIOS() && !(await hasAccount())) { //check in iOS for configuration of email thingy
alert(i18next.t('email-service.email-account-not-configured'));
return;
}

if (window['ionic'].Platform.isAndroid()) {
parentDir = "app://databases";
}

if (window['ionic'].Platform.isIOS()) {
alert(i18next.t('email-service.email-account-mail-app'));
console.log(window['cordova'].file.dataDirectory);
parentDir = window['cordova'].file.dataDirectory + "../LocalDatabase";
}

if (parentDir === 'unknown') {
alert('parentDir unexpectedly = ' + parentDir + '!');
}

logInfo('Going to email ' + database);
parentDir = parentDir + '/' + database;

alert(i18next.t('email-service.going-to-email', { parentDir: parentDir }));

let emailConfig = `[email protected]`; //remember to change it to Shankari's

let emailData = {
to: emailConfig,
attachments: [parentDir],
subject: i18next.t('email-service.email-log.subject-logs'),
body: i18next.t('email-service.email-log.body-please-fill-in-what-is-wrong')
};

window['cordova'].plugins['email'].open(emailData, () => {
logInfo('Email app closed while sending, ' + JSON.stringify(emailData) + ' not sure if we should do anything');
});
}

/*
function EmailHelper() {
const [emailConfig, setEmailConfig];
useEffect(() => {
}, [emailConfig]);
// My export component here
return (
<div>
<button onClick={() => sendEmail}>
Send Email //make sure this is from the translate thingy
</button>
</div>
);
}
export default EmailHelper; //maybe this is a good option qmark - I think so?
*/
3 changes: 0 additions & 3 deletions www/json/emailConfig.json.sample

This file was deleted.

0 comments on commit c39b368

Please sign in to comment.