Fix linting errors

This commit is contained in:
Brandon Presley 2022-07-09 13:27:19 +12:00
parent a06b9e7ee5
commit dbb3dc2eb3
7 changed files with 66 additions and 73 deletions

25
App.tsx
View File

@ -36,20 +36,19 @@ const App = () => {
const [db, setDb] = useState<SQLiteDatabase | null>(null); const [db, setDb] = useState<SQLiteDatabase | null>(null);
const dark = useColorScheme() === 'dark'; const dark = useColorScheme() === 'dark';
const init = async () => {
const gotDb = await getDb();
await gotDb.executeSql(createPlans);
await gotDb.executeSql(createSets);
setDb(gotDb);
const minutes = await getItem('minutes');
if (minutes === null) await setItem('minutes', '3');
const seconds = await getItem('seconds');
if (seconds === null) await setItem('seconds', '30');
const alarmEnabled = await getItem('alarmEnabled');
if (alarmEnabled === null) await setItem('alarmEnabled', 'false');
};
useEffect(() => { useEffect(() => {
const init = async () => {
const gotDb = await getDb();
await gotDb.executeSql(createPlans);
await gotDb.executeSql(createSets);
setDb(gotDb);
const minutes = await getItem('minutes');
if (minutes === null) await setItem('minutes', '3');
const seconds = await getItem('seconds');
if (seconds === null) await setItem('seconds', '30');
const alarmEnabled = await getItem('alarmEnabled');
if (alarmEnabled === null) await setItem('alarmEnabled', 'false');
};
init(); init();
}, []); }, []);

View File

@ -1,4 +1,4 @@
import React, {useContext, useEffect, useState} from 'react'; import React, {useCallback, useContext, useEffect, useState} from 'react';
import {FlatList, StyleSheet, View} from 'react-native'; import {FlatList, StyleSheet, View} from 'react-native';
import {List, Searchbar} from 'react-native-paper'; import {List, Searchbar} from 'react-native-paper';
import {DatabaseContext} from './App'; import {DatabaseContext} from './App';
@ -12,22 +12,22 @@ export default function BestPage() {
const [best, setBest] = useState<Best>(); const [best, setBest] = useState<Best>();
const db = useContext(DatabaseContext); const db = useContext(DatabaseContext);
const bestWeight = ` const refresh = useCallback(async () => {
SELECT name, reps, unit, MAX(weight) AS weight const bestWeight = `
FROM sets SELECT name, reps, unit, MAX(weight) AS weight
WHERE name LIKE ? FROM sets
GROUP BY name; WHERE name LIKE ?
`; GROUP BY name;
`;
const bestReps = ` const bestReps = `
SELECT name, MAX(reps) as reps, unit, weight SELECT name, MAX(reps) as reps, unit, weight
FROM sets FROM sets
WHERE name = ? WHERE name = ?
AND weight = ? AND weight = ?
GROUP BY name; GROUP BY name;
`; `;
const refresh = async () => {
const [weight] = await db.executeSql(bestWeight, [`%${search}%`]); const [weight] = await db.executeSql(bestWeight, [`%${search}%`]);
if (!weight) return setBests([]); if (!weight) return setBests([]);
let newBest: Best[] = []; let newBest: Best[] = [];
@ -39,7 +39,7 @@ export default function BestPage() {
newBest = newBest.concat(reps.rows.raw()); newBest = newBest.concat(reps.rows.raw());
} }
setBests(newBest); setBests(newBest);
}; }, [search]);
useEffect(() => { useEffect(() => {
refresh(); refresh();

View File

@ -28,16 +28,17 @@ export default function EditPlan({
const [names, setNames] = useState<string[]>([]); const [names, setNames] = useState<string[]>([]);
const db = useContext(DatabaseContext); const db = useContext(DatabaseContext);
const refresh = async () => {
const [namesResult] = await db.executeSql('SELECT DISTINCT name FROM sets');
if (!namesResult.rows.length) return setNames([]);
setNames(namesResult.rows.raw().map(({name}) => name));
if (!plan) return;
setDays(plan.days.split(','));
setWorkouts(plan.workouts.split(','));
};
useEffect(() => { useEffect(() => {
const refresh = async () => {
const [namesResult] = await db.executeSql(
'SELECT DISTINCT name FROM sets',
);
if (!namesResult.rows.length) return setNames([]);
setNames(namesResult.rows.raw().map(({name}) => name));
if (!plan) return;
setDays(plan.days.split(','));
setWorkouts(plan.workouts.split(','));
};
refresh(); refresh();
}, [plan]); }, [plan]);

View File

@ -1,5 +1,5 @@
import AsyncStorage from '@react-native-async-storage/async-storage'; import AsyncStorage from '@react-native-async-storage/async-storage';
import React, {useContext, useEffect, useState} from 'react'; import React, {useCallback, useContext, useEffect, useState} from 'react';
import {FlatList, NativeModules, StyleSheet, View} from 'react-native'; import {FlatList, NativeModules, StyleSheet, View} from 'react-native';
import {List, Searchbar} from 'react-native-paper'; import {List, Searchbar} from 'react-native-paper';
import {DatabaseContext} from './App'; import {DatabaseContext} from './App';
@ -25,23 +25,14 @@ export default function HomePage() {
ORDER BY created DESC ORDER BY created DESC
LIMIT ? OFFSET ? LIMIT ? OFFSET ?
`; `;
const getSets = ({
search,
limit,
offset,
}: {
search: string;
limit: number;
offset: number;
}) => db.executeSql(selectSets, [`%${search}%`, limit, offset]);
const refresh = async () => { const refresh = useCallback(async () => {
const [result] = await getSets({search, limit, offset: 0}); const [result] = await db.executeSql(selectSets, [`%${search}%`, limit, 0]);
if (!result) return setSets([]); if (!result) return setSets([]);
setSets(result.rows.raw()); setSets(result.rows.raw());
setOffset(0); setOffset(0);
setEnd(false); setEnd(false);
}; }, [search]);
const refreshLoader = async () => { const refreshLoader = async () => {
setRefresing(true); setRefresing(true);
@ -66,19 +57,19 @@ export default function HomePage() {
NativeModules.AlarmModule.timer(milliseconds); NativeModules.AlarmModule.timer(milliseconds);
}; };
const next = async () => { const next = useCallback(async () => {
if (end) return; if (end) return;
setRefresing(true); setRefresing(true);
const newOffset = offset + limit; const newOffset = offset + limit;
const [result] = await getSets({search, limit, offset: newOffset}).finally( const [result] = await db
() => setRefresing(false), .executeSql(selectSets, [`%${search}%`, limit, newOffset])
); .finally(() => setRefresing(false));
if (result.rows.length === 0) return setEnd(true); if (result.rows.length === 0) return setEnd(true);
if (!sets) return; if (!sets) return;
setSets([...sets, ...result.rows.raw()]); setSets([...sets, ...result.rows.raw()]);
if (result.rows.length < limit) return setEnd(true); if (result.rows.length < limit) return setEnd(true);
setOffset(newOffset); setOffset(newOffset);
}; }, [search, end]);
return ( return (
<View style={styles.container}> <View style={styles.container}>

View File

@ -1,4 +1,4 @@
import React, {useContext, useEffect, useState} from 'react'; import React, {useCallback, useContext, useEffect, useState} from 'react';
import {FlatList, StyleSheet, View} from 'react-native'; import {FlatList, StyleSheet, View} from 'react-native';
import {List, Searchbar} from 'react-native-paper'; import {List, Searchbar} from 'react-native-paper';
import {DatabaseContext} from './App'; import {DatabaseContext} from './App';
@ -14,21 +14,20 @@ export default function PlanPage() {
const [plan, setPlan] = useState<Plan>(); const [plan, setPlan] = useState<Plan>();
const db = useContext(DatabaseContext); const db = useContext(DatabaseContext);
const selectPlans = ` const refresh = useCallback(async () => {
SELECT * from plans const selectPlans = `
WHERE days LIKE ? OR workouts LIKE ? SELECT * from plans
`; WHERE days LIKE ? OR workouts LIKE ?
const getPlans = ({search}: {search: string}) => `;
db.executeSql(selectPlans, [`%${search}%`, `%${search}%`]); const getPlans = ({s}: {s: string}) =>
db.executeSql(selectPlans, [`%${s}%`, `%${s}%`]);
const refresh = async () => { const [plansResult] = await getPlans({s: search});
const [plansResult] = await getPlans({search});
setPlans(plansResult.rows.raw()); setPlans(plansResult.rows.raw());
}; }, [search, db]);
useEffect(() => { useEffect(() => {
refresh(); refresh();
}, [search]); }, [search, refresh]);
const renderItem = ({item}: {item: Plan}) => ( const renderItem = ({item}: {item: Plan}) => (
<PlanItem item={item} key={item.id} setPlan={setPlan} onRemove={refresh} /> <PlanItem item={item} key={item.id} setPlan={setPlan} onRemove={refresh} />

View File

@ -65,9 +65,9 @@ export default function SettingsPage() {
value={seconds} value={seconds}
keyboardType="numeric" keyboardType="numeric"
placeholder="30" placeholder="30"
onChangeText={seconds => { onChangeText={s => {
setSeconds(seconds); setSeconds(s);
setItem('seconds', seconds); setItem('seconds', s);
}} }}
style={styles.text} style={styles.text}
/> />

View File

@ -34,7 +34,7 @@ export default function ViewBest({
setUnit(result.rows.item(0).unit); setUnit(result.rows.item(0).unit);
}; };
refresh(); refresh();
}, [best]); }, [best, db]);
const contentInset = {top: 20, bottom: 20}; const contentInset = {top: 20, bottom: 20};
@ -65,7 +65,10 @@ export default function ViewBest({
</View> </View>
</Dialog.Content> </Dialog.Content>
<Dialog.Actions> <Dialog.Actions>
<Button icon="close" onPress={() => setBest(undefined)}> <Button
mode="contained"
icon="close"
onPress={() => setBest(undefined)}>
Close Close
</Button> </Button>
</Dialog.Actions> </Dialog.Actions>