Skip to content

Commit

Permalink
Improves Create Command - flatpickr console warning (#1295)
Browse files Browse the repository at this point in the history
* Refactor Create Command

* Improve message

* Fix phpstan

* remove doctrine/dbal

* fix phpstan

* Fix create command

* Fix phpstan
  • Loading branch information
luanfreitasdev authored Dec 28, 2023
1 parent 1b43ac7 commit 5f4e786
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 106 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
],
"require": {
"php": "^8.1",
"doctrine/dbal": "^3.7",
"livewire/livewire": "^3.1.0",
"laravel/prompts": "^0.1.13"
},
"require-dev": {
"composer/composer": "^2.6.6",
"laravel/pint": "^1.13.7",
"laradumps/laradumps-core": "^0.3.0",
"laradumps/laradumps-core": "^1.1",
"spaze/phpstan-disallowed-calls": "^2.16.1",
"larastan/larastan": "^2.7",
"pestphp/pest": "^2.28.1",
Expand Down
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/powergrid.js": "/powergrid.js?id=495809d3974beb1d1566c20a3225b674",
"/powergrid.js": "/powergrid.js?id=d96096b65479becbf69b80ca0c298e90",
"/bootstrap5.css": "/bootstrap5.css?id=03aba1df82c23db07c1d1096efcd42ae",
"/tailwind.css": "/tailwind.css?id=479d85eb8b0b8341542e0b979c84f17d",
"/tom-select.css": "/tom-select.css?id=7af730d2c4bf937316d4002948b1571d",
Expand Down
2 changes: 1 addition & 1 deletion dist/powergrid.js

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions resources/js/components/pg-flatpickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ export default (params) => ({
element: null,
selectedDates: null,
init() {
if(typeof flatpickr == "undefined") {
console.log('%c%s',
'color: #f59e0c; font-size: 1.2em; font-weight: bold; line-height: 1.5',
` PowerGrid`);

console.error('%c%s',
'font-size: 1em; line-height: 1.5',
`
Failed to mount filter: Filter::datetime('${this.dataField}') on table ['${this.tableName}']
• Install flatpickr: npm install flatpickr
• Add 'import flatpickr from "flatpickr"' in resources/js/app.js file
`
);
}

window.addEventListener(`pg:clear_flatpickr::${this.tableName}:${this.dataField}`, () => {
if (this.$refs.rangeInput && this.element) {
this.element.clear()
Expand All @@ -24,13 +41,13 @@ export default (params) => ({

const lang = this.locale.locale;

if (typeof lang !== 'undefined') {
if (typeof lang !== 'undefined' && typeof flatpickr != "undefined") {
this.locale.locale = require("flatpickr/dist/l10n/"+lang+".js").default[lang];
}

const options = this.getOptions()

if(this.$refs.rangeInput) {
if(this.$refs.rangeInput && typeof flatpickr != "undefined") {
this.element = flatpickr(this.$refs.rangeInput, options);

this.selectedDates = this.$wire.get(`filters.${this.type}.${this.dataField}.formatted`)
Expand Down
69 changes: 69 additions & 0 deletions src/Commands/Actions/DependenciesCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class DependenciesCheck
{
/**
* Check if flatpickr is installed
*
*/
public static function flatpickr(): ?string
{
$filesToCheck = [
base_path('tailwind.config.js'),
base_path('resources/js/app.js'),
];

$message = "\n💡 It seems you are not using the <comment>flatpickr</comment> plugin.\n Please check: <comment>https://livewire-powergrid.com/table/column-filters.html#filter-datetimepicker</comment> for more information.";

foreach ($filesToCheck as $file) {
if (File::exists($file) && !Str::contains(File::get($file), "flatpickr")) {
return $message;
}
}

return null;
}

/**
* Check if openspout/openspout is installed
*
*/
public static function openspout(): ?string
{
$file = base_path() . '/' . 'composer.json';

if (File::exists($file)) {
$content = File::get($file);

if (!Str::contains($content, "openspout/openspout")) {
return("\n💡 It seems you are using the <comment>openspout/openspout</comment> package.\n Please check: <comment>https://livewire-powergrid.com/table/features-setup.html#exportable</comment> for more information.");
}
}

return null;
}

/**
* Check if TailwindForms is installed
*
*/
public static function tailwindForms(): ?string
{
$file = base_path() . '/' . 'tailwind.config.js';

if (File::exists($file)) {
$content = File::get($file);

if (Str::contains($content, "require('@tailwindcss/forms')") === true) {
return("\n💡 It seems you are using the plugin <comment>Tailwindcss/form</comment>.\n Please check: <comment>https://livewire-powergrid.com/get-started/configure.html#tailwind-forms</comment> for more information.");
}
}

return null;
}
}
54 changes: 24 additions & 30 deletions src/Commands/Actions/FillableTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Doctrine\DBAL\Exception;
use Illuminate\Support\Facades\{File, Schema};
use Illuminate\Support\Str;

Expand Down Expand Up @@ -50,60 +49,55 @@ public static function eloquentBuilder(string $modelQualifiedName, string $model
continue;
}

$conn = $model->getConnection();
$connection = Schema::connection($model->getConnection()->getName());

$conn->getDoctrineSchemaManager()
->getDatabasePlatform()
->registerDoctrineTypeMapping('enum', 'string');

$hasColumn = function () use ($model, $field, $conn) {
$hasColumn = function () use ($model, $field, $connection) {
try {
return Schema::connection($conn->getDatabaseName())
->hasColumn($model->getTable(), $field);
return $connection->hasColumn($model->getTable(), $field);
} catch (\Exception) {
return Schema::hasColumn($model->getTable(), $field);
}
};

if ($hasColumn()) {
$column = $conn->getDoctrineColumn($model->getTable(), $field);
$columnType = $connection->getColumnType($model->getTable(), $field);

$title = Str::of($field)->replace('_', ' ')->ucfirst();

if (in_array($column->getType()->getName(), ['datetime', 'date'])) {
if (in_array($columnType, ['datetime', 'date'])) {
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '_formatted\', \'' . $field . '\')' . "\n" . ' ->sortable(),' . "\n\n";
}

if ($column->getType()->getName() === 'datetime') {
if ($columnType === 'datetime') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '_formatted\', fn (' . $modelUnqualifiedName . ' $model) => Carbon::parse($model->' . $field . ')->format(\'d/m/Y H:i:s\'))';
$filters .= ' Filter::datetimepicker(\'' . $field . '\'),' . "\n";

continue;
}

if ($column->getType()->getName() === 'date') {
if ($columnType === 'date') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '_formatted\', fn (' . $modelUnqualifiedName . ' $model) => Carbon::parse($model->' . $field . ')->format(\'d/m/Y\'))';
$filters .= ' Filter::datepicker(\'' . $field . '\'),' . "\n";

continue;
}

if ($column->getType()->getName() === 'boolean') {
if ($columnType === 'boolean') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '\')';
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\')' . "\n" . ' ->toggleable(),' . "\n\n";
$filters .= ' Filter::boolean(\'' . $field . '\'),' . "\n";

continue;
}

if (in_array($column->getType()->getName(), ['smallint', 'integer', 'bigint'])) {
if (in_array($columnType, ['smallint', 'integer', 'bigint'])) {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '\')';
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\'),' . "\n";

continue;
}

if ($column->getType()->getName() === 'string') {
if ($columnType === 'string') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '\')';
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\')' . "\n" . ' ->sortable()' . "\n" . ' ->searchable(),' . "\n\n";
$filters .= ' Filter::inputText(\'' . $field . '\')->operators([\'contains\']),' . "\n";
Expand Down Expand Up @@ -139,7 +133,6 @@ public static function eloquentBuilder(string $modelQualifiedName, string $model
* @param string $databaseTableName
* @param string|null $stubFile
* @return string
* @throws Exception
*/
public static function queryBuilder(string $databaseTableName, string $stubFile = null): string
{
Expand All @@ -151,51 +144,52 @@ public static function queryBuilder(string $databaseTableName, string $stubFile
$columns = "[\n";
$filters = "[\n";

foreach ($columnListing as $field) {
$conn = Schema::getConnection();

$conn->getDoctrineSchemaManager()
->getDatabasePlatform()
->registerDoctrineTypeMapping('enum', 'string');
$filteredColumns = collect($columnListing)
->filter(function ($column) {
return !in_array($column, ['password', 'remember_token', 'email_verified_at']);
})
->toArray();

$column = $conn->getDoctrineColumn($databaseTableName, $field);
/** @var string $field */
foreach ($filteredColumns as $field) {
$columnType = Schema::getColumnType($databaseTableName, $field);

$title = Str::of($field)->replace('_', ' ')->ucfirst();

if (in_array($column->getType()->getName(), ['datetime', 'date'])) {
if (in_array($columnType, ['datetime', 'date'])) {
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '_formatted\', \'' . $field . '\')' . "\n" . ' ->sortable(),' . "\n\n";
}

if ($column->getType()->getName() === 'datetime') {
if ($columnType === 'datetime') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '_formatted\', fn ($model) => Carbon::parse($model->' . $field . ')->format(\'d/m/Y H:i:s\'))';
$filters .= ' Filter::datetimepicker(\'' . $field . '\'),' . "\n";

continue;
}

if ($column->getType()->getName() === 'date') {
if ($columnType === 'date') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '_formatted\', fn ($model) => Carbon::parse($model->' . $field . ')->format(\'d/m/Y\'))';
$filters .= ' Filter::datepicker(\'' . $field . '\'),' . "\n";

continue;
}

if ($column->getType()->getName() === 'boolean') {
if ($columnType === 'boolean') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '\')';
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\')' . "\n" . ' ->toggleable(),' . "\n\n";
$filters .= ' Filter::boolean(\'' . $field . '\'),' . "\n";

continue;
}

if (in_array($column->getType()->getName(), ['smallint', 'integer', 'bigint'])) {
if (in_array($columnType, ['smallint', 'integer', 'bigint'])) {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '\')';
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\'),' . "\n";

continue;
}

if ($column->getType()->getName() === 'string') {
if ($columnType === 'string') {
$datasource .= "\n" . ' ->addColumn(\'' . $field . '\')';
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\')' . "\n" . ' ->sortable()' . "\n" . ' ->searchable(),' . "\n\n";
$filters .= ' Filter::inputText(\'' . $field . '\')->operators([\'contains\']),' . "\n";
Expand Down
28 changes: 0 additions & 28 deletions src/Commands/Actions/TailwindForm.php

This file was deleted.

Loading

0 comments on commit 5f4e786

Please sign in to comment.