Skip to content

Commit

Permalink
Merge pull request #36 from joelbutcher/modify-connected-accounts-sha…
Browse files Browse the repository at this point in the history
…red-inertia-data

[2.x] Move shared inertia data responsibility to ConnectedAccounts abstract
  • Loading branch information
joelbutcher authored Jan 11, 2021
2 parents 313303b + e5d10d4 commit b5f8072
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/ConnectedAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ public function user()
{
return $this->belongsTo(Jetstream::userModel(), 'user_id');
}

/**
* Get the data that should be shared with Inertia.
*
* @return array
*/
public function getSharedInertiaData()
{
return [
'id' => $this->id,
'provider' => $this->provider,
'created_at' => (new \DateTime($this->created_at))->format('d/m/Y H:i'),
];
}
}
12 changes: 5 additions & 7 deletions src/Http/Middleware/ShareInertiaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JoelButcher\Socialstream\Http\Middleware;

use Inertia\Inertia;
use JoelButcher\Socialstream\ConnectedAccount;
use JoelButcher\Socialstream\Socialstream;

class ShareInertiaData
Expand All @@ -22,13 +23,10 @@ public function handle($request, $next)
'show' => Socialstream::show(),
'providers' => Socialstream::providers(),
'hasPassword' => $request->user() && ! is_null($request->user()->password),
'connectedAccounts' => $request->user() ? $request->user()->connectedAccounts->map(function ($account) {
return (object) [
'id' => $account->id,
'provider' => $account->provider,
'created_at' => (new \DateTime($account->created_at))->format('d/m/Y H:i'),
];
}) : [],
'connectedAccounts' => $request->user() ? $request->user()->connectedAccounts
->map(function (ConnectedAccount $account) {
return (object) $account->getSharedInertiaData();
}) : [],
];
},
]));
Expand Down

0 comments on commit b5f8072

Please sign in to comment.