Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WALLET-388: Retrieve resource and send to the Wallet Backend service. #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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