Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get item from localStorage only when the cache is enabled #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
85 changes: 44 additions & 41 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,62 @@ const getData = (params, callback) => {
const renderedSteps = [steps[currentStep.id]];
const previousSteps = [steps[currentStep.id]];
const previousStep = {};
const unParsedCache = localStorage.getItem(cacheName);

if (cache && unParsedCache) {
try {
const data = parse(unParsedCache);
const lastStep = data.renderedSteps[data.renderedSteps.length - 1];

if (lastStep && lastStep.end) {
localStorage.removeItem(cacheName);
} else {
for (let i = 0, len = data.renderedSteps.length; i < len; i += 1) {
const renderedStep = data.renderedSteps[i];
// remove delay of cached rendered steps
data.renderedSteps[i].delay = 0;
// flag used to avoid call triggerNextStep in cached rendered steps
data.renderedSteps[i].rendered = true;
if (cache) {
const unParsedCache = localStorage.getItem(cacheName);
if (unParsedCache) {
try {
const data = parse(unParsedCache);
const lastStep = data.renderedSteps[data.renderedSteps.length - 1];

// an error is thrown when render a component from localStorage.
// So it's necessary reassing the component
if (renderedStep.component) {
const { id } = renderedStep;
data.renderedSteps[i].component = steps[id].component;
}
}
if (lastStep && lastStep.end) {
localStorage.removeItem(cacheName);
} else {
for (let i = 0, len = data.renderedSteps.length; i < len; i += 1) {
const renderedStep = data.renderedSteps[i];
// remove delay of cached rendered steps
data.renderedSteps[i].delay = 0;
// flag used to avoid call triggerNextStep in cached rendered steps
data.renderedSteps[i].rendered = true;

const { trigger, end, options } = data.currentStep;
const { id } = data.currentStep;
// an error is thrown when render a component from localStorage.
// So it's necessary reassing the component
if (renderedStep.component) {
const { id } = renderedStep;
data.renderedSteps[i].component = steps[id].component;
}
}

if (options) {
delete data.currentStep.rendered;
}
const { trigger, end, options } = data.currentStep;
const { id } = data.currentStep;

// add trigger function to current step
if (!trigger && !end) {
if (options) {
for (let i = 0; i < options.length; i += 1) {
data.currentStep.options[i].trigger = steps[id].options[i].trigger;
delete data.currentStep.rendered;
}

// add trigger function to current step
if (!trigger && !end) {
if (options) {
for (let i = 0; i < options.length; i += 1) {
data.currentStep.options[i].trigger = steps[id].options[i].trigger;
}
} else {
data.currentStep.trigger = steps[id].trigger;
}
} else {
data.currentStep.trigger = steps[id].trigger;
}
}

// execute callback function to enable input if last step is
// waiting user type
if (data.currentStep.user) {
callback();
}
// execute callback function to enable input if last step is
// waiting user type
if (data.currentStep.user) {
callback();
}

return data;
return data;
}
} catch (error) {
console.info(`Unable to parse cache named:${cacheName}. \nThe cache where probably created with an older version of react-simple-chatbot.\n`, error);
}
} catch (error) {
console.info(`Unable to parse cache named:${cacheName}. \nThe cache where probably created with an older version of react-simple-chatbot.\n`, error);
}
}

Expand Down