Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions demo/app/Sharp/Posts/PostForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ public function update($id, array $data)
->ignore(auth()->user()->isAdmin() ? [] : ['author_id'])
->save($post, $data);

if ($this->isPreview) {
return view('pages.post', [
'post' => $post,
]);
}

if (sharp()->context()->isCreation()) {
$this->notify('Your post was created, but not published yet.');
}
Expand Down
90 changes: 54 additions & 36 deletions resources/js/Pages/Form/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { Button } from '@/components/ui/button';
import { useResizeObserver } from "@vueuse/core";
import { slugify } from "@/utils";
import { api } from "@/api/api";

const props = defineProps<{
form: FormData,
Expand Down Expand Up @@ -71,6 +72,18 @@
});
}
}, { immediate: true });

const previewHtml = ref('');
let timeout: number;
watch(() => form.data, () => {
clearTimeout(timeout);
timeout = window.setTimeout(() => {
api.post(route('code16.sharp.api.form.preview', { entityKey, instanceId }), form.data)
.then(response => {
previewHtml.value = response.data.data.html;
});
}, timeout ? 200 : 0);
}, { immediate: true, deep: true });
</script>

<template>
Expand All @@ -85,44 +98,49 @@

<div class="@container">
<div :class="form.pageAlert ? 'mt-4' : 'mt-6 @3xl:mt-10'" ref="el">
<SharpForm
:form="form"
:show-error-alert="showErrorAlert"
:error-alert-message="errorAlertMessage"
v-model:tab="selectedTabSlug"
@submit="submit"
>
<template #title>
{{ form.title }}
</template>
<template #footer>
<div class="flex gap-4">
<Button variant="outline" as-child>
<Link :href="props.cancelUrl">
<template v-if="form.canEdit">
{{ __('sharp::action_bar.form.cancel_button') }}
</template>
<template v-else>
{{ __('sharp::action_bar.form.back_button') }}
</template>
</Link>
</Button>
<template v-if="form.canEdit">
<Button style="min-width: 6.5em" :disabled="form.isUploading || loading" @click="submit">
<template v-if="form.isUploading">
{{ __('sharp::action_bar.form.submit_button.pending.upload') }}
</template>
<template v-else-if="instanceId || form.config.isSingle">
{{ __('sharp::action_bar.form.submit_button.update') }}
</template>
<template v-else>
{{ __('sharp::action_bar.form.submit_button.create') }}
</template>
<div class="grid" :class="previewHtml ? 'grid-cols-2' : 'grid-cols-1'">
<SharpForm
:form="form"
:show-error-alert="showErrorAlert"
:error-alert-message="errorAlertMessage"
v-model:tab="selectedTabSlug"
@submit="submit"
>
<template #title>
{{ form.title }}
</template>
<template #footer>
<div class="flex gap-4">
<Button variant="outline" as-child>
<Link :href="props.cancelUrl">
<template v-if="form.canEdit">
{{ __('sharp::action_bar.form.cancel_button') }}
</template>
<template v-else>
{{ __('sharp::action_bar.form.back_button') }}
</template>
</Link>
</Button>
</template>
</div>
<template v-if="form.canEdit">
<Button style="min-width: 6.5em" :disabled="form.isUploading || loading" @click="submit">
<template v-if="form.isUploading">
{{ __('sharp::action_bar.form.submit_button.pending.upload') }}
</template>
<template v-else-if="instanceId || form.config.isSingle">
{{ __('sharp::action_bar.form.submit_button.update') }}
</template>
<template v-else>
{{ __('sharp::action_bar.form.submit_button.create') }}
</template>
</Button>
</template>
</div>
</template>
</SharpForm>
<template v-if="previewHtml">
<iframe class="border-0 size-full" :srcdoc="previewHtml"></iframe>
</template>
</SharpForm>
</div>
</div>
</div>
</Layout>
Expand Down
6 changes: 5 additions & 1 deletion src/Form/Eloquent/EloquentModelUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ class EloquentModelUpdater
protected array $relationships = [];
protected ?Closure $fillAfterUpdateUsing = null;

public function update(Model $instance, array $data): Model
public function update(Model $instance, array $data, bool $isPreview = false): Model
{
$this->fillInstance($instance, $data);

if ($isPreview) {
return $instance;
}

// End of "normal" attributes.
$instance->save();

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Eloquent/WithSharpFormEloquentUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function save(Model $instance, array $data): Model
->fillAfterUpdateUsing(
fn ($instanceId) => $this->formatDataAfterUpdate($data, $instanceId)
)
->update($instance, $data);
->update($instance, $data, $this->isPreview);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Form/SharpForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class SharpForm
protected bool $displayShowPageAfterCreation = false;
protected ?string $editFormTitle = null;
protected ?string $createFormTitle = null;
protected bool $isPreview = false;

final public function formLayout(): array
{
Expand Down Expand Up @@ -77,6 +78,15 @@ final public function store(array $data): mixed
return $this->update(null, $data);
}

final public function updateForPreview(mixed $id, array $data): mixed
{
$this->isPreview = true;

return tap($this->update($id, $data), function () {
$this->isPreview = false;
});
}

public function buildFormConfig(): void {}

protected function configureDisplayShowPageAfterCreation(bool $displayShowPage = true): self
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Controllers/Api/ApiFormPreviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Code16\Sharp\Http\Controllers\Api;

use Code16\Sharp\Utils\Entities\ValueObjects\EntityKey;
use Illuminate\Contracts\View\View;

class ApiFormPreviewController extends ApiController
{
public function store(EntityKey $entityKey, ?string $instanceId = null)
{
$entity = $this->entityManager->entityFor($entityKey);

$form = $entity->getFormOrFail($entityKey->multiformKey());

$form->buildFormConfig();

$formattedData = $form->formatAndValidateRequestData(request()->all(), $instanceId);
$view = $form->updateForPreview($instanceId, $formattedData);

if (! $view instanceof View) {
throw new \Exception('A view must be returned by the update() method if $this->isPreview is true.');
}

return response()->json([
'data' => [
'html' => $view->render(),
],
]);
}
}
4 changes: 4 additions & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Code16\Sharp\Http\Controllers\Api\ApiFilterAutocompleteController;
use Code16\Sharp\Http\Controllers\Api\ApiFormAutocompleteController;
use Code16\Sharp\Http\Controllers\Api\ApiFormEditorUploadFormController;
use Code16\Sharp\Http\Controllers\Api\ApiFormPreviewController;
use Code16\Sharp\Http\Controllers\Api\ApiFormUploadController;
use Code16\Sharp\Http\Controllers\Api\ApiFormUploadThumbnailController;
use Code16\Sharp\Http\Controllers\Api\ApiSearchController;
Expand Down Expand Up @@ -96,6 +97,9 @@
Route::post('/form/editors/upload/form/{entityKey}/{instanceId?}', [ApiFormEditorUploadFormController::class, 'update'])
->name('code16.sharp.api.form.editor.upload.form.update');

Route::post('/form/preview/{entityKey}/{instanceId?}', [ApiFormPreviewController::class, 'store'])
->name('code16.sharp.api.form.preview');

Route::post('/upload/thumbnail/{entityKey}/{instanceId?}', [ApiFormUploadThumbnailController::class, 'show'])
->name('code16.sharp.api.form.upload.thumbnail.show');

Expand Down
Loading