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 ...@@ -5,7 +5,8 @@ uses
WEBLib.Forms, WEBLib.Forms,
View.Home in 'View.Home.pas' {FHome: TWebForm} {*.html}, View.Home in 'View.Home.pas' {FHome: TWebForm} {*.html},
View.Compliance in 'View.Compliance.pas' {FCompliance: 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} {$R *.res}
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<FrameworkType>VCL</FrameworkType> <FrameworkType>VCL</FrameworkType>
<MainSource>TMSWebTemplate.dpr</MainSource> <MainSource>TMSWebTemplate.dpr</MainSource>
<Base>True</Base> <Base>True</Base>
<Config Condition="'$(Config)'==''">Release</Config> <Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win32</Platform> <Platform Condition="'$(Platform)'==''">Win32</Platform>
<TargetedPlatforms>1</TargetedPlatforms> <TargetedPlatforms>1</TargetedPlatforms>
<AppType>Application</AppType> <AppType>Application</AppType>
...@@ -114,6 +114,7 @@ ...@@ -114,6 +114,7 @@
<FormType>dfm</FormType> <FormType>dfm</FormType>
<DesignClass>TWebForm</DesignClass> <DesignClass>TWebForm</DesignClass>
</DCCReference> </DCCReference>
<DCCReference Include="uSmtpJsWrapper.pas"/>
<None Include="TMSWebTemplate.html"/> <None Include="TMSWebTemplate.html"/>
<None Include="css\App.css"/> <None Include="css\App.css"/>
<None Include="images\bridgewoman.jpg"> <None Include="images\bridgewoman.jpg">
...@@ -145,6 +146,37 @@ ...@@ -145,6 +146,37 @@
<ResourceId>JpgImage_5</ResourceId> <ResourceId>JpgImage_5</ResourceId>
</None> </None>
<None Include="index.html"/> <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"> <BuildConfiguration Include="Base">
<Key>Base</Key> <Key>Base</Key>
</BuildConfiguration> </BuildConfiguration>
......
...@@ -11,7 +11,6 @@ object FContactUs: TFContactUs ...@@ -11,7 +11,6 @@ object FContactUs: TFContactUs
FormStyle = fsNormal FormStyle = fsNormal
Height = 1186 Height = 1186
Left = 0 Left = 0
OnCreate = FContactUsCreate
TabOrder = 0 TabOrder = 0
Top = 0 Top = 0
Width = 1138 Width = 1138
...@@ -183,7 +182,6 @@ object FContactUs: TFContactUs ...@@ -183,7 +182,6 @@ object FContactUs: TFContactUs
object XDataWebClient1: TXDataWebClient object XDataWebClient1: TXDataWebClient
Connection = XDataWebConnection1 Connection = XDataWebConnection1
OnError = XDataWebClient1Error OnError = XDataWebClient1Error
OnRequest = XDataWebClient1Request
Left = 300 Left = 300
Top = 926 Top = 926
end end
......
...@@ -7,7 +7,7 @@ uses ...@@ -7,7 +7,7 @@ uses
WEBLib.Forms, WEBLib.Dialogs, XData.Web.Client, XData.Web.Connection, Data.DB, WEBLib.Forms, WEBLib.Dialogs, XData.Web.Client, XData.Web.Connection, Data.DB,
XData.Web.JsonDataset, XData.Web.Dataset, Vcl.Controls, Vcl.Grids, XData.Web.JsonDataset, XData.Web.Dataset, Vcl.Controls, Vcl.Grids,
WEBLib.DBCtrls, WEBLib.DB, WEBLib.Grids, Vcl.StdCtrls, WEBLib.StdCtrls, 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 type
TFContactUs = class(TWebForm) TFContactUs = class(TWebForm)
...@@ -23,11 +23,10 @@ type ...@@ -23,11 +23,10 @@ type
XDataWebClient1: TXDataWebClient; XDataWebClient1: TXDataWebClient;
procedure WebFormClose(Sender: TObject; var Action: TCloseAction); procedure WebFormClose(Sender: TObject; var Action: TCloseAction);
procedure btnSubmitClick(Sender: TObject); procedure btnSubmitClick(Sender: TObject);
procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
procedure FContactUsCreate(Sender: TObject);
procedure XDataWebConnection1Connect(Sender: TObject); procedure XDataWebConnection1Connect(Sender: TObject);
procedure XDataWebClient1Error(error: TXDataClientError); procedure XDataWebClient1Error(error: TXDataClientError);
procedure XDataWebConnection1Error(error: TXDataWebConnectionError); procedure XDataWebConnection1Error(error: TXDataWebConnectionError);
procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
private private
{ Private declarations } { Private declarations }
function IsInputValid: Boolean; function IsInputValid: Boolean;
...@@ -44,49 +43,31 @@ implementation ...@@ -44,49 +43,31 @@ implementation
{$R *.dfm} {$R *.dfm}
const
tstSuccess = 'Email sent successfully';
tstError = 'Error when attempting to send: ';
procedure TFContactUs.XDataWebConnection1Request(Args: TXDataWebConnectionRequest); procedure TFContactUs.XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
begin
// var // var
// basicAuthStr: string; // basicAuthStr: string;
// begin // coder: TBase64Encoding;
// asm
// var auth = 'admin:password';
// basicAuthStr = 'Basic ' + window.btoa(auth);
// end;
// Args.Request.Headers.SetValue('Authorization', basicAuthStr);
end;
procedure TFContactUs.XDataWebConnection1Error(error: TXDataWebConnectionError);
begin begin
ShowMessage('Unauthorized'); // try
// coder := TBase64Encoding.Create;
// basicAuthStr := coder.Encode( 'admin:password' );
// finally
// coder.Free;
// end;
// Args.Request.Headers.SetValue( 'Authorization', 'Basic ' + basicAuthStr );
end; 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); procedure TFContactUs.btnSubmitClick(Sender: TObject);
begin begin
if IsInputValid then if IsInputValid then
begin begin
SendEmail; XDataWebConnection1.Connected := True;
end end
else else
begin begin
...@@ -94,12 +75,16 @@ begin ...@@ -94,12 +75,16 @@ begin
end; end;
end; end;
procedure TFContactUs.WebFormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
function TFContactUs.IsEmailValid(AEmail: String): Boolean; function TFContactUs.IsEmailValid(AEmail: String): Boolean;
const
CPattern = '^\w+([\.-]?w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$';
begin begin
asm Result := TRegEx.IsMatch( AEmail, CPattern );
var regex = new RegExp('^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$');
@Result = regex.test(AEmail);
end;
end; end;
function TFContactUs.IsInputValid: Boolean; function TFContactUs.IsInputValid: Boolean;
...@@ -118,6 +103,25 @@ begin ...@@ -118,6 +103,25 @@ begin
end; 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; procedure TFContactUs.SendEmail;
var var
xdcResponse: TXDataClientResponse; xdcResponse: TXDataClientResponse;
...@@ -125,19 +129,30 @@ begin ...@@ -125,19 +129,30 @@ begin
try try
xdcResponse := await(XDataWebClient1.RawInvokeAsync('IEmailService.SendEmail', xdcResponse := await(XDataWebClient1.RawInvokeAsync('IEmailService.SendEmail',
[edtName.Text, edtEmail.Text, edtSubject.Text, memoMessage.Text])); [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 except
on E: EXDataClientRequestException do 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;
end; end;
procedure TFContactUs.WebFormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
initialization initialization
RegisterClass(TFContactUs); RegisterClass(TFContactUs);
end. 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