From b3b65b7af5b740702a1ef4833b1a0395b1975fd5 Mon Sep 17 00:00:00 2001 From: moroclash Date: Sat, 2 Jan 2021 17:09:48 +0200 Subject: [PATCH] fix state of choices (dropDownLists) bug --- src/components/AddBar.jsx | 55 ++++++------------------------ src/components/AddCharBar.jsx | 64 +++++++++++++++++++++++++---------- src/components/CharTable.jsx | 2 +- src/models/Prepare.js | 2 +- 4 files changed, 60 insertions(+), 63 deletions(-) diff --git a/src/components/AddBar.jsx b/src/components/AddBar.jsx index 363050d..fa355d7 100644 --- a/src/components/AddBar.jsx +++ b/src/components/AddBar.jsx @@ -1,69 +1,36 @@ import React, { Component } from 'react'; import List from './List'; import { Navbar, Form, Button } from 'react-bootstrap' -import { getState } from '../models/Prepare'; +// import { getState } from '../models/Prepare'; class AddBar extends Component { constructor(props) { super(props) this.state = { chooseAll: false, - souraValue: this.props.currentSouraID, - ayaValue: null, - charValue: null, - charIndex: null, - systemValue: null, } } - componentWillReceiveProps(nextProps){ - this.setState({ - souraValue: nextProps.currentSouraID, - ayaValue: nextProps.ayaValue, - charValue: nextProps.charValue, - charIndex: nextProps.charIndex, - systemValue: nextProps.systemValue, - }) - } - onSouraChange = (value) => { let id = Number(value.split(':')[0].trim()) this.setState({ chooseAll: value.localeCompare('0 : ALl') === 0 ? true : false, - souraValue: id }) this.props.onSouraChange(id) } onAyaChange = (value) => { - this.setState({ ayaValue: (value === 'All') ? 0 : Number(value) }) + this.props.onAyaChange((value === 'All') ? 0 : Number(value)) } onSystemChange = (value) => { - let id = Number(value.split(':')[0].trim()) - this.setState({ - systemValue: id, - }) - this.props.onSystemChange(id) + this.props.onSystemChange(Number(value.split(':')[0].trim())) } onCharChange = (value) => { let [id, val] = value.split(':') - this.setState({ charValue: val.trim(), charIndex: Number(id.trim()) }) - } - - onAdd = () => { - // console.log(this.state) - this.props.onAdd( - getState(0, - (this.state.souraValue === 0) ? this.props.Quran : this.props.Soura, - this.state.souraValue, - this.state.ayaValue, - this.state.systemValue, - this.props.Quran.systems_info[this.state.systemValue], - this.state.charValue) - ) + this.props.onCharChange(val.trim(), Number(id.trim())) } render() { @@ -72,31 +39,31 @@ class AddBar extends Component { إضافه حرف
- + ); diff --git a/src/components/AddCharBar.jsx b/src/components/AddCharBar.jsx index 6a18b18..3a55562 100644 --- a/src/components/AddCharBar.jsx +++ b/src/components/AddCharBar.jsx @@ -1,6 +1,7 @@ import React, { Component } from 'react'; import { getSouraInfo } from '../models/Prepare'; import AddBar from './AddBar'; +import { getState } from '../models/Prepare'; function getData(id, callback) { getSouraInfo(id) @@ -10,11 +11,14 @@ function getData(id, callback) { .catch(err => err) } -function getDataObject(soura, systemNum){ - return ({Soura: soura, - ayaList: Object.keys(soura.ayat), - charList: Object.keys(soura.ayat[1].systems[systemNum].groups) - }) +function getDataObject(soura, systemNum) { + return ({ + Soura: soura, + ayaList: Object.keys(soura.ayat), + charList: Object.keys(soura.ayat[1].systems[systemNum].groups), + souraID: Number(soura.soura_num), + souraValue: soura.soura_name + }) } @@ -29,26 +33,56 @@ class AddCharBar extends Component { systemValue: 0, ayaValue: 1, charValue: '', + souraValue: '', charIndex: 0, } } onSouraChange = (id) => { - getData(id, (soura) => { - this.setState(getDataObject(soura, this.state.systemValue)) - }) + if (id === 0) { + this.setState({souraID: id}) + } + else { + getData(id, (soura) => { + this.setState(getDataObject(soura, this.state.systemValue)) + }) + } } onSystemChange = (id) => { this.setState({ systemValue: id, + charList: Object.keys(this.state.Soura.ayat[1].systems[id].groups) }) } + + onAyaChange = (id) => { + this.setState({ ayaValue: id }) + } + + onCharChange = (char, id) => { + this.setState({ charValue: char, charIndex: id }) + } + + onAdd = () => { + this.props.onAdd( + getState(0, + (this.state.souraID === 0) ? this.props.Quran : this.state.Soura, + this.state.souraValue, + this.state.ayaValue, + this.state.systemValue, + this.props.Quran.systems_info[this.state.systemValue], + this.state.charValue) + ) + } + + componentDidMount() { getData(this.state.souraID, (soura) => { let obj = getDataObject(soura, this.state.systemValue) obj.charValue = obj.charList[this.state.charIndex] + obj.souraValue = this.props.Quran.swar_names[this.state.souraID] this.setState(obj) }); }; @@ -57,16 +91,12 @@ class AddCharBar extends Component { return ( + onAyaChange={this.onAyaChange} + onCharChange={this.onCharChange} + onAdd={this.onAdd} + onSystemChange={this.onSystemChange} /> ); } } diff --git a/src/components/CharTable.jsx b/src/components/CharTable.jsx index 6c6168e..1f36b70 100644 --- a/src/components/CharTable.jsx +++ b/src/components/CharTable.jsx @@ -120,7 +120,7 @@ class MuiVirtualizedTable extends React.PureComponent { align="center"//{columns[columnIndex].numeric || false ? 'center' : 'center'} >{(label.startsWith('img:')) ? : {label} diff --git a/src/models/Prepare.js b/src/models/Prepare.js index cb9e265..a7ead69 100755 --- a/src/models/Prepare.js +++ b/src/models/Prepare.js @@ -147,7 +147,7 @@ export async function getSouraInfo(soura_num){ export function getState(id, dataObj, soura_id, aya_num, system_id, system, char){ - // console.log(id, dataObj, soura_id, aya_num, system_id, system, char) + console.log(id, dataObj, soura_id, aya_num, system_id, system, char) //check if All Quran if(soura_id === 0){ return dataObj.getAllInfoForChar(id, system_id, char)