diff --git a/ts/user.ts b/ts/user.ts index b7bfaba..05afdb9 100644 --- a/ts/user.ts +++ b/ts/user.ts @@ -113,7 +113,7 @@ interface MyDetails { interface MyReferral { code: string; - remaining_uses: string; + remaining_uses: number; no_limit: boolean; expiry: number; } @@ -246,6 +246,107 @@ class ContactMethods { }; } +class ReferralCard { + private _card: HTMLElement; + private _code: string; + private _url: string; + private _expiry: Date; + private _expiryUnix: number; + private _remainingUses: number; + private _noLimit: boolean; + + private _button: HTMLButtonElement; + private _infoArea: HTMLDivElement; + private _remainingUsesEl: HTMLSpanElement; + private _expiryEl: HTMLSpanElement; + + get code(): string { return this._code; } + set code(c: string) { + this._code = c; + let url = window.location.href; + for (let split of ["#", "?", "account", "my"]) { + url = url.split(split)[0]; + } + if (url.slice(-1) != "/") { url += "/"; } + url = url + "invite/" + this._code; + this._url = url; + } + + get remaining_uses(): number { return this._remainingUses; } + set remaining_uses(v: number) { + this._remainingUses = v; + if (v > 0 && !(this._noLimit)) + this._remainingUsesEl.textContent = `${v}`; + } + + get no_limit(): boolean { return this._noLimit; } + set no_limit(v: boolean) { + this._noLimit = v; + if (v) + this._remainingUsesEl.textContent = `∞`; + else + this._remainingUsesEl.textContent = `${this._remainingUses}`; + } + + get expiry(): Date { return this._expiry; }; + set expiry(expiryUnix: number) { + this._expiryUnix = expiryUnix; + this._expiry = new Date(expiryUnix * 1000); + this._expiryEl.textContent = toDateString(this._expiry); + } + + constructor(card: HTMLElement) { + this._card = card; + this._button = this._card.querySelector(".user-referrals-button") as HTMLButtonElement; + this._infoArea = this._card.querySelector(".user-referrals-info") as HTMLDivElement; + + this._infoArea.innerHTML = ` +