Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait Collisions #10

Open
Israel5 opened this issue Jul 29, 2020 · 5 comments
Open

Trait Collisions #10

Israel5 opened this issue Jul 29, 2020 · 5 comments

Comments

@Israel5
Copy link

Israel5 commented Jul 29, 2020

Hi,
I know this issue was addressed in different ways before, but I'm really having troubles and I don't know very much how to solve it.

My stack:

PHP 7.4.7
Laravel 7.20.0
Nova 3.6.0

The problem is when I try to use https://github.com/dcasia/conditional-container and https://github.com/dcasia/nova-json-wrapper at the same time.

For both packages, you have to use the traits to have everything working, but when they are used together, we have conflicts:

class CaseX extends Resource
{

    use HasConditionalContainer, HasJsonWrapper;
    ...

Then you get this error:
image
I know it happens because both traits have repeated methods, but I don't know how to exactly solve this issue.

I've been around checking some possible workarounds, and I found some related links, like:

I read the workarounds, but I didn't understand how to create them.

Could anyone with more experience help me to solve this issue?

Thanks!

@milewski
Copy link
Member

milewski commented Jul 29, 2020

Okay you need to do this:

1 - create a php class like this:

<?php

declare(strict_types = 1);

namespace App\Nova\Concerns;

use App\Nova\Resource;

abstract class UseConditionalContainer extends Resource
{
    use HasConditionalContainer;
}

2 - Create another PHP class that extends the previous one

<?php

declare(strict_types = 1);

use App\Nova\Resource;

abstract class UseJsonWraperAndConditionalContainer extends UseConditionalContainer
{
    use HasJsonWrapper;
}

3 - Now use the UseJsonWraperAndConditionalContainer on your resource class

class CaseX extends UseJsonWraperAndConditionalContainer
{
....   
}

@Israel5
Copy link
Author

Israel5 commented Jul 29, 2020

@milewski first of all, thank you for your help, I really appreciate it! ⭐⭐⭐⭐⭐

So I did what you recommended, it worked.

What happens now, is that when I try to create complex (nested) structures, the conditional container seems to be breaking in the UI when I'm editing a resource.
image

Basically, I'll use a lot of JsonWrapper and ConditionalContainer nested on each other, like in this example:
image

Whether I use multiple JsonWrapper nested or multiple ConditionalContainer nested it works fine, but when I mix and use both, I get this error Cannot read property 'join' of undefined.
I saved the file here: https://pastebin.com/YFdTS4nx [check line 157].

If I use the example of https://github.com/dcasia/nova-json-wrapper#notes, I get the same error:
image

Obrigado!

@milewski
Copy link
Member

milewski commented Jul 29, 2020

Uhm I think this bug appeared on the latest version of nova.. when I built this package nova was on version 2.something .. I remember someone mentioning on the issues about it not working on the latest version...

meanwhile what I could recommend you to do is not nesting so many JSON Wrappers .. instead, have a single level nesting and add the attributes of your fields with dots.. for example:

JsonWrapper::make('shipping', [
   Text::make('Field Name', 'field.dot.name')
])->fillUsing(function (){
    //Arr::dot()
})

And use the fillUsing option to parse the data which will be using field.dot.name and use Arr::dot to transform it back to an array before saving it, and do the inverse on resolveUsing method

@jakubmikita
Copy link

There's a better way with trait precedence:

use HasConditionalContainer, HasJsonWrapper {
    HasConditionalContainer::availablePanelsForDetail insteadof HasJsonWrapper;
    HasConditionalContainer::availableFields insteadof HasJsonWrapper;
}

@milewski
Copy link
Member

milewski commented Apr 8, 2021

@Kubitomakita this will make only 1 method of the 2 traits run, in your example, only methods from HasConditionalContainer will be executed... and this is not right, because while it may seem to work on the index/edit/create view... jsonWrapper won't work properly on the detail view..... hance, why you must to inherit, this will ensure that all methods will be called from all traits via parrent::method() ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants