Skip to content
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"postcss-normalize": "8.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "5.0.2",
"prettier": "^2.8.8",
"prettier": "^3.2.4",
"react-dev-utils": "^11.0.3",
"react-test-renderer": "^17.0.2",
"redux-mock-store": "^1.5.4",
Expand Down
4 changes: 4 additions & 0 deletions src/containers/WebComponentLoader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const WebComponentLoader = (props) => {
const {
authKey,
identifier,
codeIdentifier,
assetIdentifier,
code,
senseHatAlwaysEnabled = false,
instructions,
Expand Down Expand Up @@ -94,6 +96,8 @@ const WebComponentLoader = (props) => {

useProject({
projectIdentifier: projectIdentifier,
codeIdentifier,
assetIdentifier,
code,
accessToken: user?.access_token,
loadRemix,
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useTranslation } from "react-i18next";

export const useProject = ({
projectIdentifier = null,
codeIdentifier = null,
assetIdentifier = null,
code = null,
accessToken = null,
loadRemix = false,
Expand Down Expand Up @@ -71,6 +73,18 @@ export const useProject = ({
return;
}

if (codeIdentifier || assetIdentifier) {
dispatch(
syncProject("load")({
codeIdentifier,
assetIdentifier,
locale: i18n.language,
accessToken: accessToken,
}),
);
return;
}

const data = defaultPythonProject;
dispatch(setProject(data));
}
Expand Down
18 changes: 16 additions & 2 deletions src/redux/EditorSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ export const syncProject = (actionName) =>
createAsyncThunk(
`editor/${actionName}Project`,
async (
{ project, identifier, locale, accessToken, autosave },
{
project,
identifier,
codeIdentifier,
assetIdentifier,
locale,
accessToken,
autosave,
},
{ rejectWithValue },
) => {
let response;
switch (actionName) {
case "load":
response = await readProject(identifier, locale, accessToken);
response = await readProject(
identifier,
codeIdentifier,
assetIdentifier,
locale,
accessToken,
);
break;
case "loadRemix":
response = await loadRemix(identifier, accessToken);
Expand Down
65 changes: 60 additions & 5 deletions src/utils/apiCallHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import omit from "lodash/omit";
import { defaultPythonProject } from "./defaultProjects";

const host = process.env.REACT_APP_API_ENDPOINT;

Expand Down Expand Up @@ -68,12 +69,66 @@ export const createRemix = async (project, accessToken) => {
);
};

export const readProject = async (projectIdentifier, locale, accessToken) => {
export const readProject = async (
projectIdentifier,
codeIdentifier,
assetIdentifier,
locale,
accessToken,
) => {
const queryString = locale ? `?locale=${locale}` : "";
return await get(
`${host}/api/projects/${projectIdentifier}${queryString}`,
headers(accessToken),
);

//todo, check if code and asset are same, check if normal identifier, load, merge, return

if (projectIdentifier) {
return await get(
`${host}/api/projects/${projectIdentifier}${queryString}`,
headers(accessToken),
);
} else {
//Load separate code and assets

const loadCode = new Promise(async (resolve, reject) => {
if (!codeIdentifier) return resolve(defaultPythonProject);

const code = await get(
`${host}/api/projects/${codeIdentifier}${queryString}`,
headers(accessToken),
);

resolve({
components: code.data.components,
identifier: code.data.identifier,
locale: code.data.locale,
name: code.data.name,
project_type: code.data.project_type,
user_id: code.data.user_id,
});
});

const loadAssets = new Promise(async (resolve, reject) => {
if (!assetIdentifier) return resolve({ image_list: [] });

const assets = await get(
`${host}/api/projects/${assetIdentifier}/images`,
headers(accessToken),
);

resolve({
image_list: assets.data.image_list,
});
});

const result = await Promise.all([loadCode, loadAssets]).then((res) => {
return res.reduce(function (acc, row) {
return { ...acc, ...row };
}, {});
});

return {
data: result,
};
}
};

export const readProjectList = async (page, accessToken) => {
Expand Down
2 changes: 2 additions & 0 deletions src/web-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class WebComponent extends HTMLElement {
"host_styles",
"auth_key",
"identifier",
"code_identifier",
"asset_identifier",
"code",
"sense_hat_always_enabled",
"instructions",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10938,10 +10938,10 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

prettier@^2.8.8:
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
prettier@^3.2.4:
version "3.2.4"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.4.tgz#4723cadeac2ce7c9227de758e5ff9b14e075f283"
integrity sha512-FWu1oLHKCrtpO1ypU6J0SbK2d9Ckwysq6bHj/uaCP26DxrPpppCLQRGVuqAxSTvhF00AcvDRyYrLNW7ocBhFFQ==

pretty-bytes@^5.3.0, pretty-bytes@^5.6.0:
version "5.6.0"
Expand Down