3

I am using redux with react-native-sqlite-storage. First I need to fetch data from api call then save it in local database. I can fetch the data using redux. but how to save data in react-native-sqlite-storage using redux? How can I make my action creator? The following code gives me desired result without using redux.

foodTblCreate = () => {
        db.transaction(function(txn) {
          txn.executeSql(
            "SELECT name from sqlite_master WHERE type='table' AND name='table_food'", [],
            function(tx, res) {
              alert(`table_food is : ${res.rows.length}`);
              if (res.rows.length === 0) {
                txn.executeSql('DROP TABLE IF EXISTS table_food', []);
                txn.executeSql(
                  "CREATE TABLE IF NOT EXISTS table_food(m_id INTEGER PRIMARY KEY AUTOINCREMENT, m_name VARCHAR(50), m_unit VARCHAR(250), cat_id VARCHAR(5), m_cal VARCHAR(6))",
                  [], 
                  (tx, results) => {  
                      for(var i=0; i<categoryArr.length; ++i) {
                        var cat_id = categoryArr[i].id;
                      for(var j=0;j<nutrition[cat_id].length; ++j) {
                        var m_name = nutrition[cat_id][j].name;
                        var m_unit = nutrition[cat_id][j].unit;
                        var cat_id = cat_id;
                        var m_cal = nutrition[cat_id][j].unit;
                        tx.executeSql(
                              "INSERT INTO table_food(m_name, m_unit, cat_id, m_cal) VALUES (?,?,?,?)",
                              [m_name, m_unit, cat_id, m_cal],
                              (tx, results) => {
                                 alert('tableFoodData : '+JSON.stringify(results));
                              }
                          );
                      } 
                    }
                    (error) => {
                      alert("Got an error in table_food"+JSON.stringify(error));
                      if (error) {
                         alert(JSON.stringify(error));
                      }
                  } 
                  }
                );
              }
            }
          );  
           (error) => {
             alert("Got an error"+JSON.stringify(error));
             if (error) {
                alert(JSON.stringify(error));
             }

           }
        });
      }

Now my question is whether I can use the loops in actionCreator and put the whole code in actionCreator breaking the process in STARTED, SUCCESS, FAIL. Or I should not use redux while saving data to local storage. Any kind of help is greatly appreciated. Thanks in advance.

Neha
  • 175
  • 1
  • 3
  • 18

0 Answers0