Skip to content

Commit 650a4d8

Browse files
authored
Merge pull request #3 from editmodelabs/fallback_content_for_missing_variable
Modify content flow with variables
2 parents 9e66e6e + fbc9978 commit 650a4d8

File tree

13 files changed

+136
-17
lines changed

13 files changed

+136
-17
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Chunk.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { useChunk } from "./useChunk";
66
export function Chunk({ children, identifier, src, contentKey, ...props }) {
77
const type = src ? "image" : undefined;
88
const defaultContent = src || children;
9-
const { Component } = useChunk(defaultContent, { identifier, type, contentKey });
9+
const variables = props.variables;
10+
const { Component } = useChunk(defaultContent, { identifier, type, contentKey, variables });
1011

1112
return <Component {...props} />;
1213
}

src/ChunkCollection.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import React, { useEffect, useState } from "react";
33

44
import { ChunkCollectionContext } from "./ChunkCollectionContext";
5-
import { api } from './utils'
6-
import { getCachedData, storeCache } from './utils/native'
5+
import { api } from './utilities'
6+
import { getCachedData, storeCache } from './utilities'
77

88
export function ChunkCollection({
99
children,

src/ChunkFieldValue.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useContext } from "react";
22

3-
import { renderChunk } from "./utils/native";
3+
import { renderChunk } from "./utilities";
44
import { ChunkCollectionContext } from "./ChunkCollectionContext";
55

66
export function ChunkFieldValue({ children, identifier, ...props }) {

src/useChunk.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import { useContext, useEffect, useState, useMemo } from "react";
33

44
import { EditmodeContext } from "./EditmodeContext";
5-
import { api, computeContentKey } from './utils'
6-
import { renderChunk, getCachedData, storeCache } from './utils/native'
5+
import { api, computeContentKey } from './utilities'
6+
import { renderChunk, getCachedData, storeCache, sanitizeContent } from './utilities'
77

8-
export function useChunk(defaultContent = "", { identifier, type, contentKey }) {
8+
export function useChunk(defaultContent = "", { identifier, type, contentKey, variables }) {
99
const { projectId, defaultChunks } = useContext(EditmodeContext);
1010
const [chunk, setChunk] = useState(undefined);
1111

@@ -34,11 +34,20 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey })
3434
useEffect(() => {
3535
// Render content
3636
(async () =>{
37-
const cachedChunk = await getCachedData(cacheId);
38-
const newChunk = cachedChunk ? JSON.parse(cachedChunk) : fallbackChunk || {
39-
chunk_type: type || "single_line_text",
40-
content: defaultContent,
41-
content_key: contentKey
37+
let cachedChunk = await getCachedData(cacheId)
38+
let newChunk;
39+
if (cachedChunk) {
40+
let dataChunk = JSON.parse(cachedChunk);
41+
const parsedChunk = sanitizeContent(dataChunk, variables, fallbackChunk)
42+
newChunk = parsedChunk;
43+
} else if (fallbackChunk) {
44+
newChunk = fallbackChunk;
45+
} else {
46+
newChunk = {
47+
chunk_type: type || "single_line_text",
48+
content: defaultContent,
49+
content_key: contentKey
50+
}
4251
}
4352

4453
if (newChunk) setChunk(newChunk)
@@ -49,7 +58,10 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey })
4958
.get(url)
5059
.then((res) => {
5160
storeCache(cacheId, res.data)
52-
if (!cachedChunk) setChunk(res.data)
61+
if (!cachedChunk) {
62+
const parsedChunk = sanitizeContent(res.data, variables, fallbackChunk)
63+
setChunk(parsedChunk)
64+
}
5365
}) // Store chunk to localstorage
5466
.catch((error) => console.log(error)); // Set error state
5567

src/utilities/api.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import axios from "axios";
2+
3+
export const api = axios.create({
4+
baseURL: "https://api.editmode.com/",
5+
headers: {
6+
Accept: "application/json",
7+
},
8+
});

src/utilities/caching.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import AsyncStorage from '@react-native-community/async-storage';
2+
import { Platform } from 'react-native';
3+
4+
export const getCachedData = Platform.OS === 'web'
5+
? (id) => localStorage.getItem(id)
6+
: async (id) => {
7+
try {
8+
return await AsyncStorage.getItem(id);
9+
} catch (error) {
10+
console.error('Error in fetching chunk.', error);
11+
}
12+
};
13+
14+
export const storeCache = Platform.OS === 'web'
15+
? (id, data) => localStorage.setItem(id, JSON.stringify(data))
16+
: async (id, data) => {
17+
try {
18+
await AsyncStorage.setItem(
19+
id,
20+
JSON.stringify(data)
21+
);
22+
} catch (error) {
23+
console.error('Error in saving chunk.', error);
24+
}
25+
};

src/utilities/computeContentKey.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hash from "@emotion/hash";
2+
import kebabCase from "lodash.kebabcase";
3+
4+
export function computeContentKey(content) {
5+
if (typeof content !== "string") {
6+
console.error(
7+
`Cannot compute chunk.content_key. Expected a string, received ${typeof content}.`
8+
);
9+
return;
10+
}
11+
12+
return `${kebabCase(content.slice(0, 32))}-${hash(content)}`;
13+
}

src/utilities/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { getCachedData, storeCache } from './caching'
2+
export { computeContentKey } from './computeContentKey'
3+
export { sanitizeContent } from './sanitizeContent'
4+
export { api } from './api'
5+
6+
export { renderChunk } from './renderChunk.jsx'

0 commit comments

Comments
 (0)