How to call one method globally across components in react native - react-redux

I have a method called submit() in one component like eg. leave.js.
I have declared all the router navigation in router.js component.
I have placed a button in leave component header which is in router.js to apply the leave.
Now, i want to call this method from the click event of the button which is placed in the header.
How can i call this method globally from another component.
Kindly help me with a solution.
Thanks in Advance,
Regards,
Janani
I have tried the below link but its not working for me.
https://github.com/kriasoft/react-starter-kit/issues/909

When you create and use a function file, it's very useful.
Example
let name = "name";
function setName(data) {
name = data
}
function getName() {
return name
}
export { setName, getName }
import {setName, getName } from "./functionpath"
componentWilmount(){
setName("John");
}
componentDidmount(){
const name = getName();
alert(name);
}

Related

Laravel Livewire refreshing parent full-page component

I'm using a full-page component that contains a modal child component that handles updating the model. When the user clicks save on the modal and the update is made, I want to refresh the parent component, which is really the overall page. However, when I save, nothing happens.
Here's my parent Component code:
class LocationComponent extends Component
{
public Location $location;
public $client;
public $legalEntities;
public $smsTypes;
public $clientContacts;
public $locationContacts;
protected $listeners = [
'refreshParent' => '$refresh'
];
public function mount()
{
$this->legalEntities = LegalEntityType::all();
$this->smsTypes = SmsType::all();
$this->roles = Role::all();
$this->client = $this->location->client()->first();
$this->clientContacts = $this->client->users()->get();
$this->locationContacts = $this->location->users()->get();
}
public function render()
{
return view('livewire.locations.edit');
}
}
And here's the child component (modal):
class LocationEditModal extends Component
{
public $states;
public Location $location;
protected $rules = [
...
];
public function mount()
{
$this->states = USState::all();
}
public function render()
{
return view('livewire.location-edit-modal');
}
public function save()
{
$validatedData = $this->validate();
$this->location->save($validatedData);
$this->dispatchBrowserEvent('closeModal');
$this->emit('refreshParent');
}
}
I added this code in my parent blade file to see if the event was firing, and it appears it's not:
<script>
Livewire.on('refreshParent', event => {
alert('the refreshParent event was fired');
});
</script>
Here's the route from my web routes file:
Route::get('/locations/{location}', [LocationComponent::class, '__invoke'])
->middleware('auth')
->name('locations.edit');
I've also tried changing $refresh to just render, but that also didn't work.
UPDATE:
Ok, after moving my event listener script to my main layout file, I'm getting the event fired alert now. Still no update of the parent component, but it's a start.
UPDATE 2:
I'm wondering if it's how I'm referencing the model data in my parent component blade view. I'm just referencing it how I normally would like this:
<p class="d-flex" id="name">{{ $location->name }}</p>
I'm not sure if my parent component is maybe not getting the updated $location?
UPDATE 3:
Based on my last update, I thought maybe I need to refresh the model, so I tried adding this to my parent component mount method, but it didn't work either:
$this->location->refresh();
Not sure if this is relevant, but in my parent component, which has the $location, it's calling the child modal component like this:
<livewire:location-edit-modal :location="$location"/>
Again, just putting that in there in case it helps with a solution. I don't know if I'm doing some kind of a circular thing that's not going to work right? Grasping at straws at this point..
RESOLUTION
Ok, this probably won't help anyone else, but it seems the theme I am using had some left over Laravel 7 things that weren't removed (I think). Per the Livewire docs about upgrading (https://laravel-livewire.com/docs/2.x/upgrading) you're supposed to remove the ->namespace($this->namespace) line from the RouteServiceProvider if you're on version 7 of Laravel. Version 8 already has it removed. I thought I was using Laravel 8, but maybe there was an upgrade done on this theme and it was never removed.
The other problem was I was using #yield('content') in my layout, and that's apparently not supported anymore. With version 2 of livewire, it should use the {{ $slot }} syntax.
Are you sharing your actual code here? I'm a bit confused since neither your LocationComponent or LocationEditModal components ever actually sets a value to $this->location.
Either way, there is no need to re-render the whole LocationComponent modal, you can just refresh the $this->location to the updated version of the location to update the values.
Something like this should work:
class LocationComponent extends Component {
protected $listeners = [
'locationUpdated'
];
public function locationUpdated()
{
$this->location->refresh();
}
}
class LocationEditModal extends Component {
public function save()
{
$this->emit('locationUpdated');
}
}
I don't believe you shared you're actual code, so it's hard to make an accurate recommendation, but it doesn't seem like your modal needs be a separate component at all. Your LocationComponent is already isolated to a single Location anyways; I would just have the save/update logic on the LocationComponent directly instead of a whole separate component.

Laravel - Action not defined but it is defined

I get this error when i try loading blade.php
Action App\Http\Controllers\InventoryItemController#change not defined.
I have change function in InventoryItemController
public function change($new_status)
{
//
}
This started when I wanted to make button
Confirm Change
I did everything same when i made Edit button and that button works normally.
UPDATE 1
My button looks like this now
<a href="{{route('change', [$inventoryitem['new_status'],
$inventoryitem['asset_id']])}}"class="btn btn-info">Confirm Change</a>
and my change function is this
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
return redirect('showasset', compact('asset','asset_id'));
}
and my route in web is like this
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
But after i click button it just redirect me to url .../change/4/1 and that's it. Nothing changes.
Using Action is deprecated in Laravel
You can use routes instead.
Define Routes in your routes files (/routes/web.php) like.
Route::get('change/{status}','InventoryItemController#change')->name('change');
and then in your view
Confirm Change
In your controller use.
public function change ($status){
// rest of the function.
}
Hope this helps
Define your controller's method in route file as following:
Route::get('url/{new_status}',InventoryItemController#change);
Answer on UPDATE 1
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
$asset->save();
return view('your_view_path',compact('variable1','variable2'));
}
Final error was in my route
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
It should be like this
Route::get('change/{new_status}/{asset_id}','InventoryItemController#change')->name('change');
After that change everything is working flawlessly. Thank you for your help guys!

How do I send data to partial views from controller in laravel?

I have setup my navigation menu from a ViewComposer (see laravel view composers: https://laravel.com/docs/5.6/views#view-composers) like this
View::composer('partials.nav', function ($view) {
$view->with('menu', Nav::all());
});
What I need is that from some controllers to setup which navigation item is active, ie "current section".
Question:
How do I send from some controllers a variable to "partials.nav" like currentNavItem?
Do I send it with the rest of the variables for returned view?
like
return view('page.blade.php",$viewVariables + $optionalVariablesForPartialsViews);
It looks spammy
Side notes:
I use laravel 5.6
Later edit
It looks Laravel 5.1 : Passing Data to View Composer might be an options. I will try and get back .
Because the $variable you want to send differs in different controller's actions yes you need to specify the $variable
return view('page.blade.php",$viewVariables,$variablesForPartialsViews);
of course you might need to set a default value for the $variable in order to avoid undefined variable error
You should handle the parameters.
for exemple:
public function compose(View $view)
{
$view->with('page', $this->getPage());
}
public function getPage()
{
$viewVariables = 2;
$optionalVariablesForPartialsViews = 1;
return $viewVariables + $optionalVariablesForPartialsViews;
}
Under your app folder make a class named yourClassNameFacade. Your class would look like this.
class yourClassNameFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'keyNameYouDecide';
}
}
Then go to the file app/Providers/AppServiceProvider.php and add to the register function
public function register()
{
$this->app->bind('keyNameYouDecide', function (){
//below your logic, in my case a call to the eloquent database model to retrieve all items.
//but you can return whatever you want and its available in your whole application.
return \App\MyEloquentClassName::all();
});
}
Then in your view or any other place you want it in your application you do this to reference it.
view is the following code:
{{ resolve('keyNameYouDecide') }}
if you want to check what is in it do this:
{{ ddd(resolve('keyNameYouDecide')) }}
anywhere else in your code you can just do:
resolve('keyNameYouDecide'))

How to trigger a method in all pages request in Yii?

In the header section of my website I want to show new message. I have a method that fetches new methods and return them. The problem is that header section is in thelayout section and I don't want to repeat one method in all of my controllers.
How to achieve this by not copying the method to all of my controllers? I want to trigger newMessages() method on every page request to gather new messages for logged in user. How to do this the right way?
In your controller overwrite the oOntroller class function beforeAction()
protected function beforeAction($event)
{
$someResult = doSomething()
if ($someResult == $someValue)
{
return true;
}
else
{
return true;
}
}
The return value can be used to stop the request dead in its tracks. So if it returns false, the controller action is not called, and vice versa().
References : http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail
You can use import controller in another controller action. something like below
class AnotherController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.admin.YourController'); // YourController is another controller in admin controller folder
echo YourController::test(); // test is action in YourController
}
}

Dynamic router name for magento controller

How would I go about creating a custom module that has a controller with an action name that is dynamic, in the sense that it can be configured by the user in the admin area at will and be automatically updated in the custom module?
You can override this method in your controller:
public function getActionMethodName($action)
{
return 'indexAction';
}
public function indexAction()
{
//action name
var_dump($this->getRequest()->getActionName());
}
Then always will go to the index action, where you can use the original action name as a parameter.
then:
http://mysite/mymodule/mycontroller/im-dracula-blablabla
Will work!
I think you can approach this by using magic php method __call on your controller.
I assumed that you store your action name in a Magento config named 'mymodule/controller/action', so you can get the value using :
Mage::getStoreConfig('mymodule/controller/action');
Then you have the controller for example Mymodule/controllers/TestController.php
And you add the method in that controller like this :
public function __call($method, $arg) {
if ($method == Mage::getStoreConfig('mymodule/controller/action')) {
//Do whatever you want
}
}
This will make your controller //Do whatever you want when you accessing it using the action you specified in the config. The basic idea is like that. Hope this helps.

Resources