react-firebase-ql 2.3.0 → 4.0.0
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/dist/client/providers/AuthProvider.provider.d.ts +12 -0
- package/dist/client/providers/AuthProvider.provider.js +45 -0
- package/dist/client/providers/AuthProvider.provider.js.map +1 -0
- package/dist/client/useStream.d.ts +8 -2
- package/dist/client/useStream.js +26 -18
- package/dist/client/useStream.js.map +1 -1
- package/esm/client/providers/AuthProvider.provider.js +41 -0
- package/esm/client/providers/AuthProvider.provider.js.map +1 -0
- package/esm/client/useStream.js +27 -19
- package/esm/client/useStream.js.map +1 -1
- package/package.json +1 -1
- package/src/client/providers/AuthProvider.provider.tsx +94 -0
- package/src/client/useStream.ts +39 -24
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ReactNode, Context } from 'react';
|
|
2
|
+
import { Auth, User } from 'firebase/auth';
|
|
3
|
+
interface AUTHSTATE {
|
|
4
|
+
FBUser?: User;
|
|
5
|
+
}
|
|
6
|
+
export declare const AUTHContext: Context<AUTHSTATE>;
|
|
7
|
+
type AUTHProviderProps = {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
auth: Auth;
|
|
10
|
+
};
|
|
11
|
+
export declare const AUTHProvider: ({ children, auth }: AUTHProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AUTHProvider = exports.AUTHContext = void 0;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const auth_1 = require("firebase/auth");
|
|
8
|
+
const firebase_client_ql_1 = require("firebase-client-ql");
|
|
9
|
+
var AUTHACTIONTYPE;
|
|
10
|
+
(function (AUTHACTIONTYPE) {
|
|
11
|
+
AUTHACTIONTYPE["SETFBUSER"] = "SETFBUSER";
|
|
12
|
+
})(AUTHACTIONTYPE || (AUTHACTIONTYPE = {}));
|
|
13
|
+
const initialAUTHState = {};
|
|
14
|
+
const AUTHReducer = (state, action) => {
|
|
15
|
+
const { type, payload } = action;
|
|
16
|
+
switch (type) {
|
|
17
|
+
case 'SETFBUSER':
|
|
18
|
+
return { ...state, FBUser: payload };
|
|
19
|
+
default:
|
|
20
|
+
return state;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const initialDispatch = () => { };
|
|
24
|
+
exports.AUTHContext = (0, react_1.createContext)(initialAUTHState);
|
|
25
|
+
const AUTHDispatch = (0, react_1.createContext)(initialDispatch);
|
|
26
|
+
const AUTHProvider = ({ children, auth }) => {
|
|
27
|
+
const [AUTHState, dispatch] = (0, react_1.useReducer)(AUTHReducer, initialAUTHState);
|
|
28
|
+
(0, react_1.useEffect)(() => {
|
|
29
|
+
const unsubscribe = (0, auth_1.onAuthStateChanged)(auth, (authUser) => {
|
|
30
|
+
try {
|
|
31
|
+
dispatch({
|
|
32
|
+
type: AUTHACTIONTYPE.SETFBUSER,
|
|
33
|
+
payload: authUser !== null && authUser !== void 0 ? authUser : undefined,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
(0, firebase_client_ql_1.errorLogger)('User Authentication error:', error);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return unsubscribe;
|
|
41
|
+
}, [auth]);
|
|
42
|
+
return ((0, jsx_runtime_1.jsx)(exports.AUTHContext.Provider, { value: AUTHState, children: (0, jsx_runtime_1.jsx)(AUTHDispatch.Provider, { value: dispatch, children: children }) }));
|
|
43
|
+
};
|
|
44
|
+
exports.AUTHProvider = AUTHProvider;
|
|
45
|
+
//# sourceMappingURL=AuthProvider.provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthProvider.provider.js","sourceRoot":"","sources":["../../../src/client/providers/AuthProv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,4BAA4B,EAAE,KAAK,CAAC,CAAC;YACnD,CAAC;QACH,CAAC,CACF,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,OAAO,CACL,uBAAC,mBAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,YACpC,uBAAC,YAAY,CAAC,QAAQ,IAAC,KAAK,EAAE,QAAQ,YACnC,QAAQ,GACa,GACH,CACxB,CAAC;AACJ,CAAC,CAAC;AA/BW,QAAA,YAAY,gBA+BvB"}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { BaseModel, whereClause } from 'firebase-client-ql';
|
|
2
|
+
type UseStreamResult<T> = {
|
|
3
|
+
data: T[];
|
|
4
|
+
loading: boolean;
|
|
5
|
+
error: unknown;
|
|
6
|
+
};
|
|
2
7
|
export declare const useStream: <T extends BaseModel>(param: {
|
|
3
|
-
model:
|
|
8
|
+
model: any;
|
|
4
9
|
where?: whereClause[];
|
|
5
10
|
reference?: string;
|
|
6
11
|
filter?: {
|
|
@@ -10,4 +15,5 @@ export declare const useStream: <T extends BaseModel>(param: {
|
|
|
10
15
|
};
|
|
11
16
|
offset?: string;
|
|
12
17
|
};
|
|
13
|
-
}
|
|
18
|
+
}) => UseStreamResult<T>;
|
|
19
|
+
export {};
|
package/dist/client/useStream.js
CHANGED
|
@@ -4,40 +4,48 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.useStream = void 0;
|
|
5
5
|
const firebase_client_ql_1 = require("firebase-client-ql");
|
|
6
6
|
const react_1 = require("react");
|
|
7
|
-
const useStream = (param
|
|
7
|
+
const useStream = (param) => {
|
|
8
|
+
const [data, setData] = (0, react_1.useState)([]);
|
|
9
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
10
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
8
11
|
(0, react_1.useEffect)(() => {
|
|
9
12
|
let unsubscribe;
|
|
13
|
+
setLoading(true);
|
|
10
14
|
try {
|
|
11
15
|
const { model, reference, where, filter } = param;
|
|
12
16
|
if (reference) {
|
|
13
|
-
unsubscribe = model.stream((
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
unsubscribe = model.stream((result) => {
|
|
18
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
19
|
+
setLoading(false);
|
|
16
20
|
}, reference);
|
|
17
21
|
}
|
|
18
22
|
else if (where) {
|
|
19
|
-
if (where.length > 0) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
23
|
+
if (where.length > 0 && where.every(w => w.value === undefined)) {
|
|
24
|
+
setData([]);
|
|
25
|
+
setLoading(false);
|
|
26
|
+
return;
|
|
25
27
|
}
|
|
26
|
-
unsubscribe = model.streamWhere(where, (
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
unsubscribe = model.streamWhere(where, (result) => {
|
|
29
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
30
|
+
setLoading(false);
|
|
29
31
|
}, 100, filter === null || filter === void 0 ? void 0 : filter.orderBy, filter === null || filter === void 0 ? void 0 : filter.offset);
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
|
-
catch (
|
|
33
|
-
(0, firebase_client_ql_1.errorLogger)('useStream error:',
|
|
34
|
+
catch (err) {
|
|
35
|
+
(0, firebase_client_ql_1.errorLogger)('useStream error:', err);
|
|
36
|
+
setError(err);
|
|
37
|
+
setLoading(false);
|
|
34
38
|
}
|
|
35
39
|
return () => {
|
|
36
|
-
if (typeof unsubscribe === 'function')
|
|
40
|
+
if (typeof unsubscribe === 'function')
|
|
37
41
|
unsubscribe();
|
|
38
|
-
}
|
|
39
42
|
};
|
|
40
|
-
}, [
|
|
43
|
+
}, [
|
|
44
|
+
JSON.stringify(param.where),
|
|
45
|
+
JSON.stringify(param.filter),
|
|
46
|
+
param.reference,
|
|
47
|
+
]);
|
|
48
|
+
return { data, loading, error };
|
|
41
49
|
};
|
|
42
50
|
exports.useStream = useStream;
|
|
43
51
|
//# sourceMappingURL=useStream.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;AAEb,2DAAyE;AACzE,
|
|
1
|
+
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;AAEb,2DAAyE;AACzE,iCAA4C;AAQrC,MAAM,SAAS,GAAG,CACvB,KAQC,EACmB,EAAE;IACtB,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,IAAA,gBAAQ,EAAM,EAAE,CAAC,CAAC;IAC1C,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAC,IAAI,CAAC,CAAC;IAC7C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAU,IAAI,CAAC,CAAC;IAElD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,WAAqC,CAAC;QAC1C,UAAU,CAAC,IAAI,CAAC,CAAC;QAEjB,IAAI,CAAC;YACH,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;YAElD,IAAI,SAAS,EAAE,CAAC;gBACd,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,MAAW,EAAE,EAAE;oBACzC,OAAO,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,CAAC;oBACtB,UAAU,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC,EAAE,SAAS,CAAC,CAAC;YAChB,CAAC;iBACI,IAAI,KAAK,EAAE,CAAC;gBACf,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,SAAS,CAAC,EAAE,CAAC;oBAChE,OAAO,CAAC,EAAE,CAAC,CAAC;oBACZ,UAAU,CAAC,KAAK,CAAC,CAAC;oBAClB,OAAO;gBACT,CAAC;gBAED,WAAW,GAAG,KAAK,CAAC,WAAW,CAC7B,KAAK,EACL,CAAC,MAAW,EAAE,EAAE;oBACd,OAAO,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,CAAC;oBACtB,UAAU,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC,EACD,GAAG,EACH,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EACf,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CACf,CAAC;YACJ,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAA,gCAAW,EAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;YACrC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACd,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QAED,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,WAAW,KAAK,UAAU;gBAAE,WAAW,EAAE,CAAC;QACvD,CAAC,CAAC;IACJ,CAAC,EAAE;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;QAC5B,KAAK,CAAC,SAAS;KAChB,CAAC,CAAC;IAEH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC,CAAC;AA9DW,QAAA,SAAS,aA8DpB"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useReducer, createContext, useEffect, } from 'react';
|
|
4
|
+
import { onAuthStateChanged } from 'firebase/auth';
|
|
5
|
+
import { errorLogger } from 'firebase-client-ql';
|
|
6
|
+
var AUTHACTIONTYPE;
|
|
7
|
+
(function (AUTHACTIONTYPE) {
|
|
8
|
+
AUTHACTIONTYPE["SETFBUSER"] = "SETFBUSER";
|
|
9
|
+
})(AUTHACTIONTYPE || (AUTHACTIONTYPE = {}));
|
|
10
|
+
const initialAUTHState = {};
|
|
11
|
+
const AUTHReducer = (state, action) => {
|
|
12
|
+
const { type, payload } = action;
|
|
13
|
+
switch (type) {
|
|
14
|
+
case 'SETFBUSER':
|
|
15
|
+
return { ...state, FBUser: payload };
|
|
16
|
+
default:
|
|
17
|
+
return state;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const initialDispatch = () => { };
|
|
21
|
+
export const AUTHContext = createContext(initialAUTHState);
|
|
22
|
+
const AUTHDispatch = createContext(initialDispatch);
|
|
23
|
+
export const AUTHProvider = ({ children, auth }) => {
|
|
24
|
+
const [AUTHState, dispatch] = useReducer(AUTHReducer, initialAUTHState);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const unsubscribe = onAuthStateChanged(auth, (authUser) => {
|
|
27
|
+
try {
|
|
28
|
+
dispatch({
|
|
29
|
+
type: AUTHACTIONTYPE.SETFBUSER,
|
|
30
|
+
payload: authUser !== null && authUser !== void 0 ? authUser : undefined,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
errorLogger('User Authentication error:', error);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return unsubscribe;
|
|
38
|
+
}, [auth]);
|
|
39
|
+
return (_jsx(AUTHContext.Provider, { value: AUTHState, children: _jsx(AUTHDispatch.Provider, { value: dispatch, children: children }) }));
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=AuthProvider.provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthProvider.provider.js","sourceRoot":"","sources":["../../../src/client/providers/AuthProvider.provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAE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C;YACL,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,WAAW,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;YACnD,CAAC;QACH,CAAC,CACF,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,OAAO,CACL,KAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,YACpC,KAAC,YAAY,CAAC,QAAQ,IAAC,KAAK,EAAE,QAAQ,YACnC,QAAQ,GACa,GACH,CACxB,CAAC;AACJ,CAAC,CAAC"}
|
package/esm/client/useStream.js
CHANGED
|
@@ -1,39 +1,47 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { errorLogger } from 'firebase-client-ql';
|
|
3
|
-
import { useEffect } from 'react';
|
|
4
|
-
export const useStream = (param
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
export const useStream = (param) => {
|
|
5
|
+
const [data, setData] = useState([]);
|
|
6
|
+
const [loading, setLoading] = useState(true);
|
|
7
|
+
const [error, setError] = useState(null);
|
|
5
8
|
useEffect(() => {
|
|
6
9
|
let unsubscribe;
|
|
10
|
+
setLoading(true);
|
|
7
11
|
try {
|
|
8
12
|
const { model, reference, where, filter } = param;
|
|
9
13
|
if (reference) {
|
|
10
|
-
unsubscribe = model.stream((
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
unsubscribe = model.stream((result) => {
|
|
15
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
16
|
+
setLoading(false);
|
|
13
17
|
}, reference);
|
|
14
18
|
}
|
|
15
19
|
else if (where) {
|
|
16
|
-
if (where.length > 0) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
20
|
+
if (where.length > 0 && where.every(w => w.value === undefined)) {
|
|
21
|
+
setData([]);
|
|
22
|
+
setLoading(false);
|
|
23
|
+
return;
|
|
22
24
|
}
|
|
23
|
-
unsubscribe = model.streamWhere(where, (
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
unsubscribe = model.streamWhere(where, (result) => {
|
|
26
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
27
|
+
setLoading(false);
|
|
26
28
|
}, 100, filter === null || filter === void 0 ? void 0 : filter.orderBy, filter === null || filter === void 0 ? void 0 : filter.offset);
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
|
-
catch (
|
|
30
|
-
errorLogger('useStream error:',
|
|
31
|
+
catch (err) {
|
|
32
|
+
errorLogger('useStream error:', err);
|
|
33
|
+
setError(err);
|
|
34
|
+
setLoading(false);
|
|
31
35
|
}
|
|
32
36
|
return () => {
|
|
33
|
-
if (typeof unsubscribe === 'function')
|
|
37
|
+
if (typeof unsubscribe === 'function')
|
|
34
38
|
unsubscribe();
|
|
35
|
-
}
|
|
36
39
|
};
|
|
37
|
-
}, [
|
|
40
|
+
}, [
|
|
41
|
+
JSON.stringify(param.where),
|
|
42
|
+
JSON.stringify(param.filter),
|
|
43
|
+
param.reference,
|
|
44
|
+
]);
|
|
45
|
+
return { data, loading, error };
|
|
38
46
|
};
|
|
39
47
|
//# sourceMappingURL=useStream.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAa,WAAW,EAAe,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAa,WAAW,EAAe,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAQ5C,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,KAQC,EACmB,EAAE;IACtB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aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CACf,CAAC;YACJ,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,WAAW,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;YACrC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACd,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QAED,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,WAAW,KAAK,UAAU;gBAAE,WAAW,EAAE,CAAC;QACvD,CAAC,CAAC;IACJ,CAAC,EAAE;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;QAC5B,KAAK,CAAC,SAAS;KAChB,CAAC,CAAC;IAEH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Dispatch,
|
|
5
|
+
ReactNode,
|
|
6
|
+
useReducer,
|
|
7
|
+
Context,
|
|
8
|
+
createContext,
|
|
9
|
+
useEffect,
|
|
10
|
+
} from 'react';
|
|
11
|
+
|
|
12
|
+
import { Auth, User, onAuthStateChanged } from 'firebase/auth';
|
|
13
|
+
import { errorLogger } from 'firebase-client-ql';
|
|
14
|
+
|
|
15
|
+
enum AUTHACTIONTYPE {
|
|
16
|
+
SETFBUSER = "SETFBUSER",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* AUTH state maintained accross
|
|
21
|
+
* all components
|
|
22
|
+
*/
|
|
23
|
+
interface AUTHSTATE {
|
|
24
|
+
FBUser?: User
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface AUTHACTION {
|
|
28
|
+
type: AUTHACTIONTYPE,
|
|
29
|
+
payload: User | undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
const initialAUTHState: AUTHSTATE = {}
|
|
34
|
+
|
|
35
|
+
const AUTHReducer = (state: AUTHSTATE, action: AUTHACTION): AUTHSTATE => {
|
|
36
|
+
|
|
37
|
+
const {type, payload} = action
|
|
38
|
+
|
|
39
|
+
switch (type) {
|
|
40
|
+
|
|
41
|
+
case 'SETFBUSER':
|
|
42
|
+
return {...state, FBUser: payload as User }
|
|
43
|
+
|
|
44
|
+
default:
|
|
45
|
+
return state
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
const initialDispatch: Dispatch<AUTHACTION> = () => {};
|
|
51
|
+
|
|
52
|
+
export const AUTHContext: Context<AUTHSTATE> =
|
|
53
|
+
createContext(initialAUTHState);
|
|
54
|
+
|
|
55
|
+
const AUTHDispatch: Context<Dispatch<AUTHACTION>> =
|
|
56
|
+
createContext(initialDispatch);
|
|
57
|
+
|
|
58
|
+
type AUTHProviderProps = {
|
|
59
|
+
children: ReactNode;
|
|
60
|
+
auth: Auth;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const AUTHProvider = ({ children, auth }: AUTHProviderProps) => {
|
|
64
|
+
const [AUTHState, dispatch] = useReducer(
|
|
65
|
+
AUTHReducer,
|
|
66
|
+
initialAUTHState
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
const unsubscribe = onAuthStateChanged(
|
|
71
|
+
auth,
|
|
72
|
+
(authUser: User | null) => {
|
|
73
|
+
try {
|
|
74
|
+
dispatch({
|
|
75
|
+
type: AUTHACTIONTYPE.SETFBUSER,
|
|
76
|
+
payload: authUser ?? undefined,
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
errorLogger('User Authentication error:', error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return unsubscribe;
|
|
85
|
+
}, [auth]);
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<AUTHContext.Provider value={AUTHState}>
|
|
89
|
+
<AUTHDispatch.Provider value={dispatch}>
|
|
90
|
+
{children}
|
|
91
|
+
</AUTHDispatch.Provider>
|
|
92
|
+
</AUTHContext.Provider>
|
|
93
|
+
);
|
|
94
|
+
};
|
package/src/client/useStream.ts
CHANGED
|
@@ -1,59 +1,74 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import { BaseModel, errorLogger, whereClause } from 'firebase-client-ql';
|
|
4
|
-
import { useEffect } from 'react';
|
|
4
|
+
import { useEffect, useState } from 'react';
|
|
5
5
|
|
|
6
|
+
type UseStreamResult<T> = {
|
|
7
|
+
data: T[];
|
|
8
|
+
loading: boolean;
|
|
9
|
+
error: unknown;
|
|
10
|
+
};
|
|
6
11
|
|
|
7
12
|
export const useStream = <T extends BaseModel>(
|
|
8
13
|
param: {
|
|
9
|
-
model:
|
|
14
|
+
model: any;
|
|
10
15
|
where?: whereClause[];
|
|
11
16
|
reference?: string;
|
|
12
17
|
filter?: {
|
|
13
18
|
orderBy?: { parameter: string; direction?: 'asc' | 'desc' };
|
|
14
19
|
offset?: string;
|
|
15
20
|
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
}
|
|
22
|
+
): UseStreamResult<T> => {
|
|
23
|
+
const [data, setData] = useState<T[]>([]);
|
|
24
|
+
const [loading, setLoading] = useState(true);
|
|
25
|
+
const [error, setError] = useState<unknown>(null);
|
|
19
26
|
|
|
20
27
|
useEffect(() => {
|
|
21
28
|
let unsubscribe: (() => void) | undefined;
|
|
29
|
+
setLoading(true);
|
|
22
30
|
|
|
23
31
|
try {
|
|
24
|
-
const {model, reference, where, filter } = param;
|
|
32
|
+
const { model, reference, where, filter } = param;
|
|
25
33
|
|
|
26
34
|
if (reference) {
|
|
27
|
-
unsubscribe = model.stream((
|
|
28
|
-
|
|
35
|
+
unsubscribe = model.stream((result: T[]) => {
|
|
36
|
+
setData(result ?? []);
|
|
37
|
+
setLoading(false);
|
|
29
38
|
}, reference);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
39
|
+
}
|
|
40
|
+
else if (where) {
|
|
41
|
+
if (where.length > 0 && where.every(w => w.value === undefined)) {
|
|
42
|
+
setData([]);
|
|
43
|
+
setLoading(false);
|
|
44
|
+
return;
|
|
37
45
|
}
|
|
46
|
+
|
|
38
47
|
unsubscribe = model.streamWhere(
|
|
39
48
|
where,
|
|
40
|
-
(
|
|
41
|
-
|
|
49
|
+
(result: T[]) => {
|
|
50
|
+
setData(result ?? []);
|
|
51
|
+
setLoading(false);
|
|
42
52
|
},
|
|
43
53
|
100,
|
|
44
54
|
filter?.orderBy,
|
|
45
55
|
filter?.offset
|
|
46
56
|
);
|
|
47
57
|
}
|
|
48
|
-
} catch (
|
|
49
|
-
errorLogger('useStream error:',
|
|
58
|
+
} catch (err) {
|
|
59
|
+
errorLogger('useStream error:', err);
|
|
60
|
+
setError(err);
|
|
61
|
+
setLoading(false);
|
|
50
62
|
}
|
|
51
63
|
|
|
52
|
-
// Proper cleanup
|
|
53
64
|
return () => {
|
|
54
|
-
if (typeof unsubscribe === 'function')
|
|
55
|
-
unsubscribe();
|
|
56
|
-
}
|
|
65
|
+
if (typeof unsubscribe === 'function') unsubscribe();
|
|
57
66
|
};
|
|
58
|
-
}, [
|
|
67
|
+
}, [
|
|
68
|
+
JSON.stringify(param.where),
|
|
69
|
+
JSON.stringify(param.filter),
|
|
70
|
+
param.reference,
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
return { data, loading, error };
|
|
59
74
|
};
|