namirasoft-account-react 1.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.
@@ -0,0 +1,20 @@
1
+ import { NamirasoftAccountServer, TokenManager } from "namirasoft-account";
2
+ import { Messenger } from "./Messenger";
3
+ import { ParsedNameValue } from "namirasoft-core";
4
+ import { RouteComponentProps } from "react-router-dom";
5
+ export interface IAccountProps {
6
+ account: {
7
+ token_manager: TokenManager;
8
+ server: NamirasoftAccountServer;
9
+ };
10
+ messenger: Messenger;
11
+ url: {
12
+ getQuery(name: string): string;
13
+ getLink(sub: string, customQuery: {
14
+ [name: string]: ParsedNameValue;
15
+ }): string;
16
+ redirect(sub: string, customQuery: {
17
+ [name: string]: ParsedNameValue;
18
+ }): void;
19
+ } & RouteComponentProps;
20
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IAccountProps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IAccountProps.js","sourceRoot":"","sources":["../src/IAccountProps.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from "react";
2
+ import { IAccountProps } from "./IAccountProps";
3
+ export interface IRouterMaker {
4
+ on401(): void;
5
+ onRenderOnLogin(props: IAccountProps): ReactNode;
6
+ onRenderOnLogout(props: IAccountProps, banned: boolean): ReactNode;
7
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IRouterMaker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IRouterMaker.js","sourceRoot":"","sources":["../src/IRouterMaker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import { Notification } from "./Notification";
2
+ export interface IRouterState {
3
+ inited?: boolean;
4
+ notifications?: Notification[];
5
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IRouterState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IRouterState.js","sourceRoot":"","sources":["../src/IRouterState.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import { IRouterState } from "./IRouterState";
2
+ export interface IRouterStateChanger {
3
+ setState(state: IRouterState): void;
4
+ getState(): IRouterState;
5
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IRouterStateChanger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IRouterStateChanger.js","sourceRoot":"","sources":["../src/IRouterStateChanger.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ import { IRouterStateChanger } from "./IRouterStateChanger";
2
+ export declare class Messenger {
3
+ static Counter: number;
4
+ private component;
5
+ constructor(component: IRouterStateChanger);
6
+ private notify;
7
+ onInformation(text: string): void;
8
+ onSuccess(text: string): void;
9
+ onWarning(text: string): void;
10
+ onError(error: Error | string): void;
11
+ delNotification(id: number): void;
12
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Messenger = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const NotificationType_1 = require("./NotificationType");
9
+ class Messenger {
10
+ constructor(component) {
11
+ this.component = component;
12
+ this.notify = this.notify.bind(this);
13
+ this.onInformation = this.onInformation.bind(this);
14
+ this.onSuccess = this.onSuccess.bind(this);
15
+ this.onWarning = this.onWarning.bind(this);
16
+ this.onError = this.onError.bind(this);
17
+ this.delNotification = this.delNotification.bind(this);
18
+ }
19
+ notify(message) {
20
+ let state = this.component.getState();
21
+ let id = Messenger.Counter++;
22
+ if (!state.notifications)
23
+ state.notifications = [];
24
+ state.notifications[id] = message;
25
+ this.component.setState(state);
26
+ setTimeout(() => {
27
+ this.delNotification(id);
28
+ }, 5000);
29
+ window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
30
+ }
31
+ onInformation(text) {
32
+ this.notify({ text, type: NotificationType_1.NotificationType.Information, color: "black" });
33
+ }
34
+ onSuccess(text) {
35
+ this.notify({ text, type: NotificationType_1.NotificationType.Success, color: "green" });
36
+ }
37
+ onWarning(text) {
38
+ this.notify({ text, type: NotificationType_1.NotificationType.Warning, color: "yellow" });
39
+ }
40
+ onError(error) {
41
+ var _a;
42
+ let text = "";
43
+ if (error instanceof Error) {
44
+ text = error.message;
45
+ if (axios_1.default.isAxiosError(error))
46
+ if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data)
47
+ text = error.response.data;
48
+ }
49
+ else
50
+ text = error;
51
+ this.notify({ text, type: NotificationType_1.NotificationType.Error, color: "red" });
52
+ }
53
+ delNotification(id) {
54
+ let state = this.component.getState();
55
+ if (state.notifications)
56
+ delete state.notifications[id];
57
+ this.component.setState(state);
58
+ }
59
+ }
60
+ exports.Messenger = Messenger;
61
+ Messenger.Counter = 1;
62
+ //# sourceMappingURL=Messenger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Messenger.js","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAG1B,yDAAsD;AAEtD,MAAa,SAAS;IAIlB,YAAY,SAA8B;QAEtC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SA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gB,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,CAAC,KAAqB;;QAEzB,IAAI,IAAI,GAAW,EAAE,CAAC;QACtB,IAAI,KAAK,YAAY,KAAK,EAC1B;YACI,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC;YACrB,IAAI,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC;gBACzB,IAAI,MAAA,KAAK,CAAC,QAAQ,0CAAE,IAAI;oBACpB,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC;SACtC;;YAEG,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,mCAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACtE,CAAC;IACD,eAAe,CAAC,EAAU;QAEtB,IAAI,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;QACtC,IAAI,KAAK,CAAC,aAAa;YACnB,OAAO,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;QACnC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;;AA5DL,8BA6DC;AA3DU,iBAAO,GAAW,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { NotificationType } from "./NotificationType";
2
+ export type Notification = {
3
+ text: string;
4
+ type: NotificationType;
5
+ color: string;
6
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Notification.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Notification.js","sourceRoot":"","sources":["../src/Notification.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ export declare enum NotificationType {
2
+ Information = "Information",
3
+ Warning = "Warning",
4
+ Success = "Success",
5
+ Error = "Error"
6
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotificationType = void 0;
4
+ var NotificationType;
5
+ (function (NotificationType) {
6
+ NotificationType["Information"] = "Information";
7
+ NotificationType["Warning"] = "Warning";
8
+ NotificationType["Success"] = "Success";
9
+ NotificationType["Error"] = "Error";
10
+ })(NotificationType || (exports.NotificationType = NotificationType = {}));
11
+ //# sourceMappingURL=NotificationType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NotificationType.js","sourceRoot":"","sources":["../src/NotificationType.ts"],"names":[],"mappings":";;;AAAA,IAAY,gBAMX;AAND,WAAY,gBAAgB;IAExB,+CAA2B,CAAA;IAC3B,uCAAmB,CAAA;IACnB,uCAAmB,CAAA;IACnB,mCAAe,CAAA;AACnB,CAAC,EANW,gBAAgB,gCAAhB,gBAAgB,QAM3B"}
@@ -0,0 +1,23 @@
1
+ import { ReactNode } from "react";
2
+ import { ParsedNameValue } from "namirasoft-core";
3
+ import { IRouterStateChanger } from "./IRouterStateChanger";
4
+ import { IRouterMaker } from "./IRouterMaker";
5
+ import { RouteComponentProps } from 'react-router-dom';
6
+ interface IProps extends RouteComponentProps {
7
+ }
8
+ export default class RouterMaker {
9
+ private component;
10
+ private props;
11
+ constructor(component: IRouterStateChanger & IRouterMaker, props: IProps);
12
+ private init;
13
+ getQuery(name: string): string;
14
+ getLink(sub: string, customQuery: {
15
+ [name: string]: ParsedNameValue;
16
+ }): string;
17
+ redirect(sub: string, customQuery: {
18
+ [name: string]: ParsedNameValue;
19
+ }): void;
20
+ onComponentDidMount(): void;
21
+ onRender(): ReactNode;
22
+ }
23
+ export {};
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const query_string_1 = __importDefault(require("query-string"));
7
+ const namirasoft_core_1 = require("namirasoft-core");
8
+ const Messenger_1 = require("./Messenger");
9
+ const namirasoft_account_1 = require("namirasoft-account");
10
+ const axios_1 = __importDefault(require("axios"));
11
+ class RouterMaker {
12
+ constructor(component, props) {
13
+ this.component = component;
14
+ this.props = props;
15
+ this.init = this.init.bind(this);
16
+ this.getQuery = this.getQuery.bind(this);
17
+ this.getLink = this.getLink.bind(this);
18
+ this.redirect = this.redirect.bind(this);
19
+ this.onComponentDidMount = this.onComponentDidMount.bind(this);
20
+ this.onRender = this.onRender.bind(this);
21
+ }
22
+ init() {
23
+ var _a;
24
+ let messenger = new Messenger_1.Messenger(this.component);
25
+ let qStorage = this.getQuery("location");
26
+ let qAPI = this.getQuery("api");
27
+ let qToken = this.getQuery("token");
28
+ if (!qStorage)
29
+ if (qAPI)
30
+ qStorage = "memory";
31
+ let storage;
32
+ if (qStorage === "memory")
33
+ storage = new namirasoft_core_1.IStorageMemory();
34
+ else
35
+ storage = new namirasoft_core_1.IStorageLocal();
36
+ let token_manager = new namirasoft_account_1.TokenManager(storage, () => this.component.setState({}));
37
+ if (qToken)
38
+ token_manager.setValue(qToken, true);
39
+ let server = new namirasoft_account_1.NamirasoftAccountServer(token_manager, (error) => {
40
+ var _a;
41
+ if (axios_1.default.isAxiosError(error))
42
+ if (error.response)
43
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
44
+ token_manager.del();
45
+ this.component.on401();
46
+ return;
47
+ }
48
+ messenger.onError(error);
49
+ });
50
+ let props = {
51
+ account: {
52
+ token_manager,
53
+ server,
54
+ },
55
+ url: Object.assign({ getLink: this.getLink, redirect: this.redirect, getQuery: this.getQuery }, this.props),
56
+ messenger,
57
+ };
58
+ let inited = true;
59
+ if (qToken)
60
+ inited = (_a = this.component.getState().inited) !== null && _a !== void 0 ? _a : false;
61
+ return { props, inited };
62
+ }
63
+ getQuery(name) {
64
+ let location = this.props.location;
65
+ let query = query_string_1.default.parse(location.search);
66
+ let item = query[name];
67
+ return new namirasoft_core_1.ObjectService(item).getString();
68
+ }
69
+ getLink(sub, customQuery) {
70
+ if (!customQuery)
71
+ customQuery = {};
72
+ let location = this.props.location;
73
+ let query = query_string_1.default.parse(location.search);
74
+ let final = Object.assign(Object.assign({}, query), customQuery);
75
+ return namirasoft_core_1.URLOperation.getLink("", sub, final);
76
+ }
77
+ redirect(sub, customQuery) {
78
+ let ans = this.getLink(sub, customQuery);
79
+ const { history } = this.props;
80
+ history.push(ans);
81
+ }
82
+ onComponentDidMount() {
83
+ let { inited, props } = this.init();
84
+ this.component.setState({ notifications: [], inited });
85
+ let token = this.getQuery("token");
86
+ if (token) {
87
+ if (!props.account.token_manager.getUserData(u => u.id, 0)) {
88
+ props.account.server.session.get()
89
+ .then(response => {
90
+ props.account.token_manager.set(response, false);
91
+ this.component.setState({ inited: true });
92
+ })
93
+ .catch(() => {
94
+ this.component.setState({ inited: true });
95
+ });
96
+ }
97
+ }
98
+ }
99
+ onRender() {
100
+ let { inited, props } = this.init();
101
+ if (!inited)
102
+ return null;
103
+ let banned = false;
104
+ if (props.account.token_manager.exists()) {
105
+ banned = props.account.token_manager.getUserData(user => user.banned, false);
106
+ if (!banned)
107
+ return this.component.onRenderOnLogin(props);
108
+ }
109
+ return this.component.onRenderOnLogout(props, banned);
110
+ }
111
+ }
112
+ exports.default = RouterMaker;
113
+ ;
114
+ //# sourceMappingURL=RouterMaker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RouterMaker.js","sourceRoot":"","sources":["../src/RouterMaker.ts"],"names":[],"mappings":";;;;;AAEA,gEAAuC;AACvC,qDAAwH;AAExH,2CAAwC;AACxC,2DAA2E;AAG3E,kDAA0B;AAO1B,MAAqB,WAAW;IAI5B,YAAY,SAA6C,EAAE,KAAa;QAEpE,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAA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,KAAK,CAAC,GAAG,EAAE;oBAER,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC9C,CAAC,CAAC,CAAC;aACV;SACJ;IACL,CAAC;IACD,QAAQ;QAEJ,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;QACpC,IAAI,CAAC,MAAM;YACP,OAAO,IAAI,CAAC;QAChB,IAAI,MAAM,GAAG,KAAK,CAAC;QACnB,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,EAAE,EACxC;YACI,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;YAC7E,IAAI,CAAC,MAAM;gBACP,OAAO,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;SACpD;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;CACJ;AAxHD,8BAwHC;AAAA,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from "./IAccountProps";
2
+ export * from "./IRouterMaker";
3
+ export * from "./IRouterState";
4
+ export * from "./IRouterStateChanger";
5
+ export * from "./Messenger";
6
+ export * from "./Notification";
7
+ export * from "./NotificationType";
8
+ export { default as RouterMaker } from "./RouterMaker";
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.RouterMaker = void 0;
21
+ __exportStar(require("./IAccountProps"), exports);
22
+ __exportStar(require("./IRouterMaker"), exports);
23
+ __exportStar(require("./IRouterState"), exports);
24
+ __exportStar(require("./IRouterStateChanger"), exports);
25
+ __exportStar(require("./Messenger"), exports);
26
+ __exportStar(require("./Notification"), exports);
27
+ __exportStar(require("./NotificationType"), exports);
28
+ var RouterMaker_1 = require("./RouterMaker");
29
+ Object.defineProperty(exports, "RouterMaker", { enumerable: true, get: function () { return __importDefault(RouterMaker_1).default; } });
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,iDAA+B;AAC/B,iDAA+B;AAC/B,wDAAsC;AACtC,8CAA4B;AAC5B,iDAA+B;AAC/B,qDAAmC;AACnC,6CAAuD;AAA9C,2HAAA,OAAO,OAAe"}
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "namirasoft-account-react",
3
+ "description": "Namira Software Corporation Account React Package",
4
+ "version": "1.0.0",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "scripts": {},
8
+ "author": "Amir Abolhasani",
9
+ "license": "MIT",
10
+ "dependencies": {
11
+ "@types/react": "^18.2.33",
12
+ "@types/react-router-dom": "^5.3.3",
13
+ "namirasoft-account": "^1.0.0",
14
+ "namirasoft-core": "^1.0.3",
15
+ "query-string": "^8.1.0",
16
+ "react": "^18.2.0",
17
+ "react-router-dom": "^5.3.4",
18
+ "run": "^1.4.0"
19
+ }
20
+ }
@@ -0,0 +1,18 @@
1
+ import { NamirasoftAccountServer, TokenManager } from "namirasoft-account";
2
+ import { Messenger } from "./Messenger";
3
+ import { ParsedNameValue } from "namirasoft-core";
4
+ import { RouteComponentProps } from "react-router-dom";
5
+
6
+ export interface IAccountProps
7
+ {
8
+ account: {
9
+ token_manager: TokenManager
10
+ server: NamirasoftAccountServer;
11
+ },
12
+ messenger: Messenger;
13
+ url: {
14
+ getQuery(name: string): string;
15
+ getLink(sub: string, customQuery: { [name: string]: ParsedNameValue }): string;
16
+ redirect(sub: string, customQuery: { [name: string]: ParsedNameValue }): void;
17
+ } & RouteComponentProps
18
+ }
@@ -0,0 +1,8 @@
1
+ import { ReactNode } from "react";
2
+ import { IAccountProps } from "./IAccountProps";
3
+ export interface IRouterMaker
4
+ {
5
+ on401(): void;
6
+ onRenderOnLogin(props: IAccountProps): ReactNode;
7
+ onRenderOnLogout(props: IAccountProps, banned: boolean): ReactNode;
8
+ }
@@ -0,0 +1,7 @@
1
+ import { Notification } from "./Notification";
2
+
3
+ export interface IRouterState
4
+ {
5
+ inited?: boolean;
6
+ notifications?: Notification[];
7
+ }
@@ -0,0 +1,7 @@
1
+ import { IRouterState } from "./IRouterState";
2
+
3
+ export interface IRouterStateChanger
4
+ {
5
+ setState(state: IRouterState): void;
6
+ getState(): IRouterState;
7
+ }
@@ -0,0 +1,67 @@
1
+ import axios from "axios";
2
+ import { IRouterStateChanger } from "./IRouterStateChanger";
3
+ import { Notification } from "./Notification";
4
+ import { NotificationType } from "./NotificationType";
5
+
6
+ export class Messenger
7
+ {
8
+ static Counter: number = 1;
9
+ private component: IRouterStateChanger;
10
+ constructor(component: IRouterStateChanger)
11
+ {
12
+ this.component = component;
13
+ this.notify = this.notify.bind(this);
14
+ this.onInformation = this.onInformation.bind(this);
15
+ this.onSuccess = this.onSuccess.bind(this);
16
+ this.onWarning = this.onWarning.bind(this);
17
+ this.onError = this.onError.bind(this);
18
+ this.delNotification = this.delNotification.bind(this);
19
+ }
20
+ private notify(message: Notification)
21
+ {
22
+ let state = this.component.getState();
23
+ let id = Messenger.Counter++;
24
+ if (!state.notifications)
25
+ state.notifications = [];
26
+ state.notifications[id] = message;
27
+ this.component.setState(state);
28
+ setTimeout(() =>
29
+ {
30
+ this.delNotification(id);
31
+ }, 5000);
32
+ window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
33
+ }
34
+ onInformation(text: string)
35
+ {
36
+ this.notify({ text, type: NotificationType.Information, color: "black" });
37
+ }
38
+ onSuccess(text: string)
39
+ {
40
+ this.notify({ text, type: NotificationType.Success, color: "green" });
41
+ }
42
+ onWarning(text: string)
43
+ {
44
+ this.notify({ text, type: NotificationType.Warning, color: "yellow" });
45
+ }
46
+ onError(error: Error | string)
47
+ {
48
+ let text: string = "";
49
+ if (error instanceof Error)
50
+ {
51
+ text = error.message;
52
+ if (axios.isAxiosError(error))
53
+ if (error.response?.data)
54
+ text = error.response.data;
55
+ }
56
+ else
57
+ text = error;
58
+ this.notify({ text, type: NotificationType.Error, color: "red" });
59
+ }
60
+ delNotification(id: number): void
61
+ {
62
+ let state = this.component.getState();
63
+ if (state.notifications)
64
+ delete state.notifications[id];
65
+ this.component.setState(state);
66
+ }
67
+ }
@@ -0,0 +1,8 @@
1
+ import { NotificationType } from "./NotificationType";
2
+
3
+ export type Notification =
4
+ {
5
+ text: string;
6
+ type: NotificationType;
7
+ color: string;
8
+ }
@@ -0,0 +1,7 @@
1
+ export enum NotificationType
2
+ {
3
+ Information = "Information",
4
+ Warning = "Warning",
5
+ Success = "Success",
6
+ Error = "Error",
7
+ }
@@ -0,0 +1,137 @@
1
+
2
+ import { ReactNode } from "react";
3
+ import queryString from 'query-string';
4
+ import { IStorage, IStorageLocal, IStorageMemory, ObjectService, ParsedNameValue, URLOperation } from "namirasoft-core";
5
+ import { IRouterStateChanger } from "./IRouterStateChanger";
6
+ import { Messenger } from "./Messenger";
7
+ import { NamirasoftAccountServer, TokenManager } from "namirasoft-account";
8
+ import { IAccountProps } from "./IAccountProps";
9
+ import { IRouterMaker } from "./IRouterMaker";
10
+ import axios from "axios";
11
+ import { RouteComponentProps } from 'react-router-dom';
12
+
13
+ interface IProps extends RouteComponentProps
14
+ {
15
+ }
16
+
17
+ export default class RouterMaker
18
+ {
19
+ private component: IRouterStateChanger & IRouterMaker;
20
+ private props: IProps;
21
+ constructor(component: IRouterStateChanger & IRouterMaker, props: IProps)
22
+ {
23
+ this.component = component;
24
+ this.props = props;
25
+ this.init = this.init.bind(this);
26
+ this.getQuery = this.getQuery.bind(this);
27
+ this.getLink = this.getLink.bind(this);
28
+ this.redirect = this.redirect.bind(this);
29
+ this.onComponentDidMount = this.onComponentDidMount.bind(this);
30
+ this.onRender = this.onRender.bind(this);
31
+ }
32
+ private init(): { props: IAccountProps, inited: boolean }
33
+ {
34
+ let messenger: Messenger = new Messenger(this.component);
35
+ let qStorage: string = this.getQuery("location");
36
+ let qAPI: string = this.getQuery("api");
37
+ let qToken: string = this.getQuery("token");
38
+ if (!qStorage)
39
+ if (qAPI)
40
+ qStorage = "memory";
41
+ let storage: IStorage;
42
+ if (qStorage === "memory")
43
+ storage = new IStorageMemory();
44
+ else
45
+ storage = new IStorageLocal();
46
+ let token_manager = new TokenManager(storage, () => this.component.setState({}));
47
+ if (qToken)
48
+ token_manager.setValue(qToken, true);
49
+ let server = new NamirasoftAccountServer(token_manager, (error: Error) =>
50
+ {
51
+ if (axios.isAxiosError(error))
52
+ if (error.response)
53
+ if (error.response?.status === 401)
54
+ {
55
+ token_manager.del();
56
+ this.component.on401();
57
+ return;
58
+ }
59
+ messenger.onError(error);
60
+ });
61
+ let props: IAccountProps = {
62
+ account: {
63
+ token_manager,
64
+ server,
65
+ },
66
+ url: {
67
+ getLink: this.getLink,
68
+ redirect: this.redirect,
69
+ getQuery: this.getQuery,
70
+ ...this.props
71
+ },
72
+ messenger,
73
+ };
74
+ let inited = true;
75
+ if (qToken)
76
+ inited = this.component.getState().inited ?? false;
77
+ return { props, inited };
78
+ }
79
+ getQuery(name: string): string
80
+ {
81
+ let location = this.props.location;
82
+ let query = queryString.parse(location.search);
83
+ let item = query[name];
84
+ return new ObjectService(item).getString();
85
+ }
86
+ getLink(sub: string, customQuery: { [name: string]: ParsedNameValue })
87
+ {
88
+ if (!customQuery)
89
+ customQuery = {};
90
+ let location = this.props.location;
91
+ let query = queryString.parse(location.search);
92
+ let final = { ...query, ...customQuery };
93
+ return URLOperation.getLink("", sub, final);
94
+ }
95
+ redirect(sub: string, customQuery: { [name: string]: ParsedNameValue }): void
96
+ {
97
+ let ans = this.getLink(sub, customQuery);
98
+ const { history } = this.props;
99
+ history.push(ans);
100
+ }
101
+ onComponentDidMount(): void
102
+ {
103
+ let { inited, props } = this.init();
104
+ this.component.setState({ notifications: [], inited });
105
+ let token = this.getQuery("token");
106
+ if (token)
107
+ {
108
+ if (!props.account.token_manager.getUserData(u => u.id, 0))
109
+ {
110
+ props.account.server.session.get()
111
+ .then(response =>
112
+ {
113
+ props.account.token_manager.set(response, false);
114
+ this.component.setState({ inited: true });
115
+ })
116
+ .catch(() =>
117
+ {
118
+ this.component.setState({ inited: true });
119
+ });
120
+ }
121
+ }
122
+ }
123
+ onRender(): ReactNode
124
+ {
125
+ let { inited, props } = this.init();
126
+ if (!inited)
127
+ return null;
128
+ let banned = false;
129
+ if (props.account.token_manager.exists())
130
+ {
131
+ banned = props.account.token_manager.getUserData(user => user.banned, false);
132
+ if (!banned)
133
+ return this.component.onRenderOnLogin(props);
134
+ }
135
+ return this.component.onRenderOnLogout(props, banned);
136
+ }
137
+ };
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./IAccountProps";
2
+ export * from "./IRouterMaker";
3
+ export * from "./IRouterState";
4
+ export * from "./IRouterStateChanger";
5
+ export * from "./Messenger";
6
+ export * from "./Notification";
7
+ export * from "./NotificationType";
8
+ export { default as RouterMaker } from "./RouterMaker";
package/tsconfig.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "module": "CommonJS",
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+ "lib": [
8
+ "es6",
9
+ "dom"
10
+ ],
11
+ "allowJs": false,
12
+ "checkJs": false,
13
+ "strict": true,
14
+ "esModuleInterop": true,
15
+ "sourceMap": true,
16
+ "resolveJsonModule": true,
17
+ "declaration": true,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": true,
20
+ "noImplicitAny": true,
21
+ "noImplicitOverride": true,
22
+ "noImplicitReturns": true,
23
+ "noImplicitThis": true,
24
+ "skipLibCheck": false,
25
+ "allowSyntheticDefaultImports": true,
26
+ "forceConsistentCasingInFileNames": true,
27
+ "noFallthroughCasesInSwitch": true,
28
+ "isolatedModules": false,
29
+ }
30
+ }