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 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(() => {
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();
}, []);

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

View File

@ -28,16 +28,17 @@ export default function EditPlan({
const [names, setNames] = useState<string[]>([]);
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(() => {
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();
}, [plan]);

View File

@ -1,5 +1,5 @@
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 {List, Searchbar} from 'react-native-paper';
import {DatabaseContext} from './App';
@ -25,23 +25,14 @@ export default function HomePage() {
ORDER BY created DESC
LIMIT ? OFFSET ?
`;
const getSets = ({
search,
limit,
offset,
}: {
search: string;
limit: number;
offset: number;
}) => db.executeSql(selectSets, [`%${search}%`, limit, offset]);
const refresh = async () => {
const [result] = await getSets({search, limit, offset: 0});
const refresh = useCallback(async () => {
const [result] = await db.executeSql(selectSets, [`%${search}%`, limit, 0]);
if (!result) return setSets([]);
setSets(result.rows.raw());
setOffset(0);
setEnd(false);
};
}, [search]);
const refreshLoader = async () => {
setRefresing(true);
@ -66,19 +57,19 @@ export default function HomePage() {
NativeModules.AlarmModule.timer(milliseconds);
};
const next = async () => {
const next = useCallback(async () => {
if (end) return;
setRefresing(true);
const newOffset = offset + limit;
const [result] = await getSets({search, limit, offset: newOffset}).finally(
() => setRefresing(false),
);
const [result] = await db
.executeSql(selectSets, [`%${search}%`, limit, newOffset])
.finally(() => setRefresing(false));
if (result.rows.length === 0) return setEnd(true);
if (!sets) return;
setSets([...sets, ...result.rows.raw()]);
if (result.rows.length < limit) return setEnd(true);
setOffset(newOffset);
};
}, [search, end]);
return (
<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 {List, Searchbar} from 'react-native-paper';
import {DatabaseContext} from './App';
@ -14,21 +14,20 @@ export default function PlanPage() {
const [plan, setPlan] = useState<Plan>();
const db = useContext(DatabaseContext);
const selectPlans = `
SELECT * from plans
WHERE days LIKE ? OR workouts LIKE ?
`;
const getPlans = ({search}: {search: string}) =>
db.executeSql(selectPlans, [`%${search}%`, `%${search}%`]);
const refresh = async () => {
const [plansResult] = await getPlans({search});
const refresh = useCallback(async () => {
const selectPlans = `
SELECT * from plans
WHERE days LIKE ? OR workouts LIKE ?
`;
const getPlans = ({s}: {s: string}) =>
db.executeSql(selectPlans, [`%${s}%`, `%${s}%`]);
const [plansResult] = await getPlans({s: search});
setPlans(plansResult.rows.raw());
};
}, [search, db]);
useEffect(() => {
refresh();
}, [search]);
}, [search, refresh]);
const renderItem = ({item}: {item: Plan}) => (
<PlanItem item={item} key={item.id} setPlan={setPlan} onRemove={refresh} />

View File

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

View File

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