Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/microsoft/pxt into srietk…
Browse files Browse the repository at this point in the history
…erk-nested-validators
  • Loading branch information
srietkerk committed Feb 10, 2024
2 parents 318c70d + 38ed80e commit d6149e4
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 89 deletions.
File renamed without changes.
File renamed without changes.
36 changes: 0 additions & 36 deletions teachertool/src/services/localStorage.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,33 @@ import { ErrorCode } from "../types/errorCode";
import { logError } from "./loggingService";
import { Rubric } from "../types/rubric";

// ----------------------------------
// Local Storage (for simple key -> value mappings of small data)
// ----------------------------------

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;
}

function setValue(key: string, val: string) {
localStorage.setItem(key, val);
}

function delValue(key: string) {
localStorage.removeItem(key);
}

// ----------------------------------
// Indexed DB (for storing larger, structured data)
// ----------------------------------

const teacherToolDbName = "makecode-project-insights";
const dbVersion = 1;
const rubricsStoreName = "rubrics";
const metadataStoreName = "metadata";
const metadataKeys = {
lastActiveRubricKey: "lastActiveRubricName",
};

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

class TeacherToolDb {
db: IDBPDatabase | undefined;
Expand All @@ -21,7 +39,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 +81,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 +100,79 @@ const getDb = (async () => {
return db;
})();

export async function getLastActiveRubricAsync(): Promise<Rubric | undefined> {
async function saveRubricToIndexedDbAsync(rubric: Rubric) {
const db = await getDb;
await db.saveRubric(rubric);
}

async function deleteRubricFromIndexedDbAsync(name: string) {
const db = await getDb;
await db.deleteRubric(name);
}

// ----------------------------------
// Exports
// ----------------------------------

export function getAutorun(): boolean {
try {
return getValue(AUTORUN_KEY, "false") === "true";
} catch (e) {
logError(ErrorCode.localStorageReadError, e);
return false;
}
}

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

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);
}
}

export async function getRubric(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 getLastActiveRubricAsync(): Promise<Rubric | undefined> {
const lastActiveRubricName = getLastActiveRubricName();
return await getRubric(lastActiveRubricName);
}

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

export async function deleteRubricAsync(name: string) {
const db = await getDb;
await db.deleteRubric(name);
await deleteRubricFromIndexedDbAsync(name);

if (getLastActiveRubricName() === name) {
setLastActiveRubricName("");
}
}
4 changes: 2 additions & 2 deletions teachertool/src/state/appStateContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AppState, initialAppState } from "./state";
import { Action } from "./actions";
import reducer from "./reducer";
import assert from "assert";
import * as LocalStorage from "../services/localStorage";
import { getAutorun } from "../services/storageService";

let state: AppState;
let dispatch: React.Dispatch<Action>;
Expand Down Expand Up @@ -41,7 +41,7 @@ export function AppStateProvider(props: React.PropsWithChildren<{}>): React.Reac
// Create the application state and state change mechanism (dispatch)
const [state_, dispatch_] = useReducer(reducer, {
...initialAppState,
autorun: LocalStorage.getAutorun(),
autorun: getAutorun(),
flags: {
...initialAppState.flags,
testCatalog,
Expand Down
6 changes: 2 additions & 4 deletions teachertool/src/transforms/addCriteriaToRubric.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { stateAndDispatch } from "../state";
import * as Actions from "../state/actions";
import { getCatalogCriteriaWithId } from "../state/helpers";
import { logDebug, logError } from "../services/loggingService";
import { CriteriaInstance, CriteriaParameterValue } from "../types/criteria";
import { nanoid } from "nanoid";
import { ErrorCode } from "../types/errorCode";
import * as AutorunService from "../services/autorunService";
import { setRubric } from "./setRubric";

export function addCriteriaToRubric(catalogCriteriaIds: string[]) {
const { state: teacherTool, dispatch } = stateAndDispatch();
Expand Down Expand Up @@ -45,8 +44,7 @@ export function addCriteriaToRubric(catalogCriteriaIds: string[]) {
newRubric.criteria.push(criteriaInstance);
}

dispatch(Actions.setRubric(newRubric));
AutorunService.poke();
setRubric(newRubric);

pxt.tickEvent("teachertool.addcriteria", {
ids: JSON.stringify(catalogCriteriaIds),
Expand Down
6 changes: 2 additions & 4 deletions teachertool/src/transforms/removeCriteriaFromRubric.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { stateAndDispatch } from "../state";
import * as Actions from "../state/actions";
import { logDebug } from "../services/loggingService";
import { CriteriaInstance } from "../types/criteria";
import * as AutorunService from "../services/autorunService";
import { setRubric } from "./setRubric";

export function removeCriteriaFromRubric(instance: CriteriaInstance) {
const { state: teacherTool, dispatch } = stateAndDispatch();
Expand All @@ -14,8 +13,7 @@ export function removeCriteriaFromRubric(instance: CriteriaInstance) {
criteria: teacherTool.rubric.criteria.filter(c => c.instanceId !== instance.instanceId),
};

dispatch(Actions.setRubric(newRubric));
AutorunService.poke();
setRubric(newRubric);

pxt.tickEvent("teachertool.removecriteria", { catalogCriteriaId: instance.catalogCriteriaId });
}
4 changes: 2 additions & 2 deletions teachertool/src/transforms/setAutorun.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { stateAndDispatch } from "../state";
import * as Actions from "../state/actions";
import * as LocalStorage from "../services/localStorage";
import * as Storage from "../services/storageService";
import * as AutorunService from "../services/autorunService";

export function setAutorun(autorun: boolean) {
const { dispatch } = stateAndDispatch();
dispatch(Actions.setAutorun(autorun));
LocalStorage.setAutorun(autorun);
Storage.setAutorun(autorun);
AutorunService.poke();
}
6 changes: 3 additions & 3 deletions teachertool/src/transforms/setRubricName.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { stateAndDispatch } from "../state";
import * as Actions from "../state/actions";
import { setRubric } from "./setRubric";

export function setRubricName(name: string) {
const { state: teacherTool, dispatch } = stateAndDispatch();
const { state: teacherTool } = stateAndDispatch();

const oldName = teacherTool.rubric.name;

Expand All @@ -14,5 +14,5 @@ export function setRubricName(name: string) {
...teacherTool.rubric,
name,
};
dispatch(Actions.setRubric(newRubric));
setRubric(newRubric);
}
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 "../services/storageService";
import { logDebug } from "../services/loggingService";
import { showToast } from "./showToast";
import { makeToast } 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 "../services/storageService";
import { Rubric } from "../types/rubric";

export async function updateStoredRubricAsync(oldRubric: Rubric | undefined, newRubric: Rubric | undefined) {
Expand Down

0 comments on commit d6149e4

Please sign in to comment.