Commit 5b115ea3 by Mac Stephens

Emails are working with the server, added message and fixed disconnect problem,…

Emails are working with the server, added message and fixed disconnect problem, added ini, working on logging
parent 1ae1c49a
......@@ -5,7 +5,8 @@ uses
WEBLib.Forms,
View.Home in 'View.Home.pas' {FHome: TWebForm} {*.html},
View.Compliance in 'View.Compliance.pas' {FCompliance: TWebForm} {*.html},
View.ContactUs in 'View.ContactUs.pas' {FContactUs: TWebForm} {*.html};
View.ContactUs in 'View.ContactUs.pas' {FContactUs: TWebForm} {*.html},
uSmtpJsWrapper in 'uSmtpJsWrapper.pas';
{$R *.res}
......
......@@ -5,7 +5,7 @@
<FrameworkType>VCL</FrameworkType>
<MainSource>TMSWebTemplate.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Release</Config>
<Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Application</AppType>
......@@ -114,6 +114,7 @@
<FormType>dfm</FormType>
<DesignClass>TWebForm</DesignClass>
</DCCReference>
<DCCReference Include="uSmtpJsWrapper.pas"/>
<None Include="TMSWebTemplate.html"/>
<None Include="css\App.css"/>
<None Include="images\bridgewoman.jpg">
......@@ -145,6 +146,37 @@
<ResourceId>JpgImage_5</ResourceId>
</None>
<None Include="index.html"/>
<None Include="images\bridgewoman3.jpg"/>
<None Include="TMSWeb\Debug\css\App.css"/>
<None Include="TMSWeb\Debug\images\bridgewoman.jpg"/>
<None Include="TMSWeb\Debug\images\bridgewoman2.jpg"/>
<None Include="TMSWeb\Debug\images\coins.jpg"/>
<None Include="TMSWeb\Debug\images\currency.png"/>
<None Include="TMSWeb\Debug\images\efx-location.jpg"/>
<None Include="TMSWeb\Debug\images\efxLogo.png"/>
<None Include="TMSWeb\Debug\images\nataliepicture.jpg"/>
<None Include="TMSWeb\Debug\index.html"/>
<None Include="TMSWeb\Debug\TMSWebTemplate.js"/>
<None Include="TMSWeb\Debug\TMSWebTemplate.js.map"/>
<None Include="TMSWeb\Debug\View.Compliance.html"/>
<None Include="TMSWeb\Debug\View.ContactUs.html"/>
<None Include="TMSWeb\Debug\View.Home.html"/>
<None Include="TMSWeb\Release\css\App.css"/>
<None Include="TMSWeb\Release\images\bridgewoman.jpg"/>
<None Include="TMSWeb\Release\images\bridgewoman2.jpg"/>
<None Include="TMSWeb\Release\images\coins.jpg"/>
<None Include="TMSWeb\Release\images\currency.png"/>
<None Include="TMSWeb\Release\images\efx-location.jpg"/>
<None Include="TMSWeb\Release\images\efxLogo.png"/>
<None Include="TMSWeb\Release\images\nataliepicture.jpg"/>
<None Include="TMSWeb\Release\index.html"/>
<None Include="TMSWeb\Release\TMSWebTemplate.js"/>
<None Include="TMSWeb\Release\View.Compliance.html"/>
<None Include="TMSWeb\Release\View.ContactUs.html"/>
<None Include="TMSWeb\Release\View.Home.html"/>
<None Include=".vscode\launch.json"/>
<None Include=".vscode\tasks.json"/>
<None Include=".vscode\tempsettings.json"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
......
......@@ -11,7 +11,6 @@ object FContactUs: TFContactUs
FormStyle = fsNormal
Height = 1186
Left = 0
OnCreate = FContactUsCreate
TabOrder = 0
Top = 0
Width = 1138
......@@ -183,7 +182,6 @@ object FContactUs: TFContactUs
object XDataWebClient1: TXDataWebClient
Connection = XDataWebConnection1
OnError = XDataWebClient1Error
OnRequest = XDataWebClient1Request
Left = 300
Top = 926
end
......
......@@ -7,7 +7,7 @@ uses
WEBLib.Forms, WEBLib.Dialogs, XData.Web.Client, XData.Web.Connection, Data.DB,
XData.Web.JsonDataset, XData.Web.Dataset, Vcl.Controls, Vcl.Grids,
WEBLib.DBCtrls, WEBLib.DB, WEBLib.Grids, Vcl.StdCtrls, WEBLib.StdCtrls,
WEBLib.ExtCtrls, WEBLib.CDS, WEBLib.REST;
WEBLib.ExtCtrls, WEBLib.CDS, WEBLib.REST, WebLib.RegularExpressions, WEBLib.Menus, WEBLib.Toast; // Add the missing import statement
type
TFContactUs = class(TWebForm)
......@@ -23,11 +23,10 @@ type
XDataWebClient1: TXDataWebClient;
procedure WebFormClose(Sender: TObject; var Action: TCloseAction);
procedure btnSubmitClick(Sender: TObject);
procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
procedure FContactUsCreate(Sender: TObject);
procedure XDataWebConnection1Connect(Sender: TObject);
procedure XDataWebClient1Error(error: TXDataClientError);
procedure XDataWebConnection1Error(error: TXDataWebConnectionError);
procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
private
{ Private declarations }
function IsInputValid: Boolean;
......@@ -44,49 +43,31 @@ implementation
{$R *.dfm}
const
tstSuccess = 'Email sent successfully';
tstError = 'Error when attempting to send: ';
procedure TFContactUs.XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
begin
// var
// basicAuthStr: string;
// begin
// asm
// var auth = 'admin:password';
// basicAuthStr = 'Basic ' + window.btoa(auth);
// end;
// Args.Request.Headers.SetValue('Authorization', basicAuthStr);
end;
procedure TFContactUs.XDataWebConnection1Error(error: TXDataWebConnectionError);
// coder: TBase64Encoding;
begin
ShowMessage('Unauthorized');
// try
// coder := TBase64Encoding.Create;
// basicAuthStr := coder.Encode( 'admin:password' );
// finally
// coder.Free;
// end;
// Args.Request.Headers.SetValue( 'Authorization', 'Basic ' + basicAuthStr );
end;
procedure TFContactUs.XDataWebConnection1Connect(Sender: TObject);
begin
console.log('Connected to XData server');
end;
procedure TFContactUs.XDataWebClient1Error(Error: TXDataClientError);
begin
console.log('Error: ', Error.ErrorMessage, ' RequestId: ', Error.RequestId, ' Code: ', Error.ErrorCode, ' Request Url: ', Error.RequestUrl);
end;
procedure TFContactUs.FContactUsCreate(Sender: TObject);
begin
console.log('ContactUs form created');
XDataWebConnection1.Connected := True;
end;
procedure TFContactUs.btnSubmitClick(Sender: TObject);
begin
if IsInputValid then
begin
SendEmail;
XDataWebConnection1.Connected := True;
end
else
begin
......@@ -94,12 +75,16 @@ begin
end;
end;
procedure TFContactUs.WebFormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
function TFContactUs.IsEmailValid(AEmail: String): Boolean;
const
CPattern = '^\w+([\.-]?w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$';
begin
asm
var regex = new RegExp('^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$');
@Result = regex.test(AEmail);
end;
Result := TRegEx.IsMatch( AEmail, CPattern );
end;
function TFContactUs.IsInputValid: Boolean;
......@@ -118,6 +103,25 @@ begin
end;
procedure TFContactUs.XDataWebConnection1Error(error: TXDataWebConnectionError);
begin
ShowMessage('Error Connecting to XData Server');
end;
procedure TFContactUs.XDataWebConnection1Connect(Sender: TObject);
begin
console.log('Connected to XData server, Sending Email');
SendEmail();
end;
procedure TFContactUs.XDataWebClient1Error(Error: TXDataClientError);
begin
console.log('Error: ', Error.ErrorMessage, ' RequestId: ', Error.RequestId, ' Code: ', Error.ErrorCode, ' Request Url: ', Error.RequestUrl);
end;
procedure TFContactUs.SendEmail;
var
xdcResponse: TXDataClientResponse;
......@@ -125,19 +129,30 @@ begin
try
xdcResponse := await(XDataWebClient1.RawInvokeAsync('IEmailService.SendEmail',
[edtName.Text, edtEmail.Text, edtSubject.Text, memoMessage.Text]));
console.log('Email sent successfully');
ShowMessage('Your message has been sent successfully. Please check your email for a response in the next 48 hours.');
// Clears the input fields after successful email sending
edtName.Text := '';
edtEmail.Text := '';
edtSubject.Text := '';
memoMessage.Text := '';
// Disconnects after sending the email
XDataWebConnection1.Connected := False;
except
on E: EXDataClientRequestException do
console.log('Error when attempting to send: ' + E.Message);
begin
ShowMessage('Error when attempting to send: ' + E.Message);
// Optionally disconnect even on error if you intend to reset the connection state
XDataWebConnection1.Connected := False;
end;
end;
end;
procedure TFContactUs.WebFormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
initialization
RegisterClass(TFContactUs);
end.
\ No newline at end of file
unit uSmtpJsWrapper;
interface
type
TSmtpJsWrapper = class
class procedure Send(const ARecipient, AName, ASender, ASubject, ABody: string);
end;
implementation
class procedure TSmtpJsWrapper.Send(const ARecipient, AName, ASender, ASubject, ABody: string);
var
LBody: string;
begin
LBody := AName + #10 + ABody;
// The PAS2JS compiler flag ignores the Delphi compliler, and uses the JavaScript compiler instead
// This allows for the code to run without errors
{$IFDEF PAS2JS}
asm
Email.send({
SecureToken : "...",
To : ARecipient,
From : ASender,
Subject : ASubject,
Body : LBody
}).then(
message => alert(message)
);
end;
{$ENDIF}
end;
end.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment