Skip to content

Commit 08af5cc

Browse files
committed
Replaced localStorage with sessionStorage
1 parent fe1f5df commit 08af5cc

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambda-feedback-segp-sandbox/sandbox-addon",
3-
"version": "0.6.9",
3+
"version": "0.6.10",
44
"description": "lambda feedback sandbox addon",
55
"keywords": [
66
"storybook-addons",

src/components/Evaluate.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ function parseStoredString(responseString: string): string | any[] | object {
2424
export function Evaluate() {
2525
const [response, updateResponse] = useState<string>("");
2626
const [args, updateArgs, resetArgs] = useArgs();
27+
2728
async function evaluate(url: string, params: JSON) {
28-
const answer = parseStoredString(JSON.stringify(JSON.parse(localStorage.getItem("wizard.input")).answer));
29-
const response = parseStoredString(localStorage.getItem("student.input"));
29+
// Check if sessionStorage items exist before parsing
30+
const wizardInput = sessionStorage.getItem("wizard.input");
31+
const studentInput = sessionStorage.getItem("student.input");
32+
33+
// Safely parse the wizard input if it exists, otherwise use a fallback value
34+
const answer = wizardInput
35+
? parseStoredString(JSON.stringify(JSON.parse(wizardInput).answer))
36+
: { defaultAnswer: '' }; // Fallback value if wizard.input is null
37+
38+
// Safely parse the student input if it exists, otherwise use a fallback value
39+
const response = studentInput ? parseStoredString(studentInput) : { defaultResponse: '' }; // Fallback value if student.input is null
3040

3141
console.log("remote eval");
3242
console.log("url", url);
@@ -44,14 +54,18 @@ export function Evaluate() {
4454
const res = await axios.post("http://localhost:3070", request);
4555
console.log(res);
4656
updateResponse(JSON.stringify(res.data));
47-
const feedback = {isCorrect: res.data.result.is_correct,
48-
isError: res.data.result.is_error ?? false,
49-
feedback: res.data.result.feedback
50-
?? (res.data.result.is_correct ? "Correct" : "Incorrect"),
51-
color: res.data.result.is_correct ? 'green':'red'};
52-
updateArgs({feedback: feedback});
53-
// {homes.map(home => <div>{home.name}</div>)}
57+
58+
const feedback = {
59+
isCorrect: res.data.result.is_correct,
60+
isError: res.data.result.is_error ?? false,
61+
feedback:
62+
res.data.result.feedback ??
63+
(res.data.result.is_correct ? "Correct" : "Incorrect"),
64+
color: res.data.result.is_correct ? "green" : "red",
65+
};
66+
updateArgs({ feedback: feedback });
5467
}
68+
5569
return (
5670
<div style={evaluateStyles.mainDiv}>
5771
<div style={{ width: "100%", display: "table" }}>

src/components/Form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function Form(props: FormProps) {
4848
);
4949

5050
window.addEventListener("storage", () => {
51-
setAnswer(JSON.stringify(JSON.parse(localStorage.getItem("wizard.input")).answer));
51+
setAnswer(JSON.stringify(JSON.parse(sessionStorage.getItem("wizard.input")).answer));
5252
})
5353

5454
return (

src/stories/Input.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IModularResponseSchema } from "@lambda-feedback-segp-sandbox/response-a
99
const initialiseMatrix = (args: any): React.FC<any> => {
1010
return () => {
1111
const [response, _] = useState<IModularResponseSchema | null>(() => {
12-
const storedResponse = localStorage.getItem("wizard.input");
12+
const storedResponse = sessionStorage.getItem("wizard.input");
1313
if (storedResponse) {
1414
try {
1515
const parsedResponse: IModularResponseSchema = JSON.parse(storedResponse);
@@ -34,7 +34,7 @@ const initialiseMatrix = (args: any): React.FC<any> => {
3434
return matrix.InputComponent({
3535
...args,
3636
handleChange: (val: IModularResponseSchema) => {
37-
localStorage.setItem("student.input", JSON.stringify(val));
37+
sessionStorage.setItem("student.input", JSON.stringify(val));
3838
}
3939
});
4040
};
@@ -47,7 +47,7 @@ const InputMeta = {
4747
},
4848
args: {
4949
handleChange: (val: IModularResponseSchema) => {
50-
localStorage.setItem("student.input", JSON.stringify(val));
50+
sessionStorage.setItem("student.input", JSON.stringify(val));
5151
},
5252
handleSubmit: fn(),
5353
},

src/stories/Wizard.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IModularResponseSchema } from "@lambda-feedback-segp-sandbox/response-a
99
const initialiseMatrix = (args: any): React.FC<any> => {
1010
return () => {
1111
const [response, setResponse] = useState<IModularResponseSchema | null>(() => {
12-
const storedResponse = localStorage.getItem("wizard.input");
12+
const storedResponse = sessionStorage.getItem("wizard.input");
1313
if (storedResponse) {
1414
try {
1515
const parsedResponse: IModularResponseSchema = JSON.parse(storedResponse);
@@ -37,7 +37,7 @@ const initialiseMatrix = (args: any): React.FC<any> => {
3737
...args,
3838
handleChange: (val: IModularResponseSchema) => {
3939
if (val && val.config && val.answer) {
40-
localStorage.setItem("wizard.input", JSON.stringify(val));
40+
sessionStorage.setItem("wizard.input", JSON.stringify(val));
4141
setResponse(val); // Update state safely
4242
}
4343
},
@@ -53,7 +53,7 @@ const WizardMeta = {
5353
args: {
5454
handleChange: (val: IModularResponseSchema) => {
5555
if (val && val.config && val.answer) {
56-
localStorage.setItem("wizard.input", JSON.stringify(val));
56+
sessionStorage.setItem("wizard.input", JSON.stringify(val));
5757
}
5858
},
5959
handleSubmit: fn(),

src/stories/input-wrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function wrapInput<P>(Input: React.FC<P>): React.FC<any> {
1515
} else {
1616
responseString = newResponse?.toString() ?? ''
1717
}
18-
localStorage.setItem('student.input', responseString)
18+
sessionStorage.setItem('student.input', responseString)
1919
}}
2020
/>
2121
)

0 commit comments

Comments
 (0)