-
Notifications
You must be signed in to change notification settings - Fork 13
Implemented deleteImage API, state and tests #945
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
Open
gabrieltmonkai
wants to merge
1
commit into
main
Choose a base branch
from
feat/MN-780/create-deleteImage-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { MonkState } from '../state'; | ||
import { MonkAction, MonkActionType } from './monkAction'; | ||
|
||
/** | ||
* The payload of a MonkDeletedOneImagePayload. | ||
*/ | ||
export interface MonkDeletedOneImagePayload { | ||
/** | ||
* The ID of the inspection to which the image was deleted. | ||
*/ | ||
inspectionId: string; | ||
/** | ||
* The image ID deleted. | ||
*/ | ||
imageId: string; | ||
} | ||
|
||
/** | ||
* Action dispatched when an image have been deleted. | ||
*/ | ||
export interface MonkDeletedOneImageAction extends MonkAction { | ||
/** | ||
* The type of the action : `MonkActionType.DELETED_ONE_IMAGE`. | ||
*/ | ||
type: MonkActionType.DELETED_ONE_IMAGE; | ||
/** | ||
* The payload of the action containing the fetched entities. | ||
*/ | ||
payload: MonkDeletedOneImagePayload; | ||
} | ||
|
||
/** | ||
* Matcher function that matches a DeletedOneImage while also inferring its type using TypeScript's type predicate | ||
* feature. | ||
*/ | ||
export function isDeletedOneImageAction(action: MonkAction): action is MonkDeletedOneImageAction { | ||
return action.type === MonkActionType.DELETED_ONE_IMAGE; | ||
} | ||
|
||
/** | ||
* Reducer function for a deletedOneImage action. | ||
*/ | ||
export function deletedOneImage(state: MonkState, action: MonkDeletedOneImageAction): MonkState { | ||
const { images, inspections, damages, parts, renderedOutputs, views } = state; | ||
const { payload } = action; | ||
|
||
const inspection = inspections.find((value) => value.id === payload.inspectionId); | ||
if (inspection) { | ||
inspection.images = inspection.images?.filter((imageId) => imageId !== payload.imageId); | ||
} | ||
const deletedImage = images.find((image) => image.id === payload.imageId); | ||
const newImages = images.filter((image) => image.id !== payload.imageId); | ||
const newDamages = damages.map((damage) => ({ | ||
...damage, | ||
relatedImages: damage.relatedImages.filter((imageId) => imageId !== payload.imageId), | ||
})); | ||
const newParts = parts.map((part) => ({ | ||
...part, | ||
relatedImages: part.relatedImages.filter((imageId) => imageId !== payload.imageId), | ||
})); | ||
const newViews = views.map((view) => ({ | ||
...view, | ||
renderedOutputs: view.renderedOutputs.filter( | ||
(outputId) => !deletedImage?.renderedOutputs.includes(outputId), | ||
), | ||
})); | ||
const newRenderedOutputs = renderedOutputs.filter( | ||
(output) => !deletedImage?.renderedOutputs.includes(output.id), | ||
); | ||
|
||
return { | ||
...state, | ||
images: [...newImages], | ||
dlymonkai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inspections: [...inspections], | ||
damages: [...newDamages], | ||
parts: [...newParts], | ||
renderedOutputs: [...newRenderedOutputs], | ||
views: [...newViews], | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/common/test/state/actions/deletedOneImage.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Damage, Image, Inspection, Part, RenderedOutput, View } from '@monkvision/types'; | ||
import { | ||
createEmptyMonkState, | ||
deletedOneImage, | ||
isDeletedOneImageAction, | ||
MonkActionType, | ||
MonkDeletedOneImageAction, | ||
} from '../../../src'; | ||
|
||
const action: MonkDeletedOneImageAction = { | ||
type: MonkActionType.DELETED_ONE_IMAGE, | ||
payload: { | ||
inspectionId: 'inspections-test', | ||
imageId: 'image-id-test', | ||
}, | ||
}; | ||
|
||
describe('DeletedOneImage action handlers', () => { | ||
describe('Action matcher', () => { | ||
it('should return true if the action has the proper type', () => { | ||
expect(isDeletedOneImageAction({ type: MonkActionType.DELETED_ONE_IMAGE })).toBe(true); | ||
}); | ||
|
||
it('should return false if the action does not have the proper type', () => { | ||
expect(isDeletedOneImageAction({ type: MonkActionType.RESET_STATE })).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Action handler', () => { | ||
it('should return a new state', () => { | ||
const state = createEmptyMonkState(); | ||
expect(Object.is(deletedOneImage(state, action), state)).toBe(false); | ||
}); | ||
|
||
it('should delete image in the state', () => { | ||
const state = createEmptyMonkState(); | ||
const outputId = 'rendered-output-id-test'; | ||
const damageId = 'damage-id-test'; | ||
const partId = 'part-id-test'; | ||
const viewId = 'view-id-test'; | ||
state.inspections.push({ | ||
id: action.payload.inspectionId, | ||
images: [action.payload.imageId] as string[], | ||
} as Inspection); | ||
state.images.push({ | ||
id: action.payload.imageId, | ||
inspectionId: action.payload.inspectionId, | ||
views: [viewId], | ||
renderedOutputs: [outputId], | ||
} as Image); | ||
state.damages.push({ | ||
id: damageId, | ||
relatedImages: [action.payload.imageId], | ||
inspectionId: action.payload.inspectionId, | ||
} as Damage); | ||
state.parts.push({ | ||
id: partId, | ||
relatedImages: [action.payload.imageId], | ||
inspectionId: action.payload.inspectionId, | ||
} as Part); | ||
state.renderedOutputs.push({ | ||
id: outputId, | ||
baseImageId: action.payload.imageId, | ||
} as RenderedOutput); | ||
state.views.push({ | ||
id: viewId, | ||
renderedOutputs: [outputId], | ||
} as View); | ||
const newState = deletedOneImage(state, action); | ||
const inspectionImages = newState.inspections.find( | ||
(ins) => ins.id === action.payload.inspectionId, | ||
)?.images; | ||
|
||
expect(inspectionImages?.length).toBe(0); | ||
expect(inspectionImages).not.toContainEqual(action.payload.imageId); | ||
expect(newState.images.length).toBe(0); | ||
expect(newState.damages.length).toBe(1); | ||
expect(newState.parts.length).toBe(1); | ||
expect(newState.views.length).toBe(1); | ||
expect( | ||
newState.damages.find((damage) => damage.relatedImages.includes(action.payload.imageId)), | ||
).toBeUndefined(); | ||
expect( | ||
newState.parts.find((part) => part.relatedImages.includes(action.payload.imageId)), | ||
).toBeUndefined(); | ||
expect(newState.renderedOutputs.length).toBe(0); | ||
expect( | ||
newState.renderedOutputs.find((output) => output.baseImageId === action.payload.imageId), | ||
).toBeUndefined(); | ||
expect( | ||
newState.views.find((view) => | ||
view.renderedOutputs.find((id) => id === action.payload.imageId), | ||
), | ||
).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.