Skip to content

Commit

Permalink
Next minor release (#83)
Browse files Browse the repository at this point in the history
* Update InertiaMiddleware.py

* fix: remove duplicate method

* fix: lazy load default shared errors from session

* add unit test for this case

* remove unused code

* clean commennts

Co-authored-by: Joseph Mancuso <[email protected]>
  • Loading branch information
girardinsamuel and josephmancuso authored Nov 6, 2021
1 parent c028d7f commit 5f6671d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
18 changes: 2 additions & 16 deletions src/masonite/inertia/core/Inertia.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ def get_page_data(self, component, props):

return page_data

def get_shared_props(self, key=None):
def get_shared_props(self, key=None, default=None):
"""Get all Inertia shared props or the one with the given key."""
if key:
return self.shared_props.get(key, None)
return data_get(self.shared_props, key, default)
else:
return self.shared_props

Expand All @@ -117,11 +117,6 @@ def share(self, key, value=None):
def flush_shared(self):
self.shared_props = {}

def get_shared(self, key=None, default=None):
if key:
return data_get(self.shared_props, key, default)
return self.shared_props

def get_props(self, all_props, component):
"""Get props to return to the page:
- when partial reload, required return 'only' props
Expand All @@ -141,7 +136,6 @@ def get_props(self, all_props, component):
if key in only_props:
props.update({key: all_props[key]})
else:
# remove lazy props
for prop_key, value in all_props.items():
if not isinstance(value, LazyProp):
props.update({prop_key: value})
Expand All @@ -159,14 +153,6 @@ def get_auth(self):
return {"user": ""}
return {"user": user.serialize()}

def get_messages(self):
session = self.application.make("session").get_driver()
return session.get_flashed_messages()

def get_errors(self):
session = self.application.make("session").get_driver()
return session.get_error_messages()

def get_component(self, component):
# TODO: check if escaping before here is needed
return html.escape(component)
10 changes: 1 addition & 9 deletions src/masonite/inertia/core/InertiaResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ def __init__(self, application, page_data, root_view):
super().__init__(application)
# TODO: weird we have to do this...
self.loaders = application.make("view").loaders
# TODO: check if more clean like this ?
# Maybe also use it to render as JSON for inertia initiated ?
# def __init__(self, component, props, root_view="app", version=None):
# self.component = component
# self.props = props
# self.root_view = root_view
# self.version = version

# inertia specifics
self.component = page_data["component"]
self.props = page_data["props"]
self.root_view = root_view
self.version = page_data["version"]
self.page_data = page_data
# TODO: do this in Provider !
# looks like it's this one which is working
self.share({"inertia": inertia_helper})

def render(self):
Expand Down
5 changes: 3 additions & 2 deletions src/masonite/inertia/middleware/InertiaMiddleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def before(self, request, response):
if not inertia.get_version():
inertia.version(self.version(request))
inertia.share(self.share(request))

inertia.set_root_view(self.set_root_view(request))

def after(self, request, response):
Expand Down Expand Up @@ -79,11 +80,11 @@ def resolve_validation_errors(self, request):
if not session.has("errors"):
return {}
else:
return session.get_flashed_messages().get("errors", {})
return session.get("errors")

def share(self, request):
"""Defines the props that are shared by default. Can be overriden."""
errors = self.resolve_validation_errors(request)
errors = self.resolve_validation_errors
return {"errors": errors}

def version(self, request):
Expand Down
1 change: 1 addition & 0 deletions src/masonite/inertia/providers/InertiaProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ def configure(self):
self.application.make("tests.response").add(InertiaTestingResponse)

def boot(self):
# looks like here it's not doing anything.
self.application.make("view").share({"inertia": inertia})
21 changes: 14 additions & 7 deletions tests/integrations/controllers/TestController.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from masonite.controllers import Controller
from masonite.request import Request
from src.masonite.inertia import Inertia
from src.masonite.inertia import lazy
from masonite.sessions import Session
from src.masonite.inertia import Inertia, lazy


class TestController(Controller):
Expand All @@ -17,11 +17,13 @@ def test(self, request: Request):
def lazy_prop():
return "6"

return request.app.make("inertia").render("Index", {"user": "Sam", "lazy": lazy_prop})
return request.app.make("inertia").render(
"Index", {"user": "Sam", "lazy": lazy_prop}
)

def inertia_with_error(self, request: Request):
request.session.flash("error", "form error")
return request.redirect("/")
def inertia_with_error(self, session: Session, inertia: Inertia):
session.flash("errors", "An error occured.")
return inertia.render("HelloWorld")

def helloworld(self, inertia: Inertia):
return inertia.render("HelloWorld", {"first_name": "Sam"}, "spa_view_2")
Expand Down Expand Up @@ -55,5 +57,10 @@ def is_authenticated(request):

def nested_props(self, view: Inertia):
return view.render(
"Index", {"count": 3, "array": [1, 2, 3], "nested": {"a": 1, "b": {"end": "finally"}}}
"Index",
{
"count": 3,
"array": [1, 2, 3],
"nested": {"a": 1, "b": {"end": "finally"}},
},
)
13 changes: 9 additions & 4 deletions tests/unit/test_inertia_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def setUp(self):
Route.get("/custom-root", "TestController@custom_root"),
Route.get("/callables", "TestController@lazy_view").name("testing.lazy_view"),
Route.get("/lazy", "TestController@with_lazy_props").name("testing.with_lazy_props"),
Route.get("/errors", "TestController@inertia_with_error").name("testing.errors"),
)

# set predictable version for unit testing
Expand Down Expand Up @@ -152,17 +153,17 @@ def test_share(self):

def test_shared_data_can_be_flushed(self):
self.application.make("inertia").share({"test": "key"})
assert self.application.make("inertia").get_shared() == {
assert self.application.make("inertia").get_shared_props() == {
"test": "key",
"errors": {},
}
self.application.make("inertia").flush_shared()
assert self.application.make("inertia").get_shared() == {}
assert self.application.make("inertia").get_shared_props() == {}

def test_get_shared(self):
self.application.make("inertia").share({"nested": {"key": "4"}})
assert self.application.make("inertia").get_shared("nested.key") == "4"
assert self.application.make("inertia").get_shared("unknown", "nope") == "nope"
assert self.application.make("inertia").get_shared_props("nested.key") == "4"
assert self.application.make("inertia").get_shared_props("unknown", "nope") == "nope"

def test_data_can_be_shared_at_anytime(self):
self.application.make("inertia").share({"test": "key2"})
Expand All @@ -175,3 +176,7 @@ def test_that_callables_props_are_resolved(self):

def test_customizing_root_view_in_controller(self):
self.get("/custom-root").assertViewIs("spa_view")

def test_that_errors_flashed_in_session_are_shared(self):
res = self.get("/errors")
res.assertInertiaHasProp("errors", "An error occured.")

0 comments on commit 5f6671d

Please sign in to comment.