Merge pull request #1035 from webrecorder/backend-init

feat: Display waiting message while backend is initializing
This commit is contained in:
Anish Lakhwara 2023-08-02 17:39:47 -07:00 committed by GitHub
commit af09d56ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,8 @@ type FormEvent =
| { type: "SHOW_SIGN_IN_WITH_PASSWORD" } | { type: "SHOW_SIGN_IN_WITH_PASSWORD" }
| { type: "SHOW_FORGOT_PASSWORD" } | { type: "SHOW_FORGOT_PASSWORD" }
| { type: "SUBMIT" } | { type: "SUBMIT" }
| { type: "BACKEND_INITIALIZED" }
| { type: "BACKEND_NOT_INITIALIZED" }
| FormSuccessEvent | FormSuccessEvent
| FormErrorEvent; | FormErrorEvent;
@ -51,6 +53,10 @@ type FormTypestate =
| { | {
value: "submittingForgotPassword"; value: "submittingForgotPassword";
context: FormContext; context: FormContext;
}
| {
value: "backendInitializing";
context: FormContext;
}; };
const initialContext = {}; const initialContext = {};
@ -71,6 +77,9 @@ const machine = createMachine<FormContext, FormEvent, FormTypestate>(
target: "signingIn", target: "signingIn",
actions: "reset", actions: "reset",
}, },
BACKEND_NOT_INITIALIZED: {
target: "backendInitializing",
},
}, },
}, },
["signingIn"]: { ["signingIn"]: {
@ -106,6 +115,14 @@ const machine = createMachine<FormContext, FormEvent, FormTypestate>(
}, },
}, },
}, },
["backendInitializing"]: {
on: {
BACKEND_INITIALIZED: {
target: "signIn",
actions: "reset",
},
},
},
}, },
}, },
{ {
@ -135,6 +152,7 @@ export class LogInPage extends LiteElement {
@state() @state()
private formState = machine.initialState; private formState = machine.initialState;
private timerId?: number;
firstUpdated() { firstUpdated() {
this.formStateService.subscribe((state) => { this.formStateService.subscribe((state) => {
@ -142,10 +160,12 @@ export class LogInPage extends LiteElement {
}); });
this.formStateService.start(); this.formStateService.start();
this.checkBackendInitialized();
} }
disconnectedCallback() { disconnectedCallback() {
this.formStateService.stop(); this.formStateService.stop();
window.clearTimeout(this.timerId);
super.disconnectedCallback(); super.disconnectedCallback();
} }
@ -262,9 +282,19 @@ export class LogInPage extends LiteElement {
class="w-full" class="w-full"
variant="primary" variant="primary"
?loading=${this.formState.value === "signingIn"} ?loading=${this.formState.value === "signingIn"}
?disabled=${this.formState.value === "backendInitializing"}
type="submit" type="submit"
>${msg("Log in")}</sl-button >${msg("Log in")}</sl-button
> >
${this.formState.value === "backendInitializing"
? html` <div class="mt-3">
<btrix-alert variant="warning" class="text-center"
>${msg(
"Please wait while Browsertrix Cloud is initializing"
)}</btrix-alert
>
</div>`
: ""}
</form> </form>
`; `;
} }
@ -309,6 +339,19 @@ export class LogInPage extends LiteElement {
`; `;
} }
async checkBackendInitialized() {
const resp = await fetch("/api/settings");
if (resp.status === 200) {
this.formStateService.send("BACKEND_INITIALIZED");
} else {
this.formStateService.send("BACKEND_NOT_INITIALIZED");
this.timerId = window.setTimeout(
() => this.checkBackendInitialized(),
5000
);
}
}
async onSubmitLogIn(event: SubmitEvent) { async onSubmitLogIn(event: SubmitEvent) {
event.preventDefault(); event.preventDefault();
this.formStateService.send("SUBMIT"); this.formStateService.send("SUBMIT");