Skip to content

Commit

Permalink
fix(devcomponents): hide if not preview mode (#59)
Browse files Browse the repository at this point in the history
* fix(devcomponents): hide if not preview mode

* test: add import for is preview mode

* refactor(slider): remove comments

* refactor(tpp): use $isPreviewMode

---------

Co-authored-by: Jonas Scholz <[email protected]>
  • Loading branch information
lksmsr and Code42Cate authored Mar 14, 2023
1 parent 7ed45fb commit 38e399d
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 52 deletions.
11 changes: 1 addition & 10 deletions components/AddSection.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<template>
<div
v-if="mode === FSXAContentMode.PREVIEW"
class="my-10 flex flex-col items-center"
>
<div class="my-10 flex flex-col items-center">
<button
class="flex w-fit items-center space-x-2 border border-black px-4 py-2 font-medium hover:bg-gray-50"
:onclick="() => $createSection(bodyName)"
Expand All @@ -28,11 +25,5 @@
</template>

<script setup lang="ts">
import { FSXAContentMode } from "fsxa-api";
defineProps<{ bodyName: string }>();
const {
public: { mode },
} = useRuntimeConfig();
</script>
18 changes: 9 additions & 9 deletions components/Page/Body.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
class="group relative my-10"
data-testid="pageBodyChild"
>
<DevOnly>
<Dev
v-if="showDev"
:content="pageBodyContent"
class="hidden group-hover:block"
component-name="section"
/>
</DevOnly>
<Dev
v-if="showDev && $isPreviewMode"
:content="pageBodyContent"
class="hidden group-hover:block"
component-name="section"
/>

<component
:is="getComponentFromPageBodyContent(pageBodyContent)"
:content="pageBodyContent"
/>
</div>
<ClientOnly>
<AddSection :body-name="pageBody.name" />
<AddSection v-if="$isPreviewMode" :body-name="pageBody.name" />
</ClientOnly>
</div>
</template>
Expand All @@ -32,6 +30,8 @@ defineProps<{ pageBody: PageBody }>();
const { showDev } = useDev();
const { $isPreviewMode } = useNuxtApp();
function getComponentFromPageBodyContent(pageBodyContent: PageBodyContent) {
switch (pageBodyContent.type) {
case "Dataset":
Expand Down
15 changes: 13 additions & 2 deletions components/PageBodyContent/Content2Section.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
<template>
<div data-testid="content2section">
<component :is="content2SectionComponent" :content="content" />
<component
:is="content2SectionComponent"
v-if="content2SectionComponent"
:content="content"
/>

<Unknown
v-if="!content2SectionComponent && $isPreviewMode"
:content="content"
:data="content.data"
/>
</div>
</template>

<script setup lang="ts">
import { Content2Section } from "fsxa-api";
const props = defineProps<{ content: Content2Section }>();
const { $isPreviewMode } = useNuxtApp();
const content2SectionComponent = computed(() => {
switch (props.content.sectionType) {
default:
return resolveComponent("Unknown");
return undefined;
}
});
</script>
15 changes: 13 additions & 2 deletions components/PageBodyContent/Dataset.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<template>
<div data-testid="dataset">
<component :is="datasetComponent" :content="content" />
<component
:is="datasetComponent"
v-if="datasetComponent"
:content="content"
/>
<Unknown
v-if="!datasetComponent && $isPreviewMode"
:content="content"
:data="content.data"
/>
</div>
</template>

<script setup lang="ts">
import { Dataset } from "fsxa-api";
const props = defineProps<{ content: Dataset }>();
const { $isPreviewMode } = useNuxtApp();
const datasetComponent = computed(() => {
switch (props.content.template) {
default:
return resolveComponent("Unknown");
return undefined;
}
});
</script>
10 changes: 8 additions & 2 deletions components/PageBodyContent/Section.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
<component
:is="sectionComponent"
v-if="sectionComponent"
:content="content"
:data="content.data"
:data-preview-id="
content.previewId?.split('.')[0] !== '' ? content.previewId : undefined
"
/>
<Unknown
v-if="!sectionComponent && $isPreviewMode"
:content="content"
:data="content.data"
/>
</div>
</template>

<script setup lang="ts">
import { Section } from "fsxa-api";
const props = defineProps<{ content: Section }>();
const { $isPreviewMode } = useNuxtApp();
const sectionComponent = computed(() => {
switch (props.content.sectionType) {
case "interesting_facts":
Expand All @@ -27,7 +33,7 @@ const sectionComponent = computed(() => {
case "teaser":
return resolveComponent("SectionTeaser");
default:
return resolveComponent("Unknown");
return undefined;
}
});
</script>
16 changes: 8 additions & 8 deletions components/Section/Header.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<template>
<div class="group relative h-64" data-testid="headerSection">
<DevOnly>
<Dev
v-if="showDev"
:content="data"
class="hidden group-hover:block"
component-name="Page Header"
/>
</DevOnly>
<Dev
v-if="showDev && $isPreviewMode"
:content="data"
class="hidden group-hover:block"
component-name="Page Header"
/>
<div
class="absolute inset-0 flex flex-col justify-center bg-black bg-opacity-80 p-6 text-white md:p-12"
>
Expand Down Expand Up @@ -40,6 +38,8 @@ import { DataEntries } from "fsxa-api";
defineProps<{ data: DataEntries }>();
const { $isPreviewMode } = useNuxtApp();
const { showDev } = useDev();
const breadcrumbs = computed(() => {
Expand Down
19 changes: 8 additions & 11 deletions components/Section/Slider.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<template>
<div data-testid="sliderSection">
<div class="group relative">
<DevOnly>
<Dev
v-if="showDev"
:content="data"
class="hidden group-hover:block"
component-name="Slider"
/>
</DevOnly>
<Dev
v-if="showDev && $isPreviewMode"
:content="data"
class="hidden group-hover:block"
component-name="Slider"
/>
<div v-if="activeSlide" class="realtive text-white">
<div class="sliderButtonContainer left-0">
<button class="sliderButton pr-8" @click="prevSlide">
Expand Down Expand Up @@ -64,9 +62,6 @@
:image="activeSlide?.data.st_picture"
/>
</div>

<!-- {{ activeImage }} {{ data.data }} -->
<!-- <pre>{{ activeSlide.data }}</pre> -->
</div>
</div>
</template>
Expand All @@ -76,6 +71,8 @@ import { Image, RichTextElement, Section } from "fsxa-api";
const { showDev } = useDev();
const { $isPreviewMode } = useNuxtApp();
interface SliderSlide extends Section {
data: {
st_button: {
Expand Down
3 changes: 2 additions & 1 deletion plugins/2.setupProxyApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FSXAProxyApi, LogLevel } from "fsxa-api";
import { FSXAContentMode, FSXAProxyApi, LogLevel } from "fsxa-api";

export default defineNuxtPlugin(() => {
const runtimeConfig = useRuntimeConfig();
Expand All @@ -14,6 +14,7 @@ export default defineNuxtPlugin(() => {
return {
provide: {
fsxaApi,
isPreviewMode: runtimeConfig.public.mode === FSXAContentMode.PREVIEW,
},
};
});
8 changes: 2 additions & 6 deletions plugins/3.tpp.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FSXAContentMode } from "fsxa-api";
import TPP_SNAP from "fs-tpp-api";
import {
onNavigationChangeHandler,
Expand All @@ -10,12 +9,9 @@ import {
* Nuxt plugin that initializes the TPP Snap, only works if you are in the FirstSpirit editor and if the environment variable FSXA_MODE is set to "preview"
*/
export default defineNuxtPlugin(() => {
const {
public: { mode },
} = useRuntimeConfig();
const { $isPreviewMode } = useNuxtApp();

// Set by environment variable
if (mode !== FSXAContentMode.PREVIEW) return;
if (!$isPreviewMode) return;

TPP_SNAP.isConnected.then(async (_isConnected: boolean) => {
const initialized = await onInit();
Expand Down
2 changes: 1 addition & 1 deletion tests/pages/[...slug].spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("slug page", () => {
addToCachedDatasets: (_route: string, _dataset: Dataset) => null,
};

// test cases broken because testutils do not wait for async data
// test cases broken because testutils do not wait for useasyncdata
describe.skip("page not cached", () => {
it("render with homepage layout prop => render homepage layout component", async () => {
vi.spyOn(content, "useContent").mockReturnValue({
Expand Down
1 change: 1 addition & 0 deletions tests/testutils/nuxtMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function useNuxtApp() {
addEventListener: (_event: string, _cb: (data: any) => void) => null,
}),
},
$isPreviewMode: true,
};
}

Expand Down

0 comments on commit 38e399d

Please sign in to comment.