Skip to content

Commit c6f3a25

Browse files
ugly form to fetch a CE license from api
1 parent b23e8a8 commit c6f3a25

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

assets/js/hubsubscription.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
11
"use strict";
22

3+
const COMMUNITY_EDITION_REGISTER_URL = API_BASE_URL + '/licenses/hub/register';
4+
35
const BILLING_PORTAL_SESSION_URL = LEGACY_STORE_URL + '/hub/billing-portal-session';
46
const CUSTOM_BILLING_URL = LEGACY_STORE_URL + '/hub/custom-billing';
57
const GENERATE_PAY_LINK_URL = LEGACY_STORE_URL + '/hub/generate-pay-link';
68
const MANAGE_SUBSCRIPTION_URL = LEGACY_STORE_URL + '/hub/manage-subscription';
79
const UPDATE_PAYMENT_METHOD_URL = LEGACY_STORE_URL + '/hub/update-payment-method';
810

11+
class CommunityEdition {
12+
13+
constructor(form, registrationData, searchParams) {
14+
this._form = form;
15+
this._registrationData = registrationData;
16+
this._registrationData.hubId = searchParams.get('hub_id');
17+
this._registrationData.returnUrl = searchParams.get('return_url');
18+
}
19+
20+
register() {
21+
if (!this._registrationData.captcha) {
22+
this._registrationData.errorMessage = 'Please complete the CAPTCHA challenge.';
23+
return;
24+
}
25+
26+
this._registrationData.inProgress = true;
27+
this._registrationData.errorMessage = '';
28+
$.ajax({
29+
url: COMMUNITY_EDITION_REGISTER_URL,
30+
type: 'POST',
31+
data: {
32+
captcha: this._registrationData.captcha,
33+
hubId: this._registrationData.hubId,
34+
email: this._registrationData.email
35+
}
36+
}).done(token => {
37+
this.onRegisterSucceeded(token);
38+
}).fail(xhr => {
39+
this.onRegisterFailed('Registering Hub failed.');
40+
});
41+
}
42+
43+
onRegisterSucceeded(token) {
44+
this._registrationData.inProgress = false;
45+
this._registrationData.errorMessage = '';
46+
if (this._registrationData.returnUrl && this._registrationData.returnUrl.length > 0) {
47+
const returnUrl = new URL(this._registrationData.returnUrl);
48+
returnUrl.searchParams.set('token', token);
49+
window.location.href = returnUrl;
50+
} else {
51+
console.warn('No return URL specified, cannot transfer token to Hub.', token);
52+
}
53+
}
54+
55+
onRegisterFailed(error) {
56+
this._registrationData.inProgress = false;
57+
this._registrationData.errorMessage = error;
58+
}
59+
60+
}
61+
962
class HubSubscription {
1063

1164
constructor(form, subscriptionData, searchParams) {

layouts/hub-billing/single.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,34 @@ <h3 class="font-headline text-xl md:text-2xl leading-relaxed mb-4">
453453
</div>
454454
</template>
455455
</form>
456+
457+
<!-- COMMUNITY EDITION -->
458+
<form x-data="{data: {captcha: null, hubId: null, email: null, returnUrl: null, inProgress: false, errorMessage: null}, captchaState: null}" x-init="communityEdition = new CommunityEdition($refs.form, data, new URLSearchParams(location.search))" x-ref="form" @submit.prevent="communityEdition.register(); $refs.captcha.reset()">
459+
<div class="w-full mb-4 md:w-1/2 md:pr-3">
460+
<label class="label-uppercase mb-2">
461+
{{ i18n "hub_billing_checkout_standard_hubid" . }}
462+
</label>
463+
<input x-model="data.hubId" type="text" class="input-box w-full mb-2" readonly/>
464+
<p class="text-xs text-gray-600">
465+
{{ i18n "hub_billing_checkout_standard_hubid_description" . }}
466+
</p>
467+
</div>
468+
<div>
469+
<label class="label-uppercase mb-2">
470+
{{ i18n "hub_billing_checkout_standard_email" . }}
471+
</label>
472+
<input x-model="data.email" x-ref="email" @blur="$refs.email.classList.add('show-invalid')" type="email" class="input-box w-full" placeholder="{{ i18n "hub_billing_checkout_standard_email_placeholder" . }}" required/>
473+
</div>
474+
475+
<button :disabled="data.inProgress || captchaState == 'verifying' || !/^\S+@\S+\.\S+$/.test(data.email)" type="submit" class="btn btn-primary w-full md:w-64">
476+
<i :class="{'fa-paper-plane': !data.inProgress, 'fa-spinner fa-spin': data.inProgress}" class="fa-solid" aria-hidden="true"></i>
477+
Create Community Edition License
478+
</button>
479+
{{ $challengeUrl := printf "%s/licenses/hub/challenge" .Site.Params.apiBaseUrl }}
480+
{{ partial "captcha.html" (dict "challengeUrl" $challengeUrl "captchaPayload" "data.captcha" "captchaState" "captchaState") }}
481+
<p :class="{'hidden': !data.errorMessage}" class="text-sm text-red-600 mt-2" x-text="data.errorMessage"></p>
482+
</form>
483+
456484
</div>
457485
{{ end }}
458486
{{ define "script" }}

0 commit comments

Comments
 (0)