From 79337e20711d301a8e89f64f4216f95cd4733b26 Mon Sep 17 00:00:00 2001 From: Philip Ye <97428041+philipye314@users.noreply.github.com> Date: Wed, 9 Oct 2024 02:24:26 -0400 Subject: [PATCH] [organization] cleaned up code (#11) Co-authored-by: philipye314 --- src/screens/HealingResources/index.tsx | 20 ++++---------------- src/screens/SeekHelp/index.tsx | 21 ++++----------------- src/supabase/queries/generalQueries.tsx | 23 +++++++++++++++++++++++ src/types/types.tsx | 4 ++++ 4 files changed, 35 insertions(+), 33 deletions(-) create mode 100644 src/types/types.tsx diff --git a/src/screens/HealingResources/index.tsx b/src/screens/HealingResources/index.tsx index 25898cb..3a52e61 100644 --- a/src/screens/HealingResources/index.tsx +++ b/src/screens/HealingResources/index.tsx @@ -1,11 +1,7 @@ import React, { useEffect, useState } from 'react'; import { Button, View } from 'react-native'; -import supabase from '../../supabase/createClient'; - -interface Resource { - summary: string; - [key: string]: any; -} +import { getHealingResourceData } from '@/supabase/queries/generalQueries'; +import { Resource } from '@/types/types'; export default function HealingResources() { const [, setSummaries] = useState([]); @@ -16,16 +12,8 @@ export default function HealingResources() { const fetchData = async () => { try { - const { data, error } = await supabase - .from('healing_resources') - .select('*'); - - if (error) { - console.error('Error fetching resources:', error); - return; - } - - setSummaries(data as Resource[]); + const data = await getHealingResourceData(); + setSummaries(data); } catch (error) { console.error('Error fetching data:', error); } diff --git a/src/screens/SeekHelp/index.tsx b/src/screens/SeekHelp/index.tsx index b738352..a6e5df6 100644 --- a/src/screens/SeekHelp/index.tsx +++ b/src/screens/SeekHelp/index.tsx @@ -1,11 +1,7 @@ import React, { useEffect, useState } from 'react'; import { Button, Text, View } from 'react-native'; -import supabase from '../../supabase/createClient'; - -interface Resource { - summary: string; - [key: string]: any; -} +import { getSeekHelpData } from '@/supabase/queries/generalQueries'; +import { Resource } from '@/types/types'; export default function SeekHelp() { const [summaries, setSummaries] = useState([]); @@ -16,17 +12,8 @@ export default function SeekHelp() { const fetchData = async () => { try { - const { data, error } = await supabase - .from('state_resources') - .select('*') - .in('state', ['California', 'National']); - - if (error) { - console.error('Error fetching resources:', error); - return; - } - - setSummaries(data as Resource[]); + const data = await getSeekHelpData(); + setSummaries(data); } catch (error) { console.error('Error fetching data:', error); } diff --git a/src/supabase/queries/generalQueries.tsx b/src/supabase/queries/generalQueries.tsx index e69de29..9ba79c1 100644 --- a/src/supabase/queries/generalQueries.tsx +++ b/src/supabase/queries/generalQueries.tsx @@ -0,0 +1,23 @@ +import { Resource } from '@/types/types'; +import supabase from '../createClient'; + +export const getHealingResourceData = async (): Promise => { + const { data, error } = await supabase.from('healing_resources').select('*'); + if (error) { + throw new Error(`Error fetching resources: ${error.message}`); + } + + return data as Resource[]; +}; + +export const getSeekHelpData = async (): Promise => { + const { data, error } = await supabase + .from('state_resources') + .select('*') + .in('state', ['California', 'National']); + + if (error) { + throw new Error(`Error fetching resources: ${error.message}`); + } + return data as Resource[]; +}; diff --git a/src/types/types.tsx b/src/types/types.tsx new file mode 100644 index 0000000..75ac80a --- /dev/null +++ b/src/types/types.tsx @@ -0,0 +1,4 @@ +export interface Resource { + summary: string; + [key: string]: any; +}