From 5f6671dea8f387ffef74da16831c17ba22b6ed87 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 6 Nov 2021 11:21:45 +0100 Subject: [PATCH] Next minor release (#83) * 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 --- src/masonite/inertia/core/Inertia.py | 18 ++-------------- src/masonite/inertia/core/InertiaResponse.py | 10 +-------- .../inertia/middleware/InertiaMiddleware.py | 5 +++-- .../inertia/providers/InertiaProvider.py | 1 + .../controllers/TestController.py | 21 ++++++++++++------- tests/unit/test_inertia_response.py | 13 ++++++++---- 6 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/masonite/inertia/core/Inertia.py b/src/masonite/inertia/core/Inertia.py index 76f4ab1..4f67224 100644 --- a/src/masonite/inertia/core/Inertia.py +++ b/src/masonite/inertia/core/Inertia.py @@ -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 @@ -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 @@ -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}) @@ -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) diff --git a/src/masonite/inertia/core/InertiaResponse.py b/src/masonite/inertia/core/InertiaResponse.py index 94678f3..fdd0ff7 100644 --- a/src/masonite/inertia/core/InertiaResponse.py +++ b/src/masonite/inertia/core/InertiaResponse.py @@ -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): diff --git a/src/masonite/inertia/middleware/InertiaMiddleware.py b/src/masonite/inertia/middleware/InertiaMiddleware.py index 7c39c67..5c2eb55 100644 --- a/src/masonite/inertia/middleware/InertiaMiddleware.py +++ b/src/masonite/inertia/middleware/InertiaMiddleware.py @@ -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): @@ -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): diff --git a/src/masonite/inertia/providers/InertiaProvider.py b/src/masonite/inertia/providers/InertiaProvider.py index 73dddb1..a273210 100644 --- a/src/masonite/inertia/providers/InertiaProvider.py +++ b/src/masonite/inertia/providers/InertiaProvider.py @@ -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}) diff --git a/tests/integrations/controllers/TestController.py b/tests/integrations/controllers/TestController.py index 3b166e5..521a731 100644 --- a/tests/integrations/controllers/TestController.py +++ b/tests/integrations/controllers/TestController.py @@ -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): @@ -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") @@ -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"}}, + }, ) diff --git a/tests/unit/test_inertia_response.py b/tests/unit/test_inertia_response.py index 6d4cffb..aa6ee2c 100644 --- a/tests/unit/test_inertia_response.py +++ b/tests/unit/test_inertia_response.py @@ -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 @@ -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"}) @@ -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.")