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

Delete images #153

Merged
merged 13 commits into from
Oct 24, 2023
15 changes: 15 additions & 0 deletions src/api/buildQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ const queries = {
}
},

deleteImages: (input) => {
return {
template: `
mutation DeleteImages($input: DeleteImagesInput!) {
deleteImages(input: $input) {
message
}
}
`,
variables: {
input
}
}
},

getImages: ({ filters, pageInfo, page }) => ({
template: `
query GetImages($input: QueryImagesInput!) {
Expand Down
42 changes: 42 additions & 0 deletions src/features/images/imagesSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ export const imagesSlice = createSlice({
const index = payload;
state.loadingStates.export.errors.splice(index, 1);
},

deleteImagesStart: (state) => {
state.loadingStates.images.isLoading = true;
state.loadingStates.images.operation = 'deleting';
state.loadingStates.images.error = null;
},

deleteImagesSuccess: (state, { payload }) => {
state.loadingStates.images.isLoading = false;
state.loadingStates.images.operation = null;
},

deleteImagesError: (state, { payload }) => {
nathanielrindlaub marked this conversation as resolved.
Show resolved Hide resolved
state.loadingStates.images.isLoading = false;
state.loadingStates.images.error = payload;
},
},
});

Expand Down Expand Up @@ -268,6 +284,9 @@ export const {
exportFailure,
clearExport,
dismissExportError,
deleteImagesStart,
deleteImagesSuccess,
deleteImagesError,
} = imagesSlice.actions;

// fetchImages thunk
Expand Down Expand Up @@ -451,6 +470,29 @@ export const getExportStatus = (documentId) => {
};
};

export const deleteImages = (imageIds) => async (dispatch, getState) => {
dispatch(deleteImagesStart(imageIds));
try {
const currentUser = await Auth.currentAuthenticatedUser();
const token = currentUser.getSignInUserSession().getIdToken().getJwtToken();

const projects = getState().projects.projects;
const selectedProj = projects.find((proj) => proj.selected);

if (token && selectedProj) {
const r = await call({
projId: selectedProj._id,
request: 'deleteImages',
input: { imageIds },
});
}
dispatch(deleteImagesSuccess(imageIds));
} catch (err) {
console.log(`error attempting to delete image: `, err);
dispatch(deleteImagesError(err));
}
}

export const selectPageInfo = state => state.images.pageInfo;
export const selectPaginatedField = state => state.images.pageInfo.paginatedField;
export const selectSortAscending = state => state.images.pageInfo.sortAscending;
Expand Down
22 changes: 19 additions & 3 deletions src/features/loupe/FullSizeImage.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useState, useRef, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { Cross2Icon, PlusIcon } from '@radix-ui/react-icons';
import { Cross2Icon, PlusIcon, TrashIcon } from '@radix-ui/react-icons';
import { useResizeObserver } from '../../app/utils';
import { styled } from '../../theme/stitches.config';
// import { CircleSpinner, SpinnerOverlay } from '../../components/Spinner';
import { selectUserUsername, selectUserCurrentRoles } from '../user/userSlice';
import { hasRole, WRITE_OBJECTS_ROLES } from '../../auth/roles';
import { hasRole, WRITE_OBJECTS_ROLES, WRITE_IMAGES_ROLES } from '../../auth/roles';
import { drawBboxStart, selectIsDrawingBbox} from './loupeSlice';
import { selectWorkingImages, labelsValidated, markedEmpty } from '../review/reviewSlice';
import { deleteImages } from '../images/imagesSlice';
import { Image } from '../../components/Image';
import BoundingBox from './BoundingBox';
import DrawBboxOverlay from './DrawBboxOverlay';
Expand Down Expand Up @@ -145,6 +146,8 @@ const FullSizeImage = ({ image, focusIndex }) => {

const handleAddObjectButtonClick = () => dispatch(drawBboxStart());

const handleDeleteButtonClick = () => dispatch(deleteImages([image._id]));

return (
<ImageWrapper ref={containerEl} className='full-size-image'>
{isDrawingBbox &&
Expand Down Expand Up @@ -192,13 +195,26 @@ const FullSizeImage = ({ image, focusIndex }) => {
</ContextMenuItemIconLeft>
nathanielrindlaub marked this conversation as resolved.
Show resolved Hide resolved
Mark empty
</ContextMenuItem>
{hasRole(userRoles, WRITE_IMAGES_ROLES) &&
<ContextMenuItem
onSelect={handleDeleteButtonClick}
>
<ContextMenuItemIconLeft>
<TrashIcon />
</ContextMenuItemIconLeft>
Delete
</ContextMenuItem>
}
</ContextMenuContent>
</ContextMenu>
<ShareImage>
<ShareImageButton imageId={image._id}/>
</ShareImage>
{hasRole(userRoles, WRITE_OBJECTS_ROLES) &&
<EditObjectButtons>
<EditObjectButton onClick={handleDeleteButtonClick}>
<TrashIcon /> Delete
</EditObjectButton>
<EditObjectButton onClick={handleMarkEmptyButtonClick}>
<Cross2Icon /> Mark empty
</EditObjectButton>
Expand All @@ -217,4 +233,4 @@ const FullSizeImage = ({ image, focusIndex }) => {
);
};

export default FullSizeImage;
export default FullSizeImage;
15 changes: 12 additions & 3 deletions src/features/review/reviewSlice.js
nathanielrindlaub marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createSlice, createAction } from '@reduxjs/toolkit';
import { Auth } from 'aws-amplify';
import { call } from '../../api';
import { findImage, findObject, findLabel } from '../../app/utils';
import { getImagesSuccess, clearImages } from '../images/imagesSlice';
import { getImagesSuccess, clearImages, deleteImagesStart } from '../images/imagesSlice';

const initialState = {
workingImages: [],
Expand All @@ -17,6 +17,11 @@ const initialState = {
isLoading: false,
operation: null, /* 'fetching', 'updating', 'deleting' */
errors: null,
},
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be removed now?

images: {
isLoading: false,
operation: null, /* 'deleting' */
errors: null,
}
}
};
Expand Down Expand Up @@ -134,8 +139,7 @@ export const reviewSlice = createSlice({
dismissLabelsError: (state, { payload }) => {
const index = payload;
state.loadingStates.labels.errors.splice(index, 1);
},

}
},

extraReducers: (builder) => {
Expand All @@ -147,6 +151,11 @@ export const reviewSlice = createSlice({
.addCase(clearImages, (state) => {
state.workingImages = [];
})
.addCase(deleteImagesStart, (state, { payload }) => {
state.workingImages = state.workingImages.filter(
({ _id }) => !payload.includes(_id)
);
})
},

});
Expand Down
Loading