Skip to content

Commit

Permalink
Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronaut committed May 7, 2024
1 parent 7efac2d commit 23626a5
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/assets/builds/pages_core/admin-dist.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions app/assets/builds/pages_core/admin-dist.js.map

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Admin
class PagesController < Admin::AdminController
include PagesCore::Admin::PageJsonHelper
include PagesCore::PageParameters

before_action :find_categories
before_action :find_page, only: %i[show edit update destroy move]
Expand Down Expand Up @@ -87,20 +88,8 @@ def default_author
User.find_by(email: PagesCore.config.default_author)
end

def page_attributes
%i[template user_id status feed_enabled published_at redirect_to
image_link news_page unique_name pinned parent_page_id serialized_tags
meta_image_id starts_at ends_at all_day image_id path_segment
meta_title meta_description open_graph_title open_graph_description]
end

def page_params
params.require(:page).permit(
PagesCore::Templates::TemplateConfiguration.all_blocks +
page_attributes,
page_images_attributes: %i[id position image_id primary _destroy],
page_files_attributes: %i[id position attachment_id _destroy]
)
params.require(:page).permit(page_content_attributes)
end

def param_categories
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/concerns/pages_core/page_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module PagesCore
module PageParameters
extend ActiveSupport::Concern

def page_attachment_attributes
{ page_images_attributes: %i[id position image_id primary _destroy],
page_files_attributes: %i[id position attachment_id _destroy] }
end

def page_content_attributes
locales = PagesCore.config.locales&.keys || [I18n.default_locale]
[page_static_attributes,
PagesCore::Templates::TemplateConfiguration.all_blocks,
:path_segment,
(PagesCore::Templates::TemplateConfiguration
.localized_blocks + %i[path_segment])
.index_with { locales },
page_attachment_attributes]
end

def page_static_attributes
%i[template user_id status feed_enabled published_at redirect_to
news_page unique_name pinned parent_page_id serialized_tags
meta_image_id starts_at ends_at all_day]
end
end
end
40 changes: 20 additions & 20 deletions app/controllers/concerns/pages_core/preview_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
module PagesCore
module PreviewPagesController
extend ActiveSupport::Concern
include PagesCore::PageParameters

included do
before_action :disable_xss_protection, only: %i[preview]
end

def preview?
@preview || false
end

def preview
render_error 403 unless logged_in?

@preview = true
@page = Page.find_by(id: params[:page_id]) || Page.new
@page.readonly!
@page.assign_attributes(preview_page_params)

render_page
end

private

def disable_xss_protection
Expand All @@ -17,31 +33,15 @@ def disable_xss_protection
response.headers["X-XSS-Protection"] = "0"
end

def permitted_page_attributes
%i[template user_id status feed_enabled published_at
redirect_to image_link news_page
unique_name pinned parent_page_id]
end

def page_params
params.require(:page).permit(
Page.localized_attributes + permitted_page_attributes
)
end

def preview_page(page)
redirect_to(page_url(content_locale, page)) && return unless logged_in?

disable_xss_protection

@preview = true
page.attributes = page_params.merge(
def preview_page_params
ActionController::Parameters.new(
JSON.parse(params.require(:preview_page))
).permit(:id, page_content_attributes).merge(
status: 2,
published_at: Time.zone.now,
locale: content_locale,
redirect_to: nil
)
render_page
end
end
end
8 changes: 2 additions & 6 deletions app/controllers/pages_core/frontend/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PagesController < ::FrontendController

before_action :load_root_pages
before_action :find_page_by_path, only: [:show]
before_action :find_page, only: %i[show preview]
before_action :require_page, only: %i[show preview]
before_action :find_page, only: %i[show]
before_action :require_page, only: %i[show]
before_action :canonicalize_url, only: [:show]
static_cache :index, :show

Expand All @@ -27,10 +27,6 @@ def index
end
end

def preview
preview_page(@page)
end

def show
respond_to do |format|
format.html { render_published_page(@page) }
Expand Down
14 changes: 13 additions & 1 deletion app/javascript/components/PageForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useState, useRef } from "react";

import { putJson, postJson } from "../lib/request";
import { openPreview } from "./PageForm/preview";
import useToastStore from "../stores/useToastStore";
import useAttachments from "./Attachments/useAttachments";
import useImageGrid from "./ImageGrid/useImageGrid";
Expand Down Expand Up @@ -81,6 +82,14 @@ export default function PageForm(props: Props) {
const errorToast = useToastStore((state) => state.error);
const noticeToast = useToastStore((state) => state.notice);

const params = () => {
return pageParams(state, {
files: filesState,
images: imagesState,
tags: tagsState
});
};

useEffect(() => {
const pageUrl =
`/admin/${locale}/pages/` +
Expand All @@ -93,7 +102,10 @@ export default function PageForm(props: Props) {

const handlePreview = (evt: React.MouseEvent) => {
evt.preventDefault();
console.log("preview");
openPreview(`/${locale}/pages/preview`, {
page_id: `${page.id}`,
preview_page: JSON.stringify(params())
});
};

const handleSubmit = (evt: React.MouseEvent) => {
Expand Down
23 changes: 23 additions & 0 deletions app/javascript/components/PageForm/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { csrfToken } from "../../lib/request";

function buildForm(url: string, body: Record<string, string>) {
const form = document.createElement("form");
form.action = url;
form.method = "POST";
form.target = "_blank";
for (const [name, value] of Object.entries(body)) {
const input = document.createElement("input");
input.type = "hidden";
input.name = name;
input.value = value;
form.appendChild(input);
}
return form;
}

export function openPreview(url: string, body: Record<string, string>) {
const form = buildForm(url, { authenticity_token: csrfToken(), ...body });
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
collection do
get "search"
post "search"
end
member do
put "preview"
post "preview"
end
resources :files, controller: "page_files"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pages_core/templates/template_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def localized_blocks

(config.get(:default, :blocks).to_a + template_blocks)
.compact
.select { |_, opts| opts[:localized] }.map(&:first)
.select { |_, opts| opts[:localized] }.map(&:first).uniq
end

private
Expand Down

0 comments on commit 23626a5

Please sign in to comment.