placementt-core 1.20.217 → 11.0.803
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +40 -40
- package/.gitattributes +2 -2
- package/lib/constants.js +10 -1
- package/lib/constants.js.map +1 -1
- package/lib/features/config.d.ts +133 -133
- package/lib/features/config.js +35 -35
- package/lib/features/contacts/contacts.d.ts +75 -75
- package/lib/features/contacts/contacts.js +105 -105
- package/lib/features/downtime/useDowntime.d.ts +11 -11
- package/lib/features/downtime/useDowntime.js +22 -22
- package/lib/features/placements/studentPlacements/studentPlacementsSlice.d.ts +63 -63
- package/lib/features/placements/studentPlacements/studentPlacementsSlice.js +81 -81
- package/lib/features/providerPlacements/providerPlacementsSlice.d.ts +19 -19
- package/lib/features/providerPlacements/providerPlacementsSlice.js +24 -24
- package/lib/features/studentPlacements/studentPlacementsSlice.d.ts +62 -62
- package/lib/features/studentPlacements/studentPlacementsSlice.js +87 -87
- package/lib/features/studentPlacements/useStudentPlacements.d.ts +6 -6
- package/lib/features/studentPlacements/useStudentPlacements.js +18 -18
- package/lib/features/userSlice.d.ts +26 -26
- package/lib/features/userSlice.js +23 -23
- package/lib/features/users/useUserFunctions.d.ts +25 -25
- package/lib/features/users/useUserFunctions.js +124 -124
- package/lib/features/users/userSlice.d.ts +46 -46
- package/lib/features/users/userSlice.js +48 -48
- package/lib/firebase/firebase.d.ts +1 -1
- package/lib/firebase/firebase.js +6 -2
- package/lib/firebase/firebase.js.map +1 -1
- package/lib/firebase/readDatabase.js +3 -1
- package/lib/firebase/readDatabase.js.map +1 -1
- package/lib/hooks.d.ts +33 -5
- package/lib/hooks.js +143 -107
- package/lib/hooks.js.map +1 -1
- package/lib/images/GatsbyBenchmarks.d.ts +1 -2
- package/lib/reduxHooks.d.ts +1 -66
- package/lib/reduxHooks.js +9 -69
- package/lib/reduxHooks.js.map +1 -1
- package/lib/tasksAndTips.d.ts +2 -2
- package/lib/tasksAndTips.js +37 -6
- package/lib/tasksAndTips.js.map +1 -1
- package/lib/typeDefinitions.d.ts +50 -5
- package/lib/util.d.ts +1 -1
- package/lib/util.js +12 -1
- package/lib/util.js.map +1 -1
- package/package.json +52 -56
- package/src/DatabaseDefinitions.ts +18 -18
- package/src/apiCalls.ts +128 -128
- package/src/config.ts +50 -50
- package/src/constants.ts +796 -787
- package/src/databaseTypes.ts +42 -42
- package/src/features/analytics/useAnalytics.tsx +63 -63
- package/src/features/contacts/contactsSlice.ts +147 -147
- package/src/features/contacts/useContacts.tsx +73 -73
- package/src/features/dropdown/useDropdown.tsx +52 -52
- package/src/features/global/downtime/useDowntime.tsx +23 -23
- package/src/features/global/users/useUserFunctions.tsx +132 -132
- package/src/features/jobs/jobsSlice.ts +71 -71
- package/src/features/placements/studentPlacements/activePlacement.ts +68 -68
- package/src/features/placements/studentPlacements/completedStudentPlacementsSlice.ts +97 -97
- package/src/features/placements/studentPlacements/upcomingStudentPlacementsSlice.ts +108 -108
- package/src/features/placements/studentPlacements/useStudentPlacements.tsx +9 -9
- package/src/features/placements/types.ts +10 -10
- package/src/features/referrals/useReferrals.tsx +56 -56
- package/src/features/updates/useUpdates.tsx +38 -38
- package/src/firebase/firebase.tsx +149 -145
- package/src/firebase/firebaseConfig.tsx +45 -45
- package/src/firebase/firebaseQuery.tsx +151 -151
- package/src/firebase/persistence.ts +84 -84
- package/src/firebase/readDatabase.tsx +236 -235
- package/src/firebase/util.tsx +352 -352
- package/src/firebase/writeDatabase.tsx +77 -77
- package/src/hooks.tsx +4353 -4323
- package/src/images/GatsbyBenchmarks.tsx +711 -711
- package/src/images/LogFuturePlacement.jsx +64 -64
- package/src/images/LogPreviousPlacement.jsx +228 -228
- package/src/images/gatsby_benchmarks.svg +466 -466
- package/src/images/log_future_placement.svg +114 -114
- package/src/images/log_previous_placement.svg +199 -199
- package/src/index.ts +34 -34
- package/src/readDatabase.tsx +3 -3
- package/src/reduxHooks.ts +232 -297
- package/src/tasksAndTips.ts +1209 -1177
- package/src/tutorialTips.ts +58 -58
- package/src/typeDefinitions.ts +1003 -958
- package/src/util.ts +160 -150
- package/tsconfig.dev.json +5 -5
- package/tsconfig.json +21 -22
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
import {addDoc, collection, CollectionReference, deleteDoc, doc,
|
|
2
|
-
DocumentData, DocumentReference, FieldValue, getCountFromServer, getDoc,
|
|
3
|
-
getDocs, onSnapshot, Query, query, QueryConstraint, QueryDocumentSnapshot, setDoc, updateDoc, where} from "firebase/firestore";
|
|
4
|
-
import {SetStateAction} from "react";
|
|
5
|
-
import {db} from "./firebaseConfig";
|
|
6
|
-
import {pathToArr} from "./util";
|
|
7
|
-
import {Products} from "../typeDefinitions";
|
|
8
|
-
import { AppDispatch } from "../config";
|
|
9
|
-
|
|
10
|
-
export default class FirebaseQuery {
|
|
11
|
-
private createQuery(queryType:"collection"|"doc", path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
|
|
12
|
-
switch (queryType) {
|
|
13
|
-
case "collection":
|
|
14
|
-
return query(collection(db, ...(pathToArr(path) as [any])), ...[...(constraints ? Array.isArray(constraints) ? constraints : [constraints] : [])]);
|
|
15
|
-
case "doc":
|
|
16
|
-
return doc(db, ...(pathToArr(path) as [any]));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async getCount(path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint):Promise<number> {
|
|
21
|
-
console.log("GET COUNT", path, constraints);
|
|
22
|
-
const countObject = await getCountFromServer(this.createQuery("collection", path, constraints) as Query).catch((e) => console.log(e));
|
|
23
|
-
if (!countObject) return 0;
|
|
24
|
-
return (countObject).data().count;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async add(path:string[]|string, data:{[key:string]:unknown}) {
|
|
28
|
-
if (path.length % 2 === 0) {
|
|
29
|
-
throw new Error("Provide a collection reference");
|
|
30
|
-
}
|
|
31
|
-
return await addDoc(this.createQuery("collection", path) as CollectionReference, data);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async set(path:string[]|string, data:{[key:string]:unknown}, merge=false) {
|
|
35
|
-
if (path.length % 2 === 1) {
|
|
36
|
-
throw new Error("Provide a document reference");
|
|
37
|
-
}
|
|
38
|
-
return await setDoc(this.createQuery("doc", path) as DocumentReference, data, {merge: merge});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async update(path:string[]|string, data:{ [key: string]: unknown}) {
|
|
42
|
-
if (path.length % 2 === 1) {
|
|
43
|
-
throw new Error("Provide a document reference");
|
|
44
|
-
}
|
|
45
|
-
console.log("update", path, data);
|
|
46
|
-
return await updateDoc(this.createQuery("doc", path) as DocumentReference, data as { [key: string]: FieldValue | Partial<unknown> | undefined});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async delete(path:string[]|string) {
|
|
50
|
-
if (path.length % 2 === 1) {
|
|
51
|
-
throw new Error("Provide a document reference");
|
|
52
|
-
}
|
|
53
|
-
await deleteDoc(this.createQuery("doc", path) as DocumentReference);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async copy(path: string[]|string, id: string, oId: string, product: Products, additionalData?: {[key: string]: unknown}) {
|
|
57
|
-
const existing = await this.getDocData([...path, id]);
|
|
58
|
-
console.log("existing", existing);
|
|
59
|
-
existing.name = await this.getUniqueDocName(path, existing.name, oId, product);
|
|
60
|
-
if (existing.default) {
|
|
61
|
-
existing.default = false;
|
|
62
|
-
}
|
|
63
|
-
return await this.add(path, {...existing, ...additionalData});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async getUniqueDocName(path: string[]|string, name: string, oId: string, product: Products): Promise<string> {
|
|
67
|
-
const q = this.createQuery("collection", path, [where("name", "==", name), where("oId", "==", oId), where("product", "==", product)]);
|
|
68
|
-
|
|
69
|
-
const querySnapshot = await getDocs(q as Query);
|
|
70
|
-
if (!querySnapshot.empty) {
|
|
71
|
-
return this.getUniqueDocName(path, name += "-", oId, product);
|
|
72
|
-
}
|
|
73
|
-
return name;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async getDocData(path:string[]|string) {
|
|
77
|
-
console.log("GET DOC", path);
|
|
78
|
-
if (path.length % 2 === 1) {
|
|
79
|
-
throw new Error("Provide a document reference");
|
|
80
|
-
}
|
|
81
|
-
const result = await getDoc(this.createQuery("doc", path) as DocumentReference).catch((error) => {
|
|
82
|
-
console.log(`Error in snapshot ${path}`, error);
|
|
83
|
-
throw error;
|
|
84
|
-
});
|
|
85
|
-
if (!result.exists()) {
|
|
86
|
-
throw new Error(`Document doesn't exist. ${typeof path === "string" ? path : path.join(", ")}`);
|
|
87
|
-
}
|
|
88
|
-
const resultData = result.data() || {};
|
|
89
|
-
resultData.id = result.id;
|
|
90
|
-
return resultData;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async getDocsWhere(path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, rawDocs=false) {
|
|
94
|
-
const q = this.createQuery("collection", path, constraints);
|
|
95
|
-
|
|
96
|
-
const docs = await getDocs(q as Query).catch((e) => console.log(e, q));
|
|
97
|
-
if (rawDocs) return docs;
|
|
98
|
-
if (!docs) return {};
|
|
99
|
-
console.log("GET DOCS WHERE", docs.size, path, constraints);
|
|
100
|
-
|
|
101
|
-
return docs.docs.reduce((acc: {[k: string]: any}, document) => {
|
|
102
|
-
const docData = {...document.data(), id: document.id};
|
|
103
|
-
acc[document.id] = docData;
|
|
104
|
-
return acc;
|
|
105
|
-
}, {});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async collectionUpdate(data:{[key:string]:unknown}, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
|
|
109
|
-
if (path.length % 2 === 0) {
|
|
110
|
-
throw new Error("Provide a collection reference");
|
|
111
|
-
}
|
|
112
|
-
const q = this.createQuery("collection", path, constraints) as Query;
|
|
113
|
-
(await getDocs(q)).docs.forEach((doc) => {
|
|
114
|
-
this.update(doc.ref.path.split("/"), data);
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
documentSnapshot(path: string[]|string, setState: SetStateAction<any>) {
|
|
119
|
-
console.log("Building snapshot query: ", path);
|
|
120
|
-
const q = this.createQuery("doc", path) as DocumentReference;
|
|
121
|
-
console.log("Query created");
|
|
122
|
-
return onSnapshot(q, (doc) => {
|
|
123
|
-
console.log("Data", doc.data());
|
|
124
|
-
setState({...doc.data(), id: doc.id});
|
|
125
|
-
}, (error) => {
|
|
126
|
-
console.log(`Error in snapshot ${path}`, error);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
collectionSnapshot(setState:SetStateAction<any>|((e: DocumentData|{[key: string]: any}) => void), path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, dispatch?: AppDispatch, raw?: boolean) {
|
|
131
|
-
const q = this.createQuery("collection", path, constraints) as Query<DocumentData>;
|
|
132
|
-
|
|
133
|
-
return onSnapshot(q, (querySnapshot: DocumentData) => {
|
|
134
|
-
if (raw) {
|
|
135
|
-
setState(querySnapshot);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
const itemList: {[key: string]: any} = {};
|
|
139
|
-
|
|
140
|
-
querySnapshot.docs.forEach((doc: QueryDocumentSnapshot) => {
|
|
141
|
-
const item = doc.data();
|
|
142
|
-
item.id = doc.id;
|
|
143
|
-
itemList[doc.id] = item;
|
|
144
|
-
});
|
|
145
|
-
dispatch ? dispatch(setState(itemList)) : setState(itemList);
|
|
146
|
-
}, (error) => {
|
|
147
|
-
console.log(`Error in snapshot ${path}`, error);
|
|
148
|
-
console.log(constraints)
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
1
|
+
import {addDoc, collection, CollectionReference, deleteDoc, doc,
|
|
2
|
+
DocumentData, DocumentReference, FieldValue, getCountFromServer, getDoc,
|
|
3
|
+
getDocs, onSnapshot, Query, query, QueryConstraint, QueryDocumentSnapshot, setDoc, updateDoc, where} from "firebase/firestore";
|
|
4
|
+
import {SetStateAction} from "react";
|
|
5
|
+
import {db} from "./firebaseConfig";
|
|
6
|
+
import {pathToArr} from "./util";
|
|
7
|
+
import {Products} from "../typeDefinitions";
|
|
8
|
+
import { AppDispatch } from "../config";
|
|
9
|
+
|
|
10
|
+
export default class FirebaseQuery {
|
|
11
|
+
private createQuery(queryType:"collection"|"doc", path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
|
|
12
|
+
switch (queryType) {
|
|
13
|
+
case "collection":
|
|
14
|
+
return query(collection(db, ...(pathToArr(path) as [any])), ...[...(constraints ? Array.isArray(constraints) ? constraints : [constraints] : [])]);
|
|
15
|
+
case "doc":
|
|
16
|
+
return doc(db, ...(pathToArr(path) as [any]));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async getCount(path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint):Promise<number> {
|
|
21
|
+
console.log("GET COUNT", path, constraints);
|
|
22
|
+
const countObject = await getCountFromServer(this.createQuery("collection", path, constraints) as Query).catch((e) => console.log(e));
|
|
23
|
+
if (!countObject) return 0;
|
|
24
|
+
return (countObject).data().count;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async add(path:string[]|string, data:{[key:string]:unknown}) {
|
|
28
|
+
if (path.length % 2 === 0) {
|
|
29
|
+
throw new Error("Provide a collection reference");
|
|
30
|
+
}
|
|
31
|
+
return await addDoc(this.createQuery("collection", path) as CollectionReference, data);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async set(path:string[]|string, data:{[key:string]:unknown}, merge=false) {
|
|
35
|
+
if (path.length % 2 === 1) {
|
|
36
|
+
throw new Error("Provide a document reference");
|
|
37
|
+
}
|
|
38
|
+
return await setDoc(this.createQuery("doc", path) as DocumentReference, data, {merge: merge});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async update(path:string[]|string, data:{ [key: string]: unknown}) {
|
|
42
|
+
if (path.length % 2 === 1) {
|
|
43
|
+
throw new Error("Provide a document reference");
|
|
44
|
+
}
|
|
45
|
+
console.log("update", path, data);
|
|
46
|
+
return await updateDoc(this.createQuery("doc", path) as DocumentReference, data as { [key: string]: FieldValue | Partial<unknown> | undefined});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async delete(path:string[]|string) {
|
|
50
|
+
if (path.length % 2 === 1) {
|
|
51
|
+
throw new Error("Provide a document reference");
|
|
52
|
+
}
|
|
53
|
+
await deleteDoc(this.createQuery("doc", path) as DocumentReference);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async copy(path: string[]|string, id: string, oId: string, product: Products, additionalData?: {[key: string]: unknown}) {
|
|
57
|
+
const existing = await this.getDocData([...path, id]);
|
|
58
|
+
console.log("existing", existing);
|
|
59
|
+
existing.name = await this.getUniqueDocName(path, existing.name, oId, product);
|
|
60
|
+
if (existing.default) {
|
|
61
|
+
existing.default = false;
|
|
62
|
+
}
|
|
63
|
+
return await this.add(path, {...existing, ...additionalData});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async getUniqueDocName(path: string[]|string, name: string, oId: string, product: Products): Promise<string> {
|
|
67
|
+
const q = this.createQuery("collection", path, [where("name", "==", name), where("oId", "==", oId), where("product", "==", product)]);
|
|
68
|
+
|
|
69
|
+
const querySnapshot = await getDocs(q as Query);
|
|
70
|
+
if (!querySnapshot.empty) {
|
|
71
|
+
return this.getUniqueDocName(path, name += "-", oId, product);
|
|
72
|
+
}
|
|
73
|
+
return name;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async getDocData(path:string[]|string) {
|
|
77
|
+
console.log("GET DOC", path);
|
|
78
|
+
if (path.length % 2 === 1) {
|
|
79
|
+
throw new Error("Provide a document reference");
|
|
80
|
+
}
|
|
81
|
+
const result = await getDoc(this.createQuery("doc", path) as DocumentReference).catch((error) => {
|
|
82
|
+
console.log(`Error in snapshot ${path}`, error);
|
|
83
|
+
throw error;
|
|
84
|
+
});
|
|
85
|
+
if (!result.exists()) {
|
|
86
|
+
throw new Error(`Document doesn't exist. ${typeof path === "string" ? path : path.join(", ")}`);
|
|
87
|
+
}
|
|
88
|
+
const resultData = result.data() || {};
|
|
89
|
+
resultData.id = result.id;
|
|
90
|
+
return resultData;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async getDocsWhere(path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, rawDocs=false) {
|
|
94
|
+
const q = this.createQuery("collection", path, constraints);
|
|
95
|
+
|
|
96
|
+
const docs = await getDocs(q as Query).catch((e) => console.log(e, q));
|
|
97
|
+
if (rawDocs) return docs;
|
|
98
|
+
if (!docs) return {};
|
|
99
|
+
console.log("GET DOCS WHERE", docs.size, path, constraints);
|
|
100
|
+
|
|
101
|
+
return docs.docs.reduce((acc: {[k: string]: any}, document) => {
|
|
102
|
+
const docData = {...document.data(), id: document.id};
|
|
103
|
+
acc[document.id] = docData;
|
|
104
|
+
return acc;
|
|
105
|
+
}, {});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async collectionUpdate(data:{[key:string]:unknown}, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
|
|
109
|
+
if (path.length % 2 === 0) {
|
|
110
|
+
throw new Error("Provide a collection reference");
|
|
111
|
+
}
|
|
112
|
+
const q = this.createQuery("collection", path, constraints) as Query;
|
|
113
|
+
(await getDocs(q)).docs.forEach((doc) => {
|
|
114
|
+
this.update(doc.ref.path.split("/"), data);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
documentSnapshot(path: string[]|string, setState: SetStateAction<any>) {
|
|
119
|
+
console.log("Building snapshot query: ", path);
|
|
120
|
+
const q = this.createQuery("doc", path) as DocumentReference;
|
|
121
|
+
console.log("Query created");
|
|
122
|
+
return onSnapshot(q, (doc) => {
|
|
123
|
+
console.log("Data", doc.data());
|
|
124
|
+
setState({...doc.data(), id: doc.id});
|
|
125
|
+
}, (error) => {
|
|
126
|
+
console.log(`Error in snapshot ${path}`, error);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
collectionSnapshot(setState:SetStateAction<any>|((e: DocumentData|{[key: string]: any}) => void), path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, dispatch?: AppDispatch, raw?: boolean) {
|
|
131
|
+
const q = this.createQuery("collection", path, constraints) as Query<DocumentData>;
|
|
132
|
+
|
|
133
|
+
return onSnapshot(q, (querySnapshot: DocumentData) => {
|
|
134
|
+
if (raw) {
|
|
135
|
+
setState(querySnapshot);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const itemList: {[key: string]: any} = {};
|
|
139
|
+
|
|
140
|
+
querySnapshot.docs.forEach((doc: QueryDocumentSnapshot) => {
|
|
141
|
+
const item = doc.data();
|
|
142
|
+
item.id = doc.id;
|
|
143
|
+
itemList[doc.id] = item;
|
|
144
|
+
});
|
|
145
|
+
dispatch ? dispatch(setState(itemList)) : setState(itemList);
|
|
146
|
+
}, (error) => {
|
|
147
|
+
console.log(`Error in snapshot ${path}`, error);
|
|
148
|
+
console.log(constraints)
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
import {Persistence, ReactNativeAsyncStorage} from "firebase/auth";
|
|
2
|
-
|
|
3
|
-
export const enum PersistenceType {
|
|
4
|
-
SESSION = "SESSION",
|
|
5
|
-
LOCAL = "LOCAL",
|
|
6
|
-
NONE = "NONE"
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type PersistedBlob = Record<string, unknown>;
|
|
10
|
-
|
|
11
|
-
export interface Instantiator<T> {
|
|
12
|
-
(blob: PersistedBlob): T;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type PersistenceValue = PersistedBlob | string;
|
|
16
|
-
|
|
17
|
-
export const STORAGE_AVAILABLE_KEY = "__sak";
|
|
18
|
-
|
|
19
|
-
export interface StorageEventListener {
|
|
20
|
-
(value: PersistenceValue | null): void;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface PersistenceInternal extends Persistence {
|
|
24
|
-
type: PersistenceType;
|
|
25
|
-
_isAvailable(): Promise<boolean>;
|
|
26
|
-
_set(key: string, value: PersistenceValue): Promise<void>;
|
|
27
|
-
_get<T extends PersistenceValue>(key: string): Promise<T | null>;
|
|
28
|
-
_remove(key: string): Promise<void>;
|
|
29
|
-
_addListener(key: string, listener: StorageEventListener): void;
|
|
30
|
-
_removeListener(key: string, listener: StorageEventListener): void;
|
|
31
|
-
// Should this persistence allow migration up the chosen hierarchy?
|
|
32
|
-
_shouldAllowMigration?: boolean;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function getReactNativePersistence(
|
|
36
|
-
storage: ReactNativeAsyncStorage
|
|
37
|
-
): Persistence {
|
|
38
|
-
// In the _getInstance() implementation (see src/core/persistence/index.ts),
|
|
39
|
-
// we expect each "externs.Persistence" object passed to us by the user to
|
|
40
|
-
// be able to be instantiated (as a class) using "new". That function also
|
|
41
|
-
// expects the constructor to be empty. Since ReactNativeStorage requires the
|
|
42
|
-
// underlying storage layer, we need to be able to create subclasses
|
|
43
|
-
// (closures, esentially) that have the storage layer but empty constructor.
|
|
44
|
-
return class implements PersistenceInternal {
|
|
45
|
-
static type = "LOCAL" as const;
|
|
46
|
-
readonly type: PersistenceType = PersistenceType.LOCAL;
|
|
47
|
-
|
|
48
|
-
async _isAvailable(): Promise<boolean> {
|
|
49
|
-
try {
|
|
50
|
-
if (!storage) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
await storage.setItem(STORAGE_AVAILABLE_KEY, "1");
|
|
54
|
-
await storage.removeItem(STORAGE_AVAILABLE_KEY);
|
|
55
|
-
return true;
|
|
56
|
-
} catch {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
_set(key: string, value: PersistenceValue): Promise<void> {
|
|
62
|
-
return storage.setItem(key, JSON.stringify(value));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
|
|
66
|
-
const json = await storage.getItem(key);
|
|
67
|
-
return json ? JSON.parse(json) : null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
_remove(key: string): Promise<void> {
|
|
71
|
-
return storage.removeItem(key);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
_addListener(_key: string, _listener: StorageEventListener): void {
|
|
75
|
-
// Listeners are not supported for React Native storage.
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
_removeListener(_key: string, _listener: StorageEventListener): void {
|
|
80
|
-
// Listeners are not supported for React Native storage.
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
}
|
|
1
|
+
import {Persistence, ReactNativeAsyncStorage} from "firebase/auth";
|
|
2
|
+
|
|
3
|
+
export const enum PersistenceType {
|
|
4
|
+
SESSION = "SESSION",
|
|
5
|
+
LOCAL = "LOCAL",
|
|
6
|
+
NONE = "NONE"
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type PersistedBlob = Record<string, unknown>;
|
|
10
|
+
|
|
11
|
+
export interface Instantiator<T> {
|
|
12
|
+
(blob: PersistedBlob): T;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type PersistenceValue = PersistedBlob | string;
|
|
16
|
+
|
|
17
|
+
export const STORAGE_AVAILABLE_KEY = "__sak";
|
|
18
|
+
|
|
19
|
+
export interface StorageEventListener {
|
|
20
|
+
(value: PersistenceValue | null): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PersistenceInternal extends Persistence {
|
|
24
|
+
type: PersistenceType;
|
|
25
|
+
_isAvailable(): Promise<boolean>;
|
|
26
|
+
_set(key: string, value: PersistenceValue): Promise<void>;
|
|
27
|
+
_get<T extends PersistenceValue>(key: string): Promise<T | null>;
|
|
28
|
+
_remove(key: string): Promise<void>;
|
|
29
|
+
_addListener(key: string, listener: StorageEventListener): void;
|
|
30
|
+
_removeListener(key: string, listener: StorageEventListener): void;
|
|
31
|
+
// Should this persistence allow migration up the chosen hierarchy?
|
|
32
|
+
_shouldAllowMigration?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getReactNativePersistence(
|
|
36
|
+
storage: ReactNativeAsyncStorage
|
|
37
|
+
): Persistence {
|
|
38
|
+
// In the _getInstance() implementation (see src/core/persistence/index.ts),
|
|
39
|
+
// we expect each "externs.Persistence" object passed to us by the user to
|
|
40
|
+
// be able to be instantiated (as a class) using "new". That function also
|
|
41
|
+
// expects the constructor to be empty. Since ReactNativeStorage requires the
|
|
42
|
+
// underlying storage layer, we need to be able to create subclasses
|
|
43
|
+
// (closures, esentially) that have the storage layer but empty constructor.
|
|
44
|
+
return class implements PersistenceInternal {
|
|
45
|
+
static type = "LOCAL" as const;
|
|
46
|
+
readonly type: PersistenceType = PersistenceType.LOCAL;
|
|
47
|
+
|
|
48
|
+
async _isAvailable(): Promise<boolean> {
|
|
49
|
+
try {
|
|
50
|
+
if (!storage) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
await storage.setItem(STORAGE_AVAILABLE_KEY, "1");
|
|
54
|
+
await storage.removeItem(STORAGE_AVAILABLE_KEY);
|
|
55
|
+
return true;
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_set(key: string, value: PersistenceValue): Promise<void> {
|
|
62
|
+
return storage.setItem(key, JSON.stringify(value));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
|
|
66
|
+
const json = await storage.getItem(key);
|
|
67
|
+
return json ? JSON.parse(json) : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_remove(key: string): Promise<void> {
|
|
71
|
+
return storage.removeItem(key);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_addListener(_key: string, _listener: StorageEventListener): void {
|
|
75
|
+
// Listeners are not supported for React Native storage.
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
_removeListener(_key: string, _listener: StorageEventListener): void {
|
|
80
|
+
// Listeners are not supported for React Native storage.
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|