Skip to content

Commit

Permalink
feat(apps/mobile): track when an image is being uploaded
Browse files Browse the repository at this point in the history
  • Loading branch information
hassankhan committed Oct 14, 2024
1 parent dae7cff commit d62f518
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/mobile/src/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { UploadButton } from '../components/UploadButton';
import { ImageList } from '../features/HomePage/ImageList';
import { NoImagesFound } from '../features/HomePage/NoImagesFound';
import { UploadImageSheet } from '../features/UploadImageModal/UploadImageSheet';
import { useAppSelector } from '../store/overrides';
import { getIsImageUploading } from '../store/selectors/getIsImageUploading';
import {
useGetMyFavouritesQuery,
useGetMyImagesQuery,
Expand All @@ -24,6 +26,7 @@ const Home = () => {
const isLoading = isImagesLoading || isFavouritesLoading || isVotesLoading;

const [isBottomSheetOpen, setIsBottomSheetOpen] = useState(false);
const isImageUploading = useAppSelector(getIsImageUploading);

const handleUploadButtonPress = useCallback(() => {
setIsBottomSheetOpen(true);
Expand Down
4 changes: 4 additions & 0 deletions apps/mobile/src/store/selectors/getIsImageUploading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { RootState } from '../store';

export const getIsImageUploading = (state: RootState) =>
state.imageActivity.isImageUploading;
33 changes: 33 additions & 0 deletions apps/mobile/src/store/slices/ImageActivitySlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createSlice } from '@reduxjs/toolkit';
import { CatApi } from '../services/CatApi';

const initialState = {
isImageUploading: false,
};

export const ImageActivitySlice = createSlice({
name: 'imageActivity',
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addMatcher(
CatApi.endpoints.uploadImage.matchPending,
(state, { payload }) => {
state.isImageUploading = true;
}
)
.addMatcher(
CatApi.endpoints.uploadImage.matchFulfilled,
(state, { payload }) => {
state.isImageUploading = false;
}
)
.addMatcher(
CatApi.endpoints.uploadImage.matchRejected,
(state, { payload }) => {
state.isImageUploading = false;
}
);
},
});
3 changes: 3 additions & 0 deletions apps/mobile/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import devToolsEnhancer from 'redux-devtools-expo-dev-plugin';
import { ToastMiddleware } from './middleware/ToastMiddleware';

import { CatApi } from './services/CatApi';
import { ImageActivitySlice } from './slices/ImageActivitySlice';

export const store = configureStore({
enhancers: (getDefaultEnhancers) =>
Expand All @@ -19,6 +20,8 @@ export const store = configureStore({
reducer: {
// RTK-Query reducers
[CatApi.reducerPath]: CatApi.reducer,
// RTK slices
[ImageActivitySlice.name]: ImageActivitySlice.reducer,
},
});

Expand Down

0 comments on commit d62f518

Please sign in to comment.