Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Editmode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import React from "react";
import { EditmodeContext } from "./EditmodeContext";

export function Editmode({ children, projectId, defaultChunks }) {
export function Editmode({ children, projectId, defaultChunks, branchId }) {
if (!projectId) {
throw new Error("<Editmode projectId={...}> is missing a valid projectId");
}

let branch;
let branch = branchId;

return (
<EditmodeContext.Provider value={{ branch, projectId, defaultChunks }}>
Expand Down
17 changes: 6 additions & 11 deletions src/useChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { api, computeContentKey } from './utilities'
import { renderChunk, getCachedData, storeCache, sanitizeContent } from './utilities'

export function useChunk(defaultContent = "", { identifier, type, contentKey, variables }) {
const { projectId, defaultChunks } = useContext(EditmodeContext);
const { projectId, defaultChunks, branch } = useContext(EditmodeContext);
const [chunk, setChunk] = useState(undefined);

if (!contentKey) {
Expand All @@ -29,8 +29,6 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey, va
);
}

const url = `chunks/${identifier || contentKey}?project_id=${projectId}`;

useEffect(() => {
// Render content
(async () =>{
Expand All @@ -46,7 +44,8 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey, va
if (newChunk) setChunk(sanitizeContent(newChunk, variables, fallbackChunk))

// Fetch new data
let error;
const branchParams = branch ? `&branch_id=${branch}` : "";
const url = `chunks/${identifier || contentKey}?project_id=${projectId}${branchParams}`;
api
.get(url)
.then((res) => {
Expand All @@ -56,13 +55,9 @@ export function useChunk(defaultContent = "", { identifier, type, contentKey, va
setChunk(parsedChunk)
}
}) // Store chunk to localstorage
.catch((error) => console.log(error)); // Set error state

if (error && identifier) {
console.warn(
`Something went wrong trying to retrieve chunk data: ${error}. Have you provided the correct Editmode identifier (${identifier}) as a prop to your Chunk component instance?`
);
}
.catch((error) => {
console.warn(`Something went wrong trying to retrieve chunk data: ${error}. Have you provided the correct Editmode identifier (${identifier || contentKey}) as a prop to your Chunk component instance?`)
}); // Set error state
})();
}, [cacheId]);

Expand Down