From aab9c378771fd1d43e83f3efb841eeb5f74c2f73 Mon Sep 17 00:00:00 2001 From: Lionel Date: Thu, 17 Dec 2020 18:09:00 +0100 Subject: [PATCH] fix(frontend): use an action to trigger the preview (#240) * fix(frontend): use an action to trigger the preview * fix: update --- .env | 1 + targets/frontend/src/gql/preview.gql.js | 5 + .../api/{elasticcloud => actions}/preview.js | 20 +- targets/frontend/src/pages/contenus/[id].js | 22 +- .../frontend/src/pages/contenus/edit/[id].js | 22 + targets/hasura/.k8s/hasura.values.yml | 2 + targets/hasura/metadata/actions.graphql | 18 + targets/hasura/metadata/actions.yaml | 17 +- targets/hasura/metadata/tables.yaml | 904 +++++++++--------- 9 files changed, 544 insertions(+), 467 deletions(-) create mode 100644 targets/frontend/src/gql/preview.gql.js rename targets/frontend/src/pages/api/{elasticcloud => actions}/preview.js (75%) diff --git a/.env b/.env index 1cbd3c648..c0377d20d 100644 --- a/.env +++ b/.env @@ -3,6 +3,7 @@ ## ACCOUNT_EMAIL_WEBHOOK_URL=http://host.docker.internal:3000/api/webhooks/account PUBLICATION_WEBHOOK_URL=http://host.docker.internal:3000/api/webhooks/publication +API_URL=http://host.docker.internal:3000/api ## ## frontend secrets diff --git a/targets/frontend/src/gql/preview.gql.js b/targets/frontend/src/gql/preview.gql.js new file mode 100644 index 000000000..99f11f44f --- /dev/null +++ b/targets/frontend/src/gql/preview.gql.js @@ -0,0 +1,5 @@ +export const previewContentAction = ` +mutation preview($data: PreviewDocument!) { + preview_document(data: $data) +} +`; diff --git a/targets/frontend/src/pages/api/elasticcloud/preview.js b/targets/frontend/src/pages/api/actions/preview.js similarity index 75% rename from targets/frontend/src/pages/api/elasticcloud/preview.js rename to targets/frontend/src/pages/api/actions/preview.js index 5cad57a74..e2701bf25 100644 --- a/targets/frontend/src/pages/api/elasticcloud/preview.js +++ b/targets/frontend/src/pages/api/actions/preview.js @@ -32,11 +32,18 @@ const fetchGlossary = memoizee(_fetchGlossary, { }); export default async function (req, res) { + const { data } = req.body.input; + if ( + req.headers["preview-secret"] !== process.env.PUBLICATION_SECRET || !process.env.ELASTICSEARCH_APIKEY_DEV || !process.env.ELASTICSEARCH_URL_DEV ) { - res.status(304).json({ message: "not modified" }); + return res.status(403).json({ + error: "Forbidden", + message: "Missing secret or env", + statusCode: "403", + }); } const glossary = await fetchGlossary(); @@ -48,23 +55,24 @@ export default async function (req, res) { node: `${process.env.ELASTICSEARCH_URL_DEV}`, }); - const { cdtnId, source, document } = req.body; try { await client.update({ body: { - doc: await transform(source, document, glossary), + doc: await transform(data.source, data.document, glossary), }, - id: cdtnId, + id: data.cdtnId, index: `cdtn-preprod_documents`, }); - res.json({ message: "doc updated!" }); + res.json({ message: "doc updated!", statusCode: 200 }); } catch (response) { if (response.body) { console.error(response.body.error); } else { console.error(response); } - res.status(response.statusCode).json({ message: response.body.error }); + res + .status(response.statusCode) + .json({ message: response.body.error, statusCode: response.statusCode }); } } diff --git a/targets/frontend/src/pages/contenus/[id].js b/targets/frontend/src/pages/contenus/[id].js index fbad970f0..a3ab86f16 100644 --- a/targets/frontend/src/pages/contenus/[id].js +++ b/targets/frontend/src/pages/contenus/[id].js @@ -8,9 +8,9 @@ import { Button } from "src/components/button"; import { Layout } from "src/components/layout/auth.layout"; import { Inline } from "src/components/layout/Inline"; import { Stack } from "src/components/layout/Stack"; +import { previewContentAction } from "src/gql/preview.gql"; import { withCustomUrqlClient } from "src/hoc/CustomUrqlClient"; import { withUserProvider } from "src/hoc/UserProvider"; -import { request } from "src/lib/request"; import { Card, jsx, Message, NavLink } from "theme-ui"; import { useMutation, useQuery } from "urql"; @@ -68,6 +68,7 @@ export function DocumentPage() { const [hasChanged, setHasChanged] = useState(false); const [submitIdle, setSubmitIdle] = useState(false); const [, executeUpdate] = useMutation(updateDocumentMutation); + const [, previewContent] = useMutation(previewContentAction); const { handleSubmit } = useForm(); const jsonDoc = useRef(null); @@ -93,13 +94,22 @@ export function DocumentPage() { text, title, } = data.document; - request("/api/elasticcloud/preview", { - body: { - cdtnId, - document: { ...document, metaDescription, text, title }, - source, + + previewContent({ + cdtnId, + document: { + ...document, + metaDescription, + text, + title, }, + source, + }).then((response) => { + if (response.errors) { + console.error("preview impossible", response.errors); + } }); + if (result.error) { console.error(result.error); } diff --git a/targets/frontend/src/pages/contenus/edit/[id].js b/targets/frontend/src/pages/contenus/edit/[id].js index 8facde957..360035f9a 100644 --- a/targets/frontend/src/pages/contenus/edit/[id].js +++ b/targets/frontend/src/pages/contenus/edit/[id].js @@ -9,6 +9,7 @@ import { EditorialContentForm } from "src/components/editorialContent/Form"; import { Layout } from "src/components/layout/auth.layout"; import { Inline } from "src/components/layout/Inline"; import { Stack } from "src/components/layout/Stack"; +import { previewContentAction } from "src/gql/preview.gql"; import { withCustomUrqlClient } from "src/hoc/CustomUrqlClient"; import { withUserProvider } from "src/hoc/UserProvider"; import { jsx, Spinner } from "theme-ui"; @@ -19,6 +20,7 @@ query getEditorialContent($cdtnId: String!) { editorialContent: documents_by_pk(cdtn_id: $cdtnId) { cdtnId: cdtn_id title + source document metaDescription: meta_description } @@ -43,6 +45,9 @@ mutation EditEditorialContent( text: $title }) { cdtnId: cdtn_id + slug + source + metaDescription: meta_description } } `; @@ -70,6 +75,7 @@ export function EditInformationPage() { const [deleteResult, deleteEditorialContent] = useMutation( deleteEditorialContentMutation ); + const [, previewContent] = useMutation(previewContentAction); async function onSubmit({ title, metaDescription, document }) { const result = await editEditorialContent({ @@ -79,6 +85,22 @@ export function EditInformationPage() { slug: slugify(title), title, }); + + previewContent({ + cdtn_id: result.cdtnId, + document: { + ...document, + metaDescription: result.metaDescription, + slug: result.slug, + title, + }, + source: result.source, + }).then((response) => { + if (response.errors) { + console.error("preview impossible", response.errors); + } + }); + if (!result.error) { router.back(); } diff --git a/targets/hasura/.k8s/hasura.values.yml b/targets/hasura/.k8s/hasura.values.yml index 5c972a307..744f48cf3 100644 --- a/targets/hasura/.k8s/hasura.values.yml +++ b/targets/hasura/.k8s/hasura.values.yml @@ -37,6 +37,8 @@ deployment: value: http://www:3000/api/webhooks/account - name: PUBLICATION_WEBHOOK_URL value: http://www:3000/api/webhooks/publication + - name: API_URL + value: http://www:3000/api envFrom: - secretRef: diff --git a/targets/hasura/metadata/actions.graphql b/targets/hasura/metadata/actions.graphql index 139597f9c..1deda82bc 100644 --- a/targets/hasura/metadata/actions.graphql +++ b/targets/hasura/metadata/actions.graphql @@ -1,2 +1,20 @@ +type Mutation { + preview_document ( + data: PreviewDocument! + ): Status +} + + +input PreviewDocument { + cdtn_id : String + document : jsonb + source : String +} + +type Status { + message : String! + statusCode : Int! +} + diff --git a/targets/hasura/metadata/actions.yaml b/targets/hasura/metadata/actions.yaml index 1edb4c2ff..e9cd0bba7 100644 --- a/targets/hasura/metadata/actions.yaml +++ b/targets/hasura/metadata/actions.yaml @@ -1,6 +1,17 @@ -actions: [] +actions: +- name: preview_document + definition: + kind: synchronous + handler: '{{API_URL}}/actions/preview' + headers: + - name: preview-secret + value_from_env: PUBLICATION_SECRET + permissions: + - role: user custom_types: enums: [] - input_objects: [] - objects: [] + input_objects: + - name: PreviewDocument + objects: + - name: Status scalars: [] diff --git a/targets/hasura/metadata/tables.yaml b/targets/hasura/metadata/tables.yaml index d2b39aad2..1eb01f1af 100644 --- a/targets/hasura/metadata/tables.yaml +++ b/targets/hasura/metadata/tables.yaml @@ -2,532 +2,532 @@ schema: auth name: refresh_tokens object_relationships: - - name: user - using: - foreign_key_constraint_on: user_id + - name: user + using: + foreign_key_constraint_on: user_id - table: schema: auth name: user_roles object_relationships: - - name: roleByRole - using: - foreign_key_constraint_on: role - - name: user - using: - foreign_key_constraint_on: user_id + - name: roleByRole + using: + foreign_key_constraint_on: role + - name: user + using: + foreign_key_constraint_on: user_id select_permissions: - - role: user - permission: - columns: - - id - - role - filter: - id: - _eq: X-Hasura-User-Id + - role: user + permission: + columns: + - id + - role + filter: + id: + _eq: X-Hasura-User-Id update_permissions: - - role: user - permission: - columns: - - role - filter: - id: - _eq: X-Hasura-User-Id - check: null + - role: user + permission: + columns: + - role + filter: + id: + _eq: X-Hasura-User-Id + check: null - table: schema: auth name: users object_relationships: - - name: role - using: - foreign_key_constraint_on: default_role + - name: role + using: + foreign_key_constraint_on: default_role array_relationships: - - name: refresh_tokens - using: - foreign_key_constraint_on: - column: user_id - table: - schema: auth - name: refresh_tokens - - name: user_roles - using: - foreign_key_constraint_on: - column: user_id - table: - schema: auth - name: user_roles + - name: refresh_tokens + using: + foreign_key_constraint_on: + column: user_id + table: + schema: auth + name: refresh_tokens + - name: user_roles + using: + foreign_key_constraint_on: + column: user_id + table: + schema: auth + name: user_roles select_permissions: - - role: user - permission: - columns: - - active - - created_at - - default_role - - email - - id - - name - - secret_token - - secret_token_expires_at - - updated_at - filter: - id: - _eq: X-Hasura-User-Id + - role: user + permission: + columns: + - active + - created_at + - default_role + - email + - id + - name + - secret_token + - secret_token_expires_at + - updated_at + filter: + id: + _eq: X-Hasura-User-Id update_permissions: - - role: user - permission: - columns: - - email - - name - filter: - id: - _eq: X-Hasura-User-Id - check: null + - role: user + permission: + columns: + - email + - name + filter: + id: + _eq: X-Hasura-User-Id + check: null event_triggers: - - name: account_email - definition: - enable_manual: false - insert: - columns: "*" - update: - columns: - - secret_token - retry_conf: - num_retries: 1 - interval_sec: 10 - timeout_sec: 60 - webhook_from_env: ACCOUNT_EMAIL_WEBHOOK_URL - headers: - - name: email-secret - value_from_env: ACCOUNT_EMAIL_SECRET + - name: account_email + definition: + enable_manual: false + insert: + columns: '*' + update: + columns: + - secret_token + retry_conf: + num_retries: 1 + interval_sec: 10 + timeout_sec: 60 + webhook_from_env: ACCOUNT_EMAIL_WEBHOOK_URL + headers: + - name: email-secret + value_from_env: ACCOUNT_EMAIL_SECRET - table: schema: public name: alert_notes object_relationships: - - name: user - using: - foreign_key_constraint_on: user_id + - name: user + using: + foreign_key_constraint_on: user_id - table: schema: public name: alert_status array_relationships: - - name: alerts - using: - foreign_key_constraint_on: - column: status - table: - schema: public - name: alerts + - name: alerts + using: + foreign_key_constraint_on: + column: status + table: + schema: public + name: alerts select_permissions: - - role: user - permission: - columns: - - name - filter: {} - allow_aggregations: true + - role: user + permission: + columns: + - name + filter: {} + allow_aggregations: true update_permissions: - - role: user - permission: - columns: - - name - filter: {} - check: {} + - role: user + permission: + columns: + - name + filter: {} + check: {} - table: schema: public name: alerts object_relationships: - - name: alert_status - using: - foreign_key_constraint_on: status - - name: source - using: - foreign_key_constraint_on: repository + - name: alert_status + using: + foreign_key_constraint_on: status + - name: source + using: + foreign_key_constraint_on: repository array_relationships: - - name: alert_notes - using: - foreign_key_constraint_on: - column: alert_id - table: - schema: public - name: alert_notes + - name: alert_notes + using: + foreign_key_constraint_on: + column: alert_id + table: + schema: public + name: alert_notes select_permissions: - - role: user - permission: - columns: - - id - - info - - status - - repository - - ref - - changes - - created_at - - updated_at - filter: {} - allow_aggregations: true + - role: user + permission: + columns: + - id + - info + - status + - repository + - ref + - changes + - created_at + - updated_at + filter: {} + allow_aggregations: true update_permissions: - - role: user - permission: - columns: - - id - - info - - status - - repository - - ref - - changes - - created_at - - updated_at - filter: {} - check: {} + - role: user + permission: + columns: + - id + - info + - status + - repository + - ref + - changes + - created_at + - updated_at + filter: {} + check: {} - table: schema: public name: document_relations object_relationships: - - name: a - using: - foreign_key_constraint_on: document_a - - name: b - using: - foreign_key_constraint_on: document_b + - name: a + using: + foreign_key_constraint_on: document_a + - name: b + using: + foreign_key_constraint_on: document_b insert_permissions: - - role: user - permission: - check: {} - columns: - - id - - document_a - - document_b - - type - - data - backend_only: false + - role: user + permission: + check: {} + columns: + - id + - document_a + - document_b + - type + - data + backend_only: false select_permissions: - - role: public - permission: - columns: - - id - - document_a - - document_b - - type - - data - filter: {} - allow_aggregations: true - - role: user - permission: - columns: - - id - - document_a - - document_b - - type - - data - filter: {} - allow_aggregations: true + - role: public + permission: + columns: + - id + - document_a + - document_b + - type + - data + filter: {} + allow_aggregations: true + - role: user + permission: + columns: + - id + - document_a + - document_b + - type + - data + filter: {} + allow_aggregations: true update_permissions: - - role: user - permission: - columns: - - data - - document_a - - document_b - - type - - id - filter: {} - check: {} + - role: user + permission: + columns: + - data + - document_a + - document_b + - type + - id + filter: {} + check: {} delete_permissions: - - role: user - permission: - filter: {} + - role: user + permission: + filter: {} - table: schema: public name: documents array_relationships: - - name: relation_a - using: - foreign_key_constraint_on: - column: document_a - table: - schema: public - name: document_relations - - name: relation_b - using: - foreign_key_constraint_on: - column: document_b - table: - schema: public - name: document_relations + - name: relation_a + using: + foreign_key_constraint_on: + column: document_a + table: + schema: public + name: document_relations + - name: relation_b + using: + foreign_key_constraint_on: + column: document_b + table: + schema: public + name: document_relations insert_permissions: - - role: user - permission: - check: {} - columns: - - cdtn_id - - created_at - - document - - initial_id - - is_available - - is_published - - is_searchable - - meta_description - - slug - - source - - text - - title - - updated_at - backend_only: false + - role: user + permission: + check: {} + columns: + - cdtn_id + - created_at + - document + - initial_id + - is_available + - is_published + - is_searchable + - meta_description + - slug + - source + - text + - title + - updated_at + backend_only: false select_permissions: - - role: public - permission: - columns: - - cdtn_id - - initial_id - - title - - meta_description - - source - - slug - - text - - document - - is_published - - is_searchable - - created_at - - updated_at - - is_available - filter: {} - allow_aggregations: true - - role: user - permission: - columns: - - cdtn_id - - created_at - - document - - initial_id - - is_available - - is_published - - is_searchable - - meta_description - - slug - - source - - text - - title - - updated_at - filter: {} - allow_aggregations: true + - role: public + permission: + columns: + - cdtn_id + - initial_id + - title + - meta_description + - source + - slug + - text + - document + - is_published + - is_searchable + - created_at + - updated_at + - is_available + filter: {} + allow_aggregations: true + - role: user + permission: + columns: + - cdtn_id + - created_at + - document + - initial_id + - is_available + - is_published + - is_searchable + - meta_description + - slug + - source + - text + - title + - updated_at + filter: {} + allow_aggregations: true update_permissions: - - role: user - permission: - columns: - - cdtn_id - - created_at - - document - - initial_id - - is_available - - is_published - - is_searchable - - meta_description - - slug - - source - - text - - title - - updated_at - filter: {} - check: {} + - role: user + permission: + columns: + - cdtn_id + - created_at + - document + - initial_id + - is_available + - is_published + - is_searchable + - meta_description + - slug + - source + - text + - title + - updated_at + filter: {} + check: {} delete_permissions: - - role: user - permission: - filter: {} + - role: user + permission: + filter: {} event_triggers: - - name: publication - definition: - enable_manual: false - update: - columns: - - is_published - retry_conf: - num_retries: 2 - interval_sec: 10 - timeout_sec: 60 - webhook_from_env: PUBLICATION_WEBHOOK_URL - headers: - - name: publication-secret - value_from_env: PUBLICATION_SECRET -- table: - schema: public - name: package_version - select_permissions: - - role: public - permission: - columns: - - repository - - version - - created_at - - updated_at - filter: {} - - role: user - permission: + - name: publication + definition: + enable_manual: false + update: columns: - - repository - - version - - created_at - - updated_at - filter: {} + - is_published + retry_conf: + num_retries: 2 + interval_sec: 10 + timeout_sec: 60 + webhook_from_env: PUBLICATION_WEBHOOK_URL + headers: + - name: publication-secret + value_from_env: PUBLICATION_SECRET - table: schema: public name: glossary insert_permissions: - - role: user - permission: - check: {} - columns: - - abbreviations - - references - - variants - - definition - - slug - - term - - created_at - - updated_at - - id - backend_only: false + - role: user + permission: + check: {} + columns: + - abbreviations + - references + - variants + - definition + - slug + - term + - created_at + - updated_at + - id + backend_only: false select_permissions: - - role: public - permission: - columns: - - abbreviations - - references - - variants - - definition - - slug - - term - - created_at - - updated_at - - id - filter: {} - - role: user - permission: - columns: - - abbreviations - - references - - variants - - definition - - slug - - term - - created_at - - updated_at - - id - filter: {} + - role: public + permission: + columns: + - abbreviations + - references + - variants + - definition + - slug + - term + - created_at + - updated_at + - id + filter: {} + - role: user + permission: + columns: + - abbreviations + - references + - variants + - definition + - slug + - term + - created_at + - updated_at + - id + filter: {} update_permissions: - - role: user - permission: - columns: - - abbreviations - - references - - variants - - definition - - slug - - term - - created_at - - updated_at - - id - filter: {} - check: null + - role: user + permission: + columns: + - abbreviations + - references + - variants + - definition + - slug + - term + - created_at + - updated_at + - id + filter: {} + check: null delete_permissions: - - role: user - permission: - filter: {} + - role: user + permission: + filter: {} - table: schema: public name: kali_blocks insert_permissions: - - role: user - permission: - check: {} - columns: - - id - - title - - idcc - - blocks - backend_only: false + - role: user + permission: + check: {} + columns: + - id + - title + - idcc + - blocks + backend_only: false select_permissions: - - role: public - permission: - columns: - - id - - title - - idcc - - blocks - filter: {} - - role: user - permission: - columns: - - idcc - - blocks - - id - - title - filter: {} + - role: public + permission: + columns: + - id + - title + - idcc + - blocks + filter: {} + - role: user + permission: + columns: + - idcc + - blocks + - id + - title + filter: {} update_permissions: - - role: user - permission: - columns: - - idcc - - blocks - - id - - title - filter: {} - check: {} + - role: user + permission: + columns: + - idcc + - blocks + - id + - title + filter: {} + check: {} delete_permissions: - - role: user - permission: - filter: {} + - role: user + permission: + filter: {} +- table: + schema: public + name: package_version + select_permissions: + - role: public + permission: + columns: + - repository + - version + - created_at + - updated_at + filter: {} + - role: user + permission: + columns: + - repository + - version + - created_at + - updated_at + filter: {} - table: schema: public name: roles array_relationships: - - name: user_roles - using: - foreign_key_constraint_on: - column: role - table: - schema: auth - name: user_roles - - name: users - using: - foreign_key_constraint_on: - column: default_role - table: - schema: auth - name: users + - name: user_roles + using: + foreign_key_constraint_on: + column: role + table: + schema: auth + name: user_roles + - name: users + using: + foreign_key_constraint_on: + column: default_role + table: + schema: auth + name: users select_permissions: - - role: user - permission: - columns: - - role - filter: {} + - role: user + permission: + columns: + - role + filter: {} - table: schema: public name: sources array_relationships: - - name: alerts - using: - foreign_key_constraint_on: - column: repository - table: - schema: public - name: alerts + - name: alerts + using: + foreign_key_constraint_on: + column: repository + table: + schema: public + name: alerts select_permissions: - - role: user - permission: - columns: - - repository - - label - - tag - - created_at - filter: {} - allow_aggregations: true + - role: user + permission: + columns: + - repository + - label + - tag + - created_at + filter: {} + allow_aggregations: true update_permissions: - - role: user - permission: - columns: - - repository - - label - - tag - - created_at - filter: {} - check: {} + - role: user + permission: + columns: + - repository + - label + - tag + - created_at + filter: {} + check: {} - table: schema: v1 name: fiches_travail_data_alerts