How to redirect in React Router without changing the url - redirect

How can I implement maybeGoToBar such that - depending on the name - I show the Foo component or the Bar component without changing the url?
function maybeGoToBar(nextState, replace, callback) {
const name = nextState.params.name;
if (name === "Bob") {
// do something to route to /bar
// but keep showing /bob/foo as the current location
// in the browser address bar
}
callback();
}
<Route path=":name/foo" onEnter={maybeGoToBar} component={Foo} >
<Route path="bar" component={Bar} >

You need to use a memory history.
Link

Related

Preact-Router - Handle routes from sub directory

I'm currently creating a SPA that can be included and run from any route.
Currently any <Link /> component I create redirects the client back to the root of the domain it's on plus the intended path.
In react-router there's a property to set a starting base path; basename.
This doesn't seem to be present in preact-router and I'd really rather not switch to react-router as it's significantly larger and I won't be using many of the additional features.
A simple example of the routes:
<Router>
<Route
path="/"
component={Home}
/>
<Route
path="/:slug"
component={Merchant}
/>
</Router>
I've seen a couple of post around the internet implying this is possible but with such little documentation it's a little tricky to piece together.
Any help is much appreciated.
Thanks.
I've ended up wrapping the preact-router Link and Router with my own components. From there I can prefix the path property value with my apps base route, e.g:
<MyRoute
path="/"
component={Home}
/>
Then somewhere within <MyRoute />:
const route = 'my/app/base/path';
let result = (route || '') + this.props.path;
result = result.replace(/([^:]\/)\/+/g, '$1');
Then render the preact-router default component with the result value, <Route path={result} />
If you're looking for hash routing, here it is: https://github.com/developit/preact-router#custom-history.
jhdevuk's Answer pointed me to the right direction.
The following Router class will do the trick (this is in TypeScript):
class SubfolderRouter extends preactRouter.Router {
render(props: preactRouter.RouterProps, state: any) {
if (state.url.indexOf(MY_FOLDER) == 0) {
state = {
...state,
url: state.url.substr(MY_FOLDER.length),
};
}
return super.render(props, state);
}
}
If your app lives in the folder const MY_FOLDER = "/myfolder", then this Router will ignore the folder of the URL. If the user navigates to
/myfolder/home/index
then the Router will look up the URL /home/index, because this is the actual route.

In AEM6, How do I hide a specific component field based on pages for certain country only?

In AEM6, How do I hide a specific component field based on pages for certain country only ?
You can write custom dialog/widget plugin to do that. This is how you attach plugin to your widget:
<title jcr:primaryType="cq:Widget"
fieldLabel="Field to hide"
plugins="hideFieldPlugin"
name="./fieldToHide"
xtype="textfield" />
Next, we need to write plugin and register it:
(function ($) {
var plugin = CQ.Ext.extend(CQ.Ext.emptyFn, {
init: function (fieldToHide) {
var url = CQ.HTTP.getPath();
if (this.shouldBeHidden(url)) {
fieldToHide.hide().disable();
}
},
shouldBeHidden: function (url) {
// some logic
return true;
}
});
CQ.Ext.ComponentMgr.registerPlugin("hideFieldPlugin", plugin);
}($CQ));
JavaScript file needs to be included in Classic UI edit mode. Best way to do that is to use your own custom clientlib or use already existing category, cq.wcm.edit.
If you have more complex logic which goes across multiple widgets, you can attach plugin on dialog level and navigate to the widget objects using dialog.find method.

Redirect to another control DNN

In my Page_Load event of my custom DNN module I retrieve the settings that I have stored using the following.
if (((string)Settings["username"] != null) && ((string)Settings["username"] != ""))
username = "";
{
username = (string)Settings["username"];
if (((string)Settings["password"] != null) && ((string)Settings["password"] != ""))
{
password = (string)Settings["password"];
}
if (((string)Settings["baseServiceUrl"] != null) && ((string)Settings["baseServiceUrl"] != ""))
{
baseServiceUrl = (string)Settings["baseServiceUrl"];
}
baseServiceUrl = "";
Now my question is how do I redirect it to my module settings(called settings.ascx) control if username, password or baseServiceurl is null.
I'm sure it's not as simple as Response.Redirect('settings.ascx');
my aim is to replace
username = "";
with a snippet similar to Response.Redirect('settings.ascx');
Please help
If you want to load a different ASCX file that is registered in DNN (registered in the Module Definition, via the MANIFEST file) you do so by calling either the EditUrl method, or the NavigateURL Method in DNN.
EditUrl("Settings") where Settings is the ControlKey defined in the Module definition.
Edit URL is available off of PortalModuleBase, assuming your controls inherit from PMB.
Thanks again Chris, your answer is correct but I decided to get the settings of the module via the modal pop up. This is what I did to get the answer to get the javascript popup script and url i right clicked on the gear icon using google chrome an inspected the element.
I then copied the contents the anchor tag href attribute, this looked a bit like
href="javascript:dnnModal.show('http://localhost/TestPage/ctl/Module/ModuleId/417?ReturnURL=/TestPage&popUp=true',/*showReturn*/false,550,950,true,'')">
In my default.aspx page I created an anchor tag without the href. I made it a server control by putting runat=server and adding an ID to it and made the visibility false (in my logic i make it visible if it does not meet my criteria)
<a runat="server" class="btn btn-success" id="settingsLink" visible="false" > <img src="/images/action_settings.gif"><span>Settings</span></a>
Next I create a method to dynamically build my link
private string settingsUrlBuilder()
{
var s = new StringBuilder();
var urlPartArray = TabController.CurrentPage.FullUrl.ToString().Split('/');
var partUrl = urlPartArray[3].ToString();
s.Append("javascript:dnnModal.show('");
s.Append(TabController.CurrentPage.FullUrl.ToString().ToLower());
s.Append("/ctl/Module/ModuleId/" + ModuleId.ToString());
s.Append("?ReturnURL=/");
s.Append(partUrl);
s.Append("&popUp=true");
s.Append("',/*showReturn*/false,550,950,true,'')");
return s.ToString();
}
This is where i use the function in the page load
settingsLink.HRef = settingsUrlBuilder(); settingsLink.Visible = true;

Non functioning default policy in SailsJS

I'm trying to implement the basic Passport integration in SailsJS. In my policies.js file, I have the default settings that every tutorial mentions.
'*': ['passport', 'sessionAuth'],
'auth': {
'*': ['passport']
}
My issue is that going to the main page localhost:1337/ doesn't seem to get passed through either policy. If I just set false there, everything still works. If I set false on the auth object for '*' though, I will get Forbidden on any /auth/* route. So, the policies seem to work, I just don't understand why the default catch-all doesn't. Thanks.
Do you use a controller or do you directly serve a view like in the sample homepage?
If you are serving the view directly with something similar to this:
// in config/routes.js
module.exports.routes = {
'/': {
view: 'homepage'
}
}
then you will have to modify it and use a controller in order to te able to use policies.
Create a route to a controller instead of a view:
// in config/routes.js
module.exports.routes = {
// Delete the previous definition and declare a route
// to a controller "index"
'get /': 'indexController.home'
}
Create the controller:
// in api/controllers/IndexController.js
module.exports = {
home: function (req, res) {
// Render the view located in "views/homepage.ejs"
res.view('homepage');
}
};
Then you will be able to manage the policies to apply to the controller index in the file config/policies.js.

How to create a simple landing page in MVC2

I'm trying to create a http://domain.com/NotAuthorized page.
went to Views\Shared and added a View called NotAuthorized witch originates the file name NotAuthorized.aspx
in my Routes I wrote
routes.MapRoute(
"NotAuthorized", // Route name
"NotAuthorized.aspx" // Route Url
);
but every time I access http://domain.com/NotAuthorized I get an error
The resource cannot be found.
What am I missing?
How can access this without using View("NotAuthorized") in the Controller, in other words, not passing through any controller.
You can't access views directly without passing through a controller. All pages in the Views folder cannot be served directly. So one way to accomplish what you are looking for is to write a custom[Authorize] attribute and set the error page:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
}
}
I still have no idea on how to accomplish it, but what I did was use the Home Controller and create an Action called NotAuthorized
public ActionResult NotAuthorized()
{
return View();
}
And add a route like
routes.MapRoute(
"NotAuthorized", // Route name
"NotAuthorized", // URL with parameters
new { controller = "Home", action = "NotAuthorized" } // Parameter defaults
);
And works fine now, I can easily redirect in any part of my Business Logic to /Notauthorized and that will route fine.