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 = {
}
},

deleteImage: (input) => {
return {
template: `
mutation DeleteImage($input: DeleteImageInput!) {
deleteImage(input: $input) {
nathanielrindlaub marked this conversation as resolved.
Show resolved Hide resolved
message
}
}
`,
variables: {
input
}
}
},

getImages: ({ filters, pageInfo, page }) => ({
template: `
query GetImages($input: QueryImagesInput!) {
Expand Down
19 changes: 16 additions & 3 deletions src/features/loupe/FullSizeImage.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 { drawBboxStart, selectIsDrawingBbox} from './loupeSlice';
import { selectWorkingImages, labelsValidated, markedEmpty } from '../review/reviewSlice';
import { selectWorkingImages, labelsValidated, markedEmpty, deleteImage } from '../review/reviewSlice';
import { Image } from '../../components/Image';
import BoundingBox from './BoundingBox';
import DrawBboxOverlay from './DrawBboxOverlay';
Expand Down Expand Up @@ -145,6 +145,8 @@ const FullSizeImage = ({ image, focusIndex }) => {

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

const handleDeleteButtonClick = () => dispatch(deleteImage(image._id));

return (
<ImageWrapper ref={containerEl} className='full-size-image'>
{isDrawingBbox &&
Expand Down Expand Up @@ -192,13 +194,24 @@ const FullSizeImage = ({ image, focusIndex }) => {
</ContextMenuItemIconLeft>
nathanielrindlaub marked this conversation as resolved.
Show resolved Hide resolved
Mark empty
</ContextMenuItem>
<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 +230,4 @@ const FullSizeImage = ({ image, focusIndex }) => {
);
};

export default FullSizeImage;
export default FullSizeImage;
48 changes: 48 additions & 0 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 @@ -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 @@ -136,6 +141,24 @@ export const reviewSlice = createSlice({
state.loadingStates.labels.errors.splice(index, 1);
},

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

deleteImageSuccess: (state, { payload }) => {
state.workingImages = state.workingImages.filter(
({ _id }) => _id !== payload
);
state.loadingStates.images.isLoading = false;
state.loadingStates.images.operation = null;
},

deleteImageError: (state, { payload }) => {
state.loadingStates.images.isLoading = false;
state.loadingStates.images.error = payload;
},
},

extraReducers: (builder) => {
Expand Down Expand Up @@ -165,8 +188,33 @@ export const {
editLabelFailure,
editLabelSuccess,
dismissLabelsError,
deleteImageStart,
deleteImageSuccess,
} = reviewSlice.actions;

export const deleteImage = (imageId) => async (dispatch, getState) => {
dispatch(deleteImageStart());
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: 'deleteImage',
input: { imageId },
});
}
dispatch(deleteImageSuccess(imageId));
} catch (err) {
console.log(`error attempting to delete image: `, err);
dispatch(deleteImageError(err));
}
}

// editLabel thunk
export const editLabel = (operation, entity, payload, projId) => {
return async (dispatch, getState) => {
Expand Down