Pode.Web icon indicating copy to clipboard operation
Pode.Web copied to clipboard

Build pode web pages dynamically

Open AndreasNick opened this issue 3 years ago • 3 comments

Hi @Badgerati I once experimented a bit to build web pages only after a "click". This has many advantages especially for troubleshooting, because you can change the page source text without having to restart Pode.

    Add-PodeWebPage -Name 'Test' -ScriptBlock {
        Write-PodeHost "Build Site"
        .   $PSScriptRoot\mypage\\ndex.ps1
    }
#....
    New-PodeWebButton -Name Settings -DisplayName "Settings"   -Icon check-decagram -ScriptBlock {
        Write-PodeHost "Settings"
    } #-Force

But now there is the problem that the routes already exist when, for example, a button is newly created with script. I solved this experimentally with a "Force" switch in the New-PodeWebButton CmdLet and then deleted the route there.

    <# Change Button 20221025 #>
    if($Force -and (Test-PodeWebRoute -Path $routePath)) {
        #Write-PodeHost "Remove Route $routePath" -ForegroundColor Yellow
        Remove-PodeWebRoute -Path $routePath -Method Post -EndpointName $EndpointName
    }

Would that be enough or is there other data to delete? Would you want to have pull requests for this function (delete route with a force switch) and include that in the base source code?

AndreasNick avatar Oct 25 '22 07:10 AndreasNick

Hi @AndreasNick,

The Route should be the only object that needs cleaning up.

I'm actually wondering if putting a -Force on Pode's Add-PodeRoute might be a cleaner approach - to prevent Pode.Web having loads and loads if (force) { remove-route } 😂.

Then we can add a -Force to required elements/layouts in Pode.Web, and pass this through to Test-PodeWebRoute and Add-PodeRoute 😄

Badgerati avatar Oct 27 '22 18:10 Badgerati

Hi @Badgerati, I can put that in and try my hand at a pull request sometime. Furthermore I have a test Remove-PodeWebPage function in the code. This is perhaps also interesting for others? I would integrate some similar functions in my fork, because I want to know if you can use an add-on system with Pode.web. So install and delete web components. A "Remove-PodeRoute" is still missing here and it would also have to search for other routes to delete for the web pages:

function Remove-PodeWebPage{
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )

    $routePath = (Get-PodeWebPagePath -Name $Name -NoAppPath)
    $pages = Get-PodeWebState -Name 'pages'
    #$pages[$routePath] = $pageMeta
    $pages[$routePath] = $Null
    
}

I'll do some more testing here :-)

AndreasNick avatar Oct 28 '22 12:10 AndreasNick

Hi @AndreasNick,

A Remove-PodeWebPage is something I can see being useful! The function would need to take the -Name as well as -Group, and could then follow a flow something like:

  • An initial test for the page via Test-PodeWebPage (like used at the of Add-PodeWebPage)
  • Generate the $routePath and get the page from Get-PodeWebState
  • Call Remove-PodeRoute on the $routePath
  • Call Remove-PodeRoute on $($routePath)/help
  • Remove the $routePath key from the pages list returned from Get-PodeWebState

^ that should cover removing everything for a page :)

There's also the possibility of adding a Remove-PodeWebPageLink as well. It looks like it would be identical to Remove-PodeWebPage though, so could just make it call that under-the-hood, haha

Badgerati avatar Nov 06 '22 11:11 Badgerati