-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b94495c
commit 93c6dfd
Showing
3 changed files
with
168 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,105 @@ | ||
import { ROUTES_PATH } from '../constants/routes.js' | ||
import Logout from "./Logout.js" | ||
import { ROUTES_PATH } from "../constants/routes.js"; | ||
import Logout from "./Logout.js"; | ||
|
||
export default class NewBill { | ||
constructor({ document, onNavigate, store, localStorage }) { | ||
this.document = document | ||
this.onNavigate = onNavigate | ||
this.store = store | ||
const formNewBill = this.document.querySelector(`form[data-testid="form-new-bill"]`) | ||
formNewBill.addEventListener("submit", this.handleSubmit) | ||
const file = this.document.querySelector(`input[data-testid="file"]`) | ||
file.addEventListener("change", this.handleChangeFile) | ||
this.fileUrl = null | ||
this.fileName = null | ||
this.billId = null | ||
new Logout({ document, localStorage, onNavigate }) | ||
//disable submit button | ||
this.document = document; | ||
this.onNavigate = onNavigate; | ||
this.store = store; | ||
const formNewBill = this.document.querySelector( | ||
`form[data-testid="form-new-bill"]` | ||
); | ||
formNewBill.addEventListener("submit", this.handleSubmit); | ||
const file = this.document.querySelector(`input[data-testid="file"]`); | ||
file.addEventListener("change", this.handleChangeFile); | ||
this.fileUrl = null; | ||
this.fileName = null; | ||
this.billId = null; | ||
new Logout({ document, localStorage, onNavigate }); | ||
} | ||
handleChangeFile = e => { | ||
e.preventDefault() | ||
const file = this.document.querySelector(`input[data-testid="file"]`).files[0] | ||
const filePath = e.target.value.split(/\\/g) | ||
const fileName = filePath[filePath.length-1] | ||
let fileExtension = fileName.split('.') | ||
fileExtension = fileExtension[1] | ||
const formData = new FormData() | ||
const email = JSON.parse(localStorage.getItem("user")).email | ||
const submitButton = document.getElementById("btn-send-bill") | ||
handleChangeFile = (e) => { | ||
e.preventDefault(); | ||
const file = this.document.querySelector(`input[data-testid="file"]`) | ||
.files[0]; | ||
const filePath = e.target.value.split(/\\/g); | ||
const fileName = filePath[filePath.length - 1]; | ||
let fileExtension = fileName.split("."); | ||
fileExtension = fileExtension[1]; | ||
const formData = new FormData(); | ||
const email = JSON.parse(localStorage.getItem("user")).email; | ||
const submitButton = document.getElementById("btn-send-bill"); | ||
const errorMessage = document.getElementById("error-message"); | ||
|
||
formData.append('file', file) | ||
formData.append('email', email) | ||
formData.append("file", file); | ||
formData.append("email", email); | ||
|
||
if (fileExtension != "png" && fileExtension != "jpg" && fileExtension != "jpeg") { | ||
submitButton.disabled = true | ||
errorMessage.style.display = "block" | ||
} | ||
else { | ||
submitButton.disabled = false | ||
errorMessage.style.display = "none" | ||
if ( | ||
fileExtension != "png" && | ||
fileExtension != "jpg" && | ||
fileExtension != "jpeg" | ||
) { | ||
submitButton.disabled = true; | ||
errorMessage.style.display = "block"; | ||
} else { | ||
submitButton.disabled = false; | ||
errorMessage.style.display = "none"; | ||
this.store | ||
.bills() | ||
.create({ | ||
data: formData, | ||
headers: { | ||
noContentType: true | ||
} | ||
}) | ||
.then(({fileUrl, key}) => { | ||
console.log(fileUrl) | ||
this.billId = key | ||
this.fileUrl = fileUrl | ||
this.fileName = fileName | ||
|
||
}).catch(error => console.error(error)) | ||
.bills() | ||
.create({ | ||
data: formData, | ||
headers: { | ||
noContentType: true, | ||
}, | ||
}) | ||
.then(({ fileUrl, key }) => { | ||
//console.log(fileUrl); | ||
this.billId = key; | ||
this.fileUrl = fileUrl; | ||
this.fileName = fileName; | ||
}) | ||
.catch((error) => console.error(error)); | ||
} | ||
} | ||
}; | ||
|
||
handleSubmit = e => { | ||
e.preventDefault() | ||
console.log('e.target.querySelector(`input[data-testid="datepicker"]`).value', e.target.querySelector(`input[data-testid="datepicker"]`).value) | ||
const email = JSON.parse(localStorage.getItem("user")).email | ||
handleSubmit = (e) => { | ||
e.preventDefault(); | ||
/* console.log( | ||
'e.target.querySelector(`input[data-testid="datepicker"]`).value', | ||
e.target.querySelector(`input[data-testid="datepicker"]`).value | ||
); */ | ||
const email = JSON.parse(localStorage.getItem("user")).email; | ||
const bill = { | ||
email, | ||
type: e.target.querySelector(`select[data-testid="expense-type"]`).value, | ||
name: e.target.querySelector(`input[data-testid="expense-name"]`).value, | ||
amount: parseInt(e.target.querySelector(`input[data-testid="amount"]`).value), | ||
date: e.target.querySelector(`input[data-testid="datepicker"]`).value, | ||
name: e.target.querySelector(`input[data-testid="expense-name"]`).value, | ||
amount: parseInt( | ||
e.target.querySelector(`input[data-testid="amount"]`).value | ||
), | ||
date: e.target.querySelector(`input[data-testid="datepicker"]`).value, | ||
vat: e.target.querySelector(`input[data-testid="vat"]`).value, | ||
pct: parseInt(e.target.querySelector(`input[data-testid="pct"]`).value) || 20, | ||
commentary: e.target.querySelector(`textarea[data-testid="commentary"]`).value, | ||
pct: | ||
parseInt(e.target.querySelector(`input[data-testid="pct"]`).value) || | ||
20, | ||
commentary: e.target.querySelector(`textarea[data-testid="commentary"]`) | ||
.value, | ||
fileUrl: this.fileUrl, | ||
fileName: this.fileName, | ||
status: 'pending' | ||
} | ||
this.updateBill(bill) | ||
this.onNavigate(ROUTES_PATH['Bills']) | ||
} | ||
status: "pending", | ||
}; | ||
this.updateBill(bill); | ||
this.onNavigate(ROUTES_PATH["Bills"]); | ||
}; | ||
|
||
// not need to cover this function by tests | ||
updateBill = (bill) => { | ||
if (this.store) { | ||
this.store | ||
.bills() | ||
.update({data: JSON.stringify(bill), selector: this.billId}) | ||
.then(() => { | ||
this.onNavigate(ROUTES_PATH['Bills']) | ||
}) | ||
.catch(error => console.error(error)) | ||
.bills() | ||
.update({ data: JSON.stringify(bill), selector: this.billId }) | ||
.then(() => { | ||
this.onNavigate(ROUTES_PATH["Bills"]); | ||
}) | ||
.catch((error) => console.error(error)); | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters