Shopware 6 : Delete data with custom Administration - javascript

I want remove Data of table 'filter_object' with related table 'filter_link' bevor starting the action of button onClickSync().
The 'filter_link' table contains two foreign key : product_id and object_id.
I tried to delete the data by id in a for loop, but this only deletes the data from the 'filter_object' table without related product. In addition it slows down the deletion when I have several data. Could help me please ?
import template from './sw-vehicles-list.html.twig';
const { Component } = Shopware;
const { Criteria } = Shopware.Data;
Component.register('sw-vehicles-list', {
template,
inject: ['repositoryFactory'],
data() {
return {
repository: null,
showAddButton: true,
isLoading: false,
object: null,
};
},
metaInfo() {
return {
title: this.$createTitle()
};
},
computed: {
filterObjectRepository() {
return this.repositoryFactory.create('filter_object');
},
filterLinkRepository() {
return this.repositoryFactory.create('filter_link');
},
productRepository() {
return this.repositoryFactory.create('product');
},
},
created() {
this.object = this.repositoryFactory.create('filter_object');
this.link = this.repositoryFactory.create('filter_link');
},
methods: {
async onClickSync() {
this.isLoading = true;
await this.removeData();
this.repository.search(new Criteria(), Shopware.Context.api).then((result) => {
if (result.length) {
var i;
var manufacturer = [];
for (i = 0; i < result.length; i++) {
manufacturer.push(result[i]['manufacturer']);
}
var manufacturerFilter = Array.from(new Set(manufacturer));
var j;
for ( j = 0; j < manufacturerFilter.length; j++) {
// some code
}
}
});
},
removeData() {
return this.filterObjectRepository.search(new Criteria(), Shopware.Context.api).then((result) => {
if (result.length) {
var i;
for (i = 0; i < result.length; ++i) {
this.filterObjectRepository.delete(result[i]['id'], Shopware.Context.api).then(this.loadObject);
}
return null;
}
});
},
loadObject() {
this.filterObjectRepository.search(new Criteria(), Shopware.Context.api).then((result) => {
this.result = result;
});
},
}
});

You can use the syncDeleted method of the repository to delete multiple records.
const ids = result.map(record => record.id);
this.filterObjectRepository.syncDeleted(ids, Shopware.Context.api)
If you want filter_link records to be deleted when deleting records from filter_object you'll have to set the foreign keys ON DELETE subclause to CASCADE.
ALTER TABLE `filter_link`
DROP FOREIGN KEY `fk.filter_link.object_id`;
ALTER TABLE `filter_link`
ADD CONSTRAINT `fk.filter_link.object_id` FOREIGN KEY (`object_id`) REFERENCES `filter_object` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Related

Trouble with array and state

Can someone explain, why I'm getting an error, when I'm removing items from an array? It works once, but then it crashes. Checked - is boolean meaning.
removeCards = () => {
console.clear();
for (let i = 0; i < this.state.cards.length; i++) {
console.log(this.state.cards[i]);
if (this.state.cards[i].checked) {
delete this.state.cards[i];
}
}
this.setState({ cards: this.state.cards });
};
In React, state is immutable. So instead of trying to alter it directly, create a copy of it and then apply that to state -
removeCards = () => {
console.clear();
const newCards = [];
for (let i = 0; i < this.state.cards; i++) {
if (!this.state.cards[i].checked) {
newCards.push(this.state.cards[i];
}
}
this.setState({ cards: newCards });
};
Maybe my trouble in that? That's how i'm chaning states from true to false and in reverse.
myFunc = (props) => {
let num = Number(props);
num--;
let cards = [...this.state.cards];
if (this.state.cards[num].checked) {
cards[num] = { ...cards[num], checked: false };
} else {
cards[num] = { ...cards[num], checked: true };
}
this.setState({ cards });
};

multiple if condition in for statement for vue.js state management to get data and return

I am using state management in vue.js for fetching products. i am fetching products with ID from data base. i need to make two conditions for return if $state has already products.
first condition is if $state.products has any products than make it return, and
second condition if one ID is passed and related date according to ID is fetched, if again same ID is passed it will return value.
but i am confused how to set condition for multiple match. i also tried logical AND operator for this, but nothing work.
please help me to solve this.
Here is my code.
Product.vue
mounted () {
this.$store.dispatch("loadProducts",this.category);
},
computed:{
...mapState([
'products'
])
},
this is my file where i am handling state management for fetching products.
index.js
const store = new Vuex.Store({
state: {
pageTitle: '',
products: [],
},
mutations: {
setTitle(state, title) {
state.pageTitle = title;
}
},
actions: {
loadProducts({ commit }, id) {
if (this.state.products.length != 0) {
var i;
for (i = 0; i < this.state.products.length; i++) {
var j = this.state.products[i].category_id;
}
} else if (this.state.products.length != 0 && id == j) {
console.log("RETURN");
return;
} else {
axios.get('/loadProducts/' + id)
.then(({ data }) => {
this.state.products = data.items;
console.log(this.state.products);
commit('set_products', data.items)
})
}
},
},
mutations: {
set_products(state, products) {
state.products = products;
}
}
});

setState() in Reactjs updating multiple properties instead of one

When I update 'classProficienciesChoices' in my state using setState() it is updating not only that property, but also where I derived the 'classProficienciesChoices' info from in the 'classSelected' property, AND ALSO from where I derived the classSelected info from in the 'classesInfo' property.
The same function I update 'classProficienciesChoices' I also update 'classProficiencies', and it updates properly in the one property I tell it to, and not the elements where the information was derived from.
Any insight would be helpful. The Create component has other components nested and none of them have state and only use props passed. There are navigation, selection, and information display components nested.
import React, { Component } from 'react'
import Create from './Create'
class App extends Component {
constructor(props) {
super(props);
const url = 'http://www.dnd5eapi.co/api/';
fetch(url + 'classes')
.then(result => result.json())
.then(result => { this.setState({ classes: result, }, this.getInfo(result)) });
}
state = {
classes: {}, //assigned value from API call in constructor
classesInfo: [], //assigned value from API call in getInfo()
classSelected: {}, //assigned a value derived from classInfo in displayClassInfo()
classProficiencies: [], //assigned a value derived from classSelected in setStartingProficiencies()
classProficienciesChoices: [], //assigned a value derived from classSelected in setStartingProficiencies()
}
getInfo(data) {
let info = []
const url = 'http://www.dnd5eapi.co'
for (var i = 0; i < data.results.length; i++) {
fetch(url + data.results[i].url)
.then(result => result.json())
.then(result => info.push(result))
}
this.setState({ classesInfo: info, })
}
}
setStartingProficiencies(chosenClass) {
const profs = chosenClass.proficiencies.map((prof) => {
return prof;
});
const proChoice = chosenClass.proficiency_choices.map((choices) => {
return choices;
});
this.setState({ classProficiencies: profs, classProficienciesChoices: proChoice, });
}
addProficiency = (proficiencyName) => {
const { classProficienciesChoices } = this.state
// classProficienciesChoices: [
// { choose: 2, type: 'proficiencies', from: [{ name: 'someName', url: 'someUrl' }, { name: 'someName', url: 'someUrl' }] },
// ]
// different classes have more objects in the parent array
let newChoiceArray = classProficienciesChoices.map((choices) => {
return choices
})
for (var i = 0; i < newChoiceArray.length; i++) {
for (var j = 0; j < newChoiceArray[i].from.length; j++) {
if (newChoiceArray[i].from[j].name === proficiencyName) {
let newChoices = newChoiceArray[i].from.filter(function (proficiency) { return proficiency.name !== pIndex })
let newProficiency = newChoiceArray[i].from.filter(function (proficiency) { return proficiency.name === pIndex })
newChoiceArray[i].from = newChoices //I think this is the problem
this.setState(state => ({
classProficiencies: [...state.classProficiencies, newProficiency[0]],
proficienciesChoices: newChoiceArray,
}))
}
}
}
}
displayClassInfo = index => {
const { classesInfo } = this.state
for (let i = 0; i < classesInfo.length; i++) {
if (classesInfo[i].index === index) {
const classSelected = classesInfo.filter(function (cClass) { return cClass.name === classesInfo[i].name })
this.setState({ classSelected: classSelected[0], isClassSelected: true }, this.setStartingProficiencies(classSelected[0]),)
break;
}
}
}
render() {
const { classes, classesInfo, classSelected, isClassSelected, classProficiencies, classProficienciesChoices } = this.state
return (<Create classes={classes} classesInfo={classesInfo} displayClassInfo={this.displayClassInfo} classSelected={classSelected} isClassSelected={isClassSelected} category='classes' classProficiencies={classProficiencies} classProficienciesChoices={classProficienciesChoices} addProficiency={this.addProficiency} />);
}
}
export default App
You call setState four times, of course it will update the state multiple times.
SOLVED!!! Stumbled across turning the array into a string and then back. It breaks the reference and creates an entirely new array. Reference type got me.
setStartingProficiencies(chosenClass) {
const proficiencies = JSON.parse(JSON.stringify(chosenClass.proficiencies))
const proficienciesChoices = JSON.parse(JSON.stringify(chosenClass.proficiency_choices))
this.setState({ classProficiencies: proficiencies, classProficienciesChoices: proficienciesChoices, });
}

TypeError: Cannot read property 'style' of undefined

export class EstimateForm extends React.Component<IEstimateFormProps,
IEstimateFormState> {
state: IEstimateFormState = {
cellUpdateCss: 'red',
toRow: null,
fromRow: null,
estimateList: null,
estimateItemList: [],
poseList: null,
levelList: null,
partList: null,
selectedEstimate: null,
totalEstimateItems: 0,
selectedIndexes: [],
totalEstimateAmount: 0,
grid: null,
projectId: 0,
};
constructor(props, context) {
super(props, context);
this.state.estimateList = this.props.estimateList;
}
rowGetter = i => {
const row = this.state.estimateItemList[i];
const selectRevison = this.state.selectedEstimate.revision;
if (row['pose.poseName']) {
const poseCode =
row['pose.poseName'].substring(row['pose.poseName'].lastIndexOf('[') + 1,
row['pose.poseName'].lastIndexOf(']'));
for (const pose of this.state.poseList) {
if (pose.poseCode === poseCode) {
row.pose = pose;
}
}
}
if (row['level.levelName']) {
const levelCode = row['level.levelName'].substring(
row['level.levelName'].lastIndexOf('[') + 1,
row['level.levelName'].lastIndexOf(']')
);
for (const level of this.state.levelList) {
if (level.levelCode === levelCode) {
row.level = level;
}
}
}
if (row['level.part.partName']) {
const partCode = row['level.part.partName'].substring(
row['level.part.partName'].lastIndexOf('[') + 1,
row['level.part.partName'].lastIndexOf(']')
);
for (const part of this.state.partList) {
if (part.partCode === partCode) {
row.part = part;
}
}
}
row.get = key => eval('row.' + key);
row.totalCost = (row.materialCost + row.laborCost) * row.amount;
const changeColor = {
backgroundcolor: 'red'
};
const all = document.getElementsByClassName('react-grid-Row') as
HTMLCollectionOf<HTMLElement>;
debugger; if (row.revision > selectRevison) {
for (let i = this.state.fromRow; i <= this.state.toRow; i++) {
all[i].style.color = 'red'; //HERE
}
return row;
}
}
handleGridRowsUpdated = ({ fromRow, toRow, updated }) => {
const rows = this.state.estimateItemList.slice();
for (let i = fromRow; i <= toRow; i++) {
const rowToUpdate = rows[i];
const updatedRow = update(rowToUpdate, { $merge: updated });
rows[i] = updatedRow;
}
this.setState({ estimateItemList: rows, fromRow: (fromRow), toRow: (toRow)
}, () => {
});
};
saveEstimateItems = () => {
if (this.state.selectedEstimate == null) {
toast.warn(<Translate
contentKey="bpmApp.estimateForm.pleaseSelectEstimate">Please select an
estimate</Translate>);
return;
}
render() {
return ()
}
I wanna to change the row color when the condition row.revision > this.state.selectedEstimate.revision . How can I prevent the change of this.color. However TypeError: Cannot read property 'style' of undefined get error but row color is not change. how can i solve this problem it is my first project in react and i dont know where is the problemThanks for your feedback guys.
Okay, so without the rest of the context because your pasted code is difficult to read and understand, the simplest reason for your issue is in this chunk:
const all = document.getElementsByClassName('react-grid-Row') as
HTMLCollectionOf<HTMLElement>;
debugger; if (row.revision > selectRevison) {
for (let i = this.state.fromRow; i <= this.state.toRow; i++) {
all[i].style.color = 'red'; //HERE
}
Essentially there's multiple things that could go wrong here, but most likely there are either no rows with that class on the page, or less than your this.state.fromRow, I see you've got the debugger in there, but you are missing a few things:
You aren't doing a null check on all to make sure you are finding something
You aren't checking whether all.length > this.state.fromRow
You aren't breaking the for loop if all.length < this.state.toRow
It's failing because all[i] doesn't exist, or there's no values:
all = [0, 1]
and you are looking for all[3] for example
Throw in those fallbacks and check what all is on page load and you should be able to figure it out.

Angular 2 incremental counter not working

i'm strugling with counting and incrementing a number in a for. I'm using Angular 2, typescript, Ionic 2 and Firebase.
Here's the deal, i'm queryng something from firebase, since it's a object i can't use .length on it so i declared a counter: number = 0; in the top of my code and inside my snapshot.val() i'm incrementing it, but in the end when i do a console.log it's 0.
Here is my code (i've took away useless code to post here):
firebase.database().ref('MedicoPacientes/' + id).once('value', snapshot => {
for (var key in snapshot.val()) {
//PEGA TUDO DENTRO DO HISTORICO DE UM USUARIO QUE SEJA TRUE
historico.child(key).orderByChild('permissoes').equalTo(true).once('value', snap => {
for (var key in snap.val()) {
this.counter++;
//also tryed this.counter += 1 and this.counter = this.counter +1;
}
});
historico.child(key).orderByChild('permissoes/' + cpf).equalTo(id).once('value', snap => {
for (var key in snap.val()) {
this.counter++;
//also tryed this.counter += 1 and this.counter = this.counter +1;
}
})
}
})
So it's a for inside a for because i need to get a key before getting the registers i need to count.
Algo i'm saving this registers in a variable that is the object (it's the part i've took from the code to post here) and i also tryed iterating this object and incrementing it
in the start of my code i have:
export class MedicoAlertasPage {
counter: number = 0;
Does someone know what i'm doing wrong?
EDIT 1
Here is the complete page code:
export class MedicoAlertasPage {
paciente: any;
semHistorico: boolean;
limit: any;
counter: number = 0;
meuId: any;
contador: number = 15;
alertas: any[] = [];
alertasFiltrado: any[];
constructor(public navCtrl: NavController, public storage: Storage, public loading: LoadingController) {
}
//COPY THE VALUES TO USE IN THE FILTER
inicializaAlertas() {
this.alertasFiltrado = this.alertas;
}
//FILTER ON TYPING
filtraAcompanhamentos(ev: any) {
this.inicializaAlertas();
// set val to the value of the searchbar
let val = ev.target.value;
// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
this.alertasFiltrado = this.alertasFiltrado.filter((item) => {
return (item.nome.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
//LOAD USER HISTORY
ionViewWillEnter() {
let l = this.loading.create({ content: "Aguarde..." });
l.present();
this.storage.get('id').then(id => {
this.storage.get('cpf').then(cpf => {
let historico = firebase.database().ref('/Historico/');
firebase.database().ref('MedicoPacientes/' + id).once('value', snapshot => {
for (var key in snapshot.val()) {
//GET ALL THE DATA THAT IS TRUE
historico.child(key).orderByChild('permissoes').equalTo(true).once('value', snap => {
for (var key in snap.val()) {
this.counter++;
this.alertas.unshift({
chave: key,
data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
hora: Moment.unix(Number(key)).format('HH:mm'),
descricao: snap.val()[key].descricao,
paciente: snap.val()[key].nome
});
}
});
//GET ALL DATA OF A SELECTED USER
historico.child(key).orderByChild('permissoes/' + cpf).equalTo(id).once('value', snap => {
for (var key in snap.val()) {
this.counter++;
this.alertas.unshift({
chave: key,
data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
hora: Moment.unix(Number(key)).format('HH:mm'),
descricao: snap.val()[key].descricao,
paciente: snap.val()[key].nome
});
}
})
}
this.inicializaAlertas();
l.dismiss();
})
});
})
}
}
Change your code to this:
ionViewWillEnter() {
let l = this.loading.create({ content: "Aguarde..." });
l.present();
this.storage.get('id').then(id => {
this.storage.get('cpf').then(cpf => {
let historico = firebase.database().ref('/Historico/');
firebase.database().ref('MedicoPacientes/' + id).once('value', snapshot => {
for (var key in snapshot.val()) {
//GET ALL THE DATA THAT IS TRUE
historico.child(key).orderByChild('permissoes').equalTo(true).once('value', snap => {
for (var key in snap.val()) {
this.counter++;
this.alertas.unshift({
chave: key,
data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
hora: Moment.unix(Number(key)).format('HH:mm'),
descricao: snap.val()[key].descricao,
paciente: snap.val()[key].nome
});
}
//GET ALL DATA OF A SELECTED USER
historico.child(key).orderByChild('permissoes/' + cpf).equalTo(id).once('value', snap => {
for (var key in snap.val()) {
this.counter++;
this.alertas.unshift({
chave: key,
data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
hora: Moment.unix(Number(key)).format('HH:mm'),
descricao: snap.val()[key].descricao,
paciente: snap.val()[key].nome
});
}
this.inicializaAlertas();
l.dismiss();
})
});
}
})
});
})
}

Categories

Resources