Giter Site home page Giter Site logo

kontent-ai / sample-app-vue Goto Github PK

View Code? Open in Web Editor NEW
23.0 24.0 36.0 3.52 MB

A sample website written in JavaScript and Vue utilizing the Kontent.ai Delivery API to manage and retrieve content.

Home Page: https://kontent-sample-app-vue.netlify.app

License: MIT License

HTML 0.29% Vue 19.28% CSS 45.19% TypeScript 35.24%
spa vue headless-cms content-delivery delivery-api vuejs hacktoberfest kontent-ai kontent-ai-sample

sample-app-vue's Introduction

Kontent.ai sample Vue.js single-page application

Netlify Status Live Demo Stack Overflow Discord

This is a sample website written in Typescript utilizing the Kontent.ai Delivery API to retrieve content. You can register your account for free at https://app.kontent.ai.

Application setup

  1. Install the latest version of NodeJS and npm. You can download both at https://nodejs.org/en/download/.
  2. Clone the sample application repository.
  3. Navigate to the root folder of the application in the command line.
  4. Type npm install to install required npm packages.
  5. Type npm run dev to start a development server.
  6. The application opens in your browser at http://localhost:5173.

Connecting to your sample project

On the first run of the app, you'll be presented with a configuration page. It will allow you to connect the app to your Kontent.ai sample project or create a new one. You'll also be able to start a trial and convert to a developer plan when the trial expires.

If you want to open the configuration page after the project is already connected to the app. Just open the URL http://localhost:5173/Admin/Configuration.

Alternatively, you can connect your project manually as per the chapter below.

Connecting to your project manually

If you want to change the source Kontent.ai project, follow these steps:

  1. In Kontent.ai, choose Environment settings from the app menu.
  2. Under the General settings, copy Environment ID.
  3. Create and open a .env.local file in the sample application folder.
  4. On the first line, add your Environment ID constant using the format VITE_VUE_APP_ENVIRONMENT_ID=00000000-0000-0000-0000-000000000000.
  5. Save the file.

Now, when you run the application, it will retrieve the content from your sample project. This setup has a higher priority than setting your sample project via the Configuration page.

Previewing content from your project

To preview unpublished content in the sample application, follow these steps:

  1. In Kontent.ai, choose Project settings from the app menu.
  2. Under Production environment settings, choose API keys.
  3. Copy your Environment ID and Preview API key.
  4. Create and open a .env.local file in the sample application folder.
  5. On the first line, add your Environment ID constant using the format VITE_VUE_APP_ENVIRONMENT_ID=00000000-0000-0000-0000-000000000000.
  6. On the next line, add your Preview API key using the format VUE_APP_PREVIEW_API_KEY=00000000-0000-0000-0000-000000000000.
  7. Save the file.

Now, when you now run the application, you will see all project content including the unpublished version of content items.

Content administration

  1. Navigate to https://app.kontent.ai in your browser.
  2. Sign in with your credentials.
  3. Manage content in the content administration interface of your sample project.

You can learn more about content editing at Kontent.ai Learn.

Content delivery

You can retrieve content either through the Kontent.ai Delivery SDKs or the Kontent.ai Delivery API:

  • For published content, use https://deliver.kontent.ai/ENVIRONMENT_ID/items.
  • For unpublished content, use https://preview-deliver.kontent.ai/ENVIRONMENT_ID/items.

For more info about the API, see the API reference.

You can find the Delivery and other SDKs at https://github.com/kontent-ai.

Data origin

This sample wants to showcase loading content from Kontent.ai as well as loading some part of the site from static JSON resources.

Basically, the content that you can't find in the Localization folder is loaded from Kontent.ai.

  • The data provided from Localization are using the vue-i18n plugin, so every call in components using t('KEYWORD') in components is loading data from these JSONs as "Banner" section" (component here)

Model mapping and data fetching

There are two types of model mapping in this application:

content type -> DTO -> component

Content type definitions are generated from content types via Kontent.ai model generator tool. All types can be found in src/Models folder. The _project.ts contains information about the project structure such as project languages as well as other structure information like codenames about content types.

content type -> DTO -> view model -> component

Some models displayed in views might require an adjustment from content types. For example, the content type Cafe contains fields for city and street and we would like to have a model containing an address in the format city, street. You can find an example of such a view model in CafeModel.tsx located in the src/ViewModels folder. Converting Cafe into CafeModel can be done by the function located in src/Utilities/CafeListing.ts.

Data fetching

This solution fetches data using the Kontent.ai Delivery client. For more implementation detail on how to set up the client see src/Client.js. After your client is set up, you are able to deliver your content to your project. The following example showcases how to use a Kontent.ai delivery client to fetch data.

const fetchBrewer = () => {
  var query = Client.items<Brewer>()
    .type('brewer')
    .equalsFilter('url_pattern', route.params.brewerSlug as string)

  if(language) {
    query.languageParameter(language)
  }

  query
    .toPromise()
    .then(response => {
      // store data to the state variable of your component.
      brewer.value = response.data.items[0]
    }
}

Language fallbacks

To deal with content that is not available in the current language, this project uses a method called language fallbacks. It will fetch the content in the language set as a fallback language in the Kontent.ai project and redirect the website to the URL with a prefix of the given language. However, it is possible to disable language fallbacks by adding a filter of system.language to your query. For more information about getting localized content check this link.

var query = Client.items<AboutUs>().type('about_us');

if (language) {
  query
    .languageParameter(language)
    .equalsFilter('system.language', 'es-ES');
}

Handling 404

For the not found resources, prefixed 404 pages are used for both languages. As the content on one page should be in one language, this approach might help you to optimize SEO. If language is not set in the URL the application uses the last used language, which is set in cookies.

Deployment

You can use, for example, surge to deploy your app live. Check out the step-by-step guide on our blog.

sample-app-vue's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sample-app-vue's Issues

Add Preview Edit Links

Kentico Kontent admin UI now supports navigating to specific content items via URLs in a certain format.

The goal of this task is to enrich the markup of this sample application with the links.

Suggested approach:

Motivation

We want to present to our customers an example of this feature – how they can use that

Solution

When
I open About Us preview
Then
I see enabled "Edit mode" in the fixed bar on the bottom of the page
And
I am able to click on each element from the page About Us

When
I open About Us preview
Then
I see the link "Open in KC" in the fixed bar on the bottom of the page
And
I am able to click on it

When
I click on toggle button Edit mode
Then
Edit mode is turned off
And
I am able to preview page without distraction - only the fixed bar is presented

Design notes

  • Do not show button for menu/navigation,Language selector, Dancing goat heading
  • For Text element – show edit button next to the element (on the right)
  • For Richt text element (and others) – show edit button in the center of the element
  • While in Edit mode, I am able to click anywhere on the element (not just on the button) to be redirected to KC
  • When I hover over the element, the button should be in hover state

Support Node v17+

Motivation

The sample app is not supporting node higher than v17 (eg. 'v19.1.0', npm: '9.1.1')

It throws the following warnings when installing npm:
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: '@achrinza/[email protected]',
npm WARN EBADENGINE required: { node: '8 || 10 || 12 || 14 || 16 || 17' },
npm WARN EBADENGINE current: { node: 'v19.1.0', npm: '9.1.1' }
npm WARN EBADENGINE }

It throws the following error when running npm server: ERR_OSSL_EVP_UNSUPPORTED

Proposed solution

Support node engines higher than v17

Additional context

There is a workaround for the solution: https://towardsdev.com/fixing-err-ossl-evp-unsupported-error-on-nodejs-17-25c21066601c (I have used Method 1 for Windows)

Cannot leave configuration in web spotlight

Brief bug description

When I open the sample app in the web spotlight, I can't leave the configuration page. Submitting the form doesn't work, because the cookie is not accessible to the page in the iframe. You need to set the SameSite=None;Secure policy. See https://kontent.ai/learn/tutorials/develop-apps/build-strong-foundation/set-up-preview/#a-set-up-preview-for-web-spotlight

Repro steps

  1. Set the deployed sample app's URL as a preview URL and enable web spotlight.
  2. Open web spotlight
  3. Set your projectID and submit the form.
  4. The app still doesn't open (you stay on the configuration page)

Expected behavior

The app should open the homepage route with the project id in a cookie.

Test environment

  • chrome

Improve readme to better describe the sample application

Motivation

Make the people more aware of the sample app functionality

Proposed solution

Look ak the react sample app and adjust the readme of this repository

Additional context

Add any other context, screenshots, or reference links about the feature request here.

Replace hardcoded sample project items count in SelectedProject.js

Motivation

In /src/Utilities/SelectedProject.js there is a hardcoded SAMPLE_PROJECT_ITEM_COUNT constant which is used in the wizard.

This wizard checks if the Delivery API has already accessible all items in the user's Dancing Goat project and if the project is fully accessible. If the count of all items in the project is more or equals to items from constant then we consider the project to be ready.

However, if the user's sample Dancing Goat project changes (we add/remove new content) this logic might not work. Under the hood, we clone the Dancing Goat project from 'one-source-of-truth' project.

Proposed solution

The goal of this issue is to implement logic which will get a count of items in from this source (-of-truth) project (projectId: 975bf280-fd91-488c-994c-2f04416e5ee3) and store it to an existing (non)constant which will be checked the same way as it is right now.

Note: It might be a good idea to adjust if condition in Configuration.js, too.
Something like ... if (response.items.length > SAMPLE_PROJECT_ITEM_COUNT) in waitUntilProjectAccessible function.

Handle missing values in the preview

Motivation

The app crashes unexpectedly in the preview mode for articles without the image or products without the price. Preview Delivery API returns items in the draft step and some values can be missing (even required fields).

Proposed solution

  • The app should handle missing values and display some placeholder without data or with some explanation (e.g,. "Price TBA")
  • check other possible occurences of this problem

Additional context

image

Configuration admin page is not up-to-date with the latest design

Brief bug description

The Kontent design changed quite a lot since (Admin/Configuration.vue)[https://github.com/Kentico/kontent-sample-app-vue/blob/master/src/components/Admin/Configuration.vue] was last updated. It should look completely different now. Check out kontent-ai/sample-app-net#128 for an inspiration.

Repro steps

  1. Clone repository fresh (or clearn up projectID)
  2. Run the app
  3. See the configuration in the old design:
    image

Expected behavior

The configuration should now look like this:
image

Update the projects dynamic parts

Today I setup this sample project for the first time. I'm new to this product and would use this sample project as a way to find my way around Kentiko Kontent / SaaS. I used the documentation page as a guide (https://docs.kontent.ai/tutorials/develop-apps/get-started/run-sample-app?tech=javascript#a-making-changes-to-your-project).

After the successful first run I of course go and try to change an item in the CMS in order to see that change being reflected on my sample site. Unfortunately I was disappointed not to see this change happen. After a talk with your excellent in app chat and digging around a little myself I come to the realisation that not every part of this sample project is being loaded dynamically from the CMS.

I understand the need to show that you can combine the dynamic pages with static ones. However since it's main purpose is showing off the connection with Kentiko Kontent I'd at least expect the home page (the first page you see) to be dynamic. Personally I'd create a separate section of the site to show off the static parts. But barring that, it would have been nice to know at least that not all parts are actually dynamic, and which parts are.

So in short; at least add some documentation somewhere (where it can't be missed) stating that not all content in the sample project is being loaded dynamically, where to find that static content in the code and additionally maybe state which content parts are dynamic and which are not.

Lastly, maybe move the entire static part to its own section. Though as long as it's clear where the content comes from this is less of an issue. I also understand the need for a page with a mixture of dynamic / non-dynamic content.

Hardcoded assets in app

Motivation

Currently, it's unclear at first what content is hardcoded in the app and what is drawn from Kentico Kontent. For example, the Hero unit on the homepage is not drawn from Kentico Kontent. So it's possible to run the app and get confused when changing the Hero unit in Kentico Kontent doesn't change the content in the app.

Proposed solution

  • create an account in Kentico Kontent and let it generate a sample project for you
  • search for jpg in this repo
  • replace all hardcoded assets with ones from Kentico Kontent.
    -- use the JavaScript SDK to retrieve the assets using their codenames
    -- see the docs for more details about how to work with Kentico Kontent API
  • remove replaced assets from this repository

Additional context

Typeresolvers

Hi Guys,

In the Client.js I noticed that the typeresolvers are initialised:

let typeResolvers = [
  new TypeResolver('about_us', () => new AboutUs()),
  new TypeResolver('accessory', () => new Accessory()),
  new TypeResolver('article', () => new Article()),
  new TypeResolver('brewer', () => new Brewer()),
  new TypeResolver('cafe', () => new Cafe()),
  new TypeResolver('coffee', () => new Coffee()),
  new TypeResolver('fact_about_us', () => new FactAboutUs()),
  new TypeResolver('grinder', () => new Grinder()),
  new TypeResolver('hero_unit', () => new HeroUnit()),
  new TypeResolver('home', () => new Home()),
  new TypeResolver('hosted_video', () => new HostedVideo()),
  new TypeResolver('office', () => new Office()),
  new TypeResolver('tweet', () => new Tweet())
];

From the KenticoCloudDeliveryTypeScriptSDK:

image

But, correct me if i am wrong, they are never used? Why bother configuring them? Or are they also used for parsing links in Rich text elements?

Add eslint support

Motivation

Add eslint support and also may add it to the CI

Proposed solution

An ideal solution for the above problems.

Additional context

Add any other context, screenshots, or reference links about the feature request here.

Extract API keys definition to .env

Motivation

According to #19 (comment) - it is easily possible to commit API keys as a part of the pull request in Client.js file.

Currently, it is possible to configure the project :

When you configure your project manually it is required to temporarilly change the source code in Client.js file and that could leads to commit this unintended change that was used for, let's say testing purposes.

If the project ID was loaded from environment, as an environment variable, it would eliminate possibility for unintended commit to Client.js.
Loading environment variables is possible utins dotenv library.

Proposed solution

Best practice nowadays is to use environment variables stored in .env file.

  • Set up .gitignore to ignore .env file
  • Configure Client.js to use .env definition for keys (projectId and previewApiKey)
  • Update Readme

Additional context

Unsubscribing from observables

Hey,

When you are loading data like:

let fetchProductStatuses = () => {
    Client.taxonomy("product_status")
        .get()
        .subscribe(response => {
            productStatuses = response.taxonomy.terms;
            notifyChange();
        });
}

You should also have a way of unsubscribing from the Observable when its no longer needed (usually when user goes to another page, but can be different). The consequences of not doing so can be quite important because the subscription code upon completion can be executed after user left the page. Ideally, you should cancel the HTTP request (as well as the execution of code in your subscribe section) when user leaves the page or you no longer need it.

In Angular, this can be best achieved with takeUntil that uses a notifier on ngDestroy which gets called when component gets destroyed. In React, you could use onUnmount. Not sure about Vue, but surely there is something similar.

➡️ beforeDestroyed

To see that your code is cancelling request properly, add a delay to your Observables and try switching pages. You should see that upon switching to another page, the request is cancelled in network tab in dev tools of your browser. See the top answer here to see how it can be used with Angular.

Similar PR in the React sample app (for inspiration): kontent-ai/sample-app-react#66

Define common code format

It would be great to have a common format specification for the components.

At least have all the vue components with same format:

  • I recommend 2 spaces for javascript code and 4 for html part
  • set up .eslintrc.json

Upgrade to Vue 3

Motivation

Upgrade to to newer version of Vue as part of innovation of this repo. Also upgrade other libraries. Convert the code to use new Composition API. Add description of the app (add a paragraph about language fallbacks)

Proposed solution

Try to update the packages to resolve dependabot warnings.

Additional context

https://vuejs.org/api/composition-api-setup.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.