Skip to content

Commit

Permalink
feat: implements bulk delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Apr 24, 2024
1 parent d9db137 commit 7c79fed
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/js/app.js": "/js/app.js?id=ab09a08f7ca5e793049ca0c7871f09b6",
"/js/app.js": "/js/app.js?id=cca782392d5c21be6a3420881af9c366",
"/js/manifest.js": "/js/manifest.js?id=22bb55ce20c9c883a45b5c11d8eca703",
"/js/vendor.js": "/js/vendor.js?id=a21c2dcb4fdd557ce1ec6c040ad40be4",
"/css/app.css": "/css/app.css?id=d75c395a1c5c29d8b4c39d09727b550c"
Expand Down
2 changes: 2 additions & 0 deletions resources/js/actions/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import CreateWizard from "./create-wizard";
import ViewMediaDirectory from "./view-media-directory";
import Dashboard from "./dashboard";
import SelectOne from "./select-one";
import BulkDelete from "./bulk-delete";

export default {
'bulk-delete': BulkDelete,
'index': Index,
'edit': Edit,
'create': Create,
Expand Down
62 changes: 62 additions & 0 deletions resources/js/actions/bulk-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React, {useState} from 'react';
import api from "../api/api";
import path from "../state/path";
import ui from "../core/ui/util";
import Button from "../core/ui/button";
import i18n from "../util/i18n";

function BulkDelete(props) {

const [state, setState] = useState({
options: [],
selectedOptionId: null,
});

const ids = `${props.path.params.id}`;

const bulkDelete = async () => {
// Load the data from the backend (with id as param)
await api.execute.get(props.path, props.id,'delete', {
ids: ids.split(',').map(id => parseInt(id)),
});
// Redirect
redirect();
// Notify the user
ui.notify('Deleted all items');
}

const redirect = () => {
path.handleRedirect(props);
}

return (
<div className="delete">
<div className="delete__text">
Are you sure you wish to delete these {props.plural}?
</div>
<div className="delete__footer">
<div className="delete__confirm">
<Button onClick={e => bulkDelete()} text={i18n.get('snippets.delete_singular_confirm', {singular: props.plural})}/>
</div>
<div className="delete__cancel">
<Button onClick={redirect} text={i18n.get('snippets.delete_singular_cancel')} style={'secondary'}/>
</div>
</div>
</div>
);
}

BulkDelete.defaultProps = {
type: '',
components: [],
path: {},
id: 0,
data: {},
params: null,
redirect: 'index',
redirectBack: false,
singular: '',
plural: ''
};

export default BulkDelete;
49 changes: 49 additions & 0 deletions src/Action/BulkDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace ReinVanOyen\Cmf\Action;

use Illuminate\Http\Request;
use ReinVanOyen\Cmf\Http\Resources\ModelCollection;
use ReinVanOyen\Cmf\Traits\CanRedirect;
use ReinVanOyen\Cmf\Traits\HasSingularPlural;

class BulkDelete extends Action
{
use CanRedirect;
use HasSingularPlural;

/**
* @param string $meta
* @throws \ReinVanOyen\Cmf\Exceptions\InvalidMetaException
*/
public function __construct(string $meta)
{
$this->meta($meta);
$this->singular($meta::getSingular());
$this->plural($meta::getPlural());
}

/**
* @return string
*/
public function type(): string
{
return 'bulk-delete';
}

/**
* @param Request $request
* @return bool
*/
public function apiDelete(Request $request): bool
{
$modelClass = $this->getMeta()::getModel();
$allModels = $modelClass::whereIn('id', $request->input('ids'))->get();

foreach ($allModels as $model) {
$model->delete();
}

return true;
}
}

0 comments on commit 7c79fed

Please sign in to comment.