From c541baf179ff4630fcf9b675ca04fbe224e8fb03 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 1 Feb 2022 17:09:08 +0100 Subject: [PATCH] fix(client): update invalid stored code (#44437) * fix: repair invalid stored code --- .../Challenges/redux/code-storage-epic.js | 35 ++++++++++++++++--- .../learn/challenges/code-storage.js | 27 +++++++------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/client/src/templates/Challenges/redux/code-storage-epic.js b/client/src/templates/Challenges/redux/code-storage-epic.js index 46c2023afb..0d409e1c9e 100644 --- a/client/src/templates/Challenges/redux/code-storage-epic.js +++ b/client/src/templates/Challenges/redux/code-storage-epic.js @@ -121,8 +121,6 @@ function saveCodeEpic(action$, state$) { ); } -// TODO: Ignored temporarily while we update the code storage UI -// eslint-disable-next-line no-unused-vars function loadCodeEpic(action$, state$) { return action$.pipe( ofType(actionTypes.challengeMounted), @@ -143,16 +141,45 @@ function loadCodeEpic(action$, state$) { if (codeFound && isFilesAllPoly(codeFound)) { finalFiles = challengeFiles.reduce((challengeFiles, challengeFile) => { let foundChallengeFile = {}; + // TODO: after sufficient time, say 6 months from this commit, we can + // assume that the majority of users have revisited any pages with old + // stored code. At this point we can remove everything related to + // indexjsCode. + let indexjsCode = null; if (Array.isArray(codeFound)) { foundChallengeFile = codeFound.find( x => x.fileKey === challengeFile.fileKey ); + indexjsCode = codeFound.find(x => x.fileKey === 'indexjs'); } else { // TODO: After sufficient time, remove parsing of old code-storage format // This was pushed to production with https://github.com/freeCodeCamp/freeCodeCamp/pull/43023 foundChallengeFile = codeFound[challengeFile.fileKey]; + indexjsCode = codeFound['indexjs']; + } + let isCodeFound; + // Fix the format of the old file + if (indexjsCode) { + indexjsCode.fileKey = 'scriptjs'; + delete indexjsCode.key; + indexjsCode.history = ['script.js']; + indexjsCode.name = 'script'; + indexjsCode.path = 'script.js'; + } + + if (foundChallengeFile) { + isCodeFound = Object.keys(foundChallengeFile).length > 0; + } else if (indexjsCode) { + isCodeFound = Object.keys(indexjsCode).length > 0; + foundChallengeFile = indexjsCode; + // Repair the store, by replacing old style code with the repaired + // file + store.set(id, [indexjsCode]); + } else { + // The stored code is neither old code nor new, so we do not know + // how to handle it. The safest option is to delete it. + store.remove(id); } - const isCodeFound = Object.keys(foundChallengeFile).length > 0; return [ ...challengeFiles, { @@ -175,4 +202,4 @@ function loadCodeEpic(action$, state$) { ); } -export default combineEpics(saveCodeEpic, clearCodeEpic); +export default combineEpics(saveCodeEpic, loadCodeEpic, clearCodeEpic); diff --git a/cypress/integration/learn/challenges/code-storage.js b/cypress/integration/learn/challenges/code-storage.js index 2c1b389444..cdf490f62b 100644 --- a/cypress/integration/learn/challenges/code-storage.js +++ b/cypress/integration/learn/challenges/code-storage.js @@ -19,18 +19,17 @@ describe('Challenge with editor', function () { cy.get('@editor', { timeout: 10000 }).contains(editorContents); }); - // DISABLED until we update the local storage UI - // it('renders code from localStorage after "Ctrl + S"', () => { - // const editorContents = `

Hello

`; - // cy.get(selectors.editor).as('editor').contains(editorContents); - // cy.get('@editor') - // .click() - // .focused() - // .type(`{movetoend}

Hello World

{ctrl+s}`); - // cy.contains("Saved! Your code was saved to your browser's local storage."); - // cy.reload(); - // cy.get('@editor', { timeout: 10000 }).contains( - // '

Hello

Hello World

' - // ); - // }); + it('renders code from localStorage after "Ctrl + S"', () => { + const editorContents = `

Hello

`; + cy.get(selectors.editor).as('editor').contains(editorContents); + cy.get('@editor') + .click() + .focused() + .type(`{movetoend}

Hello World

{ctrl+s}`); + cy.contains("Saved! Your code was saved to your browser's local storage."); + cy.reload(); + cy.get('@editor', { timeout: 10000 }).contains( + '

Hello

Hello World

' + ); + }); });