Skip to content

Commit

Permalink
#12: nextJS data-fetching minor steps
Browse files Browse the repository at this point in the history
  • Loading branch information
espen42 committed Aug 26, 2021
1 parent a8adafd commit de5b744
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion next/enonic.connection.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const apiUrl = "http://localhost:8080/site/hmdb/draft/hmdb/api";
export const apiUrlDraft = "http://localhost:8080/site/hmdb/draft/hmdb/api";
export const apiUrlMaster = "http://localhost:8080/site/hmdb/master/hmdb/api";
export const appName = "com.enonic.nextpoc.hmdb"; // <-- appName in hmdb/gradle.properties
export const appNameUnderscored = appName.replace(/\./g, '_');
export const appNameDashed = appName.replace(/\./g, '-');
4 changes: 2 additions & 2 deletions next/src/pages_old/movies/[movieSubPath].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {GetServerSideProps, GetStaticProps} from "next";
import {fetchContentItem} from "../../shared/data";
import {fetchContentGet} from "../../shared/data";
import getMovieQuery, {Movie} from "../../shared/data/queries/getMovie";
import {appNameUnderscored} from "../../../enonic.connection.config";
import MoviePage from "../../components/templates/movie";
Expand All @@ -22,7 +22,7 @@ export default Page;

export const fetchMovie = async (personSubPath): Promise<Movie> => {
const movieQuery = getMovieQuery(appNameUnderscored, personSubPath);
return fetchContentItem(movieQuery);
return fetchContentGet(movieQuery);
}

// SSR
Expand Down
4 changes: 2 additions & 2 deletions next/src/pages_old/persons/[personSubPath].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {GetServerSideProps, GetStaticProps} from "next";
import {fetchContentItem} from "../../shared/data";
import {fetchContentGet} from "../../shared/data";
import getPersonQuery, {Person} from "../../shared/data/queries/getPerson";
import {appNameUnderscored} from "../../../enonic.connection.config";
import PersonPage from "../../components/templates/person";
Expand All @@ -21,7 +21,7 @@ export default Page;

export const fetchPerson = async (personSubPath): Promise<Person> => {
const personQuery = getPersonQuery(appNameUnderscored, personSubPath);
return fetchContentItem(personQuery);
return fetchContentGet(personQuery);
}

// SSR
Expand Down
14 changes: 8 additions & 6 deletions next/src/shared/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { apiUrl } from '../../../enonic.connection.config';
import { apiUrlDraft, apiUrlMaster } from '../../../enonic.connection.config';


type QueryChildrenResult<T> = {
data: {
Expand All @@ -16,7 +17,8 @@ type QueryGetResult<T> = {
};
};

const fetchData = async <T>(query: string) => {
const fetchData = async <T>(query: string, isDraft?: boolean) => {
const apiUrl = isDraft ? apiUrlDraft : apiUrlMaster;
return await fetch(apiUrl, {
method: "post",
body: JSON.stringify({ query, variables: null }),
Expand All @@ -31,10 +33,10 @@ const fetchData = async <T>(query: string) => {
};


export const fetchContentChildren = async <T extends any[]> (query: string): Promise<T> =>
fetchData<QueryChildrenResult<T>>(query)
export const fetchContentChildren = async <T extends any[]> (query: string, isDraft?: boolean): Promise<T> =>
fetchData<QueryChildrenResult<T>>(query, isDraft)
.then(res => res.data.guillotine.getChildren);

export const fetchContentItem = async <T> (query: string): Promise<T> =>
fetchData<QueryGetResult<T>>(query)
export const fetchContentItem = async <T> (query: string, isDraft?: boolean): Promise<T> =>
fetchData<QueryGetResult<T>>(query, isDraft)
.then(res => res.data.guillotine.get);

0 comments on commit de5b744

Please sign in to comment.