Skip to content

Commit

Permalink
Move Last Active Rubric Name to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
thsparks committed Feb 8, 2024
1 parent 305bf5f commit 5e44ea3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
34 changes: 3 additions & 31 deletions teachertool/src/services/indexedDbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { Rubric } from "../types/rubric";
const teacherToolDbName = "makecode-project-insights";
const dbVersion = 1;
const rubricsStoreName = "rubrics";
const metadataStoreName = "metadata";
const metadataKeys = {
lastActiveRubricKey: "lastActiveRubricName",
};

type MetadataEntry = { key: string; value: any };

Expand All @@ -21,7 +17,6 @@ class TeacherToolDb {
this.db = await openDB(teacherToolDbName, dbVersion, {
upgrade(db) {
db.createObjectStore(rubricsStoreName, { keyPath: "name" });
db.createObjectStore(metadataStoreName, { keyPath: "key" });
},
});
}
Expand Down Expand Up @@ -64,27 +59,6 @@ class TeacherToolDb {
}
}

private async getMetadataEntryAsync(key: string): Promise<MetadataEntry | undefined> {
return this.getAsync<MetadataEntry>(metadataStoreName, key);
}

private async setMetadataEntryAsync(key: string, value: any): Promise<void> {
return this.setAsync<MetadataEntry>(metadataStoreName, { key, value });
}

private async deleteMetadataEntryAsync(key: string): Promise<void> {
return this.deleteAsync(metadataStoreName, key);
}

public async getLastActiveRubricNameAsync(): Promise<string | undefined> {
const metadataEntry = await this.getMetadataEntryAsync(metadataKeys.lastActiveRubricKey);
return metadataEntry?.value;
}

public saveLastActiveRubricNameAsync(name: string): Promise<void> {
return this.setMetadataEntryAsync(metadataKeys.lastActiveRubricKey, name);
}

public getRubric(name: string): Promise<Rubric | undefined> {
return this.getAsync<Rubric>(rubricsStoreName, name);
}
Expand All @@ -104,25 +78,23 @@ const getDb = (async () => {
return db;
})();

export async function getLastActiveRubricAsync(): Promise<Rubric | undefined> {
export async function getRubricFromIndexedDbAsync(name: string): Promise<Rubric | undefined> {
const db = await getDb;

let rubric: Rubric | undefined = undefined;
const name = await db.getLastActiveRubricNameAsync();
if (name) {
rubric = await db.getRubric(name);
}

return rubric;
}

export async function saveRubricAsync(rubric: Rubric) {
export async function saveRubricToIndexedDbAsync(rubric: Rubric) {
const db = await getDb;
await db.saveRubric(rubric);
await db.saveLastActiveRubricNameAsync(rubric.name);
}

export async function deleteRubricAsync(name: string) {
export async function deleteRubricFromIndexedDbAsync(name: string) {
const db = await getDb;
await db.deleteRubric(name);
}
22 changes: 19 additions & 3 deletions teachertool/src/services/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logError } from "./loggingService";

const KEY_PREFIX = "teachertool";
const AUTORUN_KEY = [KEY_PREFIX, "autorun"].join("/");
const LAST_ACTIVE_RUBRIC_KEY = [KEY_PREFIX, "lastActiveRubric"].join("/");

function getValue(key: string, defaultValue?: string): string | undefined {
return localStorage.getItem(key) || defaultValue;
Expand All @@ -16,7 +17,7 @@ function delValue(key: string) {
localStorage.removeItem(key);
}

function getAutorun(): boolean {
export function getAutorun(): boolean {
try {
return getValue(AUTORUN_KEY, "false") === "true";
} catch (e) {
Expand All @@ -25,12 +26,27 @@ function getAutorun(): boolean {
}
}

function setAutorun(autorun: boolean) {
export function setAutorun(autorun: boolean) {
try {
setValue(AUTORUN_KEY, autorun.toString());
} catch (e) {
logError(ErrorCode.localStorageWriteError, e);
}
}

export { getAutorun, setAutorun };
export function getLastActiveRubricName(): string {
try {
return getValue(LAST_ACTIVE_RUBRIC_KEY) ?? "";
} catch (e) {
logError(ErrorCode.localStorageReadError, e);
return "";
}
}

export function setLastActiveRubricName(name: string) {
try {
setValue(LAST_ACTIVE_RUBRIC_KEY, name);
} catch (e) {
logError(ErrorCode.localStorageWriteError, e);
}
}
4 changes: 2 additions & 2 deletions teachertool/src/transforms/tryLoadLastActiveRubricAsync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getLastActiveRubricAsync } from "../services/indexedDbService";
import { logDebug, logError } from "../services/loggingService";
import { getLastActiveRubricAsync } from "../utils/storageHelpers";
import { logDebug } from "../services/loggingService";
import { postNotification } from "./postNotification";
import { makeNotification } from "../utils";
import { setRubric } from "./setRubric";
Expand Down
2 changes: 1 addition & 1 deletion teachertool/src/transforms/updateStoredRubric.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deleteRubricAsync, saveRubricAsync } from "../services/indexedDbService";
import { deleteRubricAsync, saveRubricAsync } from "../utils/storageHelpers";
import { Rubric } from "../types/rubric";

export async function updateStoredRubricAsync(oldRubric: Rubric | undefined, newRubric: Rubric | undefined) {
Expand Down
21 changes: 21 additions & 0 deletions teachertool/src/utils/storageHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Rubric } from "../types/rubric";
import * as LocalStorage from "../services/localStorage";
import * as IndexedDb from "../services/indexedDbService";

export async function getLastActiveRubricAsync(): Promise<Rubric | undefined> {
const lastActiveRubricName = LocalStorage.getLastActiveRubricName();
return await IndexedDb.getRubricFromIndexedDbAsync(lastActiveRubricName);
}

export async function saveRubricAsync(rubric: Rubric) {
await IndexedDb.saveRubricToIndexedDbAsync(rubric);
LocalStorage.setLastActiveRubricName(rubric.name);
}

export async function deleteRubricAsync(name: string) {
await IndexedDb.deleteRubricFromIndexedDbAsync(name);

if (LocalStorage.getLastActiveRubricName() === name) {
LocalStorage.setLastActiveRubricName("");
}
}

0 comments on commit 5e44ea3

Please sign in to comment.