Skip to content

Commit

Permalink
fixed back-compat for ->vuex()
Browse files Browse the repository at this point in the history
  • Loading branch information
reed-jones committed Feb 13, 2020
1 parent eff58d9 commit f308b81
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
15 changes: 10 additions & 5 deletions packages/@phased/state/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export const hydrate = (vuexState: VuexStore, options: VuexcellentOptions = defa
...options
}
// PHP Converts the empty array to a... empty array, booo
let phaseState = window.__PHASE_STATE__ || {}
if (Array.isArray(phaseState) && !phaseState.length) {
phaseState = {}
let INITIAL = window.__PHASE_STATE__ || {};
if (Array.isArray(INITIAL) && !INITIAL.length) {
INITIAL = {}
}
let { mutations, actions, ...phaseState } = INITIAL

// merge incoming (store) options with window.__PHASE_STATE__
const mergedState = <VuexStore>(
Expand All @@ -51,8 +52,12 @@ export const hydrate = (vuexState: VuexStore, options: VuexcellentOptions = defa

// inject plugin
newState.plugins = newState.plugins
? [VuexcellentPlugin, ...newState.plugins]
: [VuexcellentPlugin];
? [
VuexcellentPlugin,
...newState.plugins
] : [
VuexcellentPlugin
];

} else if (options.generateMutations) {
console.error(
Expand Down
36 changes: 17 additions & 19 deletions packages/Phased/Routing/PhasedRoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,23 @@ protected function recursiveMergeConfigFrom($paths, $key)
*/
public function registerRouterMacro(): void
{
foreach (['phase', 'vuex'] as $macro) {
Route::macro($macro, function (...$args) {
$route = $this->match(['GET', 'HEAD'], ...$args);

$controller = $route->action['controller'] ?? null;

// make sure its not a closure
// & make sure its controller@method
if (is_string($controller) && Str::is('*@*', $controller)) {
Phase::addRoute($route->uri, $route->action);
} else {
throw new Exception("Route::phase is not compatible with closures.\n"
."Please use the controller@method syntax.\n"
."Failed on '{$route->uri}' route.");
}

return $route;
});
}
Route::macro('phase', function (...$args) {
$route = $this->match(['GET', 'HEAD'], ...$args);

$controller = $route->action['controller'] ?? null;

// make sure its not a closure
// & make sure its controller@method
if (is_string($controller) && Str::is('*@*', $controller)) {
Phase::addRoute($route->uri, $route->action);
} else {
throw new Exception("Route::phase is not compatible with closures.\n"
."Please use the controller@method syntax.\n"
."Failed on '{$route->uri}' route.");
}

return $route;
});
}

/**
Expand Down
13 changes: 12 additions & 1 deletion packages/Phased/State/Mixins/VuexResponseMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@
class VuexResponseMixin
{
public function phase()
{
return $this->phaseResponse();
}

public function vuex()
{
return $this->phaseResponse();
}

private function phaseResponse()
{
return function (array $data = [], int $status = 200, array $headers = [], int $options = 0) {
try {
$data = array_merge(['$vuex' => Vuex::toArray()], $data);

return $this->json($data, $status, $headers, $options);
} catch (Exception $err) {
return $this->json([
'phase_error' => 'An error occurred while phasing'
'phase_error' => 'An error occurred while phasing',
], 422);
}
};
Expand Down

0 comments on commit f308b81

Please sign in to comment.