From 1851f7edd4c0076176191e8c13a86fd40add5ee3 Mon Sep 17 00:00:00 2001 From: navins94 Date: Fri, 26 Jan 2024 20:57:42 +0530 Subject: [PATCH 1/5] [#1084] Add administration data to users table --- app/src/database/crud/crud-forms.js | 6 +++--- app/src/database/tables.js | 1 + app/src/pages/AuthForm.js | 7 ++++++- app/src/pages/DataSync/ForSelection.js | 6 ++---- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/src/database/crud/crud-forms.js b/app/src/database/crud/crud-forms.js index a0c7bc1ae..730a1a978 100644 --- a/app/src/database/crud/crud-forms.js +++ b/app/src/database/crud/crud-forms.js @@ -69,9 +69,9 @@ const formsQuery = () => { const updateQuery = query.update('forms', { formId }, { latest: latest }); return await conn.tx(db, updateQuery, [formId]); }, - getMyForms: async ({ id }) => { - const sqlQuery = 'SELECT id, name FROM forms WHERE id = $1'; - const { rows } = await conn.tx(db, sqlQuery, [id]); + getMyForms: async () => { + const sqlQuery = 'SELECT id, name FROM forms'; + const { rows } = await conn.tx(db, sqlQuery); if (!rows.length) { return {}; diff --git a/app/src/database/tables.js b/app/src/database/tables.js index 484edb1ee..112cace4c 100644 --- a/app/src/database/tables.js +++ b/app/src/database/tables.js @@ -7,6 +7,7 @@ export const tables = [ password: 'TEXT', active: 'TINYINT', token: 'TEXT', + administrationList: 'TEXT', }, }, { diff --git a/app/src/pages/AuthForm.js b/app/src/pages/AuthForm.js index e7fbd3e16..cd18c4410 100644 --- a/app/src/pages/AuthForm.js +++ b/app/src/pages/AuthForm.js @@ -47,6 +47,7 @@ const AuthForm = ({ navigation }) => { active: 1, token: data?.syncToken, password: data?.passcode, + administrationList: data.administrationList, }); UserState.update((s) => { s.id = newUserId; @@ -108,7 +109,11 @@ const AuthForm = ({ navigation }) => { s.token = bearerToken; }); - const userID = await handleActiveUser({ ...data, passcode }); + const userID = await handleActiveUser({ + ...data, + passcode, + administrationList: JSON.stringify(data.administrations), + }); await handleGetAllForms(data.formsUrl, userID); diff --git a/app/src/pages/DataSync/ForSelection.js b/app/src/pages/DataSync/ForSelection.js index ddce03ad0..7644116cf 100644 --- a/app/src/pages/DataSync/ForSelection.js +++ b/app/src/pages/DataSync/ForSelection.js @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react'; import { ScrollView, View } from 'react-native'; import { ListItem } from '@rneui/themed'; import { BaseLayout } from '../../components'; -import { UIState, UserState } from '../../store'; +import { UIState } from '../../store'; import { i18n } from '../../lib'; import { crudForms } from '../../database/crud'; @@ -11,12 +11,10 @@ const FormSelection = ({ navigation }) => { const activeLang = UIState.useState((s) => s.lang); const trans = i18n.text(activeLang); - const { id: currentUserId } = UserState.useState((s) => s); - useEffect(() => { const fetchForms = async () => { try { - const formsData = await crudForms.getMyForms({ id: currentUserId }); + const formsData = await crudForms.getMyForms(); setForms(formsData); } catch (error) { console.error('Error fetching forms:', error); From 82b9adaf38ba47172c68358fe8f3a3a7d027f35a Mon Sep 17 00:00:00 2001 From: navins94 Date: Fri, 26 Jan 2024 21:34:50 +0530 Subject: [PATCH 2/5] [#1084] Add search functionality --- app/src/database/crud/crud-users.js | 17 ++++ app/src/pages/DataSync/AdministrationList.js | 87 ++++++++++++++++---- 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/app/src/database/crud/crud-users.js b/app/src/database/crud/crud-users.js index f4bdaaa51..ec9153766 100644 --- a/app/src/database/crud/crud-users.js +++ b/app/src/database/crud/crud-users.js @@ -45,6 +45,23 @@ const usersQuery = () => { const { rows } = await conn.tx(db, query.read('users', { password: passcode }), [passcode]); return rows; }, + getUserAdministrationListChunk: async (userId, start, end) => { + try { + const sqlQuery = 'SELECT administrationList FROM users WHERE id = ?'; + const { rows } = await conn.tx(db, sqlQuery, [userId]); + + if (!rows.length || !rows._array[0].administrationList) { + return []; + } + + const fullList = JSON.parse(rows._array[0].administrationList); + const chunk = fullList.slice(start, end); + return chunk; + } catch (error) { + console.error('Get user administration list chunk:', error); + return []; + } + }, }; }; diff --git a/app/src/pages/DataSync/AdministrationList.js b/app/src/pages/DataSync/AdministrationList.js index 534985333..d66bf3dde 100644 --- a/app/src/pages/DataSync/AdministrationList.js +++ b/app/src/pages/DataSync/AdministrationList.js @@ -1,17 +1,76 @@ import React, { useState, useEffect } from 'react'; -import { ScrollView, StyleSheet, View } from 'react-native'; +import { ActivityIndicator, FlatList, ScrollView, StyleSheet, View } from 'react-native'; import { ListItem, Button } from '@rneui/themed'; import { BaseLayout } from '../../components'; -import { UIState } from '../../store'; +import { UIState, UserState } from '../../store'; import { i18n } from '../../lib'; import Icon from 'react-native-vector-icons/Ionicons'; +import { crudUsers } from '../../database/crud'; +const PAGE_SIZE = 50; // Adjust as needed const FormSelection = ({ navigation, route }) => { const params = route?.params || null; + const [filteredData, setFilteredData] = useState([]); const [search, setSearch] = useState(''); + const [data, setData] = useState([]); + const [loading, setLoading] = useState(false); + const [hasMore, setHasMore] = useState(true); const activeLang = UIState.useState((s) => s.lang); const trans = i18n.text(activeLang); + const { id: currentUserId } = UserState.useState((s) => s); + + const loadMoreData = async () => { + if (loading || !hasMore) return; + setLoading(true); + + const start = data.length; + const end = start + PAGE_SIZE; + const newData = await crudUsers.getUserAdministrationListChunk(currentUserId, start, end); + + if (newData.length < PAGE_SIZE) { + setHasMore(false); + } + + const updatedData = [...data, ...newData]; + setData(updatedData); + filterData(search, updatedData); + + setLoading(false); + }; + + const filterData = (searchText, list) => { + const searchWords = searchText + .toLowerCase() + .split(' ') + .filter((word) => word.trim() !== ''); + const filtered = list.filter((item) => { + return searchWords.every((word) => item.name.toLowerCase().includes(word)); + }); + setFilteredData(filtered); + }; + + useEffect(() => { + loadMoreData(); + }, []); + + useEffect(() => { + filterData(search, data); + }, [search, data]); + + const renderItem = ({ item }) => ( + + + {item.name} + +