[WIP] Фикс запросов, добавил валидацию основных данных

This commit is contained in:
megavrilinvv
2023-08-17 18:17:16 +03:00
parent 65918134ec
commit fbb89646ea
5 changed files with 26 additions and 37 deletions

View File

@@ -4,7 +4,7 @@
:is-check-change="isCheckChange" :is-check-change="isCheckChange"
:is-loading-data="isLoadingData" :is-loading-data="isLoadingData"
:is-edit="isEdit" :is-edit="isEdit"
:save="updateBasicData" :save="saveChange"
:cancel="cancelEdit" :cancel="cancelEdit"
:open-edit="openEdit" :open-edit="openEdit"
) )
@@ -121,7 +121,6 @@ export default {
...mapActions({ ...mapActions({
createAddress: "postCreateAddress", createAddress: "postCreateAddress",
updateAddress: "postUpdateAddress", updateAddress: "postUpdateAddress",
deleteAddress: "deleteAddress",
}), }),
updateBasicData() { updateBasicData() {
const photoFormData = new FormData(); const photoFormData = new FormData();
@@ -152,8 +151,6 @@ export default {
postAdresses(updateData, initData, key) { postAdresses(updateData, initData, key) {
let isInitDataEmpty = [...Object.keys(removeEmptyFields(initData))] let isInitDataEmpty = [...Object.keys(removeEmptyFields(initData))]
.length; .length;
let isUpdateDataEmpty = [...Object.keys(removeEmptyFields(updateData))]
.length;
if (Object.keys(removeEmptyFields(updateData)).length) { if (Object.keys(removeEmptyFields(updateData)).length) {
if (!isInitDataEmpty) if (!isInitDataEmpty)
return this.createAddress({ return this.createAddress({
@@ -168,8 +165,6 @@ export default {
category: key, category: key,
}); });
} }
if (isInitDataEmpty && !isUpdateDataEmpty)
return this.deleteAddress({ id: this.basic.personalData.id });
}, },
checkChangeInput() { checkChangeInput() {
this.isCheckChange = checkChangeData(this.initDataBasic, this.basic); this.isCheckChange = checkChangeData(this.initDataBasic, this.basic);
@@ -187,22 +182,10 @@ export default {
const form = this.$refs.formBasicData; const form = this.$refs.formBasicData;
form.validate().then((validate) => { form.validate().then((validate) => {
if (validate) { if (validate) {
let person = this.basic.personalData; this.updateBasicData();
this.$store.dispatch("setMedicalCardData", { this.isLoadingData = false;
...this.medicalCard, this.isEdit = false;
person: { this.isCheckChange = false;
...this.medicalCard.person,
...person,
},
});
//let updateBasic = this.updateBasicData();
// Promise.allSettled(updateBasic)
// .then(() => this.$store.dispatch("getMedicalCardData"))
// .then(() => {
// this.isLoadingData = false;
// this.isEdit = false;
// this.isCheckChange = false;
// });
} else { } else {
getFieldsNameUnvalidated(form).forEach((errorKey) => { getFieldsNameUnvalidated(form).forEach((errorKey) => {
addNotification( addNotification(

View File

@@ -109,7 +109,7 @@ export default {
...mapActions({ ...mapActions({
createContact: "postCreateContact", createContact: "postCreateContact",
updateContact: "postUpdateContact", updateContact: "postUpdateContact",
deleteContacts: "deleteContact", delContacts: "deleteContact",
}), }),
checkNetworksField(index) { checkNetworksField(index) {
if (this.contacts.networks[index].kind) if (this.contacts.networks[index].kind)
@@ -130,16 +130,16 @@ export default {
...this.contacts.emails, ...this.contacts.emails,
...this.contacts.networks, ...this.contacts.networks,
]; ];
const notEmptyContacts = allNewContacts.filter((el) => el.kind); const notEmptyContacts = allNewContacts.filter((el) => !el.id);
return this.getRequestArrayData(this.initAllContacts, notEmptyContacts); return this.changeContacts(this.initAllContacts, notEmptyContacts);
}, },
getRequestArrayData(initData, notEmptyContacts) { changeContacts(initData, notEmptyContacts) {
const deleteRequests = initData const deleteRequests = initData
.filter((el) => !notEmptyContacts.find((data) => data.id === el.id)) .filter((el) => !notEmptyContacts.find((data) => data.id === el.id))
.map((el) => this.deleteContacts({ id: el.id })); .map((el) => this.delContacts({ id: el.id }));
const requests = notEmptyContacts.map((el) => { const requests = notEmptyContacts.map((el) => {
const requestObj = { const requestObj = {
category: el.kind, category: el.category || el.kind,
value: el.value, value: el.value,
}; };
delete requestObj.id; delete requestObj.id;

View File

@@ -61,22 +61,28 @@ export const baseDataForm = [
key: "region", key: "region",
label: "Регион", label: "Регион",
type: "text", type: "text",
rules: [(val) => ruleNotValue(val)],
error: "Поле 'Регион' должны быть заполнены",
}, },
{ {
key: "city", key: "city",
label: "Город", label: "Город",
type: "text", type: "text",
rules: [(val) => ruleNotValue(val)],
error: "Поле 'Город' должны быть заполнены",
}, },
{ {
key: "street", key: "street",
label: "Улица", label: "Улица",
type: "text", type: "text",
rules: [(val) => ruleNotValue(val)],
error: "Поле 'Улица' должны быть заполнены",
}, },
{ {
key: "house", key: "house",
label: "Дом", label: "Дом",
type: "text", type: "text",
rules: [(val) => !/^[\W0]/.test(val)], rules: [(val) => ruleNotValue(val)],
error: error:
"Номер дома должен начинатся не с нуля и только с цифробуквенного символа", "Номер дома должен начинатся не с нуля и только с цифробуквенного символа",
}, },

View File

@@ -45,7 +45,7 @@ function request(method, url, headers = {}, body, type = "") {
requestOptions.body = JSON.stringify(body); requestOptions.body = JSON.stringify(body);
} }
} }
if (method === "POST" || method === "PUT") { if (method === "POST" || method === "PUT" || method === "PATCH") {
if (type && type === "formData") { if (type && type === "formData") {
requestOptions = { requestOptions = {
method: method, method: method,
@@ -83,6 +83,10 @@ function post(url, body, type, headers, attempts = 3) {
return handleRequest("POST", url, headers, attempts, body, type); return handleRequest("POST", url, headers, attempts, body, type);
} }
function patch(url, body, type, headers, attempts = 3) {
return handleRequest("PATCH", url, headers, attempts, body, type);
}
function put(url, headers, body, attempts = 3) { function put(url, headers, body, attempts = 3) {
return handleRequest("PUT", url, headers, attempts, null, body); return handleRequest("PUT", url, headers, attempts, null, body);
} }
@@ -92,4 +96,5 @@ export const fetchWrapper = {
del, del,
post, post,
put, put,
patch,
}; };

View File

@@ -437,14 +437,11 @@ const actions = {
}); });
}, },
postUpdateAddress(context, { id, address, category }) { postUpdateAddress(context, { id, address, category }) {
fetchWrapper.post(`address/${id}`, { fetchWrapper.patch(`address/${id}`, {
...address, ...address,
category: category, category: category,
}); });
}, },
deleteAddress(context, { id }) {
fetchWrapper.del(`address/${id}`);
},
postCreateContact(context, { obj, id }) { postCreateContact(context, { obj, id }) {
fetchWrapper.post("contacts", { fetchWrapper.post("contacts", {
person_id: id, person_id: id,
@@ -452,9 +449,7 @@ const actions = {
}); });
}, },
postUpdateContact(context, { obj, id }) { postUpdateContact(context, { obj, id }) {
fetchWrapper.post(`contacts/${id}`, { fetchWrapper.patch(`contacts/${id}`, obj);
...obj,
});
}, },
deleteContact(context, { id }) { deleteContact(context, { id }) {
fetchWrapper.del(`contacts/${id}`); fetchWrapper.del(`contacts/${id}`);