-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move and Delete block, wip on Add Component
- Loading branch information
1 parent
7070dec
commit 572a9d9
Showing
7 changed files
with
268 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { BlockContent } from "../entities/BlockContent"; | ||
import { BlockStructure } from "../entities/BlockStructure"; | ||
import { ComponentContent } from "../entities/ComponentContent"; | ||
import { PrimitiveFieldType } from "../entities/ElementType"; | ||
import { Repeater } from "../entities/Repeater"; | ||
import columnStructure from "./columnStructure.json"; | ||
import { createTestingPinia } from "@pinia/testing"; | ||
import { setActivePinia } from "pinia"; | ||
import { useZoneStore } from "../store/zoneStore"; | ||
|
||
describe("Add Component", () => { | ||
const newColumnBlock: BlockContent = { | ||
id: "some_random_id", | ||
block_id: "columnBlock", | ||
fields: [ | ||
{ | ||
id: "some_random_id", | ||
type: "text" as PrimitiveFieldType.TEXT, | ||
label: "Column block title", | ||
content: { | ||
value: "Column title", | ||
}, | ||
}, | ||
{ | ||
id: "some_random_id", | ||
component_id: "banner", | ||
type: "component", | ||
label: "Banner", | ||
fields: [ | ||
{ | ||
id: "some_random_id", | ||
type: "text" as PrimitiveFieldType.TEXT, | ||
label: "Image", | ||
content: { | ||
value: "BannerImage", | ||
}, | ||
}, | ||
{ | ||
id: "some_random_id", | ||
type: "text" as PrimitiveFieldType.TEXT, | ||
label: "Intro", | ||
content: { | ||
value: "Wonderful intro", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: "columnComponentId", | ||
component_id: "column", | ||
type: "repeater", | ||
label: "Columns", | ||
sub_elements: [], | ||
}, | ||
], | ||
}; | ||
|
||
beforeEach(() => { | ||
const pinia = createTestingPinia({ | ||
initialState: { | ||
zone: { | ||
availableBlocks: [columnStructure as BlockStructure], | ||
content: [newColumnBlock], | ||
}, | ||
}, | ||
}); | ||
setActivePinia(pinia); | ||
}); | ||
|
||
it("does not change state if component does not exist", () => { | ||
const zoneStore = useZoneStore(); | ||
expect( | ||
(zoneStore.content[0].fields[2] as Repeater<ComponentContent>) | ||
.sub_elements | ||
).toHaveLength(0); | ||
zoneStore.addComponent(0, "undefinedId"); | ||
expect( | ||
(zoneStore.content[0].fields[2] as Repeater<ComponentContent>) | ||
.sub_elements | ||
).toHaveLength(0); | ||
}); | ||
|
||
it("adds a new component at indicated place", () => { | ||
const zoneStore = useZoneStore(); | ||
zoneStore.addComponent(0, "column"); | ||
expect( | ||
(zoneStore.content[0].fields[2] as Repeater<ComponentContent>) | ||
.sub_elements | ||
).toHaveLength(1); | ||
}); | ||
|
||
it("adds component fields to new component", () => {}); | ||
|
||
it("adds component after existing components", () => {}); | ||
|
||
it("adds sub-components", () => {}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { BlockContent } from "../entities/BlockContent"; | ||
import { PrimitiveFieldType } from "../entities/ElementType"; | ||
import { createTestingPinia } from "@pinia/testing"; | ||
import { setActivePinia } from "pinia"; | ||
import { useZoneStore } from "../store/zoneStore"; | ||
|
||
const newColumnBlock: BlockContent = { | ||
id: "some_random_id", | ||
block_id: "columnBlock", | ||
fields: [ | ||
{ | ||
id: "some_random_id", | ||
type: "text" as PrimitiveFieldType.TEXT, | ||
label: "Column block title", | ||
content: { | ||
value: "Column title", | ||
}, | ||
}, | ||
{ | ||
id: "some_random_id", | ||
component_id: "banner", | ||
type: "component", | ||
label: "Banner", | ||
fields: [ | ||
{ | ||
id: "some_random_id", | ||
type: "text" as PrimitiveFieldType.TEXT, | ||
label: "Image", | ||
content: { | ||
value: "BannerImage", | ||
}, | ||
}, | ||
{ | ||
id: "some_random_id", | ||
type: "text" as PrimitiveFieldType.TEXT, | ||
label: "Intro", | ||
content: { | ||
value: "Wonderful intro", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: "some_random_id", | ||
component_id: "column", | ||
type: "repeater", | ||
label: "Columns", | ||
sub_elements: [], | ||
}, | ||
], | ||
}; | ||
const emptyBlockContent1: BlockContent = { | ||
id: "empty_block_content_1", | ||
block_id: "emptyBlock", | ||
fields: [], | ||
}; | ||
|
||
const emptyBlockContent2: BlockContent = { | ||
id: "empty_block_content_2", | ||
block_id: "emptyBlock", | ||
fields: [], | ||
}; | ||
|
||
describe("Delete Block", () => { | ||
beforeEach(() => { | ||
const pinia = createTestingPinia({ | ||
initialState: { | ||
zone: { | ||
content: [emptyBlockContent1, newColumnBlock, emptyBlockContent2], | ||
}, | ||
}, | ||
stubActions: false, | ||
}); | ||
setActivePinia(pinia); | ||
}); | ||
|
||
it("deletes specified block", () => { | ||
const zoneStore = useZoneStore(); | ||
expect(zoneStore.content).toHaveLength(3); | ||
expect(zoneStore.content[1].block_id).toBe("columnBlock"); | ||
zoneStore.deleteBlock(1); | ||
expect(zoneStore.content).toHaveLength(2); | ||
expect(zoneStore.content[1].block_id).toBe("emptyBlock"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
ComponentFieldStructure, | ||
ComponentStructure, | ||
} from "../entities/ComponentStructure"; | ||
|
||
import { BlockContent } from "../entities/BlockContent"; | ||
import { BlockStructure } from "./../entities/BlockStructure"; | ||
import { ComponentContent } from "../entities/ComponentContent"; | ||
import { Repeater } from "../entities/Repeater"; | ||
import { buildNewSingleComponentFromStructure } from "../utils/builder"; | ||
|
||
export const addComponent = ( | ||
zoneStore, | ||
blockIndex: number, | ||
componentId: string | ||
) => { | ||
console.debug("Coucou"); | ||
const blockStructure: BlockStructure = zoneStore.getBlockStructure( | ||
zoneStore.content[blockIndex].block_id | ||
); | ||
const blockContent: BlockContent = zoneStore.content[blockIndex]; | ||
const componentStructure: ComponentStructure = findBlockStructure( | ||
blockStructure, | ||
componentId | ||
); | ||
console.debug("Structure found : " + componentStructure); | ||
const componentContent: ComponentContent | Repeater<ComponentContent> = | ||
buildNewSingleComponentFromStructure(componentStructure); | ||
const newBlockContent = insertNewComponent( | ||
blockContent, | ||
componentContent, | ||
componentId | ||
); | ||
zoneStore.content[blockIndex] = newBlockContent; | ||
}; | ||
|
||
const findBlockStructure = ( | ||
structure: BlockStructure | ComponentStructure, | ||
componentId: string | ||
): ComponentStructure => { | ||
return Object.values(structure.fields).find( | ||
(field: ComponentFieldStructure) => { | ||
if (field.type !== "component") return false; | ||
return field.id === componentId; | ||
} | ||
) as ComponentStructure; | ||
}; | ||
|
||
const insertNewComponent = ( | ||
content: BlockContent | ComponentContent, | ||
newComponentContent: ComponentContent, | ||
componentId: string | ||
): BlockContent | ComponentContent => { | ||
const repeater = content.fields.find((field) => { | ||
if (field.type !== "repeater") return false; | ||
field.component_id === componentId; | ||
}) as Repeater<ComponentContent>; | ||
repeater.sub_elements.push(newComponentContent); | ||
console.debug(content); | ||
return content; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const deleteBlock = (zoneStore, blockIndex: number) => { | ||
const newArray = zoneStore.content.toSpliced(blockIndex, 1); | ||
zoneStore.content = newArray; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters