Skip to content

Commit

Permalink
190 login to save only logs in (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
danhalson authored Dec 13, 2024
1 parent 7604f9c commit 8925ee8
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

### Fixed

- Log in to save and log in now preserve the cache (#1137)

## [0.28.12] - 2024-12-09

### Fixed
Expand Down
1 change: 1 addition & 0 deletions src/components/SaveButton/SaveButton.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe("When project is loaded", () => {
expect(logInHandler).toHaveBeenCalled();
});
});

describe("with webComponent=false", () => {
let store;

Expand Down
6 changes: 2 additions & 4 deletions src/containers/WebComponentLoader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const WebComponentLoader = (props) => {
hostStyles, // Pass in styles from the host
identifier,
instructions,
theme,
loadRemixDisabled = false,
outputOnly = false,
outputPanels = ["text", "visual"],
Expand All @@ -47,10 +48,10 @@ const WebComponentLoader = (props) => {
senseHatAlwaysEnabled = false,
showSavePrompt = false,
sidebarOptions = [],
theme,
useEditorStyles = false, // If true use the standard editor styling for the web component
withProjectbar = false,
withSidebar = false,
loadCache = true, // Always use cache unless explicitly disabled
} = props;
const dispatch = useDispatch();
const { t } = useTranslation();
Expand All @@ -60,7 +61,6 @@ const WebComponentLoader = (props) => {
? JSON.parse(localStorage.getItem(authKey))
: null;
const user = useSelector((state) => state.auth.user || localStorageUser);
const [loadCache, setLoadCache] = useState(!!!user);
const [loadRemix, setLoadRemix] = useState(!!user);
const project = useSelector((state) => state.editor.project);
const projectOwner = useSelector((state) => state.editor.project.user_name);
Expand Down Expand Up @@ -99,10 +99,8 @@ const WebComponentLoader = (props) => {

useEffect(() => {
if (remixLoadFailed) {
setLoadCache(true);
setLoadRemix(false);
} else {
setLoadCache(!!!user);
setLoadRemix(!!user);
}
}, [user, project, remixLoadFailed]);
Expand Down
7 changes: 4 additions & 3 deletions src/containers/WebComponentLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ describe("When no user is in state", () => {
instructions={instructions}
authKey={authKey}
theme="light"
loadCache={true}
/>
</CookiesProvider>
</Provider>,
Expand All @@ -328,7 +329,7 @@ describe("When no user is in state", () => {
code,
accessToken: "my_token",
loadRemix: true,
loadCache: false,
loadCache: true,
remixLoadFailed: false,
reactAppApiEndpoint: "http://localhost:3009",
});
Expand Down Expand Up @@ -456,7 +457,7 @@ describe("When user is in state", () => {
code: undefined,
accessToken: "my_token",
loadRemix: true,
loadCache: false,
loadCache: true,
remixLoadFailed: false,
reactAppApiEndpoint: "http://localhost:3009",
});
Expand Down Expand Up @@ -486,7 +487,7 @@ describe("When user is in state", () => {
code: undefined,
accessToken: "my_token",
loadRemix: false,
loadCache: false,
loadCache: true,
remixLoadFailed: false,
reactAppApiEndpoint: "http://localhost:3009",
});
Expand Down
34 changes: 11 additions & 23 deletions src/hooks/useProjectPersistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,24 @@ export const useProjectPersistence = ({
useEffect(() => {
const saveProject = async () => {
if (Object.keys(project).length !== 0) {
if (saveTriggered || (user && localStorage.getItem("awaitingSave"))) {
const identifier = project?.identifier;
const accessToken = user?.access_token;
const params = { reactAppApiEndpoint, accessToken };

if (saveTriggered) {
if (isOwner(user, project)) {
dispatch(
syncProject("save")({
reactAppApiEndpoint,
project,
accessToken: user.access_token,
autosave: false,
}),
);
} else if (user && project.identifier) {
await dispatch(
syncProject("remix")({
reactAppApiEndpoint,
project,
accessToken: user.access_token,
}),
syncProject("save")({ ...params, project, autosave: false }),
);
// Ensure the remixed project is loaded, otherwise we'll get in a mess
} else if (user && identifier) {
await dispatch(syncProject("remix")({ ...params, project }));
if (loadRemix) {
dispatch(
syncProject("loadRemix")({
reactAppApiEndpoint,
identifier: project.identifier,
accessToken: user.access_token,
}),
// Ensure the remixed project is loaded, otherwise we'll get in a mess
await dispatch(
syncProject("loadRemix")({ ...params, identifier }),
);
}
}
localStorage.removeItem("awaitingSave");
}
}
};
Expand Down
44 changes: 0 additions & 44 deletions src/hooks/useProjectPersistence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,6 @@ describe("When logged in", () => {
});
});

describe("When project has identifier and awaiting save", () => {
beforeEach(() => {
localStorage.setItem("awaitingSave", "true");
syncProject.mockImplementationOnce(jest.fn((_) => remixProject));
syncProject.mockImplementationOnce(jest.fn((_) => loadProject));
renderHook(() =>
useProjectPersistence({
user: user2,
project: project,
}),
);
jest.runAllTimers();
});

test("Project remixed and saved to database", () => {
expect(remixProject).toHaveBeenCalledWith({
project,
accessToken: user2.access_token,
});
});
});

describe("When project has identifier and save triggered", () => {
beforeEach(() => {
syncProject.mockImplementationOnce(jest.fn((_) => remixProject));
Expand Down Expand Up @@ -277,28 +255,6 @@ describe("When logged in", () => {
});
});

describe("When project has no identifier and awaiting save", () => {
beforeEach(() => {
localStorage.setItem("awaitingSave", "true");
syncProject.mockImplementationOnce(jest.fn((_) => saveProject));
renderHook(() =>
useProjectPersistence({
user: user2,
project: { ...project, identifier: null },
}),
);
jest.runAllTimers();
});

test("Project saved to database", () => {
expect(saveProject).toHaveBeenCalledWith({
project: { ...project, identifier: null },
accessToken: user2.access_token,
autosave: false,
});
});
});

describe("When project has no identifier and save triggered", () => {
beforeEach(() => {
syncProject.mockImplementationOnce(jest.fn((_) => saveProject));
Expand Down
1 change: 0 additions & 1 deletion src/web-component.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"download",
"settings",
"info",
"use_editor_styles"
]),
);

Expand Down
2 changes: 2 additions & 0 deletions src/web-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class WebComponent extends HTMLElement {
"use_editor_styles",
"with_projectbar",
"with_sidebar",
"load_cache",
];
}

Expand All @@ -86,6 +87,7 @@ class WebComponent extends HTMLElement {
"use_editor_styles",
"with_projectbar",
"with_sidebar",
"load_cache",
].includes(name)
) {
value = newVal !== "false";
Expand Down

0 comments on commit 8925ee8

Please sign in to comment.