Skip to content

Commit

Permalink
Merge pull request AlchemyCMS#2262 from tvdeyen/fix-sitemap
Browse files Browse the repository at this point in the history
Fix new Sitemap
  • Loading branch information
v-ruiz committed Mar 21, 2022
1 parent 13d0c39 commit 17ec259
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 25 deletions.
19 changes: 13 additions & 6 deletions package/src/node_tree.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Sortable from "sortablejs"
import ajax from "./utils/ajax"
import { patch } from "./utils/ajax"
import { on } from "./utils/events"

function displayNodeFolders() {
document.querySelectorAll("li.menu-item").forEach((el) => {
const leftIconArea = el.querySelector(".nodes_tree-left_images")
const list = el.querySelector(".children")
const node = { folded: el.dataset.folded === "true", id: el.dataset.id }
const node = {
folded: el.dataset.folded === "true",
id: el.dataset.id,
type: el.dataset.type
}

if (list.children.length > 0 || node.folded) {
leftIconArea.innerHTML = HandlebarsTemplates.node_folder({ node: node })
Expand All @@ -17,13 +21,15 @@ function displayNodeFolders() {
}

function onFinishDragging(evt) {
const url = Alchemy.routes.move_api_node_path(evt.item.dataset.id)
const url = Alchemy.routes[evt.item.dataset.type].move_api_path(
evt.item.dataset.id
)
const data = {
target_parent_id: evt.to.dataset.nodeId,
new_position: evt.newIndex
}

ajax("PATCH", url, data)
patch(url, data)
.then(() => {
const message = Alchemy.t("Successfully moved menu item")
Alchemy.growl(message)
Expand All @@ -38,10 +44,11 @@ function handleNodeFolders() {
on("click", ".nodes_tree", ".node_folder", function () {
const nodeId = this.dataset.nodeId
const menu_item = this.closest("li.menu-item")
const url = Alchemy.routes.toggle_folded_api_node_path(nodeId)
const url =
Alchemy.routes[this.dataset.recordType].toggle_folded_api_path(nodeId)
const list = menu_item.querySelector(".children")

ajax("PATCH", url)
patch(url)
.then(() => {
list.classList.toggle("folded")
menu_item.dataset.folded =
Expand Down
14 changes: 12 additions & 2 deletions package/src/sitemap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// The admin sitemap Alchemy class
import PageSorter from "./page_sorter"
import { on } from "./utils/events"
import { patch } from "./utils/ajax"
import { createSortables, displayPageFolders } from "./page_sorter"

export default class Sitemap {
// Storing some objects.
Expand Down Expand Up @@ -48,8 +52,14 @@ export default class Sitemap {
})
}

fetch(url) {
return fetch(url).catch((error) => console.warn(`Request failed: ${error}`))
patch(Alchemy.routes.fold_admin_page_path(pageId))
.then(async (response) => {
this.reRender(pageId, await response.json())
spinner.stop()
})
.catch(this.errorHandler)
}.bind(this)
)
}

// Renders the sitemap
Expand Down
86 changes: 70 additions & 16 deletions package/src/utils/__tests__/ajax.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import xhrMock from "xhr-mock"
import ajax from "../ajax"
import { get, patch, post } from "../ajax"

const token = "s3cr3t"

Expand All @@ -8,13 +8,13 @@ beforeEach(() => {
xhrMock.setup()
})

describe("ajax('get')", () => {
describe("get", () => {
it("sends X-CSRF-TOKEN header", async () => {
xhrMock.get("/users", (req, res) => {
expect(req.header("X-CSRF-TOKEN")).toEqual(token)
return res.status(200).body('{"message":"Ok"}')
})
await ajax("get", "/users")
await get("/users")
})

it("sends Content-Type header", async () => {
Expand All @@ -24,22 +24,22 @@ describe("ajax('get')", () => {
)
return res.status(200).body('{"message":"Ok"}')
})
await ajax("get", "/users")
await get("/users")
})

it("sends Accept header", async () => {
xhrMock.get("/users", (req, res) => {
expect(req.header("Accept")).toEqual("application/json")
return res.status(200).body('{"message":"Ok"}')
})
await ajax("get", "/users")
await get("/users")
})

it("returns JSON", async () => {
xhrMock.get("/users", (_req, res) => {
return res.status(200).body('{"email":"mail@example.com"}')
})
await ajax("get", "/users").then((res) => {
await get("/users").then((res) => {
expect(res.data).toEqual({ email: "mail@example.com" })
})
})
Expand All @@ -49,7 +49,7 @@ describe("ajax('get')", () => {
return res.status(200).body('email => "mail@example.com"')
})
expect.assertions(1)
await ajax("get", "/users").catch((e) => {
await get("/users").catch((e) => {
expect(e.message).toMatch("Unexpected token")
})
})
Expand All @@ -59,7 +59,7 @@ describe("ajax('get')", () => {
return Promise.reject(new Error())
})
expect.assertions(1)
await ajax("get", "/users").catch((e) => {
await get("/users").catch((e) => {
expect(e.message).toEqual("An error occurred during the transaction")
})
})
Expand All @@ -69,7 +69,7 @@ describe("ajax('get')", () => {
return res.status(401).body('{"error":"Unauthorized"}')
})
expect.assertions(1)
await ajax("get", "/users").catch((e) => {
await get("/users").catch((e) => {
expect(e.error).toEqual("Unauthorized")
})
})
Expand All @@ -79,19 +79,27 @@ describe("ajax('get')", () => {
return res.status(401).body("Unauthorized")
})
expect.assertions(1)
await ajax("get", "/users").catch((e) => {
await get("/users").catch((e) => {
expect(e.message).toMatch("Unexpected token")
})
})

it("params get attached as query string", async () => {
xhrMock.get("http://localhost/users?name=foo", (_req, res) => {
return res.status(200).body(`{"name":"foo"}`)
})
const { data } = await get("/users", { name: "foo" })
expect(data.name).toEqual("foo")
})
})

describe("ajax('post')", () => {
describe("patch", () => {
it("sends X-CSRF-TOKEN header", async () => {
xhrMock.post("/users", (req, res) => {
expect(req.header("X-CSRF-TOKEN")).toEqual(token)
return res.status(200).body('{"message":"Ok"}')
})
await ajax("post", "/users")
await patch("/users")
})

it("sends Content-Type header", async () => {
Expand All @@ -101,23 +109,69 @@ describe("ajax('post')", () => {
)
return res.status(200).body('{"message":"Ok"}')
})
await ajax("post", "/users")
await patch("/users")
})

it("sends Accept header", async () => {
xhrMock.post("/users", (req, res) => {
expect(req.header("Accept")).toEqual("application/json")
return res.status(200).body('{"message":"Ok"}')
})
await ajax("post", "/users")
await patch("/users")
})

it("sends method override data", async () => {
xhrMock.post("http://localhost/users", (req, res) => {
expect(req.body()).toEqual('{"_method":"patch"}')
return res.status(200).body('{"message":"Ok"}')
})
await patch("/users")
})

it("sends JSON data", async () => {
xhrMock.post("/users", (req, res) => {
xhrMock.post("http://localhost/users", (req, res) => {
expect(req.body()).toEqual(
'{"email":"mail@example.com","_method":"patch"}'
)
return res.status(200).body('{"message":"Ok"}')
})
await patch("/users", { email: "mail@example.com" })
})
})

describe("post", () => {
it("sends X-CSRF-TOKEN header", async () => {
xhrMock.post("http://localhost/users", (req, res) => {
expect(req.header("X-CSRF-TOKEN")).toEqual(token)
return res.status(200).body('{"message":"Ok"}')
})
await post("/users")
})

it("sends Content-Type header", async () => {
xhrMock.post("http://localhost/users", (req, res) => {
expect(req.header("Content-Type")).toEqual(
"application/json; charset=utf-8"
)
return res.status(200).body('{"message":"Ok"}')
})
await post("/users")
})

it("sends Accept header", async () => {
xhrMock.post("http://localhost/users", (req, res) => {
expect(req.header("Accept")).toEqual("application/json")
return res.status(200).body('{"message":"Ok"}')
})
await post("/users")
})

it("sends JSON data", async () => {
xhrMock.post("http://localhost/users", (req, res) => {
expect(req.body()).toEqual('{"email":"mail@example.com"}')
return res.status(200).body('{"message":"Ok"}')
})
await ajax("post", "/users", { email: "mail@example.com" })
await post("/users", { email: "mail@example.com" })
})
})

Expand Down
14 changes: 13 additions & 1 deletion package/src/utils/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ function getToken() {
return metaTag.attributes.content.textContent
}

export default function ajax(method, url, data) {
export function get(url, params) {
return ajax("GET", url, params)
}

export function patch(url, data = {}) {
return ajax("POST", url, { ...data, _method: "patch" })
}

export function post(url, data) {
return ajax("POST", url, data)
}

export default function ajax(method, path, data) {
const xhr = new XMLHttpRequest()
const promise = buildPromise(xhr)

Expand Down

0 comments on commit 17ec259

Please sign in to comment.