mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 12:30:06 +00:00
feat(sasjs-tests): update tests, use vite and minimal deps
This commit is contained in:
90
sasjs-tests/src/components/LoginForm.ts
Normal file
90
sasjs-tests/src/components/LoginForm.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { appContext } from '../core/AppContext'
|
||||
|
||||
export class LoginForm extends HTMLElement {
|
||||
private shadow: ShadowRoot
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.shadow = this.attachShadow({ mode: 'open' })
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.render()
|
||||
this.attachEventListeners()
|
||||
}
|
||||
|
||||
render() {
|
||||
this.shadow.innerHTML = `
|
||||
<link rel="stylesheet" href="${new URL(
|
||||
'./LoginForm.css',
|
||||
import.meta.url
|
||||
)}">
|
||||
<h1>SASjs Tests</h1>
|
||||
<form id="login-form">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" name="username" placeholder="Enter username" required />
|
||||
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password" placeholder="Enter password" required />
|
||||
|
||||
<button type="submit" id="submit-btn">Log In</button>
|
||||
<div class="error" id="error"></div>
|
||||
</form>
|
||||
`
|
||||
}
|
||||
|
||||
attachEventListeners() {
|
||||
const form = this.shadow.getElementById('login-form') as HTMLFormElement
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault()
|
||||
await this.handleLogin()
|
||||
})
|
||||
}
|
||||
|
||||
async handleLogin() {
|
||||
const username = (
|
||||
this.shadow.getElementById('username') as HTMLInputElement
|
||||
).value
|
||||
const password = (
|
||||
this.shadow.getElementById('password') as HTMLInputElement
|
||||
).value
|
||||
const submitBtn = this.shadow.getElementById(
|
||||
'submit-btn'
|
||||
) as HTMLButtonElement
|
||||
const errorDiv = this.shadow.getElementById('error') as HTMLDivElement
|
||||
|
||||
errorDiv.textContent = ''
|
||||
submitBtn.textContent = 'Logging in...'
|
||||
submitBtn.disabled = true
|
||||
|
||||
try {
|
||||
const adapter = appContext.getAdapter()
|
||||
if (!adapter) {
|
||||
throw new Error('Adapter not initialized')
|
||||
}
|
||||
|
||||
const response = await adapter.logIn(username, password)
|
||||
|
||||
if (response && response.isLoggedIn) {
|
||||
appContext.setIsLoggedIn(true)
|
||||
this.dispatchEvent(
|
||||
new CustomEvent('login-success', {
|
||||
bubbles: true,
|
||||
composed: true
|
||||
})
|
||||
)
|
||||
} else {
|
||||
throw new Error('Login failed')
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
errorDiv.textContent =
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'Login failed. Please try again.'
|
||||
submitBtn.textContent = 'Log In'
|
||||
submitBtn.disabled = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define('login-form', LoginForm)
|
||||
Reference in New Issue
Block a user