2022-07-07 05:19:05 +08:00
< script >
import { slide } from "svelte/transition";
import ApiClient from "@/utils/ApiClient";
import CommonHelper from "@/utils/CommonHelper";
2022-07-19 00:44:10 +08:00
import { pageTitle } from "@/stores/app";
2022-08-15 00:30:45 +08:00
import { setErrors } from "@/stores/errors";
2022-07-07 05:19:05 +08:00
import { addSuccessToast } from "@/stores/toasts";
2022-08-16 12:36:15 +08:00
import tooltip from "@/actions/tooltip";
2022-08-09 21:16:09 +08:00
import PageWrapper from "@/components/base/PageWrapper.svelte";
2022-07-07 05:19:05 +08:00
import Field from "@/components/base/Field.svelte";
import ObjectSelect from "@/components/base/ObjectSelect.svelte";
import RedactedPasswordInput from "@/components/base/RedactedPasswordInput.svelte";
import SettingsSidebar from "@/components/settings/SettingsSidebar.svelte";
2022-08-15 00:30:45 +08:00
import EmailTemplateAccordion from "@/components/settings/EmailTemplateAccordion.svelte";
2022-08-21 19:30:36 +08:00
import EmailTestPopup from "@/components/settings/EmailTestPopup.svelte";
2022-07-07 05:19:05 +08:00
const tlsOptions = [
2022-08-15 00:30:45 +08:00
{ label : "Auto (StartTLS)" , value : false } ,
2022-07-07 05:19:05 +08:00
{ label : "Always" , value : true } ,
];
2022-07-19 00:44:10 +08:00
$pageTitle = "Mail settings";
2022-08-21 19:30:36 +08:00
let testPopup;
2022-08-15 00:30:45 +08:00
let originalFormSettings = {} ;
2022-07-07 05:19:05 +08:00
let formSettings = {} ;
let isLoading = false;
let isSaving = false;
2022-08-15 00:30:45 +08:00
$: initialHash = JSON.stringify(originalFormSettings);
2022-07-07 05:19:05 +08:00
$: hasChanges = initialHash != JSON.stringify(formSettings);
loadSettings();
async function loadSettings() {
isLoading = true;
try {
2022-08-02 22:00:14 +08:00
const settings = (await ApiClient.settings.getAll()) || {} ;
2022-07-07 05:19:05 +08:00
init(settings);
} catch (err) {
ApiClient.errorResponseHandler(err);
}
isLoading = false;
}
async function save() {
if (isSaving || !hasChanges) {
return;
}
isSaving = true;
try {
2022-08-02 22:00:14 +08:00
const settings = await ApiClient.settings.update(CommonHelper.filterRedactedProps(formSettings));
2022-07-07 05:19:05 +08:00
init(settings);
2022-08-15 00:30:45 +08:00
setErrors({} );
2022-07-07 05:19:05 +08:00
addSuccessToast("Successfully saved mail settings.");
} catch (err) {
ApiClient.errorResponseHandler(err);
}
isSaving = false;
}
function init(settings = {} ) {
formSettings = {
meta: settings?.meta || {} ,
smtp: settings?.smtp || {} ,
};
2022-08-15 00:30:45 +08:00
originalFormSettings = JSON.parse(JSON.stringify(formSettings));
}
function reset() {
formSettings = JSON.parse(JSON.stringify(originalFormSettings || {} ));
2022-07-07 05:19:05 +08:00
}
< / script >
< SettingsSidebar / >
2022-08-09 21:16:09 +08:00
< PageWrapper >
2022-07-07 05:19:05 +08:00
< header class = "page-header" >
< nav class = "breadcrumbs" >
< div class = "breadcrumb-item" > Settings< / div >
2022-07-19 00:44:10 +08:00
< div class = "breadcrumb-item" > { $pageTitle } </ div >
2022-07-07 05:19:05 +08:00
< / nav >
< / header >
< div class = "wrapper" >
< form class = "panel" autocomplete = "off" on:submit | preventDefault = {() => save ()} >
< div class = "content txt-xl m-b-base" >
< p > Configure common settings for sending emails.< / p >
< / div >
{ #if isLoading }
< div class = "loader" / >
{ : else }
2022-08-15 00:30:45 +08:00
< div class = "grid m-b-base" >
2022-07-07 05:19:05 +08:00
< div class = "col-lg-6" >
< Field class = "form-field required" name = "meta.senderName" let:uniqueId >
< label for = { uniqueId } > Sender name </ label >
< input
type="text"
id={ uniqueId }
required
bind:value={ formSettings . meta . senderName }
/>
< / Field >
< / div >
< div class = "col-lg-6" >
< Field class = "form-field required" name = "meta.senderAddress" let:uniqueId >
< label for = { uniqueId } > Sender address </ label >
< input
type="email"
id={ uniqueId }
required
bind:value={ formSettings . meta . senderAddress }
/>
< / Field >
< / div >
2022-08-15 00:30:45 +08:00
< / div >
2022-07-07 05:19:05 +08:00
2022-08-15 00:30:45 +08:00
< div class = "accordions" >
< EmailTemplateAccordion
single
key="meta.verificationTemplate"
title={ 'Default "Verification" email template' }
bind:config={ formSettings . meta . verificationTemplate }
/>
< EmailTemplateAccordion
single
key="meta.resetPasswordTemplate"
title={ 'Default "Password reset" email template' }
bind:config={ formSettings . meta . resetPasswordTemplate }
/>
< EmailTemplateAccordion
single
key="meta.confirmEmailChangeTemplate"
title={ 'Default "Confirm email change" email template' }
bind:config={ formSettings . meta . confirmEmailChangeTemplate }
/>
2022-07-07 05:19:05 +08:00
< / div >
< hr / >
2022-08-16 02:31:25 +08:00
< Field class = "form-field form-field-toggle m-b-sm" let:uniqueId >
2022-07-07 05:19:05 +08:00
< input type = "checkbox" id = { uniqueId } required bind:checked = { formSettings . smtp . enabled } / >
2022-08-16 12:36:15 +08:00
< label for = { uniqueId } >
< span class = "txt" > Use SMTP mail server < strong > (recommended)< / strong > < / span >
< i
class="ri-information-line link-hint"
use:tooltip={{
text: 'By default PocketBase uses the unix "sendmail" command for sending emails. For better emails deliverability it is recommended to use a SMTP mail server.',
position: "top",
}}
/>
< / label >
2022-07-07 05:19:05 +08:00
< / Field >
2022-08-16 12:36:15 +08:00
{ #if formSettings . smtp . enabled }
2022-07-07 05:19:05 +08:00
< div class = "grid" transition:slide | local = {{ duration : 150 }} >
< div class = "col-lg-6" >
< Field class = "form-field required" name = "smtp.host" let:uniqueId >
< label for = { uniqueId } > SMTP server host </ label >
< input
type="text"
id={ uniqueId }
required
bind:value={ formSettings . smtp . host }
/>
< / Field >
< / div >
< div class = "col-lg-3" >
< Field class = "form-field required" name = "smtp.port" let:uniqueId >
< label for = { uniqueId } > Port</label >
< input
type="number"
id={ uniqueId }
required
bind:value={ formSettings . smtp . port }
/>
< / Field >
< / div >
< div class = "col-lg-3" >
< Field class = "form-field required" name = "smtp.tls" let:uniqueId >
< label for = { uniqueId } > TLS Encryption </ label >
< ObjectSelect
id={ uniqueId }
items={ tlsOptions }
bind:keyOfSelected={ formSettings . smtp . tls }
/>
< / Field >
< / div >
< div class = "col-lg-6" >
< Field class = "form-field" name = "smtp.username" let:uniqueId >
< label for = { uniqueId } > Username</label >
< input type = "text" id = { uniqueId } bind:value= { formSettings . smtp . username } />
< / Field >
< / div >
< div class = "col-lg-6" >
< Field class = "form-field" name = "smtp.password" let:uniqueId >
< label for = { uniqueId } > Password</label >
< RedactedPasswordInput
id={ uniqueId }
bind:value={ formSettings . smtp . password }
/>
< / Field >
< / div >
<!-- margin helper -->
< div class = "col-lg-12" / >
< / div >
{ /if }
< div class = "flex" >
< div class = "flex-fill" / >
2022-08-21 19:30:36 +08:00
2022-08-15 00:30:45 +08:00
{ #if hasChanges }
< button
type="button"
class="btn btn-secondary btn-hint"
disabled={ isSaving }
on:click={() => reset ()}
>
< span class = "txt" > Cancel< / span >
< / button >
2022-08-21 19:30:36 +08:00
< button
type="submit"
class="btn btn-expanded"
class:btn-loading={ isSaving }
disabled={ ! hasChanges || isSaving }
on:click={() => save ()}
>
< span class = "txt" > Save changes< / span >
< / button >
{ : else }
< button
type="button"
class="btn btn-expanded btn-outline"
on:click={() => testPopup ? . show ()}
>
< i class = "ri-mail-check-line" / >
< span class = "txt" > Send test email< / span >
< / button >
2022-08-15 00:30:45 +08:00
{ /if }
2022-07-07 05:19:05 +08:00
< / div >
{ /if }
< / form >
< / div >
2022-08-09 21:16:09 +08:00
< / PageWrapper >
2022-08-21 19:30:36 +08:00
< EmailTestPopup bind:this = { testPopup } / >