Skip to content

Commit

Permalink
edit and delete text replaed by icons
Browse files Browse the repository at this point in the history
Signed-off-by: Ritesh Singh <1124ritesh@gmail.com>
  • Loading branch information
riteshsingh1 committed May 6, 2021
1 parent eaf83ac commit 28af236
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 48 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "imritesh/livecrud",
"description": "Generate Basic Crud Operations With Livewire and Tailwind Css For Laravel",
"license": "MIT",
"version": "v2.1.4",
"version": "v2.1.5",
"authors": [
{
"name": "Ritesh Singh",
Expand Down
111 changes: 72 additions & 39 deletions src/Commands/LiveCrudView.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class LiveCrudView extends GeneratorCommand


protected $emailNames = [
'email',
'email_address',
'email',
'email_address',
];

public function handle()
{
if (!is_dir(resource_path('views/livewire'))){
if (!is_dir(resource_path('views/livewire'))) {
mkdir(resource_path('views/livewire'));
}
$viewPath = resource_path('views/livewire/').strtolower($this->arguments()['name']).'.blade.php';
$viewPath = resource_path('views/livewire/') . strtolower($this->arguments()['name']) . '.blade.php';
$content = file_get_contents($this->getStub());
$content = $this->buildContent($content);
file_put_contents($viewPath, $content);
Expand Down Expand Up @@ -54,9 +54,9 @@ public function getForm()
foreach ($columns as $column) {
if ($column != 'created_at' || $column != 'updated_at') {
if ($c == 1) {
$str .= $this->makeInput($column). PHP_EOL;
$str .= $this->makeInput($column) . PHP_EOL;
} else {
$str .= $this->makeInput($column). PHP_EOL;
$str .= $this->makeInput($column) . PHP_EOL;
}
}
$c++;
Expand All @@ -69,12 +69,10 @@ public function makeInput($name)
$type = $this->getType($name);
$label = ucfirst(str_replace('-', ' ', Str::slug($name)));
$message = '{{ $message }}';
if(config('livecrud.template') == 'tailwind')
{
if (config('livecrud.template') == 'tailwind') {
return "<div><label class='block'><span class='text-gray-700 @error('{$name}') text-red-500 @enderror'>{$label}</span><input type='{$type}' class='mt-1 block w-full rounded-md border-gray-300 shadow-sm @error('{$name}') border-red-500 @enderror focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50' wire:model='{$name}'>@error('{$name}')<span class='text-red-500 text-sm'>{$message}</span>@enderror</label></div>";
}
if(config('livecrud.template') == 'bootstrap')
{
if (config('livecrud.template') == 'bootstrap') {
return "<div class='form-group'><label for='{$name}'>{$label}</label><input type='{$type}' class='form-control @error('{$name}') is-invalid @enderror' wire:model='{$name}'>@error('{$name}')<div class='invalid-feedback'>{$message}</div>@enderror</div>";

}
Expand All @@ -84,12 +82,10 @@ public function makeInput($name)

public function getType($name)
{
if(in_array(strtolower($name), $this->emailNames))
{
if (in_array(strtolower($name), $this->emailNames)) {
return 'email';
}
if (strtolower($name) == 'password')
{
if (strtolower($name) == 'password') {
return 'password';
}
return 'text';
Expand All @@ -102,55 +98,92 @@ public function getRenderedData()
$columns = $model->getFillable();
$str = '';
$c = 1;
$str.='@foreach($rows as $row)'.PHP_EOL;
$str.='<tr>';
foreach ($columns as $column) {
if ($column != 'created_at' || $column != 'updated_at') {
if ($c == 1) {
$str .= $this->getDynamicData(str_replace('-', ' ', Str::slug($column))). PHP_EOL;
} else {
$str .= $this->getDynamicData(str_replace('-', ' ', Str::slug($column))). PHP_EOL;
$str .= '@foreach($rows as $row)' . PHP_EOL;
$str .= '<tr>';
if (config('livecrud.template') == 'tailwind') {
foreach ($columns as $column) {
if ($column != 'created_at' || $column != 'updated_at') {
if ($c == 1) {
$str .= $this->getDynamicData(str_replace('-', ' ', Str::slug($column))) . PHP_EOL;
} else {
$str .= $this->getDynamicData(str_replace('-', ' ', Str::slug($column))) . PHP_EOL;
}
}
$c++;
}
$c++;
}
$str.='<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
$str .= '<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="text-indigo-600 hover:text-indigo-900" wire:click="edit({{ $row->id }})">Edit</a>
<a href="#" class="text-indigo-600 hover:text-indigo-900" wire:click="confirmDelete({{ $row->id }})">Delete</a>
</td></tr>';
$str.= '@endforeach'.PHP_EOL;
$str .= '@endforeach' . PHP_EOL;

}
if (config('livecrud.template') == 'bootstrap') {
foreach ($columns as $column) {
if ($column != 'created_at' || $column != 'updated_at') {
if ($c == 1) {
$str .= $this->getDynamicData(str_replace('-', ' ', Str::slug($column))) . PHP_EOL;
} else {
$str .= $this->getDynamicData(str_replace('-', ' ', Str::slug($column))) . PHP_EOL;
}
}
$c++;
}
$str .= '<td>
<a href="#" class="text-primary" wire:click.prevent="edit({{ $row->id }})">
<svg xmlns="http://www.w3.org/2000/svg" style="width:20px; height: 20px;" viewBox="0 0 20 20" fill="currentColor">
<path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z" />
<path fill-rule="evenodd" d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" clip-rule="evenodd" />
</svg>
</a>
<a href="#" class="text-danger" wire:click.prevent="confirmDelete({{ $row->id }})"> <svg xmlns="http://www.w3.org/2000/svg" style="width:20px; height: 20px;" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd" />
</svg></a>
</td></tr>';
$str .= '@endforeach' . PHP_EOL;

}
return $str;
}

public function getDynamicData($name) :string
public function getDynamicData($name): string
{
return ' <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $row->'.$name.'}}</td>'.PHP_EOL;
if (config('livecrud.template') == 'bootstrap') {
return ' <td>{{ $row->' . $name . '}}</td>' . PHP_EOL;
}
return ' <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $row->' . $name . '}}</td>' . PHP_EOL;
}

public function getHeadings(): string
{
$class = 'App\\Models\\' . $this->arguments()['name'];
$model = new $class;
$columns = $model->getFillable();
$c = 1;
$str ='';
$str = '';
foreach ($columns as $column) {
if ($column != 'created_at' || $column != 'updated_at') {
if ($c == 1) {
$str .= $this->getInput(str_replace('-', ' ', Str::slug($column))). PHP_EOL;
$str .= $this->getInput(str_replace('-', ' ', Str::slug($column))) . PHP_EOL;
} else {
$str .= $this->getInput(str_replace('-', ' ', Str::slug($column))). PHP_EOL;
$str .= $this->getInput(str_replace('-', ' ', Str::slug($column))) . PHP_EOL;
}
}
$c++;
}

return $str;
}

public function getInput($name): string
{
if (config('livecrud.template') == 'bootstrap') {
return '<th>' . strtoupper($name) . '
</th>' . PHP_EOL;
}
return '<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
'.strtoupper($name).'
</th>'.PHP_EOL;
' . strtoupper($name) . '
</th>' . PHP_EOL;
}


Expand All @@ -159,21 +192,21 @@ public function getInput($name): string
*
* @return string
* @throws \Exception
* @throws \Exception
*/
protected function getStub()
{
if(config('livecrud.template') == 'bootstrap')
{
if (file_exists(base_path() . '/stubs/bootstrap.view.php.stub')){
if (config('livecrud.template') == 'bootstrap') {
if (file_exists(base_path() . '/stubs/bootstrap.view.php.stub')) {
return base_path() . '/stubs/bootstrap.view.php.stub';
}
return base_path().'/vendor/imritesh/livecrud/src/stubs/bootstrap.view.php.stub';
return base_path() . '/vendor/imritesh/livecrud/src/stubs/bootstrap.view.php.stub';
}

if (file_exists(base_path() . '/stubs/view.php.stub')){
if (file_exists(base_path() . '/stubs/view.php.stub')) {
return base_path() . '/stubs/view.php.stub';
}
return base_path().'/vendor/imritesh/livecrud/src/stubs/view.php.stub';
return base_path() . '/vendor/imritesh/livecrud/src/stubs/view.php.stub';
}

}
10 changes: 8 additions & 2 deletions src/stubs/bootstrap.crud.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class {{ name }} extends Component
{
$this->mode = 'create';
$this->showForm = true;

$this->emit("showForm");
}


Expand All @@ -59,13 +61,15 @@ class {{ name }} extends Component

{{ setedibleval }}


$this->emit("showForm");
$this->showForm = true;
}

public function closeForm()
{
$this->showForm = false;

$this->emit("hideForm");
}

public function store()
Expand All @@ -77,8 +81,10 @@ class {{ name }} extends Component
{{ codeTosave }}

$this->resetForm();
$this->emit("hideForm");
session()->flash('message', 'Record Saved Successfully');
$this->showForm = false;

}

public function resetForm()
Expand All @@ -96,7 +102,7 @@ class {{ name }} extends Component
{{ codeTosave }}

$this->resetForm();

$this->emit("hideForm");
session()->flash('message', 'Record Updated Successfully');
}

Expand Down
12 changes: 6 additions & 6 deletions src/stubs/bootstrap.view.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<b class="capitalize">Success!</b> {{ session('message') }}
</span>
<button wire:click="clearFlash()"
class="float-right btn-xs btn-success btn-outline">
class="float-right btn-xs btn btn-outline-success">
<span>×</span>
</button>
</div>
Expand All @@ -23,7 +23,6 @@
<div class="col-md-9 float-right">
<button type="button"
class="btn btn-primary float-right"
data-toggle="modal" data-target="#exampleModal"
wire:click="create">
Add New User
</button>
Expand All @@ -33,16 +32,17 @@
<div class="flex row">
<div class="card-body">
<table width="100%" class="table table-bordered">
<thead>
<tr>

{{ headings }}

<th scope="col" class="relative px-6 py-3">
<th scope="col">
<span class="sr-only">Actions</span>
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<tbody>
{{ renderedData }}
</tbody>
</table>
Expand All @@ -57,11 +57,11 @@

{{-- create / edit form --}}

<div class="modal fade" wire:ignore.self id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal fade" wire:ignore.self id="showForm" tabindex="-1" role="dialog" aria-labelledby="showFormLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> {{ $mode == 'create' ? 'Add New Record' : 'Update Record ' }}</h5>
<h5 class="modal-title" id="showFormLabel"> {{ $mode == 'create' ? 'Add New Record' : 'Update Record ' }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
Expand Down

0 comments on commit 28af236

Please sign in to comment.