Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { View, Text, StyleSheet } from 'react-native'
- import React, { useEffect, useState } from 'react'
- import { Link } from 'expo-router'
- import databaseHelper from '../service/databasehelper.js'
- import AppContext from '../service/AppContext.js'
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- display: 'flex',
- alignItems: 'center',
- justifyContent: 'center'
- },
- linkContainer: {
- display: 'flex',
- flexDirection: 'column',
- justifyContent: 'space-around',
- width: '50%',
- margin: 100,
- },
- linkText: {
- fontSize: 20,
- textAlign: 'center',
- fontWeight: 'bold'
- }
- })
- export default function Index() {
- //Github copilot gave me the knowledge to fix my error regarding using async functions in useEffect
- //The previous error was that await functions returned a promise, and useEffect does not support promises/returning anything (except clean up)
- //Now the initiliasing of the db is wrapped in an async function, this async function does not return anything.
- //asyncInitDb is also local to the scope of UseEffect.
- //The [] at the end of useEffect is an empty dependency array, this means that the useEffect will only run once, when the component mounts.
- useEffect(() => {
- async function asyncInitDb() {
- const dbInitVal = await databaseHelper.readOperatingValues("SELECT value FROM operatingValues WHERE name = 'dbInit'");
- try {
- if (dbInitVal === 0) {
- await databaseHelper.initDatabase()
- databaseHelper.readDb()
- }
- } catch (TypeError) {
- console.log("DB not initialised")
- await databaseHelper.initDatabase()
- databaseHelper.readDb()
- }
- }
- asyncInitDb();
- }, []);
- const [template1, setTemplate1] = useState([]);
- const [template2, setTemplate2] = useState([]);
- const [isLoading, setIsLoading] = useState(true); //Prevents data from rendering before data is fetched from the database
- useEffect(() => {
- /**
- * Inserts workout templates into the database and reads all templates.
- * @returns {Promise<void>}
- */
- async function testTemplatesAndRead() {
- const templatesInit = await databaseHelper.readOperatingValues("SELECT value FROM operatingValues WHERE name = 'templatesInit'");
- if (templatesInit[0].value === 0) {
- //Create template the "pull" template.
- await databaseHelper.insertWorkoutTemplate(1, "Push", 1, 0, 20, 1, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 1, 0, 15, 2, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 1, 0, 10, 3, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 7, 55, 8, 1, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 7, 55, 6, 2, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 7, 55, 4, 3, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 9, 25, 8, 1, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 9, 25, 6, 2, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 9, 25, 4, 3, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 14, 25, 10, 1, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 14, 25, 8, 2, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 14, 25, 6, 3, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 12, 30, 8, 1, 1);
- await databaseHelper.insertWorkoutTemplate(1, "Push", 12, 30, 6, 2, 1);
- //The "push" template.
- await databaseHelper.insertWorkoutTemplate(2, "Pull", 4, 12, 8, 1, 1);
- await databaseHelper.insertWorkoutTemplate(2, "Pull", 4, 12, 6, 2, 1);
- await databaseHelper.insertWorkoutTemplate(2, "Pull", 6, 120, 8, 1, 1);
- await databaseHelper.insertWorkoutTemplate(2, "Pull", 6, 120, 6, 2, 1);
- await databaseHelper.insertWorkoutTemplate(2, "Pull", 11, 0, 8, 1, 1);
- await databaseHelper.insertWorkoutTemplate(2, "Pull", 11, 0, 6, 2, 1);
- await databaseHelper.readOperatingValues("UPDATE operatingValues SET value = 1 WHERE name = 'templatesInit'");
- }
- const tempTemplate1 = await databaseHelper.readTemplates("SELECT * FROM Templates INNER JOIN Workouts ON Templates.workoutId = Workouts.workoutId WHERE Templates.templateId = 1");
- console.log("This is template1" + tempTemplate1);
- setTemplate1(tempTemplate1);
- const tempTemplate2 = await databaseHelper.readTemplates("SELECT * FROM Templates INNER JOIN Workouts ON Templates.workoutId = Workouts.workoutId WHERE Templates.templateId = 2");
- console.log("This is template2" + tempTemplate2);
- setTemplate2(tempTemplate2);
- setIsLoading(false); //Indicates that the data has been fetched from the database
- }
- testTemplatesAndRead();
- }, []);
- return (
- <AppContext.Provider value={{ template1, template2, isLoading }}>
- <View style={styles.container}>
- <View style={styles.linkContainer}>
- <Link push href="/home">
- <Text style={styles.linkText}>Home</Text>
- </Link>
- </View>
- <View style={styles.linkContainer}>
- <Link push href="/trainer">
- <Text style={styles.linkText}>Trainer</Text>
- </Link>
- </View>
- </View>
- </AppContext.Provider>
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement