· 6 years ago · May 10, 2019, 04:42 PM
1async componentDidMount() {
2 db.transaction(tx => {
3 tx.executeSql(
4 'create table if not exists companies (id integer primary key not null, title text, body text, field_address text, field_city text, field_contact_person_email text, field_contact_person_name text, field_image text, field_industry text, field_state text, field_website text, field_zip_code text);'
5 );
6 tx.executeSql('create table if not exists industries (id integer primary key not null, title text);');
7 });
8 await this._fetchCompaniesFromAPI()
9 .then(companies => {
10 db.transaction(tx => tx.executeSql('delete from companies'));
11 return companies.map(item => {
12 db.transaction(
13 tx => {
14 tx.executeSql(
15 'insert into companies (title, body, field_address, field_city, field_contact_person_email, field_contact_person_name, field_image, field_industry, field_state, field_website, field_zip_code) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
16 [
17 item.title,
18 item.body,
19 item.field_address,
20 item.field_city,
21 item.field_contact_person_email,
22 item.field_contact_person_name,
23 item.field_image,
24 item.field_industry,
25 item.field_state,
26 item.field_website,
27 item.field_zip_code
28 ]
29 );
30 },
31 () => console.error
32 );
33 });
34 })
35 .then(() => {
36 db.transaction(tx => {
37 tx.executeSql('select * from companies', [], async (_, { rows }) => {
38 this.setState({
39 companies: ALPHABET.map(letter => {
40 return {
41 character: letter,
42 data: rows._array.filter(item => {
43 return item.title.charAt(0).toUpperCase() === letter;
44 })
45 };
46 })
47 });
48 });
49 });
50 })
51 .catch(console.error)
52 .finally(() => {
53 this.setState({ isAppReady: true });
54 });
55 }