-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ref } from "vue"; | ||
|
||
export const USE_SPACE_SUBMIT_ANSWER = "useSpaceSubmitAnswer"; | ||
|
||
const useSpace = ref(false); | ||
|
||
function loadCache() { | ||
const value = localStorage.getItem(USE_SPACE_SUBMIT_ANSWER); | ||
if (value === "false") { | ||
store(false); | ||
} else if (!value) { | ||
store(useSpace.value); | ||
} else { | ||
store(true); | ||
} | ||
} | ||
|
||
const store = function (value: boolean) { | ||
useSpace.value = value; | ||
localStorage.setItem(USE_SPACE_SUBMIT_ANSWER, `${value}`); | ||
}; | ||
|
||
const toggleUseSpaceSubmitAnswer = () => { | ||
store(!useSpace.value); | ||
}; | ||
|
||
const isUseSpaceSubmitAnswer = () => useSpace.value; | ||
|
||
export function useSpaceSubmitAnswer() { | ||
loadCache(); | ||
return { | ||
useSpace, | ||
isUseSpaceSubmitAnswer, | ||
toggleUseSpaceSubmitAnswer, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { it, expect, describe, beforeEach } from "vitest"; | ||
import { USE_SPACE_SUBMIT_ANSWER, useSpaceSubmitAnswer } from "../submitKey"; | ||
|
||
describe("auto play sound", () => { | ||
beforeEach(() => { | ||
localStorage.removeItem(USE_SPACE_SUBMIT_ANSWER); | ||
}); | ||
it("should be false if no cache", () => { | ||
const { useSpace } = useSpaceSubmitAnswer(); | ||
expect(useSpace.value).toBe(false); | ||
}); | ||
|
||
it("should be equal to cache value if it exists", () => { | ||
localStorage.setItem(USE_SPACE_SUBMIT_ANSWER, "false"); | ||
const { useSpace } = useSpaceSubmitAnswer(); | ||
expect(useSpace.value).toBe(false); | ||
}); | ||
|
||
it("should be toggle value", () => { | ||
const { useSpace, toggleUseSpaceSubmitAnswer } = useSpaceSubmitAnswer(); | ||
useSpace.value = false | ||
toggleUseSpaceSubmitAnswer(); | ||
expect(useSpace.value).toBe(true); | ||
}); | ||
}); |