Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 2x 1x 1x 1x 1x |
import { ROUTES_PATH } from '../constants/routes.js'
export let PREVIOUS_LOCATION = ''
// we use a class so as to test its methods in e2e tests
export default class Login {
constructor({ document, localStorage, onNavigate, PREVIOUS_LOCATION, store }) {
this.document = document
this.localStorage = localStorage
this.onNavigate = onNavigate
this.PREVIOUS_LOCATION = PREVIOUS_LOCATION
this.store = store
const formEmployee = this.document.querySelector(`form[data-testid="form-employee"]`)
formEmployee.addEventListener("submit", this.handleSubmitEmployee)
const formAdmin = this.document.querySelector(`form[data-testid="form-admin"]`)
formAdmin.addEventListener("submit", this.handleSubmitAdmin)
}
handleSubmitEmployee = e => {
e.preventDefault()
const user = {
type: "Employee",
email: e.target.querySelector(`input[data-testid="employee-email-input"]`).value,
password: e.target.querySelector(`input[data-testid="employee-password-input"]`).value,
status: "connected"
}
this.localStorage.setItem("user", JSON.stringify(user))
this.login(user)
.catch(
(err) => this.createUser(user)
)
.then(() => {
this.onNavigate(ROUTES_PATH['Bills'])
this.PREVIOUS_LOCATION = ROUTES_PATH['Bills']
PREVIOUS_LOCATION = this.PREVIOUS_LOCATION
this.document.body.style.backgroundColor="#fff"
})
}
handleSubmitAdmin = e => {
e.preventDefault()
const user = {
type: "Admin",
email: e.target.querySelector(`input[data-testid="admin-email-input"]`).value,
password: e.target.querySelector(`input[data-testid="admin-password-input"]`).value,
status: "connected"
}
this.localStorage.setItem("user", JSON.stringify(user))
this.login(user)
.catch(
(err) => this.createUser(user)
)
.then(() => {
this.onNavigate(ROUTES_PATH['Dashboard'])
this.PREVIOUS_LOCATION = ROUTES_PATH['Dashboard']
PREVIOUS_LOCATION = this.PREVIOUS_LOCATION
document.body.style.backgroundColor="#fff"
})
}
// not need to cover this function by tests
login = (user) => {
if (this.store) {
return this.store
.login(JSON.stringify({
email: user.email,
password: user.password,
})).then(({jwt}) => {
localStorage.setItem('jwt', jwt)
})
} else {
return null
}
}
// not need to cover this function by tests
createUser = (user) => {
if (this.store) {
return this.store
.users()
.create({data:JSON.stringify({
type: user.type,
name: user.email.split('@')[0],
email: user.email,
password: user.password,
})})
.then(() => {
console.log(`User with ${user.email} is created`)
return this.login(user)
})
} else {
return null
}
}
}
|