Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(mobile): add delete action to document action sheet (#1349)

Corentin Thomasset57f314a6c703
diff --git a/apps/mobile/src/modules/documents/components/document-action-sheet.tsx b/apps/mobile/src/modules/documents/components/document-action-sheet.tsx
index 7cb9e418..f638ccaa 100644
--- a/apps/mobile/src/modules/documents/components/document-action-sheet.tsx
+++ b/apps/mobile/src/modules/documents/components/document-action-sheet.tsx
@@ -3,6 +3,7 @@ import type { Document } from '@/modules/documents/documents.types';
import type { IconName } from '@/modules/ui/components/icon';
import type { ThemeColors } from '@/modules/ui/theme.constants';
import { formatBytes } from '@corentinth/chisels';
+import { useQueryClient } from '@tanstack/react-query';
import { router } from 'expo-router';
import * as Sharing from 'expo-sharing';
import {
@@ -13,9 +14,9 @@ import {
TouchableWithoutFeedback,
View,
} from 'react-native';
-import { useAuthClient } from '@/modules/api/providers/api.provider';
+import { useApiClient, useAuthClient } from '@/modules/api/providers/api.provider';
import { configLocalStorage } from '@/modules/config/config.local-storage';
-import { fetchDocumentFile } from '@/modules/documents/documents.services';
+import { deleteDocument, fetchDocumentFile } from '@/modules/documents/documents.services';
import { Icon } from '@/modules/ui/components/icon';
import { useAlert } from '@/modules/ui/providers/alert-provider';
import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
@@ -25,20 +26,24 @@ type DocumentActionSheetProps = {
document: CoerceDates<Document> | undefined;
onClose: () => void;
excludedActions?: ActionsKey[];
+ onDeleted?: () => void;
};
-export type ActionsKey = 'view' | 'share';
+export type ActionsKey = 'view' | 'share' | 'delete';
export function DocumentActionSheet({
visible,
document,
onClose,
excludedActions = [],
+ onDeleted,
}: DocumentActionSheetProps) {
const themeColors = useThemeColor();
const styles = createStyles({ themeColors });
const { showAlert } = useAlert();
const authClient = useAuthClient();
+ const apiClient = useApiClient();
+ const queryClient = useQueryClient();
if (document === undefined) {
return null;
@@ -103,13 +108,55 @@ export function DocumentActionSheet({
}
};
+ const handleDelete = () => {
+ onClose();
+
+ showAlert({
+ title: 'Delete Document',
+ message: `Are you sure you want to delete "${document.name}"? It will be moved to the trash.`,
+ buttons: [
+ { text: 'Cancel', style: 'cancel' },
+ {
+ text: 'Delete',
+ style: 'destructive',
+ onPress: async () => {
+ try {
+ await deleteDocument({
+ organizationId: document.organizationId,
+ documentId: document.id,
+ apiClient,
+ });
+
+ await queryClient.invalidateQueries({
+ queryKey: ['organizations', document.organizationId, 'documents'],
+ });
+
+ onDeleted?.();
+ } catch {
+ showAlert({
+ title: 'Error',
+ message: 'Failed to delete document',
+ });
+ }
+ },
+ },
+ ],
+ });
+ };
+
// Extract MIME type subtype, fallback to full MIME type if subtype is missing
const mimeParts = document.mimeType.split('/');
const mimeSubtype = mimeParts[1];
const displayMimeType =
mimeSubtype != null && mimeSubtype !== '' ? mimeSubtype.toUpperCase() : document.mimeType;
- const actions: { key: ActionsKey; label: string; icon: IconName; onPress: () => void }[] = [
+ const actions: {
+ key: ActionsKey;
+ label: string;
+ icon: IconName;
+ onPress: () => void;
+ destructive?: boolean;
+ }[] = [
{
key: 'view',
label: 'View document',
@@ -122,6 +169,13 @@ export function DocumentActionSheet({
icon: 'share',
onPress: handleDownloadAndShare,
},
+ {
+ key: 'delete',
+ label: 'Delete',
+ icon: 'trash-2',
+ onPress: handleDelete,
+ destructive: true,
+ },
];
const filteredActions = actions.filter((action) => !excludedActions.includes(action.key));
@@ -160,9 +214,24 @@ export function DocumentActionSheet({
activeOpacity={0.6}
>
<View style={styles.actionIconContainer}>
- <Icon name={action.icon} size={20} color={themeColors.foreground} />
+ <Icon
+ name={action.icon}
+ size={20}
+ color={
+ action.destructive === true
+ ? themeColors.destructive
+ : themeColors.foreground
+ }
+ />
</View>
- <Text style={styles.actionText}>{action.label}</Text>
+ <Text
+ style={[
+ styles.actionText,
+ action.destructive === true && { color: themeColors.destructive },
+ ]}
+ >
+ {action.label}
+ </Text>
</TouchableOpacity>
))}
</View>
diff --git a/apps/mobile/src/modules/documents/documents.services.ts b/apps/mobile/src/modules/documents/documents.services.ts
index d7610180..36d242d0 100644
--- a/apps/mobile/src/modules/documents/documents.services.ts
+++ b/apps/mobile/src/modules/documents/documents.services.ts
@@ -97,6 +97,21 @@ export async function fetchDocument({
};
}
+export async function deleteDocument({
+ organizationId,
+ documentId,
+ apiClient,
+}: {
+ organizationId: string;
+ documentId: string;
+ apiClient: ApiClient;
+}) {
+ await apiClient({
+ method: 'DELETE',
+ path: `/api/organizations/${organizationId}/documents/${documentId}`,
+ });
+}
+
export async function fetchDocumentFile({
document,
organizationId,
diff --git a/apps/mobile/src/modules/documents/screens/document-details.screen.tsx b/apps/mobile/src/modules/documents/screens/document-details.screen.tsx
index 992a79ae..f271ad93 100644
--- a/apps/mobile/src/modules/documents/screens/document-details.screen.tsx
+++ b/apps/mobile/src/modules/documents/screens/document-details.screen.tsx
@@ -406,6 +406,7 @@ export function DocumentDetailsScreen() {
document={document}
onClose={() => setIsActionSheetVisible(false)}
excludedActions={['view']}
+ onDeleted={() => router.back()}
/>
{renderContent()}
diff --git a/apps/mobile/src/modules/documents/screens/document-view.screen.tsx b/apps/mobile/src/modules/documents/screens/document-view.screen.tsx
index c30fb1a4..ee4d9a17 100644
--- a/apps/mobile/src/modules/documents/screens/document-view.screen.tsx
+++ b/apps/mobile/src/modules/documents/screens/document-view.screen.tsx
@@ -264,6 +264,7 @@ export default function DocumentViewScreen() {
document={documentFile?.doc}
onClose={() => setIsActionSheetVisible(false)}
excludedActions={['view']}
+ onDeleted={() => router.dismissTo('/(app)/(with-organizations)/(tabs)/list')}
/>
{renderContent()}