Skip to content

Commit 8925ee8

Browse files
authored
190 login to save only logs in (#1152)
Closes RaspberryPiFoundation/editor-standalone#190
1 parent 7604f9c commit 8925ee8

8 files changed

+24
-75
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9+
### Fixed
10+
11+
- Log in to save and log in now preserve the cache (#1137)
12+
913
## [0.28.12] - 2024-12-09
1014

1115
### Fixed

src/components/SaveButton/SaveButton.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ describe("When project is loaded", () => {
149149
expect(logInHandler).toHaveBeenCalled();
150150
});
151151
});
152+
152153
describe("with webComponent=false", () => {
153154
let store;
154155

src/containers/WebComponentLoader.jsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const WebComponentLoader = (props) => {
3737
hostStyles, // Pass in styles from the host
3838
identifier,
3939
instructions,
40+
theme,
4041
loadRemixDisabled = false,
4142
outputOnly = false,
4243
outputPanels = ["text", "visual"],
@@ -47,10 +48,10 @@ const WebComponentLoader = (props) => {
4748
senseHatAlwaysEnabled = false,
4849
showSavePrompt = false,
4950
sidebarOptions = [],
50-
theme,
5151
useEditorStyles = false, // If true use the standard editor styling for the web component
5252
withProjectbar = false,
5353
withSidebar = false,
54+
loadCache = true, // Always use cache unless explicitly disabled
5455
} = props;
5556
const dispatch = useDispatch();
5657
const { t } = useTranslation();
@@ -60,7 +61,6 @@ const WebComponentLoader = (props) => {
6061
? JSON.parse(localStorage.getItem(authKey))
6162
: null;
6263
const user = useSelector((state) => state.auth.user || localStorageUser);
63-
const [loadCache, setLoadCache] = useState(!!!user);
6464
const [loadRemix, setLoadRemix] = useState(!!user);
6565
const project = useSelector((state) => state.editor.project);
6666
const projectOwner = useSelector((state) => state.editor.project.user_name);
@@ -99,10 +99,8 @@ const WebComponentLoader = (props) => {
9999

100100
useEffect(() => {
101101
if (remixLoadFailed) {
102-
setLoadCache(true);
103102
setLoadRemix(false);
104103
} else {
105-
setLoadCache(!!!user);
106104
setLoadRemix(!!user);
107105
}
108106
}, [user, project, remixLoadFailed]);

src/containers/WebComponentLoader.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ describe("When no user is in state", () => {
315315
instructions={instructions}
316316
authKey={authKey}
317317
theme="light"
318+
loadCache={true}
318319
/>
319320
</CookiesProvider>
320321
</Provider>,
@@ -328,7 +329,7 @@ describe("When no user is in state", () => {
328329
code,
329330
accessToken: "my_token",
330331
loadRemix: true,
331-
loadCache: false,
332+
loadCache: true,
332333
remixLoadFailed: false,
333334
reactAppApiEndpoint: "http://localhost:3009",
334335
});
@@ -456,7 +457,7 @@ describe("When user is in state", () => {
456457
code: undefined,
457458
accessToken: "my_token",
458459
loadRemix: true,
459-
loadCache: false,
460+
loadCache: true,
460461
remixLoadFailed: false,
461462
reactAppApiEndpoint: "http://localhost:3009",
462463
});
@@ -486,7 +487,7 @@ describe("When user is in state", () => {
486487
code: undefined,
487488
accessToken: "my_token",
488489
loadRemix: false,
489-
loadCache: false,
490+
loadCache: true,
490491
remixLoadFailed: false,
491492
reactAppApiEndpoint: "http://localhost:3009",
492493
});

src/hooks/useProjectPersistence.js

+11-23
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,24 @@ export const useProjectPersistence = ({
3838
useEffect(() => {
3939
const saveProject = async () => {
4040
if (Object.keys(project).length !== 0) {
41-
if (saveTriggered || (user && localStorage.getItem("awaitingSave"))) {
41+
const identifier = project?.identifier;
42+
const accessToken = user?.access_token;
43+
const params = { reactAppApiEndpoint, accessToken };
44+
45+
if (saveTriggered) {
4246
if (isOwner(user, project)) {
43-
dispatch(
44-
syncProject("save")({
45-
reactAppApiEndpoint,
46-
project,
47-
accessToken: user.access_token,
48-
autosave: false,
49-
}),
50-
);
51-
} else if (user && project.identifier) {
5247
await dispatch(
53-
syncProject("remix")({
54-
reactAppApiEndpoint,
55-
project,
56-
accessToken: user.access_token,
57-
}),
48+
syncProject("save")({ ...params, project, autosave: false }),
5849
);
59-
// Ensure the remixed project is loaded, otherwise we'll get in a mess
50+
} else if (user && identifier) {
51+
await dispatch(syncProject("remix")({ ...params, project }));
6052
if (loadRemix) {
61-
dispatch(
62-
syncProject("loadRemix")({
63-
reactAppApiEndpoint,
64-
identifier: project.identifier,
65-
accessToken: user.access_token,
66-
}),
53+
// Ensure the remixed project is loaded, otherwise we'll get in a mess
54+
await dispatch(
55+
syncProject("loadRemix")({ ...params, identifier }),
6756
);
6857
}
6958
}
70-
localStorage.removeItem("awaitingSave");
7159
}
7260
}
7361
};

src/hooks/useProjectPersistence.test.js

-44
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,6 @@ describe("When logged in", () => {
195195
});
196196
});
197197

198-
describe("When project has identifier and awaiting save", () => {
199-
beforeEach(() => {
200-
localStorage.setItem("awaitingSave", "true");
201-
syncProject.mockImplementationOnce(jest.fn((_) => remixProject));
202-
syncProject.mockImplementationOnce(jest.fn((_) => loadProject));
203-
renderHook(() =>
204-
useProjectPersistence({
205-
user: user2,
206-
project: project,
207-
}),
208-
);
209-
jest.runAllTimers();
210-
});
211-
212-
test("Project remixed and saved to database", () => {
213-
expect(remixProject).toHaveBeenCalledWith({
214-
project,
215-
accessToken: user2.access_token,
216-
});
217-
});
218-
});
219-
220198
describe("When project has identifier and save triggered", () => {
221199
beforeEach(() => {
222200
syncProject.mockImplementationOnce(jest.fn((_) => remixProject));
@@ -277,28 +255,6 @@ describe("When logged in", () => {
277255
});
278256
});
279257

280-
describe("When project has no identifier and awaiting save", () => {
281-
beforeEach(() => {
282-
localStorage.setItem("awaitingSave", "true");
283-
syncProject.mockImplementationOnce(jest.fn((_) => saveProject));
284-
renderHook(() =>
285-
useProjectPersistence({
286-
user: user2,
287-
project: { ...project, identifier: null },
288-
}),
289-
);
290-
jest.runAllTimers();
291-
});
292-
293-
test("Project saved to database", () => {
294-
expect(saveProject).toHaveBeenCalledWith({
295-
project: { ...project, identifier: null },
296-
accessToken: user2.access_token,
297-
autosave: false,
298-
});
299-
});
300-
});
301-
302258
describe("When project has no identifier and save triggered", () => {
303259
beforeEach(() => {
304260
syncProject.mockImplementationOnce(jest.fn((_) => saveProject));

src/web-component.html

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"download",
5353
"settings",
5454
"info",
55-
"use_editor_styles"
5655
]),
5756
);
5857

src/web-component.js

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class WebComponent extends HTMLElement {
6767
"use_editor_styles",
6868
"with_projectbar",
6969
"with_sidebar",
70+
"load_cache",
7071
];
7172
}
7273

@@ -86,6 +87,7 @@ class WebComponent extends HTMLElement {
8687
"use_editor_styles",
8788
"with_projectbar",
8889
"with_sidebar",
90+
"load_cache",
8991
].includes(name)
9092
) {
9193
value = newVal !== "false";

0 commit comments

Comments
 (0)