2021-10-20 18:18:01 +00:00
|
|
|
import {BillingModel} from 'app/client/models/BillingModel';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as css from 'app/client/ui/BillingPageCss';
|
2022-06-08 17:54:00 +00:00
|
|
|
import {IBillingOrgSettings} from 'app/common/BillingAPI';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {checkSubdomainValidity} from 'app/common/orgNameUtils';
|
|
|
|
import * as roles from 'app/common/roles';
|
|
|
|
import {Organization} from 'app/common/UserAPI';
|
2022-06-08 17:54:00 +00:00
|
|
|
import {Disposable, dom, DomArg, IDisposableOwnerT, makeTestId, Observable} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
const testId = makeTestId('test-bp-');
|
|
|
|
|
|
|
|
export interface IFormData {
|
|
|
|
settings?: IBillingOrgSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Optional autofill vales to pass in to the BillingForm constructor.
|
|
|
|
interface IAutofill {
|
|
|
|
settings?: Partial<IBillingOrgSettings>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An object containing a function to check the validity of its observable value.
|
|
|
|
// The get function should return the observable value or throw an error if it is invalid.
|
|
|
|
interface IValidated<T> {
|
|
|
|
value: Observable<T>;
|
|
|
|
checkValidity: (value: T) => void|Promise<void>; // Should throw with message on invalid values.
|
|
|
|
isInvalid: Observable<boolean>;
|
|
|
|
get: () => T|Promise<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BillingForm extends Disposable {
|
|
|
|
private readonly _settings: BillingSettingsForm|null;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
org: Organization|null,
|
2021-10-20 18:18:01 +00:00
|
|
|
billingModel: BillingModel,
|
2022-06-08 17:54:00 +00:00
|
|
|
options: {settings: boolean, domain: boolean},
|
2020-10-02 15:10:00 +00:00
|
|
|
autofill: IAutofill = {}
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
// Org settings form.
|
2021-10-20 18:18:01 +00:00
|
|
|
this._settings = options.settings ? new BillingSettingsForm(billingModel, org, {
|
2022-06-08 17:54:00 +00:00
|
|
|
showHeader: true,
|
2020-10-02 15:10:00 +00:00
|
|
|
showDomain: options.domain,
|
|
|
|
autofill: autofill.settings
|
|
|
|
}) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom() {
|
|
|
|
return [
|
|
|
|
this._settings ? this._settings.buildDom() : null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this will throw if any values are invalid.
|
|
|
|
public async getFormData(): Promise<IFormData> {
|
|
|
|
const settings = this._settings ? await this._settings.getSettings() : undefined;
|
|
|
|
return {
|
|
|
|
settings,
|
|
|
|
};
|
|
|
|
}
|
2021-11-04 17:31:08 +00:00
|
|
|
|
|
|
|
// Make a best-effort attempt to focus the element with the error.
|
|
|
|
public focusOnError() {
|
|
|
|
// We don't have a good way to do it, we just try to do better than nothing. Also we don't
|
|
|
|
// have access to the form container, so look at css.inputError element in the full document.
|
|
|
|
const elem = document.querySelector(`.${css.paymentBlock.className} .${css.inputError.className}:not(:empty)`);
|
|
|
|
const parent = elem?.closest(`.${css.paymentBlock.className}`);
|
|
|
|
if (parent) {
|
|
|
|
const input: HTMLInputElement|null =
|
|
|
|
parent.querySelector(`.${css.billingInput.className}-invalid`) ||
|
|
|
|
parent.querySelector('input');
|
|
|
|
if (input) {
|
|
|
|
input.focus();
|
|
|
|
input.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abstract class which includes helper functions for creating a form whose values are verified.
|
|
|
|
abstract class BillingSubForm extends Disposable {
|
|
|
|
protected readonly formError: Observable<string> = Observable.create(this, '');
|
2021-11-04 17:31:08 +00:00
|
|
|
protected shouldAutoFocus = false;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates an input whose value is validated on blur. Input text turns red and the validation
|
|
|
|
// error is shown on negative validation.
|
|
|
|
protected billingInput(validated: IValidated<string>, ...args: Array<DomArg<any>>) {
|
|
|
|
return css.billingInput(validated.value, {onInput: true},
|
|
|
|
css.billingInput.cls('-invalid', validated.isInvalid),
|
|
|
|
dom.on('blur', () => this._onBlur(validated)),
|
|
|
|
...args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async _onBlur(validated: IValidated<string>): Promise<void> {
|
|
|
|
// Do not show empty input errors on blur.
|
|
|
|
if (validated.value.get().length === 0) { return; }
|
|
|
|
try {
|
|
|
|
await validated.get();
|
|
|
|
this.formError.set('');
|
|
|
|
} catch (e) {
|
|
|
|
this.formError.set(e.message);
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 17:31:08 +00:00
|
|
|
|
|
|
|
protected maybeAutoFocus() {
|
|
|
|
if (this.shouldAutoFocus) {
|
|
|
|
this.shouldAutoFocus = false;
|
|
|
|
return (elem: HTMLElement) => { setTimeout(() => elem.focus(), 0); };
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the billing settings form, including the org name and the org subdomain values.
|
|
|
|
*/
|
|
|
|
class BillingSettingsForm extends BillingSubForm {
|
2020-10-26 14:45:31 +00:00
|
|
|
private readonly _name: IValidated<string> = createValidated(this, checkRequired('Company name'));
|
2020-10-02 15:10:00 +00:00
|
|
|
// Only verify the domain if it is shown.
|
2020-10-26 14:45:31 +00:00
|
|
|
private readonly _domain: IValidated<string> = createValidated(this,
|
2020-10-02 15:10:00 +00:00
|
|
|
this._options.showDomain ? d => this._verifyDomain(d) : () => undefined);
|
|
|
|
|
|
|
|
constructor(
|
2021-10-20 18:18:01 +00:00
|
|
|
private readonly _billingModel: BillingModel,
|
2020-10-02 15:10:00 +00:00
|
|
|
private readonly _org: Organization|null,
|
|
|
|
private readonly _options: {
|
|
|
|
showHeader: boolean;
|
|
|
|
showDomain: boolean;
|
|
|
|
autofill?: Partial<IBillingOrgSettings>;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
const autofill = this._options.autofill;
|
|
|
|
if (autofill) {
|
|
|
|
this._name.value.set(autofill.name || '');
|
|
|
|
this._domain.value.set(autofill.domain || '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom() {
|
|
|
|
const noEditAccess = Boolean(this._org && !roles.canEdit(this._org.access));
|
2021-11-04 17:31:08 +00:00
|
|
|
const initDomain = this._options.autofill?.domain;
|
2020-10-02 15:10:00 +00:00
|
|
|
return css.paymentBlock(
|
2022-06-08 17:54:00 +00:00
|
|
|
this._options.showHeader ? css.paymentLabel('Team name') : null,
|
2020-10-02 15:10:00 +00:00
|
|
|
css.paymentRow(
|
|
|
|
css.paymentField(
|
|
|
|
this.billingInput(this._name,
|
|
|
|
dom.boolAttr('disabled', () => noEditAccess),
|
|
|
|
testId('settings-name')
|
|
|
|
),
|
|
|
|
noEditAccess ? css.paymentFieldInfo('Organization edit access is required',
|
|
|
|
testId('settings-name-info')
|
|
|
|
) : null
|
|
|
|
)
|
|
|
|
),
|
|
|
|
this._options.showDomain ? css.paymentRow(
|
|
|
|
css.paymentField(
|
2022-06-08 17:54:00 +00:00
|
|
|
css.paymentLabel('Team subdomain'),
|
2020-10-02 15:10:00 +00:00
|
|
|
this.billingInput(this._domain,
|
|
|
|
dom.boolAttr('disabled', () => noEditAccess),
|
|
|
|
testId('settings-domain')
|
|
|
|
),
|
|
|
|
noEditAccess ? css.paymentFieldInfo('Organization edit access is required',
|
|
|
|
testId('settings-domain-info')
|
2021-08-18 01:44:11 +00:00
|
|
|
) : null,
|
2021-11-04 17:31:08 +00:00
|
|
|
dom.maybe((use) => initDomain && use(this._domain.value) !== initDomain, () =>
|
|
|
|
css.paymentFieldDanger('Any saved links will need updating if the URL changes')
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
css.paymentField({style: 'flex: 0 1 0;'},
|
|
|
|
css.inputHintLabel('.getgrist.com')
|
|
|
|
)
|
|
|
|
) : null,
|
|
|
|
css.inputError(
|
|
|
|
dom.text(this.formError),
|
|
|
|
testId('settings-form-error')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throws if any value is invalid.
|
|
|
|
public async getSettings(): Promise<IBillingOrgSettings|undefined> {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
name: await this._name.get(),
|
|
|
|
domain: await this._domain.get()
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
this.formError.set(e.message);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throws if the entered domain contains any invalid characters or is already taken.
|
|
|
|
private async _verifyDomain(domain: string): Promise<void> {
|
2021-08-18 01:44:11 +00:00
|
|
|
// OK to retain current domain.
|
|
|
|
if (domain === this._options.autofill?.domain) { return; }
|
2020-10-02 15:10:00 +00:00
|
|
|
checkSubdomainValidity(domain);
|
2021-10-20 18:18:01 +00:00
|
|
|
const isAvailable = await this._billingModel.isDomainAvailable(domain);
|
2020-10-02 15:10:00 +00:00
|
|
|
if (!isAvailable) { throw new Error('Domain is already taken.'); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:18:01 +00:00
|
|
|
|
2020-10-26 14:45:31 +00:00
|
|
|
function checkFunc(func: (val: string) => boolean, message: string) {
|
|
|
|
return (val: string) => {
|
|
|
|
if (!func(val)) { throw new Error(message); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkRequired(propertyName: string) {
|
|
|
|
return checkFunc(Boolean, `${propertyName} is required.`);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Creates a validated object, which includes an observable and a function to check
|
|
|
|
// if the current observable value is valid.
|
|
|
|
function createValidated(
|
|
|
|
owner: IDisposableOwnerT<any>,
|
2021-04-26 21:54:09 +00:00
|
|
|
checkValidity: (value: string) => void|Promise<void>,
|
2020-10-02 15:10:00 +00:00
|
|
|
): IValidated<string> {
|
|
|
|
const value = Observable.create(owner, '');
|
|
|
|
const isInvalid = Observable.create<boolean>(owner, false);
|
|
|
|
owner.autoDispose(value.addListener(() => { isInvalid.set(false); }));
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
isInvalid,
|
|
|
|
checkValidity,
|
|
|
|
get: async () => {
|
|
|
|
const _value = value.get();
|
|
|
|
try {
|
|
|
|
await checkValidity(_value);
|
|
|
|
} catch (e) {
|
|
|
|
isInvalid.set(true);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
isInvalid.set(false);
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|