-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9db137
commit 7c79fed
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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,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; |
This file contains 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,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; | ||
} | ||
} |