Skip to content

Commit

Permalink
Create base page creation form backend
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 21, 2023
1 parent ba16231 commit e617125
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@

use Hyde\Hyde;
use OutOfBoundsException;
use Hyde\Pages\BladePage;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Pages\DocumentationPage;
use Illuminate\Support\HtmlString;
use Hyde\Foundation\Facades\Routes;
use Illuminate\Support\Facades\Process;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Framework\Actions\AnonymousViewCompiler;
use Desilva\Microserve\Request;
use Composer\InstalledVersions;
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use Symfony\Component\HttpKernel\Exception\HttpException;

use function abort;
Expand Down Expand Up @@ -62,6 +68,7 @@ protected function handlePostRequest(): void
{
$actions = array_combine($actions = [
'openInEditor',
'createPage',
], $actions);

$action = $this->request->data['action'] ?? abort(400, 'Must provide action');
Expand All @@ -72,6 +79,10 @@ protected function handlePostRequest(): void
$page = Routes::getOrFail($routeKey)->getPage();
$this->openInEditor($page);
}

if ($action === 'createPage') {
$this->createPage();
}
}

public function show(): string
Expand Down Expand Up @@ -174,6 +185,38 @@ protected function openInEditor(HydePage $page): void
}
}

protected function createPage(): void
{
if ($this->enableEditor()) {
// Required data
$title = $this->request->data['titleInput'] ?? abort(400, 'Must provide title');
$content = $this->request->data['contentInput'] ?? abort(400, 'Must provide content');
$pageType = $this->request->data['pageTypeSelection'] ?? abort(400, 'Must provide page type');

// Optional data
$postDescription = $this->request->data['postDescription'] ?? null;
$postCategory = $this->request->data['postCategory'] ?? null;
$postAuthor = $this->request->data['postAuthor'] ?? null;
$postDate = $this->request->data['postDate'] ?? null;

// Match page class
$pageClass = match ($pageType) {
'blade-page' => BladePage::class,
'markdown-page' => MarkdownPage::class,
'markdown-post' => MarkdownPost::class,
'documentation-page' => DocumentationPage::class,
default => throw new HttpException(400, "Invalid page type '$pageType'"),
};

if ($pageClass === MarkdownPost::class) {
$creator = new CreatesNewMarkdownPostFile($title, $postDescription, $postCategory, $postAuthor, $postDate, $content);
} else {
$creator = new CreatesNewPageSourceFile($title, $pageClass, false, $content);
}
$creator->save();
}
}

protected static function injectDashboardButton(string $contents): string
{
return str_replace('</body>', sprintf('%s</body>', self::button()), $contents);
Expand Down

0 comments on commit e617125

Please sign in to comment.