Skip to content

Commit

Permalink
docs: Initial docs setup for FaustJs (#320)
Browse files Browse the repository at this point in the history
* feat: docusaurus setup

* chore: clean up docusaurus config

* docs: start data fetching in next

* docs: usePage next hook

* docs: useGeneralSettings hook in next

* docs: WIP params, ssg, ssr, data fetching

* docs: next client generation

* docs: data fetching hooks, custom client usage

* docs: usePages hook

* docs: ssr/ssg

* docs: (#306) create CPT guide

* fix: custom post types guide title
  • Loading branch information
blakewilson authored Jun 30, 2021
1 parent 67ef7d9 commit 0d64c0d
Show file tree
Hide file tree
Showing 33 changed files with 27,891 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/website/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Dependencies
/node_modules

# Production
/build

# Generated files
.docusaurus
.cache-loader

# Misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
33 changes: 33 additions & 0 deletions internal/website/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Website

This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator.

## Installation

```console
yarn install
```

## Local Development

```console
yarn start
```

This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.

## Build

```console
yarn build
```

This command generates static content into the `build` directory and can be served using any static contents hosting service.

## Deployment

```console
GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
```

If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
3 changes: 3 additions & 0 deletions internal/website/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
};
34 changes: 34 additions & 0 deletions internal/website/docs/getting-started/setting-up-wordpress.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Setting Up WordPress

## WordPress

To create a headless WordPress site, you'll first need a WordPress instance! If you don't already have one, we recommend [Local](https://localwp.com/) to try things out locally, or you can use a live WordPress site.

## Plugins

You'll also need to download/activate two plugins for your WordPress site:

1. Download, upload, and activate the `wpe-headless` plugin. [(Download)](https://wp-product-info.wpesvc.net/v1/plugins/wpe-headless?download)
2. Install [WP GraphQL](https://wordpress.org/plugins/wp-graphql/) from WordPress

### Why do I need these plugins?

#### WPE Headless

The plugin ensures that your WordPress site runs smoothly as a headless CMS. From smart content redirects to enabling post previewing to providing the right data in WPGraphQL, installing the plugin gives you the things you need to run WordPress as a headless CMS.

#### WP GraphQL

[WP GraphQL](https://wordpress.org/plugins/wp-graphql/) turns your WordPress site into a fully queryable GraphQL API. We use this API to communicate with the frontend.

### Set your headless frontend URL

Now that you have the plugins installed, navigate to Settings -> Headless and set your Front-end site URL. This is the URL where your frontend will live. For local development this will usually be something like `http://localhost:3000` or `http://localhost:8000`.

<img src="/docs/img/frontend-site-url-settings.png" alt="The Headless WordPress admin interface with a red rectangle around the front-end site URL field" />

## Usage

Now that you have WordPress setup to support headless, you can now use the framework! We've made guides to easily get you started with the frontend framework of your choice:

- [Usage with Next.js](/next/getting-started)
264 changes: 264 additions & 0 deletions internal/website/docs/guides/custom-post-types.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
---
id: custom-post-types-guide
slug: interacting-with-custom-post-types
title: Interacting With Custom Post Types
---

# Interacting with custom post types

Querying data in custom post types is just as easy as querying for posts or pages in the framework.

Before we start, if you haven't already, [setup your WordPress site for headless](/getting-started/setting-up-wordpress).

## Create a custom post type

We'll be using the [Atlas Content Modeler](https://github.com/wpengine/atlas-content-modeler) plugin to create custom post types for this guide, but you can choose to create your custom post types in any way you'd like.

Atlas Content Modeler is a WordPress plugin to create custom post types and fields for headless WordPress sites. Start by downloading the plugin and activating it on your WordPress site [(Download)](https://wp-product-info.wpesvc.net/v1/plugins/atlas-content-modeler?download)

Now, navigate to WP Admin -> Content Modeler.

<img src="/docs/img/empty-content-modeler.png" alt="The Atlas Content Modeler admin interface" />

Let's create a custom post type for our team members. Click the "Get Started" button, and fill out the fields to create a new Content Model. Be sure to mark "API Visibility" to "Public" so we can query the data.

<img src="/docs/img/team-members-content-modeler.png" alt="The Atlas Content Modeler admin interface with fields for a team member content type" />

## Create fields for your custom post type

Now, that we've created our custom post type, let's add some fields to it. From WP Admin -> Content Modeler, select the Team Members custom post type.

We'll add three fields:

1. A Media field type. The name will be "Profile Pic" and the API identifier "profilePic"
2. A Text field type. The name will be "Full Name" and the API identifier "fullName". We'll also select "Use this field as the entry title"
3. A Rich Text field type. The name will be "Bio" and the API identifier "bio".

<img src="/docs/img/team-members-content-modeler-fields.png" alt="The Atlas Content Modeler admin interface with filled custom fields" />

## Create some team members

Now that you have created a custom post type and fields, you can start entering data. Navigate to WP Admin -> Team Members to start creating!

For this example, I've created two team members, Jane and Jonh Doe, with bios and profile pics:

<img src="/docs/img/content-modeler-add-team-member.png" alt="Atlas Content Modeler Custom Post Type interface for adding a new item" />

## Create your starter headless project

From here, you have your WordPress site setup with the necessary headless plugins, and data. Now, we need to create a frontend app to consume this data.

We are going to use a starter project in Next.js, but the same concepts apply in other frontend frameworks.

[Follow the steps in our Getting started with Next.js usage guide to create a frontend app.](/next/getting-started)

Once your frontend app is created, the structure should look something like this:

```
my-app/
src/
client/
..
pages/
..
components/
..
scss/
..
public/
.env.local
.env.local.sample
.env.test.sample
.eslintrc
.gitignore
gqless.config.js
next-env.d.ts
package.json
README.md
tsconfig.json
```

### Regenerate your schema

Your schema is a TypeScript representation of your WordPress site. Since we have added some custom post types and fields, we'll want to regenerate this schema to ensure it's up to date. To regenerate your schema from the Next.js starter, simply run:

```bash
npm run generate
```

[Learn more about generating your client/schema](next/generating-client)

### Run the dev server

Run the following command to start the dev server:

```bash
npm run dev
```

You should now be able to access the starter project at [http://localhost:3000](http://localhost:3000)

### Create a team page

We'll create a team page in `src/pages/team.tsx` to display our team members:

```tsx title="src/pages/team.tsx"
import Head from 'next/head';
import { Header, Footer } from '../components';
import { client } from '../client';

export default function Team() {
const { useGeneralSettings } = client;
const generalSettings = useGeneralSettings();

return (
<>
<Header
title={generalSettings.title}
description={generalSettings.description}
/>

<Head>
<title>Custom Page - {generalSettings.title}</title>
</Head>

<main className="content content-single">
<div className="wrap">
<h2>Team Members</h2>
</div>
</main>

<Footer copyrightHolder={generalSettings.title} />
</>
);
}
```

This will look something like:

<img src="/docs/img/nextjs-empty-team-members-page.png" alt="An empty web page with a large title for team members" />

### Query for team members

When querying for custom post types, you can leverage the `useQuery` hook that is exported from the client. This allows you to access all of WordPress' data. You'll also notice it's typed from your schema, making things super easy to find.

<img src="/docs/img/useQuery-typed.png" alt="A screenshot of the useQuery hook and its typings" />

Let's get our team members using the `useQuery` hook:

```tsx title="src/pages/team.tsx" {6,8}
import Head from 'next/head';
import { Header, Footer } from '../components';
import { client } from '../client';

export default function Team() {
const { useGeneralSettings, useQuery } = client;
const generalSettings = useGeneralSettings();
const teamMembers = useQuery().teamMembers()?.nodes;

return (
<>
<Header
title={generalSettings.title}
description={generalSettings.description}
/>

<Head>
<title>Custom Page - {generalSettings.title}</title>
</Head>

<main className="content content-single">
<div className="wrap">
<h2>Team Members</h2>
</div>
</main>

<Footer copyrightHolder={generalSettings.title} />
</>
);
}
```

Finally, we'll create a `teamMember.tsx` component to display each team member:

```tsx title="/src/components/teamMember.tsx"
import { TeamMember as TeamMemberType } from 'client';

interface TeamMemberProps {
teamMember: TeamMemberType;
}

export default function TeamMember({ teamMember }: TeamMemberProps) {
return (
<div>
<img
src={teamMember?.profilePic.mediaItemUrl}
alt={teamMember?.profilePic?.altText}
/>
<h2>{teamMember?.fullName}</h2>
<div
className="bio"
dangerouslySetInnerHTML={{ __html: teamMember?.bio }}
/>
</div>
);
}
```

Notice how we are getting the `TeamMember` type from the client and generated schema.

### Put it all together

Finally, let's hook up our `TeamMember` component to the Team page.

```tsx title="src/pages/team.tsx" {4,27-29}
import Head from 'next/head';
import { Header, Footer } from '../components';
import { client } from '../client';
import TeamMember from '../components/teamMember';

export default function Team() {
const { useGeneralSettings, useQuery } = client;
const generalSettings = useGeneralSettings();

const teamMembers = useQuery().teamMembers()?.nodes;

return (
<>
<Header
title={generalSettings.title}
description={generalSettings.description}
/>

<Head>
<title>Meet the Team - {generalSettings.title}</title>
</Head>

<main className="content content-single">
<div className="wrap">
<h2>Team Members</h2>

{teamMembers.map((teamMember) => (
<TeamMember key={teamMember.id} teamMember={teamMember} />
))}
</div>
</main>

<Footer copyrightHolder={generalSettings.title} />
</>
);
}
```

Now, head back over to [http://localhost:3000/team](http://localhost:3000/team) to see your WordPress data come to life!

<img src="/docs/img/nextjs-team-page.png" alt="A web page with a list of team members" />

## Learnings

In this guide, we demonstrated:

* How to create custom post types/fields using Atlas Content Modeler
* Creating data for your custom post types
* Creating a starter frontend project to consume your WordPress data
* Using the `useQuery` custom React hook in the framework to query your custom post type data in GraphQL
8 changes: 8 additions & 0 deletions internal/website/docs/introduction.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
id: introduction
title: Introduction
---

# Introduction

WP Engine's Headless WordPress Framework provides a set of tools to make building front-end applications with WordPress as the headless CMS a pleasant experience for both developers and publishers. This framework consists of a WordPress plugin, a set of npm packages, and guides to get you started building headless WordPress sites React/Next.js/etc.
Loading

0 comments on commit 0d64c0d

Please sign in to comment.