Skip to content

Commit

Permalink
[feat] complete form fetching from supabase with hardcoded caseUid.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronniebeggs committed Nov 12, 2023
1 parent c50a6bd commit 554ca2c
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 139 deletions.
126 changes: 42 additions & 84 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions src/Components/FormListItem/FormListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ export default function FormListItem(formData: Form) {
router.push({
pathname: `Cases/Forms/FormView`,
params: {
id: formData.pdfLink,
title: formData.title,
date: formData.date,
pdfLink: formData.pdfLink,
...formData,
},
})
}
Expand Down
7 changes: 1 addition & 6 deletions src/Components/FormsCard/FormsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ export default function FormsCard(caseData: Case) {
router.push({
pathname: `/Cases/Forms`,
params: {
blurb: caseData.blurb,
summary: caseData.summary,
image: caseData.image,
caseSite: caseData.caseSite,
date: caseData.date,
lawFirm: caseData.lawFirm,
...caseData,
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function FormView() {
<TouchableOpacity onPress={() => router.back()} style={styles.button}>
<Text>Go Back</Text>
</TouchableOpacity>
<WebView source={{ uri: formData.pdfLink }} />
<WebView source={{ uri: formData.formUrl }} />
</View>
);
}
2 changes: 0 additions & 2 deletions src/app/(BottomTabNavigation)/Cases/Forms/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import { CaseUid, Form } from '../../../../types/types';
export default function FormsScreen() {
const id: CaseUid = 'a366a017-2834-4365-83f1-91605ba5c80a';

// const [isLoading, setIsLoading] = useState<boolean>(true);
const [forms, setForms] = useState<Form[]>([]);

async function fetchFormsOnLoad() {
fetchFormObjects(id).then(data => {
// setIsLoading(false);
if (data.length > 0) {
setForms(data);
}
Expand Down
44 changes: 21 additions & 23 deletions src/app/(BottomTabNavigation)/Cases/Forms/utils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import {
getFormObjects,
getFormUrl,
// getFormsByCaseUid,
getPartialForms,
getPublicFormUrl,
} from '../../../../supabase/queries/cases';
import { CaseUid, Form } from '../../../../types/types';

/** WIP: fetch and return a list of pdf links associated with a specific case */
/**
* Fetch an array of `Form` objects associated with a given `caseUid`.
*
* @param caseUid uid for the target Case
* @returns array of `Form` objects
*/
export async function fetchFormObjects(caseUid: CaseUid): Promise<Form[]> {
const formMetaDataList = await getFormObjects(caseUid);
const partialFormsList = await getPartialForms(caseUid);

// console.log(formMetaDataList);

const forms: Form[] = [];

formMetaDataList.map(async item => {
const formUrl = await getFormUrl(item.filename);
const form: Form = {
...item,
formUrl,
};
console.log(form);
forms.push(form);
});

Promise.all(forms).then(forms => console.log(forms));
// console.log('formsPromise: ' + formsPromise);

return forms;
return await Promise.all(
partialFormsList.map(async partialForm => {
const formUrl = await getPublicFormUrl(
`${caseUid}/${partialForm.filename}`,
);
const form: Form = {
...partialForm,
formUrl,
};
return form;
}),
);
}
36 changes: 19 additions & 17 deletions src/supabase/queries/cases.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Case, CaseUid, UserUid } from '../../types/types';
import { Case, CaseUid, UserUid, FormPartial } from '../../types/types';
import supabase from '../createClient';

/**
Expand Down Expand Up @@ -95,7 +95,13 @@ export function parseCase(item: any): Case {
return formattedCase;
}

export async function getFormUrl(filename: string): Promise<string> {
/**
* Fetch the supabase storage publicUrl associated with the given form filename.
*
* @param filename name of form file stored in supabase storage
* @returns publicUrl of the form
*/
export async function getPublicFormUrl(filename: string): Promise<string> {
try {
// fetch the form objects from supabase storage
const { data } = await supabase.storage
Expand All @@ -109,16 +115,15 @@ export async function getFormUrl(filename: string): Promise<string> {
}
}

type FormMetaData = {
id: CaseUid;
title: string;
filename: string;
date: Date;
};

export async function getFormObjects(
/**
* Fetch the `FormMetaData` for forms associated with a given case.
*
* @param caseUid uid of target case.
* @returns list of `FormMetaData` objects.
*/
export async function getPartialForms(
caseUid: CaseUid,
): Promise<FormMetaData[]> {
): Promise<FormPartial[]> {
try {
// fetch rows with the matching CaseUid
const { data } = await supabase
Expand All @@ -130,18 +135,15 @@ export async function getFormObjects(
throw new Error(`no forms found for the given case: ${caseUid}`);
}

const forms: FormMetaData[] = [];

data.map(async item => {
const form: FormMetaData = {
return data.map(item => {
const form: FormPartial = {
id: item.formId,
title: item.title,
filename: item.filename,
date: item.date,
};
forms.push(form);
return form;
});
return forms;
} catch (error) {
// eslint-disable-next-line no-console
console.warn('(getFormObjects)', error);
Expand Down
4 changes: 2 additions & 2 deletions src/types/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export interface Case {
lawFirm: string;
}

export interface FormMetaData {
export interface FormPartial {
id: FormUid;
title: string;
filename: string;
date: Date;
}

export interface Form extends FormMetaData {
export interface Form extends FormPartial {
formUrl: string;
}

Expand Down

0 comments on commit 554ca2c

Please sign in to comment.