Skip to content

Commit

Permalink
WALLET-388: Retrieve resource and send to the Wallet Backend service. (
Browse files Browse the repository at this point in the history
…#7)

* Retrieve resource and send to the Wallet Backend service. No longer calling the collect endpoint
  • Loading branch information
jholleran authored Jul 25, 2024
1 parent bbf968d commit 7c2587e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
41 changes: 27 additions & 14 deletions api/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,39 @@ export const fetchFiles = async (): Promise<WalletFile[]> => {
};

export const postFile = async (file: FileObject): Promise<void> => {
console.log(process.env.EXPO_PUBLIC_WALLET_API);

const formData = new FormData();
formData.append("file", {
name: file.name,
type: mime.getType(file.name) || "application/octet-stream",
type:
file.contentType || mime.getType(file.name) || "application/octet-stream",
uri: file.uri,
} as unknown as Blob);

await fetch(`${process.env.EXPO_PUBLIC_WALLET_API}/wallet`, {
method: "PUT",
body: formData,
});
try {
const response = await fetch(
`${process.env.EXPO_PUBLIC_WALLET_API}/wallet`,
{
method: "PUT",
body: formData,
}
);

if (response.ok) {
console.debug(
`Uploaded file to Wallet. HTTP response status:${response.status}`
);
} else {
throw Error(
`Failed to upload file to Wallet. HTTP response status from Wallet Backend service:${
response.status
}`
);
}
} catch (error) {
throw Error("Failed to retrieve and upload file to Wallet", {
cause: error,
});
}
};

export const deleteFile = async (fileId: string): Promise<void> => {
Expand All @@ -57,10 +77,3 @@ export const getFile = async (fileId: string): Promise<Blob> => {
"GET"
);
};

export const collectFileToPod = async (uri: string): Promise<Blob> => {
return makeApiRequest<Blob>(
`wallet/collect?url=${encodeURIComponent(uri)}`,
"PUT"
);
};
14 changes: 11 additions & 3 deletions app/(tabs)/home/download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useLocalSearchParams, useNavigation } from "expo-router";
import { ThemedText } from "@/components/ThemedText";
import CustomButton from "@/components/Button";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { collectFileToPod } from "@/api/files";
import { postFile } from "@/api/files";
import { FontAwesome6 } from "@expo/vector-icons";
import IconResourceName from "@/components/common/IconResourceName";
import { RDF_CONTENT_TYPE } from "@/utils/constants";
Expand All @@ -41,10 +41,14 @@ const Page: React.FC<FileDetailProps> = () => {
const parentNavigation = useNavigation("/(tabs)");

const mutation = useMutation({
mutationFn: collectFileToPod,
mutationFn: postFile,
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["files"] });
},
onError: (error) => {
// TODO: there needs to be better error handling here...
console.warn(error);
},
mutationKey: ["filesMutation"],
});
const { uri, contentType } = useLocalSearchParams();
Expand All @@ -56,7 +60,11 @@ const Page: React.FC<FileDetailProps> = () => {

const { goBack } = useNavigation();
const onSaveToWallet = async () => {
mutation.mutate(uri as string);
mutation.mutate({
uri: uri as string,
name: fileName,
contentType,
});
goBack();
};
useLayoutEffect(() => {
Expand Down

0 comments on commit 7c2587e

Please sign in to comment.