A headless CMS is a great tool for building modern applications. Unlike a traditional monolithic CMS like WordPress and Drupal, a headless CMS allows developers to connect their content to any frontend architecture of their choice, allowing for more flexibility, functionality and performance.
With a customizable Headless CMS like Strapi for example, we have the ability to choose a database for our content, integrate a media library and even expand the backend functionality using plugins that can be found on the Strapi marketplace.
In this tutorial, we'll cover the concept of a Headless CMS and its benefits. We'll set up a working Strapi backend with PostgreSQL as our database and Cloudinary for image uploads.
We'll also look into how we can build our frontend using Nuxt 3 which gives us SSR support right out of the box and is compatible with Vue3. We're also going to set up GraphQL instead of the default REST API.
At the end of this tutorial, we'd have created a modern photo sharing app where users can sign in and upload photos and add comments.
We will learn how to set up Strapi with Postgres and integrate our Strapi backend with the Strapi comments plugin. We’ll also be able to build the frontend with the new Vue 3 Composition API, GraphQL, and SSR.
Let’s take a quick look at these concepts and technologies that are at the core of our backend.
Headless CMS is a type of Content Management System (CMS) that usually provides an Application Programming Interface (API) which allows the frontend to be built independently from the backend (or CMS). Unlike traditional CMS options like WordPress, a Headless CMS allows developers to build the frontend of their application with any framework of their choice.
Strapi is a world-leading open-source headless CMS. Strapi makes it very easy to build custom APIs either REST APIs or GraphQL that can be consumed by any client or front-end framework of choice.
Strapi runs an HTTP server based on Koa, a back-end JavaScript framework. It also supports databases like SQLite and Postgres (which we’ll be using in this tutorial).
PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. As one of the databases Strapi currently supports, Postgres is a solid choice over SQLite and we’ll see how we can set it up.
To follow this tutorial, you’ll need to have a few things in place, including:
First, we’ll create a new Strapi app. To do this:
yarn create strapi-app photo-app-api
Quickstart is recommended, which uses the default database (SQLite)
Once the installation is complete, the Strapi admin dashboard should automatically open in your browser at http://localhost:1337/admin/
.
For windows users, refer to this guide on how to Set Up a PostgreSQL Database on Windows. Also, refer to the Postgres documentation to see how to create a new database.
Once installed, start the Postgres server and obtain the port, username, and password.
To create a new database, in your terminal, run:
createdb -U postgres photosdb
postgres
superuserTo use PostgreSQL in Strapi, we’ll add some configurations in our ./config/database.js
file.
./config/database.js
and paste the below code in the file:1 // ./config/database.js
2 module.exports = ({ env }) => ({
3 connection: {
4 client: 'postgres',
5 connection: {
6 host: env('DATABASE_HOST', 'localhost'),
7 port: env.int('DATABASE_PORT', 5432),
8 database: env('DATABASE_NAME', 'photosdb'),
9 user: env('DATABASE_USERNAME', 'user'),
10 password: env('DATABASE_PASSWORD', '1234'),
11 schema: env('DATABASE_SCHEMA', 'public'), // Not required
12 ssl: {
13 rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false),
14 },
15 },
16 debug: false,
17 },
18 });
connection
object, we set the client
to postgres
. This client is the PostgreSQL database client to create the connection to the DB.host
is the hostname of the PostgreSQL server we set it to localhost
.port
is set to 5432
, and this is the default port of the PostgreSQL server.database
is set to the photosdb
, and this is the name of the database we created in the PostgreSQL server.password
is the password of our PostgreSQL server.username
is the username of our PostgreSQL. It is set to Postgres
because it is the username of our PostgreSQL server.schema
is the database schema, and it is set to the public
here. This schema is used to expose databases to the public.With this, our Strapi is using PostgreSQL to store data.
yarn develop
N/B: If you encounter an error: Cannot find module 'pg'
, install pg
. Run:
yarn add pg
#OR
npm install pg
Once the app has started, it should open the admin registration page at http://localhost:1337
. Proceed to create an admin account.
To get started with GraphQL in our Strapi backend, we’ll install the GraphQL plugin first. To do that:
yarn strapi install graphql
#OR
npm run strapi install graphql
Let’s create the content type for our posts.
First, in the Configurations modal, set the display name - **Post**
, then create the fields for the collection
Caption
- Text (Long Text)photo
- Media (Multiple media)user
- Relation (one-to-many relation with Users
from users-permissions
)The collection type should look like this:
Finally, we have to install the Strapi comments plugin.
yarn add strapi-plugin-comments@latest
comments
.1 // ./config/plugins.js
2
3 module.exports = ({ env }) => ({
4 //...
5 comments: {
6 relatedContentTypes: {
7 posts: {
8 contentManager: true,
9 isSingle: true,
10 key: "title",
11 value: "id",
12 },
13 },
14 enabled: true,
15 config: {
16 enabledCollections:["api::post.post"],
17 badWords: false,
18 moderatorRoles: ["Authenticated"],
19 approvalFlow: ["api::post.post"],
20 entryLabel: {
21 "*": ["Title", "title", "Name", "name", "Subject", "subject"],
22 "api::post.post": ["MyField"],
23 },
24 reportReasons: {
25 MY_CUSTOM_REASON: "MY_CUSTOM_REASON",
26 },
27 gql: {
28 // ...
29 },
30 },
31 },
32 graphql: {
33 }
34 });
You can see more information on configuration from the Strapi Comments plugin docs.
yarn build && yarn develop
When we open our admin dashboard, the Comments plugin should appear in the Plugins section of Strapi sidebar.
Next, we’ll configure our Comments plugin. Navigate to SETTINGS > COMMENTS PLUGIN > CONFIGURATION then confgure the following settings:
1- General configuration - *Enable comments only for* : **`Posts`**
2- Additional configuration
3- Bad words filtering: **`Enabled`**
4- GraphQL queries authorization: **`Enabled`**
Click on SAVE to save the configuration and restart the server for the changes to take effect.
By default, we won't be able to access any API from Strapi unless we set the permissions.
Go to PUBLIC and enable the following actions for
Posts
find
findOne
findAllFlat
findAllInHierarchy
findOne
✅ User > find
Next, to set permissions for authenticated users, go to AUTHENTICATED and enable the following actions for
count
findOne
find
In the second stage, we are going to set up our frontend.
npx nuxi init photos-app-frontend
cd photos-app-frontend
yarn install
#OR
npm install
Once the installation is complete, add Tailwind CSS to the project using the @nuxt/tailwind
module. We’ll also install the tailwind form plugin. This module helps set up Tailwind CSS (version 3) in our Nuxt 3 application in seconds.
yarn add --dev @nuxtjs/tailwindcss @tailwindcss/forms
#OR
npm install --save-dev @nuxtjs/tailwindcss @tailwindcss/forms
modules
section in nuxt.config.ts
:1 // nuxt.config.ts
2 // ...
3 export default defineNuxtConfig({
4 modules: ['@nuxtjs/tailwindcss']
5 })
tailwind.config.js
by running: npx tailwindcss init
tailwind.config.js
1 // tailwind.config.js
2 module.exports = {
3 theme: {
4 // ...
5 },
6 plugins: [
7 require('@tailwindcss/forms'),
8 // ...
9 ],
10 }
/.assets/css/main.css
file:1 // ./assets/css/main.css
2
3 @tailwind base;
4 @tailwind components;
5 @tailwind utilities;
nuxt.config.ts
file, enter the following:1 // ./nuxt.config.ts
2 // ...
3 export default defineNuxtConfig({
4 modules: ['@nuxtjs/tailwindcss'],
5 tailwindcss: {
6 cssPath: '~/assets/css/main.css',
7 }
8 })
We’ll use Heroicons for our icon library. To get started, install the Vue library:
yarn add @heroicons/vue
#OR
npm install @heroicons/vue
We want users to be able to register, sign in, and sign out. To do that, we first need to define our user
and session
state.
We’ll be using Nuxt runtime config to access global config in our application like the Strapi GraphQL URLs. In the ./nuxt.config.ts
file,
1 // ./nuxt.config.ts
2
3 import { defineNuxtConfig } from 'nuxt'
4 export default defineNuxtConfig({
5 // ...
6 runtimeConfig: {
7 public: {
8 graphqlURL: process.env.STRAPI_GRAPGHQL || 'http://localhost:1337/graphql',
9 strapiURL: process.env.STRAPI_URL || 'http://localhost:1337',
10 }
11 },
12 })
Click here to view the code on GitHub.
This way, we can have access to these URLs throughout our application.
Nuxt 3 provides useState
composable to create a reactive and SSR-friendly shared state across components.
./composables/state.js
and enter the following:1 // ./composables/state.js
2
3 // READ USER STATE
4 export const useUser = () => useState("user", () => ({}));
5 // SET USER STATE
6 export const useSetUser = (data) => useState("set-user", () => (useUser().value = data));
7 // USE SESSION STATE
8 export const useSession = () => useState("session", () => ({ pending: true, data: null }));
9 // SET SESSION STATE
10 export const useSetSession = (data) => {
11 // save session state to localstorage
12 localStorage.setItem("session", JSON.stringify(data));
13 useState("set-session", () => {
14 // update session state
15 useSession().value.pending = false;
16 useSession().value.data = data;
17 // update user state
18 useUser().value = data?.user;
19 });
20 };
Click here to view the code on Github
Here, we’re using [useState](https://v3.nuxtjs.org/guide/features/state-management)
, an SSR-friendly [ref](https://vuejs.org/api/reactivity-core.html#ref)
replacement. Its value will be preserved after server-side rendering (during client-side hydration) and shared across all components using a unique key.
The useState
function accepts two parameters, the key
which is a string and a function.
So, we create state for useUser
& useSession
which will be used to read/get the current user and session state. setUser
& setSession
will be used to set state with setSession
saving the session data to localStorage
and setting the useUser
state as well.
Next, we’ll create a composable function that we’ll use to send requests to our Strapi backend.
./composables/sendReq.js
1 // ./composables/sendReq.js
2
3 // function to send requests
4 // pass GraphQL URL and request options
5 export const sendReq = async (graphqlURL, opts) => {
6 try {
7 let res = await fetch(graphqlURL, {
8 method: "POST",
9 // fetch options
10 ...opts,
11 });
12 let result = await res.json();
13 console.log(result.errors);
14 // Handle request errors
15 if (result.errors) {
16 result.errors.forEach((error) => alert(error.message));
17 // Throw an error to exit the try block
18 throw Error(JSON.stringify(result.errors));
19 }
20 // save result response to page data state
21 return result.data;
22 } catch (error) {
23 console.log(error);
24 return {
25 errors: error,
26 };
27 }
28 }
Click here to view the code on GitHub
Here, we create a sendReq
function which will be used to send requests and return the data
or errors
. In order to catch errors from the request, we check if result.errors
and then loop through errors
array to alert each error message. Then throw the Error
to exit the try
block to the catch
block.
Now, let's proceed to create our authentication page.
layouts/default.vue
1 <!-- layouts/default.vue -->
2 <template>
3 <div class="layout default">
4 <SiteHeader />
5 <slot />
6 </div>
7 </template>
<SiteHeader/>
component. In a new file components/SiteHeader.vue
enter the following:1 <!-- ./components/SiteHeader.vue -->
2 <script setup>
3 import { RefreshIcon } from "@heroicons/vue/outline";
4 const session = useSession();
5 console.log({ session: session });
6 </script>
7 <template>
8 <header class="site-header">
9 <div class="wrapper">
10 <NuxtLink to="/">
11 <figure class="site-logo"><h1>Photos</h1></figure>
12 </NuxtLink>
13 <nav class="site-nav">
14 <!-- Hide if session state is pending -->
15 <ul v-if="!session.pending" class="links">
16 <!-- Render Register link if no user in session -->
17 <li v-if="!session.data?.user" class="link">
18 <NuxtLink to="/auth/register">
19 <button class="cta">Register</button>
20 </NuxtLink>
21 </li>
22 <!-- Render Sign in link if no user in session -->
23 <li v-if="!session.data?.user" class="link">
24 <NuxtLink to="/auth/sign-in">
25 <button class="cta">Sign in</button>
26 </NuxtLink>
27 </li>
28 <!-- Else, Render Sign out link if user in session -->
29 <li v-else class="link">
30 <NuxtLink to="/auth/sign-out">
31 <button class="cta">Sign out</button>
32 </NuxtLink>
33 </li>
34 </ul>
35 <!-- Display loading if session state is pending -->
36 <div v-else class="cta">
37 <RefreshIcon class="icon stroke animate-rotate" />
38 </div>
39 </nav>
40 </div>
41 </header>
42 </template>
43
44 <style scoped>
45 /* ... */
46 </style>
In the <script>
, we get the session
state from useSession()
which is automatically imported by Nuxt. Using v-if
, we conditionally render the links to our auth pages depending on if session.user
exists or not.
./app.vue
:1 <!-- ./app.vue -->
2 <script setup>
3 import "@/assets/css/main.css";
4 const router = useRouter();
5 const session = useSession();
6 onMounted(() => {
7 // set session from localStorage
8 useSetSession(JSON.parse(localStorage.getItem("session")));
9
10 // route guard to prevent users from routing to
11 // sign in and register page when alrady logged in
12 router.beforeEach((to, from) => {
13 if ((to.path === "/auth/register" || to.path === "/auth/sign-in") && session.value?.jwt) {
14 return false;
15 }
16 });
17 });
18 </script>
19 <template>
20 <NuxtLayout name="default">
21 <!-- Render page depending on route -->
22 <NuxtPage />
23 </NuxtLayout>
24 </template>
In the <script>
, we set the session from localStorage
and use useRouter
to implement a route guard to prevent routing to register
and sign-in
pages when already signed in.
Now, we create our home page ./pages/index.vue
:
1 <!-- ./pages/index.vue -->
2 <script setup>
3 const user = useUser();
4 </script>
5 <template>
6 <main class="home-main">
7 <div class="wrapper">
8 <header>
9 <h1 class="text-4xl font-bold">Hey, {{ user?.username }}</h1>
10 </header>
11 </div>
12 </main>
13 </template>
14 <style scoped>
15 /* ... */
16 </style>
Here, in the <script>
, we simply get the user
state from useUser()
. In the <template/>
, we display the user username.
Next, we’ll create our authentication pages
./pages/auth/register.vue
1 <!-- ./pages/register.vue -->
2 <script setup>
3
4 // retrive GraphQL URL from runtime config
5 const {
6 public: { graphqlURL },
7 } = useRuntimeConfig();
8
9 // session & set session
10 const session= useSession();
11 const setSession = useSetSession;
12
13 // page state
14 const isLoading = ref(false);
15
16 // form state
17 const name = ref("");
18 const email = ref("");
19 const password = ref("");
20
21 // handle form submit
22 const handleRegister = async (e) => {
23 e.preventDefault();
24 if (name && email && password) {
25 isLoading.value = true;
26
27 // GraphQL Register Query
28 let registerQuery = {
29 query: `mutation($username: String!, $email: String!, $password: String!,){
30 register(input: {username: $username, email: $email, password: $password}){
31 jwt
32 user{
33 id
34 username
35 email
36 }
37 }
38 }`,
39 variables: {
40 username: name.value,
41 email: email.value,
42 password: password.value,
43 },
44 };
45
46 try {
47 const { register, errors } = await sendReq(graphqlURL, { body: JSON.stringify(registerQuery), headers: { "Content-Type": "application/json" } });
48
49 // throw error if any
50 if (errors) throw Error(errors);
51
52 // set session, notify and navigate to the home page
53 setSession(register);
54 alert("Registeration successful!");
55 navigateTo("/");
56 } catch (error) {
57 console.log(error);
58 } finally {
59 isLoading.value = false;
60 }
61 }
62 };
63 </script>
64 <template>
65 <main class="site-main register-main">
66 <div class="wrapper">
67 <div class="form-cont">
68 <form @submit="handleRegister" class="form auth-form">
69 <div class="wrapper">
70 <header class="form-header">
71 <h3 class="text-3xl mb-4">Sign Up</h3>
72 </header>
73 <section class="input-section">
74 <div class="form-control">
75 <label for="name">Name</label>
76 <input v-model="name" id="name" name="name" type="text" class="form-input" required />
77 </div>
78 <div class="form-control">
79 <label for="email">Email address</label>
80 <input v-model="email" id="email" name="email" type="email" class="form-input" required />
81 </div>
82 <div class="form-control">
83 <label for="password">Password</label>
84 <input v-model="password" id="password" name="password" type="password" class="form-input" required />
85 </div>
86 <div class="action-cont">
87 <button :disabled="isLoading" class="cta">{{ isLoading ? "..." : "Register" }}</button>
88 </div>
89 </section>
90 </div>
91 </form>
92 </div>
93 </div>
94 </main>
95 </template>
In the <script>
, we have handleRegister()
function which gets called when the registration form is submitted. We get the form data from the name
, email
and password
refs and v-model
on the input elements in the <template>
.
Then, we create a GraphQL mutation
with the data inserted as variables. We’re using the Fetch API to send the request to our Strapi GraphQL endpoint which will be configured in our Nuxt runtimeConfig
.
If the request was succesful, we save the response to session
, if there were errors, we throw
it. These are the values from our query that will be returned:
1 jwt
2 user{
3 id
4 username
5 email
6 }
Next, we’ll create the sign in page.
./pages/auth/sign-in.vue
1 <!-- ./pages/auth/sign-in.vue -->
2 <script setup>
3
4 // retrive GraphQL URL from runtime config
5 const {
6 public: { graphqlURL },
7 } = useRuntimeConfig();
8
9 // session & user state
10 const session = useSession();
11 const setSession = useSetSession;
12
13 // page state
14 const isLoading = ref(false);
15 const data = ref({});
16 const error = ref({});
17
18 // form state
19 const name = ref("");
20 const email = ref("");
21 const password = ref("");
22
23 // handle form submit
24 const handleSignIn = async (e) => {
25 e.preventDefault();
26 if (name && email && password) {
27 // GraphQL Sign in Query
28 let signInQuery = {
29 query: `mutation( $email: String!, $password: String!) {
30 login(input: { identifier: $email, password: $password }) {
31 jwt,
32 user{
33 id
34 username
35 email
36 }
37 }
38 }`,
39 variables: { email: email.value, password: password.value },
40 };
41 try {
42 isLoading.value = true;
43 const { login, errors } = await sendReq(graphqlURL, { body: JSON.stringify(signInQuery), headers: { "Content-Type": "application/json" } });
44 if (errors) throw Error(errors);
45 setSession(login);
46
47 // notify and navigate to the home page
48 alert("Sign in successful!");
49 navigateTo("/");
50 } catch (error) {
51 console.log(error);
52 } finally {
53 isLoading.value = false;
54 }
55 }
56 };
57 </script>
58 <template>
59 <main class="site-main sign-in-main">
60 <div class="wrapper">
61 <div class="form-cont">
62 <form @submit="handleSignIn" class="form auth-form">
63 <div class="wrapper">
64 <header class="form-header">
65 <h3 class="text-3xl mb-4">Sign In</h3>
66 </header>
67 <section class="input-section">
68 <div class="form-control">
69 <label for="email">Email address</label>
70 <input v-model="email" id="email" name="email" type="email" class="form-input" required />
71 </div>
72 <div class="form-control">
73 <label for="password">Password</label>
74 <input v-model="password" id="password" name="password" type="password" class="form-input" required />
75 </div>
76 <div class="action-cont">
77 <button :disabled="isLoading" class="cta">{{ isLoading ? "..." : "Sign in" }}</button>
78 </div>
79 </section>
80 </div>
81 </form>
82 </div>
83 </div>
84 </main>
85 </template>
The sign-in
page is basically the same as the register
page. Except we’re using the signInQuery
which returns the response with login
.
Also, we remove the name
input from the form in the <template>
.
The sign-out
page is pretty simple.
Create a new file ./pages/sign-out.vue
.
1 <!-- ./pages/sign-out.vue -->
2
3 <script setup>
4 onMounted(() => {
5 // set session to null
6 useSetSession(null);
7
8 setTimeout(() => {
9 alert("You've signed out. See you back soon!");
10 navigateTo("/");
11 }, 1000);
12 });
13 </script>
14 <template>
15 <main class="sign-out-main auth-main">
16 <div class="wrapper">
17 <h3 class="text-3xl">Signing Out...</h3>
18 </div>
19 </main>
20 </template>
Here, we simply set the session
state to null
, which in turn sets the localStorage
and user
state to null
.
Now, let’s try to register a new user.
Awesome.
Let’s hop back into our admin dashboard and create a new entry in the Post
collection type with our new user.
Click on SAVE and PUBLISH.
We’ll have to create a Post
component which will be used to display a post.
./components/Post.vue
1 <!-- ./components/Post.vue -->
2 <script setup>
3 // import icons
4 import { ChatIcon, PaperAirplaneIcon } from "@heroicons/vue/solid";
5 // define `post` props
6 defineProps(["post"]);
7 const {
8 public: { strapiURL },
9 } = useRuntimeConfig();
10 // helper function to get username from `post` attributes
11 const getUsername = (post) => post?.attributes?.user.data?.attributes?.username;
12 // helper to get and display date
13 const showDateTime = (post) => new Date(post?.attributes?.updatedAt).toDateString() + " | " + new Date(post?.attributes?.updatedAt).toLocaleTimeString();
14 </script>
15 <template>
16 <li class="post">
17 <header class="post-header">
18 <div class="author-cont">
19 <div class="avatar">
20 <!-- Get first letter in user name -->
21 <p>{{ getUsername(post)?.substr(0, 1) }}</p>
22 </div>
23 <p class="username">{{ getUsername(post) }}</p>
24 </div>
25 <!-- Post options go here -->
26 </header>
27 <ul class="photos">
28 <li v-for="photo in post?.attributes?.photo.data" :key="photo.id" class="photo">
29 <div class="img-cont">
30 <img :src="strapiURL + photo?.attributes?.url" :alt="post?.attributes?.caption" />
31 </div>
32 </li>
33 </ul>
34 <p class="caption">{{ post?.attributes?.caption }}</p>
35 <div class="info-cont text-xs text-gray-600">
36 <div class="time">{{ showDateTime(post) }}</div>
37 </div>
38 <div class="comment-cont">
39 <ChatIcon class="icon solid" />
40 <form class="comment-form">
41 <div class="form-control">
42 <textarea name="comment" id="comment" cols="30" rows="1" class="form-textarea comment-textarea" placeholder="Leave a comment"> </textarea>
43 </div>
44 <button class="cta submit-btn">
45 <PaperAirplaneIcon class="icon solid rotate-90" />
46 </button>
47 </form>
48 </div>
49 </li>
50 </template>
51
52 <style scoped>
53 /* ... */
54 </style>
Here, we define the props
for our component with the defineProps()
method which accepts a props array or object. We also have a few helper functions which help with displaying some information like avatar and date.
Next, let’s modify our home page to fetch the posts and render the component.
Back in our ./pages/index.vue
page,
1 <!-- ./pages/index.vue -->
2
3 <script setup>
4 import { RefreshIcon } from "@heroicons/vue/outline";
5
6 const {
7 public: { graphqlURL, strapiURL },
8 } = useRuntimeConfig();
9
10 const session = useSession();
11 const user = useUser();
12
13 // page state
14 const isLoading = ref(false);
15 const posts = ref({});
16 const error = ref({});
17
18 // function to get posts
19 const getPosts = async (page) => {
20
21 // post query with page and page size variables
22 const postsQuery = {
23 query: `query($page: Int, $pageSize: Int) {
24 posts(pagination: {page: $page, pageSize:$pageSize}, sort: "updatedAt:desc") {
25 data {
26 id
27 attributes {
28 user {
29 data {
30 id
31 attributes {
32 username
33 }
34 }
35 }
36 photo {
37 id
38 data {
39 attributes {
40 url
41 }
42 }
43 }
44 caption
45 updatedAt
46 }
47 }
48 meta {
49 pagination {
50 total
51 pageSize
52 page
53 }
54 }
55 }
56 }
57 `,
58 variables: {
59 page: parseInt(page),
60 pageSize: 10,
61 },
62 };
63 try {
64 let { posts, errors } = await sendReq(graphqlURL, { body: JSON.stringify(postsQuery), headers: { "Content-Type": "application/json" } });
65 if (errors) throw Error(errors);
66 return posts;
67 } catch (error) {
68 console.log({ error });
69 }
70 };
71
72 // Get current route page by destructuring useAsyncData context to get route > query > page
73 const { data } = await useAsyncData("posts", async ({ _route: { query } }) => {
74 let { page } = query;
75 return await getPosts(page);
76 });
77 </script>
As we can see here in our <script>
section, we have an async getPosts()
function which takes page
as a parameter. Within this function, we define the postsQuery
with $page
and $pageSize
variables which determines the page and number of posts in a page we get from our query.
In order to determine the page, within the useAsyncData()
function, we have access to the request context from the ctx
parameter in the useAsyncData(``'``posts``'``, (ctx) => {})
. By destructuring obtain the page
from the route query.
Next, in our <template>
1 <!-- ./pages/index.vue -->
2
3 <template>
4 <main class="site-main home-main">
5 <div class="wrapper">
6 <header>
7 <h1 class="text-4xl font-bold">Hey, {{ user?.username || "Stranga!" }}</h1>
8 </header>
9 <div class="content-wrapper">
10 <aside class="create-post-aside">
11 <div class="wrapper">
12 <header class="px-2 mb-2">
13 <h3 class="font-medium">Create a new post</h3>
14 </header>
15 <!-- Editor goes here -->
16 <!-- Pagination goes here -->
17 </div>
18 </aside>
19 <section class="posts-section">
20 <ul class="posts">
21 <Post v-for="post in data?.data" :key="post.id" :post="post" />
22 </ul>
23 </section>
24 </div>
25 </div>
26 </main>
27 </template>
28 <style scoped>
29 /* ... */
30 </style>
With that, we should have our first post displayed like this:
Next, we’ll create our Editor
component to upload pictures and create posts.
Eventually, our posts will be more than just one and we’ll need a way to navigate through them, let’s quickly create a pagination component for that.
./components/Pagination.vue
1 <!-- ./components/Pagination.vue -->
2
3 <script setup>
4 defineProps(["pagination"]);
5 // helper function to create pagination
6 const getPagination = ({ page, pageSize, total, pageCount }) => {
7 // Get previous page number with `1` as the lowest
8 let prev = Math.abs(page - 1) > 0 ? Math.abs(page - 1) : 1;
9 // Get next page with number of total pages as highest
10 let next = page + 1 < pageCount ? page + 1 : pageCount;
11 // console.log({ pages, prev, next });
12 return {
13 pageCount, page, pageSize, total, prev, next,};
14 };
15 </script>
16 <template>
17 <div class="pagination">
18 <ul class="list">
19 <li>
20 <a :href="`?page=` + getPagination(pagination).prev">
21 <button class="cta">Previous</button>
22 </a>
23 </li>
24 <li v-for="n in getPagination(pagination).pageCount" :key="n">
25 <a :href="`?page=${n}`">
26 <button class="cta">
27 {{ n }}
28 </button>
29 </a>
30 </li>
31 <li>
32 <a :href="`?page=` + getPagination(pagination).next">
33 <button class="cta">Next</button>
34 </a>
35 </li>
36 </ul>
37 </div>
38 </template>
In this component we get the pagination
data as a prop which we defined in the defineProps()
function. Then, we create a getPagination()
helper function which calculates the next
and previous
page numbers. Let’s add it in our ./pages/index.vue
file:
1 <!-- ./pages/index.vue -->
2
3 <template>
4 <main class="site-main home-main">
5 <div class="wrapper">
6 <!-- ... -->
7 <div class="content-wrapper">
8 <aside class="create-post-aside">
9 <div class="wrapper">
10 <!-- ... -->
11 <!-- Editor goes here -->
12 <Pagination :pagination="data?.meta?.pagination" />
13 </div>
14 </aside>
15 <!-- ... -->
16 </div>
17 </div>
18 </main>
19 </template>
Next, we’ll work on the Editor
component to create posts.
In order to create posts, we will need to send authenticated requests with our mutation queries. To create a Post we will need to send two requests to:
To do this:
./components/Editor.vue
.1 <!-- ./components/Editor.vue -->
2
3 <script setup>
4 // import icons
5 import { PlusCircleIcon } from "@heroicons/vue/solid";
6
7 // Get graphqlURL from runtime config
8 const {
9 public: { graphqlURL },
10 } = useRuntimeConfig();
11
12 // init useRouter
13 const router = useRouter();
14
15 // Get `user` & `session` application state
16 const user = useUser();
17 const session = useSession().value;
18
19 // data state
20 const isLoading = ref(false);
21 const data = ref(null);
22 const error = ref({});
23
24 // component states
25 const imagesURL = ref([]);
26 const images = ref({});
27 const fileBtnText = ref("Choose Images");
28 const caption = ref("");
29
30 // header object for fetch request
31 let headersList = {
32 Accept: "*/*",
33 // set authorization token
34 Authorization: `Bearer ${session.data.jwt}`,
35 "Content-Type": "application/json",
36 };
37
38 // function to generate preview URLs for selected images before upload
39 const previewImg = (e) => {
40 // save files to `images` state
41 images.value = e.target.files;
42
43 // create iterable array from `FileList`
44 let files = Array.from(images.value);
45
46 // iterate over `files` array to create temporary URLs
47 // URLs will be used to preview selected images before upload
48 imagesURL.value = [];
49
50 files.forEach((file, i) => {
51 let url = URL.createObjectURL(file);
52 imagesURL.value.push(url);
53 });
54
55 // set upload file button content to [number of] files selected
56 let length = imagesURL.value.length;
57 fileBtnText.value = `${length} file${length != 1 ? "s" : ""} selected`;
58 };
59
60 // function to upload files to Strapi media library and get the uploaded file `ids`
61 const uploadFiles = async () => {
62
63 // mutation to upload multiple files
64 const uploadMultipleMutation = {
65 query: `
66 mutation($files: [Upload]!){
67 multipleUpload(files: $files){
68 data{
69 id
70 }
71 }
72 }
73 `,
74 variables: {
75 // dynamically generate array with null values to match the number of images selected
76 // files: [null, null, null]
77 files: imagesURL.value.map((x) => null),
78 },
79 };
80
81 // image variables map
82 /*
83 {
84 "0": [
85 "variables.files.0"
86 ],
87 "1": [
88 "variables.files.1"
89 ],
90 "2": [
91 "variables.files.2"
92 ]
93 }
94 */
95 let map = {};
96
97 // init `FormData()`
98 const formData = new FormData();
99
100 // append `operations` to FormData(), which conatin the graphQL query
101 formData.append("operations", JSON.stringify(uploadMultipleMutation));
102
103 // create map for variable files
104 for (let i = 0; i < images.value.length; i++) {
105 map[`${i}`] = [`variables.files.${i}`];
106 }
107
108 // add mapp to `FormData()`
109 formData.append("map", JSON.stringify(map));
110
111 // append images to formData
112 // with names corresponding to the keys in `map`
113 for (let i = 0; i < images.value.length; i++) {
114 const image = images.value[i];
115 formData.append(`${i}`, image);
116 }
117
118 try {
119 // send request with `formdata` and authorization headers
120 let { multipleUpload, errors } = await sendReq(graphqlURL, { body: formData, headers: { Authorization: headersList.Authorization } });
121 if (errors) throw Error(errors);
122 console.log(multipleUpload);
123 return multipleUpload;
124 } catch (error) {
125 console.log(error);
126 }
127 };
128
129 // function to create post
130 const createPost = async (e) => {
131 e.preventDefault();
132 isLoading.value = true;
133 // run if images and caption are not null
134 if (images && caption) {
135 // upload images and get the IDs
136 let uploads = await uploadFiles();
137
138 // mutation query for creating posts
139 let createPostQuery = {
140 query: `
141 mutation($data: PostInput!){
142 createPost(data: $data){
143 data{
144 id
145 attributes{
146 caption
147 photo{
148 data{
149 id
150 attributes{
151 url
152 }
153 }
154 }
155 user{
156 data{
157 id
158 }
159 }
160 updatedAt
161 }
162 }
163 }
164 }
165 `,
166 variables: {
167 data: {
168 caption: caption.value,
169 user: user.value.id,
170 // photo: [array of uploaded photo ids]
171 // e.g. photo: [21, 22, 23]
172 photo: uploads.map((file) => file.data.id),
173 publishedAt: new Date(),
174 },
175 },
176 };
177
178 try {
179 // send request to create post
180 const { createPost, errors } = await sendReq(graphqlURL, { body: JSON.stringify(createPostQuery), headers: headersList });
181 if (errors) throw Error(errors);
182
183 // save to state
184 data.value = createPost;
185
186 // reload page
187 window.location.replace("/");
188 } catch (error) {
189 console.log(error);
190 } finally {
191 isLoading.value = false;
192 }
193 }
194 };
195 </script>
196 <template>
197 <form :disabled="isLoading || data" @submit="createPost" class="upload-form">
198 <div class="wrapper">
199 <div class="form-control file">
200 <div class="img-cont upload-img-cont">
201 <!-- Selected image previews -->
202 <ul v-if="imagesURL.length >= 1" class="images">
203 <li v-for="(src, i) in imagesURL" :key="i" class="image">
204 <img :src="src" alt="Image" class="upload-img" />
205 </li>
206 </ul>
207 </div>
208 <label for="image-input" class="file-label" :class="{ 'mt-4': imagesURL.length > 0 }">
209 <span type="button" class="cta file-btn">
210 {{ fileBtnText }}
211 </span>
212 <input
213 :disabled="isLoading || data"
214 @change="previewImg"
215 id="image-input"
216 class="file-input"
217 type="file"
218 accept=".png, .jpg, .jpeg"
219 multiple
220 required
221 />
222 </label>
223 </div>
224 <div class="form-control">
225 <label for="caption">Caption</label>
226 <textarea
227 :disabled="isLoading || data"
228 v-model="caption"
229 name="caption"
230 id="caption"
231 cols="30"
232 rows="1"
233 class="form-textarea caption-textarea"
234 placeholder="Enter you caption"
235 required
236 >
237 </textarea>
238 </div>
239 <div class="action-cont">
240 <button :disabled="isLoading || data" type="submit" class="cta w-icon">
241 <PlusCircleIcon v-if="!data" class="icon solid" />
242 <span v-if="!data">{{ !isLoading ? "Create Post" : "Hang on..." }}</span>
243 <span v-else>Post created! 🚀</span>
244 </button>
245 </div>
246 </div>
247 </form>
248 </template>
249 <style scoped>
250 /* ... */
251 </style>
Let’s go over a few things happening here.
First, we have the previewImg()
function which generates a temporary URL which will be used to display images that have been selected for uploads. It also gets the total number of selected files and updates the fileBtnText
state accordingly.
Next, we have the uploadFiles()
function which does the following:
FormData()
and append the uploadMultipleMutation
query to it with the operations
name.for
loop with each file from the images
array state mapped to it’s index, the map should end up like this:1 // create map for variable files
2 for (let i = 0; i < images.value.length; i++) {
3 map[`${i}`] = [`variables.files.${i}`];
4 }
5
6 // image variables map
7 {
8 "0": [
9 "variables.files.0"
10 ],
11 "1": [
12 "variables.files.1"
13 ],
14 "2": [
15 "variables.files.2"
16 ]
17 }
Then, the map
is appended to formData()
with the "``map``"
name.
Finally, we send the request with formdata
and authorization headers.
After that, we have the createPost()
function which runs on form submit. When fired, the function runs the uploadFiles()
function and gets the image IDs. We also define the createPostQuery
mutatation query where we pass in the caption
, user
, photo
- which is an array of the IDs of the uploaded images and publishedAt
to immediately publish the post once created.
Back in our ./pages/index.vue
homepage, let’s add the <Editor />
component where we added the placeholder comment.
1 <!-- ./pages/index.vue -->
2
3 <template>
4 <main class="site-main home-main">
5 <div class="wrapper">
6 <!-- ... -->
7 <div class="content-wrapper">
8 <aside class="create-post-aside">
9 <div class="wrapper">
10 <!-- ... -->
11 <!-- Render editor if session is not pending -->
12 <div v-if="!session.pending" class="editor-con">
13 <Editor v-if="user?.email" />
14 <div v-else class="unauthenticated-message px-2">
15 <p>Sorry, you have to sign in to post</p>
16 </div>
17 </div>
18 <div v-if="session.pending" :class="{ 'loading-state': session.pending }">
19 <RefreshIcon class="icon stroke animate-rotate" />
20 </div>
21 <Pagination :pagination="data?.meta?.pagination" />
22 </div>
23 </aside>
24 <!-- ... -->
25 </div>
26 </div> </main>
27 </template>
Now, if we try to create a post, we should have something like this:
Splendid. Next, let’s see how we can edit and delete posts.
In order to be able to edit and delete posts, we will have to make a few changes to our current code.
First, let’s add the state that holds the current post to be edited.
In our ./composables/state.js
file:
1 // ./composables/state.js
2 // ...
3
4 // READ CURRENT POST STATE
5 export const usePost = () => useState("post", () => ({}));
6 // SET CURRENT POST STATE
7 export const useSetPost = (data) =>
8 useState("set-post", () => {
9 usePost().value = data;
10 console.log({ data, usePost: usePost().value });
11 });
Next, we have to add the options to edit and delete a post to the
In our ./components/Post.vue
template, we render the options dropdown only when the user.id
matches.
1 <!-- ./components/Post.vue -->
2
3 <template>
4 <li class="post">
5 <header class="post-header">
6 <div class="author-cont">
7 <div class="avatar">
8 <!-- Get first letter in user name -->
9 <p>{{ getUsername(post)?.substr(0, 1) }}</p>
10 </div>
11 <p class="username">{{ getUsername(post) }}</p>
12 </div>
13 <!-- Render if logged in user id matches with post user id -->
14 <div v-if="user.id == getUserID(post)" class="options dropdown">
15 <button class="dropdown-btn">
16 <DotsHorizontalIcon class="icon solid" />
17 </button>
18 <ul class="dropdown-list">
19 <li>
20 <button @click="editPost" class="w-icon w-full">
21 <PencilIcon class="icon solid" />
22 <span>Edit</span>
23 </button>
24 </li>
25 <li>
26 <button @click="deletePost" class="w-icon w-full">
27 <TrashIcon class="icon solid" />
28 <span>Delete</span>
29 </button>
30 </li>
31 </ul>
32 </div>
33 </header>
34 <!-- ... -->
35 </li>
36 </template>
In the <script>
, let’s initialize the current post state and also create the getUserID()
, editPost()
and deletePost()
functions.
1 <!-- ./components/Post.vue -->
2
3 <script setup>
4 // import icons
5 import { ChatIcon, PaperAirplaneIcon, DotsHorizontalIcon, PencilIcon, TrashIcon } from "@heroicons/vue/solid";
6
7 // define `post` props
8 const props = defineProps(["post"]);
9
10 // Get graphqlURL from runtime config
11 const {
12 public: { graphqlURL, strapiURL },
13 } = useRuntimeConfig();
14
15 // Get global session and user state
16 const session = useSession();
17 const user = useUser();
18
19 // Get & Set global curresnt post state
20 const currentPost = usePost();
21 const setCurrentPost = useSetPost;
22
23 // header object for fetch request
24 let headersList = {
25 Accept: "*/*",
26 Authorization: `Bearer ${session.value?.data?.jwt}`,
27 "Content-Type": "application/json",
28 };
29
30 // ... other helper functions
31 // helper function to get username from `post` attributes
32 const getUserID = (post) => post?.attributes?.user.data?.id;
33
34 // function to edit post
35 const editPost = () => {
36 // get post data from props
37 const post = props.post;
38 // set post action to edit
39 post.action = "edit";
40 // set modified post to global state
41 setCurrentPost(post);
42 };
43
44 // function to delete post
45 const deletePost = async () => {
46 // confirm deletion
47 const confirmDelete = confirm("Are you sure you want to delete this post?");
48 // run if user confirms deletion
49 if (confirmDelete) {
50 // mutation query for deleting post
51 let deletePostQuery = {
52 query: `
53 mutation($id: ID!){
54 deletePost(id: $id){
55 data{
56 id
57 }
58 }
59 }
60 `,
61 variables: {
62 id: props.post.id,
63 },
64 };
65 try {
66 // send request to delete post
67 const { deletePost, errors } = await sendReq(graphqlURL, { body: JSON.stringify(deletePostQuery), headers: headersList });
68 if (errors) throw Error(errors);
69
70 // reload page
71 window.location.reload();
72 } catch (error) {
73 console.log(error);
74 } finally {
75 isLoading.value = false;
76 }
77 }
78 };
79
80 </script>
Let’s quickly go over the two functions we just created:
editPost()
- This function simply gets the post
data from the component props
, sets the action
key to "``edit``"
and sets the currentPost
application state to the modified post
. This way, we can set up a watch
function in the <Editor />
component to modify the editor state accordingly.deletePost()
- This function, after the action has been confirmed, sends a request with deletePost
mutation in deletePostQuery
which deletes the post by id
.Now, we have to be able to handle the edit post action from the <Editor />
component.
In our ./components/Editor.vue
file, we have to add a few things.
First, in the <template>
we need to run a different function - handleSubmit()
when the form is submitted. This function will be responsible for determining whether to run the createPost()
or a new editPost()
function.
We also slightly modify the submit button and add a new reset
button which will run the resetAction()
function to reset the component state.
1 <!-- ./components/Editor.vue -->
2
3 <template>
4 <form :disabled="isLoading || data" @submit="handleSubmit" class="upload-form">
5 <div class="wrapper">
6 <!-- ... -->
7 <div class="action-cont">
8 <!-- Submit button -->
9 <button :disabled="isLoading || data" type="submit" class="cta w-icon capitalize">
10 <PlusCircleIcon v-if="!data" class="icon solid" />
11 <span v-if="!data">{{ !isLoading ? `${action} Post` : "Hang on..." }}</span>
12 <span v-else>Successfull! 🚀</span>
13 </button>
14 <!-- Reset button -->
15 <button @click="resetAction" type="button" v-show="action == 'edit'" class="cta w-icon">
16 <XCircleIcon class="icon solid" />
17 <span>Reset</span>
18 </button>
19 </div>
20 </div>
21 </form>
22 </template>
In our <script>
, we’ll first initialize the state variables
1 <!-- ./components/Editor.vue -->
2 <script setup>
3 // import icons
4 import { PlusCircleIcon, XCircleIcon } from "@heroicons/vue/solid";
5
6 // Get graphqlURL & strapiURL from runtime config
7 const {
8 public: { graphqlURL, strapiURL },
9 } = useRuntimeConfig();
10
11 // Get `user` & `session` application state
12 const user = useUser();
13 const session = useSession();
14
15 // Get global current post state
16 const currentPost = usePost();
17 const setCurrentPost = useSetPost;
18
19 // to hold the image data from the current post
20 const currentImages = ref([]);
21
22 // data state
23 const isLoading = ref(false);
24 const data = ref(null);
25
26 // component states
27 const imagesURL = ref([]);
28 const images = ref({});
29 const fileBtnText = ref("Choose Images");
30 const caption = ref("");
31 const action = ref("create");
32
33 // header object for fetch request
34 let headersList = {
35 Accept: "*/*",
36 // set authorization token
37 Authorization: `Bearer ${session.value.data?.jwt}`,
38 "Content-Type": "application/json",
39 };
40
41 // ...
42 </script>
Next, we create the required functions:
1 <!-- ./components/Editor.vue -->
2 <script setup>
3 // ...
4
5 // function to run the create or edit post functions depending on the action
6 const handleSubmit = (e) => {
7 e.preventDefault();
8 action.value == "create" ? createPost() : editPost();
9 };
10
11 // function to create post
12 const createPost = async () => {
13 isLoading.value = true;
14 // run if `images` has no files and caption are not null
15 if (images.value.length > 0 && caption) {
16 // ...
17 }
18 }
19
20 // function to edit post
21 const editPost = async () => {
22 isLoading.value = true;
23 // if caption is not null
24 if (caption) {
25 // set uploads to uploaded files if user selects new files to upload
26 // else, set uploads to images from the current post data
27 let uploads = images.value.length > 0 ? await uploadFiles().then((res) => res.map((file) => file.data)) : currentImages.value;
28
29 // query to update post
30 let updatePostQuery = {
31 query: `
32 mutation($id: ID!, $data: PostInput!) {
33 updatePost(id: $id, data: $data) {
34 data {
35 id
36 attributes {
37 user {
38 data {
39 id
40 }
41 }
42 caption
43 photo {
44 data {
45 id
46 }
47 }
48 }
49 }
50 }
51 }
52 `,
53 variables: {
54 // set id to current post id
55 id: currentPost.value.id,
56 data: {
57 caption: caption.value,
58
59 // photo: [array of uploaded photo ids]
60 // e.g. photo: [21, 22, 23]
61 photo: uploads.map((file) => file.id),
62 },
63 },
64 };
65 try {
66 // send request to update post
67 const { updatePost, errors } = await sendReq(graphqlURL, { body: JSON.stringify(updatePostQuery), headers: headersList });
68
69 if (errors) throw Error(errors);
70
71 // save to state
72 data.value = updatePost;
73 // reload page
74 window.location.replace("/");
75 } catch (error) {
76 console.log(error);
77 } finally {
78 isLoading.value = false;
79 }
80 }
81 };
82 // function to reset action to "create"
83 const resetAction = () => {
84 // set current post to empty object
85 setCurrentPost({});
86 // reset component state
87 action.value = "create";
88 caption.value = "";
89 imagesURL.value = [];
90 };
91
92 // watch for changes in the current post data
93 watch(
94 currentPost,
95 (post) => {
96 // catch block catches errors that occur whenever the state is reset
97 try {
98 // destructure post data
99 const {
100 attributes: { photo, caption: _caption },
101 action: _action,
102 } = post;
103 // generate image URLs and ids from post photo data
104 currentImages.value = photo.data.map((image) => {
105 let src = strapiURL + "" + image.attributes.url;
106 let id = image.id;
107 return { src, id };
108 });
109 // set imagesURL to currentImages `src`
110 imagesURL.value = currentImages.value.map((image) => image.src);
111 // update component state to post data
112 caption.value = _caption;
113 action.value = _action;
114 } catch (error) {
115 console.log(error);
116 }
117 },
118 { deep: true }
119 );
120 </script>
What’s going on here is pretty straightforward. The handleSubmit()
button runs either the createPost()
or editPost()
function depending on the action
.
The editPost()
function if pretty similar to createPost()
except for the fact that the uploads
data is gotten differently.
In the editPost()
function, we see this line of code:
1 // set uploads to uploaded files if user selects new files to upload
2 // else, set uploads to images from the current post data
3 let uploads = images.value.length > 0 ? await uploadFiles().then((res) => res.map((file) => file.data)) : currentImages.value;
With this, if the user selects new images to update the post (which means that images.value
will contain those selected images and will have a length greater than 0), the uploadFiles()
function will upload the files and return an array of image id
s.
If the user does not select any image on the other hand, the uploads
will be equal to the original images of the post.
We also have the watch
function which listens to changes to the currentPost
state and updates the component state accordingly.
🚨 It’s important to note that you’ll have to remove the
required
attribute from the fileinput
so that the form will be able to submit even if the user did not select any new files.
Let’s create a new component for displaying, creating and editing comments for each post. Create a new file - ./components/Comments.vue
and copy over the comment markup from the <Post />
component.
In the <script>
, we’ll create a few functions to get, create, edit and remove comments. First, let’s set up the application state and a few helper functions:
1 <!-- ./components/Comments.vue -->
2
3 <script setup>
4 // import icons
5 import { ChatIcon, PaperAirplaneIcon, XCircleIcon } from "@heroicons/vue/solid";
6 import { RefreshIcon } from "@heroicons/vue/outline";
7
8 // define component props
9 const { post } = defineProps(["post"]);
10
11 // Get graphqlURL from runtime config
12 const {
13 public: { graphqlURL },
14 } = useRuntimeConfig();
15
16 // Get `user` & `session` application state
17 const user = useUser();
18 const session = useSession();
19
20 // component state
21 const comments = ref([]);
22 const comment = ref({});
23 const action = ref("create");
24 const isLoading = ref(false);
25
26 // ref for comment <ul> list item
27 const commentList = ref(null);
28
29 // header object for fetch request
30 let headersList = {
31 Authorization: `Bearer ${session.value?.data?.jwt}`,
32 "Content-Type": "application/json",
33 };
34
35 // helper to get and display date
36 const showDateTime = (dateString) => new Date(dateString).toDateString() + " | " + new Date(dateString).toLocaleTimeString();
37
38 // function to activate edit action
39 const handleEdit = (data) => {
40 comment.value = { ...data };
41 action.value = "edit";
42 };
43
44 // function to reset comment state
45 const resetAction = () => {
46 comment.value = {};
47 action.value = "create";
48 };
49
50 // ...
51
52 </script>
We’re doing a few things here. For the component state, we have:
comments
- Which will be an array of all comments fetched from a post.comment
- An object containing a single comment dataaction
- Which will determine if a comment is to be created or edited when the user clicks the submit button. isLoading
- Component loading stateWe also have a ref
to a DOM element - commentList
, which is the <ul>
elemt which will contain all fetched comments.
A few helper functions we should take note of are:
handleEdit()
- Which sets the comment
value and also sets the action
to "``edit``"
resetAction()
- This resets the comment
value and resets the action
to "``create``"
Next, let’s look at how we can fetch comments.
To get comments, we’ll need to use the findAllFlat
query provided by the Strapi Comments plugin for fetching all comments.
1 <!-- ./components/Comments.vue -->
2
3 <script setup>
4
5 // ...
6
7 // function to get comments
8 const getComments = async () => {
9 // comments query
10 const commentsQuery = {
11 query: `
12 query($relation: String!) {
13 findAllFlat(relation: $relation, sort: "updatedAt:desc") {
14 data {
15 id
16 content
17 approvalStatus
18 updatedAt
19 author {
20 id
21 name
22 }
23 }
24 }
25 }
26 `,
27 variables: {
28 relation: `api::post.post:${post?.id}`,
29 },
30 };
31 try {
32 // send request and return results or errors
33 let { findAllFlat, errors } = await sendReq(graphqlURL, { body: JSON.stringify(commentsQuery), headers: { "Content-Type": "application/json" } });
34 if (errors) throw Error(errors);
35 return findAllFlat;
36 } catch (error) {
37 console.log({ error });
38 }
39 };
40
41 // watch function to update the comments list scroll postion
42 watch(
43 comments,
44 (comments) => {
45 // if commentList element exists
46 if (commentList.value) {
47 setTimeout(() => {
48 // scoll comment list to the top
49 commentList.value.scroll(0, 0);
50 }, 100);
51 }
52 },
53 { deep: true }
54 );
55
56 // fetch comments and save to acomments array
57 comments.value = await getComments();
58 </script>
59
60Here, we get the comments by running `await getComments()` and assigning the result to `comments.value`
61We also have a simple `watch` function which scrolls the `commentList` element to the top whenever the `comments` state changes.
62Let’s take a look at how we can create comments
To create a comment, we’ll have to use the createComment
mutation query. Also, we’ll create a handleSubmit()
function which will either run a create or edit function depending in the current action.
1 <!-- ./components/Comments.vue -->
2
3 <script setup>
4 // ...
5
6 // function to either create or edit comments
7 const handleSubmit = (e) => {
8 e.preventDefault();
9 action.value == "create" ? createComment() : editComment(comment.value?.id);
10 };
11
12 // function to create comments
13 const createComment = async () => {
14 // check if the comment is not empty or just spaces
15 if (comment.value.content.trim()) {
16 isLoading.value = true;
17 // create comment mutation
18 const createCommentQuery = {
19 query: `
20 mutation($input: CreateComment!) {
21 createComment(input: $input) {
22 id
23 content
24 approvalStatus
25 updatedAt
26 author {
27 id
28 name
29 }
30 }
31 }
32 `,
33 variables: {
34 input: { relation: `api::post.post:${post?.id}`, content: comment.value.content.trim() },
35 },
36 };
37 try {
38 // send request and return results or errors
39 let { createComment, errors } = await sendReq(graphqlURL, { body: JSON.stringify(createCommentQuery), headers: headersList });
40 if (errors) throw Error(errors);
41
42 // add created comment to top of comments array
43 comments.value.data = [createComment, ...comments.value.data];
44
45 // reset comment and action state
46 resetAction();
47 } catch (error) {
48 console.log({ error });
49 } finally {
50 isLoading.value = false;
51 }
52 }
53 };
54
55 // ...
56 </script>
Here, you can see that we add the newly created comment createComment
to the comments
array. We then reset the comment state with resetAction()
. Let’s proceed to how we can edit a comment.
This is pretty similar to how we created a comment. Here, we’ll be using the updateComment
mutation query.
1 <!-- ./components/Comments.vue -->
2
3 <script setup>
4 // ...
5
6 // function to edit comment based on it's `id`
7 const editComment = async (id) => {
8 // check if the comment is not empty or just spac
9 if (comment.value.content.trim()) {
10 isLoading.value = true;
11 // update comment mutataion
12 const updateCommentQuery = {
13 query: `
14 mutation($input: UpdateComment!) {
15 updateComment(input: $input) {
16 id
17 content
18 approvalStatus
19 updatedAt
20 author {
21 id
22 name
23 }
24 }
25 }
26 `,
27 variables: {
28 input: { id, relation: `api::post.post:${post?.id}`, content: comment.value.content.trim() },
29 },
30 };
31 try {
32 // send request and return results or errors
33 let { updateComment, errors } = await sendReq(graphqlURL, { body: JSON.stringify(updateCommentQuery), headers: headersList });
34 if (errors) throw Error(errors);
35 // get index of comment to edit
36 let editIndex = comments.value.data.findIndex(({ id }) => id == updateComment.id);
37 // remove comment from array at that index
38 comments.value.data.splice(editIndex, 1);
39 // add edited comment to top of array
40 comments.value.data.unshift(updateComment);
41 // reset comment and action state
42 resetAction();
43 } catch (error) {
44 console.log({ error });
45 } finally {
46 isLoading.value = false;
47 }
48 }
49 };
50
51 // ...
52 </script>
In this case, we don’t simply add the updated comment updateComment
to the comments
array. Instead we get the index - editIndex
by matching the id
. Then we remove the current comment from the array and add the updated comment to the top of the array with the unshift()
method.
Finally, to remove a comment we simply use the removeComment
mutation query.
1 <!-- ./components/Comments.vue -->
2
3 <script setup>
4 // ...
5
6 // function to remove comment
7 const removeComment = async (id) => {
8 // confirm if the user wants to proceed to remove the comment
9 let confirmRemove = confirm("Are you sure you want to remove this comment?");
10 // if confirmed
11 if (confirmRemove) {
12 isLoading.value = true;
13 // remove comment mutation
14 const removeCommentQuery = {
15 query: `
16 mutation($input: RemoveComment!) {
17 removeComment(input: $input) {
18 id
19 content
20 approvalStatus
21 author {
22 id
23 name
24 }
25 }
26 }
27 `,
28 variables: {
29 input: { relation: `api::post.post:${post?.id}`, id },
30 },
31 };
32 try {
33 // reset comment and action state
34 let { removeComment, errors } = await sendReq(graphqlURL, { body: JSON.stringify(removeCommentQuery), headers: headersList });
35 if (errors) throw Error(errors);
36
37 // get index of comment to remove
38 let removeIndex = comments.value.data.findIndex(({ id }) => id == removeComment.id);
39 // remove comment from array
40 comments.value.data.splice(removeIndex, 1);
41 } catch (error) {
42 console.log({ error });
43 } finally {
44 isLoading.value = false;
45 }
46 }
47 };
48
49 // ...
50 </script>
Here, once the comment has been removed, we remove from the comments
array by getting the index - removeIndex
and using the splice()
method to remove it at that index.
Now we can create our template.
1 <template>
2 <div class="comments">
3 <ul ref="commentList" class="comment-list" :class="{ 'border-t': comments.data[0] }">
4 <li v-for="comment in comments?.data" :key="comment.id" class="comment">
5 <main class="body">
6 <p class="author">
7 {{ comment.author?.name }}
8 </p>
9 <pre class="content">{{ comment?.content }}</pre>
10 </main>
11 <footer class="actions">
12 <span class="details"> {{ showDateTime(comment?.updatedAt) }} </span>
13 <div v-if="user?.id == comment.author?.id" class="options">
14 <button @click="() => handleEdit(comment)">Edit</button> |
15 <button @click="() => removeComment(comment.id)" class="hover:text-red-500">Remove</button>
16 </div>
17 </footer>
18 </li>
19 </ul>
20 <div v-if="user?.id" class="comment-cont">
21 <ChatIcon class="icon solid" />
22 <form @submit="handleSubmit" class="comment-form">
23 <div class="form-control">
24 <textarea
25 v-model="comment.content"
26 name="comment"
27 id="comment"
28 cols="30"
29 rows="1"
30 class="form-textarea comment-textarea"
31 placeholder="Leave a comment"
32 required
33 >
34 </textarea>
35 </div>
36 <!-- Reset button -->
37 <button @click="resetAction" type="button" v-show="action == 'edit'" class="cta submit-btn">
38 <XCircleIcon class="icon solid" />
39 </button>
40 <!-- Send Button -->
41 <button :disabled="isLoading" class="cta submit-btn">
42 <PaperAirplaneIcon v-if="!isLoading" class="icon solid rotate-90" />
43 <RefreshIcon v-else class="icon stroke animate-rotate" />
44 </button>
45 </form>
46 </div>
47 </div>
48 </template>
49 <style scoped> /* ... */ </style>
🚨 Take note of the
v-if
's. We’re mostly using the conditionv-if="user?.id"
to display certain elements only when the user is signed in. On the other hand, we use the conditionv-if="user?.id == comment.author?.id"
to render elements only if the signed in user owns that comment.
Back in the <Post />
component, let’s add our new <Comments />
component:
1 <!-- ./components/Post.vue -->
2
3 <template>
4 <li class="post">
5 <header class="post-header">
6 <!-- ... -->
7 </header>
8 <ul class="photos">
9 <!-- ... -->
10 </ul>
11 <article class="details">
12 <h3 class="caption">{{ post?.attributes?.caption }}</h3>
13 <span class="time">{{ showDateTime(post) }}</span>
14 </article>
15 <Comments :post="post" />
16 </li>
17 </template>
To use Cloudinary as a media provider, we have to first set up a Cloudinary account. Go to your Cloudinary Console to obtain API keys.
Let’s head back to our Strapi backend and integrate Cloudinary. Create a new .env
file in the root folder of the project.
1 #.env
2 CLOUDINARY_NAME=<cloudinary_name_here>
3 CLOUDINARY_KEY=<cloudinary_key_here>
4 CLOUDINARY_SECRET=<cloudinary_secret_here>
5 CLOUDINARY_FOLDER=strapi-photos-app
Stop the server and install the @strapi/provider-upload-cloudinary package:
# using yarn
yarn add @strapi/provider-upload-cloudinary
# using npm
npm install @strapi/provider-upload-cloudinary --save
Next, configure the plugin in the ./config/plugins.js
file
1 // ./config/plugins.js
2
3 module.exports = ({ env }) => ({
4 //...
5 upload: {
6 config: {
7 provider: 'cloudinary',
8 providerOptions: {
9 cloud_name: env('CLOUDINARY_NAME'),
10 api_key: env('CLOUDINARY_KEY'),
11 api_secret: env('CLOUDINARY_SECRET'),
12 },
13 actionOptions: {
14 upload: {},
15 uploadStream: {
16 folder: env('CLOUDINARY_FOLDER'),
17 },
18 delete: {},
19 },
20 },
21 },
22 //...
23 });
NOTE: In order to fix the preview issue on the Strapi dashboard where after uploading a photo, it will upload to Cloudinary, but the preview of the photo won’t display on the Strapi admin dashboard, replace
strapi::security
string with the object below in./config/middlewares.js
.
1 // config/middlewares.js
2
3 module.exports = [
4 'strapi::errors',
5 {
6 name: 'strapi::security',
7 config: {
8 contentSecurityPolicy: {
9 useDefaults: true,
10 directives: {
11 'connect-src': ["'self'", 'https:'],
12 'img-src': ["'self'", 'data:', 'blob:', 'dl.airtable.com', 'res.cloudinary.com'],
13 'media-src': ["'self'", 'data:', 'blob:', 'dl.airtable.com', 'res.cloudinary.com'],
14 upgradeInsecureRequests: null,
15 },
16 },
17 },
18 },
19 'strapi::cors',
20 'strapi::poweredBy',
21 'strapi::logger',
22 'strapi::query',
23 'strapi::body',
24 'strapi::session',
25 'strapi::favicon',
26 'strapi::public',
27 ];
Awesome. Restart the Strapi app for the changes to take effect.
Back in our frontend however, we need to make a few changes to make sure that the images display correctly. This is because, up until now, the image URLs returned by Strapi were relative URLs (e.g. /uploads/<file_name>.jpeg
) and we had to append this image URL to the Strapi URL (http://localhost:1337/
) in order to load the image.
This won’t be necessary anymore with Cloudinary integrated. We can now replace any instance of :src="strapiURL + photo?.attributes?.url"
with just :src="photo?.attributes?.url"
.
With Cloudinary integrated, we can now easily deploy our Strapi application to Heroku and not worry about losing the images due to Heroku’s storage. We won’t be covering deployment however as it’s beyond the scope of this tutorial.
In this tutorial we’ve managed to build a photo hsaring app with many of its core functionalities. With this we're able to learn and understand how we can setup a Strapi nstance with a Postgres database instead of the default SQLite, add and and manage our content with GraphQL instead of REST and finally integrate Cloudinary for image uploads. On the frontend, we learnt how to setup an SSR enabled application with Nuxt 3 and communicate with our GraphQL backend using the native Fetch API.
With this we can now see how we can build almost anything using Strapi as a Headless CMS.
Here are a few resources you might find useful.
Are you stuck anywhere? Here are links to the code for this project: