Skip to content

Commit

Permalink
BUGFIX: Return proper result when trying to delete used asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebobo committed Sep 15, 2023
1 parent 894c08a commit 082ca17
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 17 deletions.
54 changes: 54 additions & 0 deletions Classes/Domain/Model/Dto/MutationResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Flowpack\Media\Ui\Domain\Model\Dto;

use Neos\Flow\Annotations as Flow;

/**
* @Flow\Proxy(false)
*/
class MutationResult implements \JsonSerializable
{

private bool $success;
private ?array $messages;
private ?array $data;

public function __construct(bool $success, array $messages = null, array $data = null)
{
$this->success = $success;
$this->messages = $messages;
$this->data = $data;
}

public function isSuccess(): bool
{
return $this->success;
}

public function getMessages(): ?array
{
return $this->messages;
}

public function getData(): ?array
{
return $this->data;
}

public function toArray(): array
{
return [
'success' => $this->success,
'messages' => $this->messages,
'data' => $this->data,
];
}

public function jsonSerialize(): array
{
return $this->toArray();
}
}
15 changes: 8 additions & 7 deletions Classes/GraphQL/Resolver/Type/MutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

use Doctrine\Common\Collections\ArrayCollection;
use Flowpack\Media\Ui\Domain\Model\Dto\MutationResult;
use Flowpack\Media\Ui\Domain\Model\HierarchicalAssetCollectionInterface;
use Flowpack\Media\Ui\Exception;
use Flowpack\Media\Ui\GraphQL\Context\AssetSourceContext;
Expand All @@ -27,7 +28,6 @@
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Http\Factories\FlowUploadedFile;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\Tag;
Expand Down Expand Up @@ -104,9 +104,8 @@ class MutationResolver implements ResolverInterface

/**
* @throws Exception
* @throws AssetServiceException
*/
public function deleteAsset($_, array $variables, AssetSourceContext $assetSourceContext): bool
public function deleteAsset($_, array $variables, AssetSourceContext $assetSourceContext): MutationResult
{
[
'id' => $id,
Expand All @@ -115,21 +114,23 @@ public function deleteAsset($_, array $variables, AssetSourceContext $assetSourc

$assetProxy = $assetSourceContext->getAssetProxy($id, $assetSourceId);
if (!$assetProxy) {
return false;
return new MutationResult(false, ['Asset could not be resolved']);
}
$asset = $assetSourceContext->getAssetForProxy($assetProxy);

if (!$asset) {
throw new Exception('Cannot delete asset that was never imported', 1591553708);
return new MutationResult(false, ['Cannot delete asset that was never imported']);
}

try {
$this->assetRepository->remove($asset);
} catch (IllegalObjectTypeException $e) {
} catch (AssetServiceException $e) {
return new MutationResult(false, [$e->getMessage()]);
} catch (\Exception $e) {
throw new Exception('Failed to delete asset', 1591537315);
}

return true;
return new MutationResult(true, ['Asset deleted']);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion Resources/Private/GraphQL/schema.root.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type Mutation {
copyrightNotice: String
): Asset!

deleteAsset(id: AssetId!, assetSourceId: AssetSourceId!): Boolean!
deleteAsset(id: AssetId!, assetSourceId: AssetSourceId!): DeleteAssetResult!

replaceAsset(
id: AssetId!
Expand Down Expand Up @@ -323,6 +323,11 @@ type Image {
alt: String
}

type DeleteAssetResult {
success: Boolean!
messages: [String!]!
}

"""
The result of a single file upload
"""
Expand Down
8 changes: 4 additions & 4 deletions Resources/Private/JavaScript/core/src/hooks/useDeleteAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface DeleteAssetVariables {
}

export default function useDeleteAsset() {
const [action, { error, data }] = useMutation<{ deleteAsset: boolean }, DeleteAssetVariables>(DELETE_ASSET);
const [action, { error, data }] = useMutation<{ deleteAsset: MutationResult }, DeleteAssetVariables>(DELETE_ASSET);
const setSelectedAsset = useSetRecoilState(selectedAssetIdState);
const assetRemoved = useEvent(assetRemovedEvent);

Expand All @@ -35,9 +35,9 @@ export default function useDeleteAsset() {
cache.evict({ id: cache.identify({ __typename: 'Asset', id: assetId }) });
cache.gc();
},
}).then(({ data: { deleteAsset: success } }) => {
if (!success) {
throw new Error('Could not delete asset');
}).then(({ data: { deleteAsset } }) => {
if (!deleteAsset.success) {
throw new Error(deleteAsset.messages.join(', '));
}

assetRemoved({ assetId, assetSourceId });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { gql } from '@apollo/client';

const DELETE_ASSET = gql`
mutation DeleteAsset($id: AssetId!, $assetSourceId: AssetSourceId!) {
deleteAsset(id: $id, assetSourceId: $assetSourceId)
deleteAsset(id: $id, assetSourceId: $assetSourceId) {
success
messages
}
}
`;

Expand Down
5 changes: 5 additions & 0 deletions Resources/Private/JavaScript/core/typings/MutationResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface MutationResult {
success: boolean;
messages: string[];
data: any[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ const AssetActions: React.FC<ItemActionsProps> = ({ asset }: ItemActionsProps) =

return true;
} catch ({ message }) {
Notify.error(
translate('action.deleteAsset.error', 'Error while trying to delete the asset'),
message
);
Notify.error(message);
}
}

Expand Down

0 comments on commit 082ca17

Please sign in to comment.