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

feat/OMHD-475: Google Drive delete #13

Merged
merged 1 commit into from
Aug 5, 2024
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
17 changes: 10 additions & 7 deletions apps/sample-app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BottomSheetModalProvider } from '@gorhom/bottom-sheet';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { PaperProvider } from 'react-native-paper';
import { RootSiblingParent } from 'react-native-root-siblings';
import { SafeAreaProvider } from 'react-native-safe-area-context';

import { ContextsProvider } from '@/contexts/provider/ContextsProvider';
import { QueryClientProvider } from '@/data/client/QueryClientProvider';
Expand Down Expand Up @@ -33,12 +34,14 @@ const Providers = ({ children }: ProvidersProps) => {

export const App = () => {
return (
<ContextsProvider>
<QueryClientProvider>
<Providers>
<RootNavigationContainer />
</Providers>
</QueryClientProvider>
</ContextsProvider>
<SafeAreaProvider>
<ContextsProvider>
<QueryClientProvider>
<Providers>
<RootNavigationContainer />
</Providers>
</QueryClientProvider>
</ContextsProvider>
</SafeAreaProvider>
);
};
22 changes: 10 additions & 12 deletions apps/sample-app/ReactotronConfig.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Uncomment when needed - lefthook is checking node_modules for some reason, so for now it's commented out
const Reactotron = require('reactotron-react-native').default;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// import Reactotron from 'reactotron-react-native';

// Reactotron.configure({
// name: 'React Native Demo',
// })
// .useReactNative({
// networking: {
// ignoreUrls: /symbolicate/,
// },
// })
// .connect();
Reactotron.configure({
name: 'React Native Demo',
})
.useReactNative({
networking: {
ignoreUrls: /symbolicate/,
},
})
.connect();
4 changes: 4 additions & 0 deletions apps/sample-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ import { AppRegistry } from 'react-native';
import { App } from './App';
import { name as appName } from './app.json';

if (__DEV__) {
require('./ReactotronConfig');
}

AppRegistry.registerComponent(appName, () => App);
2 changes: 1 addition & 1 deletion apps/sample-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"react-native-safe-area-context": "^4.10.8",
"react-native-screens": "^3.32.0",
"react-native-vector-icons": "^10.1.0",
"reactotron-react-native": "^5.1.7",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to dev dependencies.

"use-debounce": "^10.0.2"
},
"devDependencies": {
Expand All @@ -53,6 +52,7 @@
"jest": "^29.6.3",
"react-native-dotenv": "^3.4.11",
"react-test-renderer": "18.2.0",
"reactotron-react-native": "^5.1.7",
"typescript": "5.2.2"
},
"engines": {
Expand Down
60 changes: 57 additions & 3 deletions apps/sample-app/src/components/FileListItem/FileListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useCallback, useRef } from 'react';
import { useCallback, useMemo, useRef } from 'react';
import { Alert, Image } from 'react-native';

import { BottomSheetModal } from '@gorhom/bottom-sheet';
import { File, StorageEntity } from '@openmobilehub/storage-core';
import { IconButton, List } from 'react-native-paper';
import { Style } from 'react-native-paper/lib/typescript/components/List/utils';

import { useSnackbar } from '@/contexts/snackbar/SnackbarContent';
import { useRequireStorageClient } from '@/contexts/storage/useRequireStorageClient';
import { useDeleteFileMutation } from '@/data/mutation/useDeleteFileMutation';
import { usePermanentDeleteFileMutation } from '@/data/mutation/usePermanentDeleteFileMutation';

import { BottomSheet } from '../bottomSheet';
import { BottomSheetContent } from '../bottomSheetContent';
import { styles } from './FileListItem.styles';
Expand All @@ -19,6 +24,23 @@ interface Props {
export const FileListItem = ({ file, onPress }: Props) => {
const bottomSheetModalRef = useRef<BottomSheetModal>(null);

const storageClient = useRequireStorageClient();

const { showSnackbar } = useSnackbar();

const handleSettled = useCallback(() => {
bottomSheetModalRef.current?.close();
}, []);

const deleteFileMutation = useDeleteFileMutation(
storageClient,
handleSettled
);
const permanentDeleteFileMutation = usePermanentDeleteFileMutation(
storageClient,
handleSettled
);

const icon =
file instanceof File ? getIconForFileListItem(file.mimeType) : URL_FOLDER;

Expand All @@ -30,12 +52,44 @@ export const FileListItem = ({ file, onPress }: Props) => {
Alert.alert('Not implemented', 'This feature is not implemented yet');
};

const deleteHandlers = useMemo(() => {
return {
onSuccess: () => {
showSnackbar('File permanently deleted');
},
onError: () => {
showSnackbar('Failed to permanently delete file');
},
};
}, [showSnackbar]);

const handleDeletePress = () => {
Alert.alert('Not implemented', 'This feature is not implemented yet');
deleteFileMutation.mutate(
{ fileId: file.id },
{
...deleteHandlers,
}
);
};

const handlePermanentDeletePress = () => {
Alert.alert('Not implemented', 'This feature is not implemented yet');
Alert.alert(
'Are you sure?',
'This action cannot be undone. If you want to move file to trash, use regular delete.',
[
{
text: 'Cancel',
style: 'cancel',
},
{
text: 'Delete',
style: 'destructive',
onPress: () => {
permanentDeleteFileMutation.mutate({ fileId: file.id });
},
},
]
);
};

const renderLeftIcon = useCallback(
Expand Down
5 changes: 4 additions & 1 deletion apps/sample-app/src/contexts/provider/ContextsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ReactNode } from 'react';
import { AuthContextProvider } from '@/contexts/auth/AuthContext';
import { StorageContextProvider } from '@/contexts/storage/StorageContext';

import { SnackbarContextProvider } from '../snackbar/SnackbarContent';
import { UIContextProvider } from '../ui/UIContext';

interface Props {
Expand All @@ -13,7 +14,9 @@ export const ContextsProvider = ({ children }: Props) => {
return (
<AuthContextProvider>
<StorageContextProvider>
<UIContextProvider>{children}</UIContextProvider>
<UIContextProvider>
<SnackbarContextProvider>{children}</SnackbarContextProvider>
</UIContextProvider>
</StorageContextProvider>
</AuthContextProvider>
);
Expand Down
80 changes: 80 additions & 0 deletions apps/sample-app/src/contexts/snackbar/SnackbarContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, { useCallback, useContext, useState } from 'react';

import { Snackbar } from 'react-native-paper';

type ButtonAction = () => void;

export type SnackbarContextType = {
showSnackbar: (
text: string,
options?: {
duration?: number;
buttonLabel?: string;
buttonAction: ButtonAction;
}
) => void;
} | null;

export const SnackbarContext = React.createContext<SnackbarContextType>(null);

const DEFAULT_SNACKBAR_DURATION: number = 2800;

export type Props = { children: React.ReactNode };

export const SnackbarContextProvider = ({ children }: Props) => {
const [visible, setVisible] = useState(false);
const [text, setText] = useState<string | null>(null);
const [buttonLabel, setButtonLabel] = useState<string | null>(null);
const [buttonAction, setButtonAction] = useState<ButtonAction | null>(null);
const [duration, setDuration] = useState(DEFAULT_SNACKBAR_DURATION);
const [key, setKey] = useState<number>(-1);

const onDismissSnackBar = useCallback(() => {
setVisible(false);
}, []);

return (
<SnackbarContext.Provider
value={{
showSnackbar(newText, options) {
setKey(Date.now());
setText(newText);
setButtonLabel(options?.buttonLabel ?? null);
setButtonAction(options?.buttonAction ?? null);
setDuration(options?.duration ?? DEFAULT_SNACKBAR_DURATION);
setVisible(true);
},
}}
>
{children}

<Snackbar
key={key}
visible={visible}
onDismiss={onDismissSnackBar}
duration={duration}
action={{
label: buttonLabel ?? 'Dismiss',
onPress: () => {
buttonAction?.();
onDismissSnackBar();
},
}}
>
{text}
</Snackbar>
</SnackbarContext.Provider>
);
};

export const useSnackbar = () => {
const context = useContext(SnackbarContext);

if (context == null) {
throw new Error(
'useSnackbarContext must be used within a SnackbarContextProvider'
);
}

return context;
};
23 changes: 23 additions & 0 deletions apps/sample-app/src/data/mutation/useDeleteFileMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IStorageClient, StorageException } from '@openmobilehub/storage-core';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { QK_LIST_FILES } from '../client/queryKeys';

type MutationData = {
fileId: string;
};

export const useDeleteFileMutation = (
storageClient: IStorageClient,
onSettled?: () => void
) => {
const queryClient = useQueryClient();

return useMutation<void, StorageException, MutationData>({
mutationFn: ({ fileId }) => storageClient.deleteFile(fileId),
onSettled: () => {
queryClient.invalidateQueries({ queryKey: [QK_LIST_FILES] });
onSettled?.();
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IStorageClient, StorageException } from '@openmobilehub/storage-core';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { QK_LIST_FILES } from '../client/queryKeys';

type MutationData = {
fileId: string;
};

export const usePermanentDeleteFileMutation = (
storageClient: IStorageClient,
onSettled?: () => void
) => {
const queryClient = useQueryClient();

return useMutation<void, StorageException, MutationData>({
mutationFn: async ({ fileId }) =>
await storageClient.permanentlyDeleteFile(fileId),
onSettled: () => {
queryClient.invalidateQueries({ queryKey: [QK_LIST_FILES] });
onSettled?.();
},
});
};
4 changes: 3 additions & 1 deletion apps/sample-app/src/data/query/useSearchFilesQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { QK_LIST_FILES, QK_SEARCH_FILES } from '../client/queryKeys';

export const useSearchFilesQuery = (
storageClient: IStorageClient,
query: string
query: string,
enabled = true
) => {
return useQuery<StorageEntity[], StorageException>({
queryKey: [QK_LIST_FILES, QK_SEARCH_FILES, query],
queryFn: () => storageClient.search(query),
enabled,
});
};
3 changes: 2 additions & 1 deletion apps/sample-app/src/screens/fileViewer/FileViewerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export const FileViewerScreen = () => {

const searchFilesQuery = useSearchFilesQuery(
storageClient,
debouncedSearchQuery
debouncedSearchQuery,
searchQuery.length > 0
);

const handleStorageEntityPress = (file: StorageEntity) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/StorageClient.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export interface IStorageClient {
parentId?: string
): Promise<StorageEntity>;
localFileUpload(file: LocalFile, folderId: string): Promise<StorageEntity>;
deleteFile(fileId: string): Promise<void>;
permanentlyDeleteFile(fileId: string): Promise<void>;
}
9 changes: 9 additions & 0 deletions packages/googledrive/src/GoogleDriveStorageApiService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiException, type LocalFile } from '@openmobilehub/storage-core';
import { FileSystem } from 'react-native-file-access';

import type { CommonRequestBody } from './data/body/CommonRequestBody';
import type { CreateFileRequestBody } from './data/body/CreateFileRequestBody';
import { type FileListRemote } from './data/response/FileListRemote';
import type { GoogleDriveStorageApiClient } from './GoogleDriveStorageApiClient';
Expand Down Expand Up @@ -156,4 +157,12 @@ export class GoogleDriveStorageApiService {

return null;
}

async updateFileMetadata(fileId: string, body: CommonRequestBody) {
await this.client.axiosClient.patch(`${FILES_PARTICLE}/${fileId}`, body);
}

async deleteFile(fileId: string) {
await this.client.axiosClient.delete(`${FILES_PARTICLE}/${fileId}`);
}
}
8 changes: 8 additions & 0 deletions packages/googledrive/src/GoogleDriveStorageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ export class GoogleDriveStorageClient implements IStorageClient {
localFileUpload(file: LocalFile, folderId: string) {
return this.repository.localFileUpload(file, folderId);
}

async deleteFile(fileId: string) {
return this.repository.deleteFile(fileId);
}

async permanentlyDeleteFile(fileId: string) {
return this.repository.permanentlyDeleteFile(fileId);
}
}
Loading