@churchapps/apphelper 0.3.42 → 0.3.45

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.
Files changed (34) hide show
  1. package/dist/helpers/ApiHelper.d.ts.map +1 -1
  2. package/dist/helpers/ApiHelper.js +3 -3
  3. package/dist/helpers/ApiHelper.js.map +1 -1
  4. package/dist/helpers/ErrorHelper.d.ts +2 -2
  5. package/dist/helpers/ErrorHelper.d.ts.map +1 -1
  6. package/dist/helpers/ErrorHelper.js.map +1 -1
  7. package/dist/helpers/FileHelper.d.ts +1 -1
  8. package/dist/helpers/FileHelper.d.ts.map +1 -1
  9. package/dist/helpers/FileHelper.js +2 -6
  10. package/dist/helpers/FileHelper.js.map +1 -1
  11. package/dist/helpers/UserHelper.d.ts +1 -1
  12. package/dist/helpers/UserHelper.d.ts.map +1 -1
  13. package/dist/helpers/UserHelper.js +10 -3
  14. package/dist/helpers/UserHelper.js.map +1 -1
  15. package/dist/pageComponents/LoginPage.d.ts +1 -0
  16. package/dist/pageComponents/LoginPage.d.ts.map +1 -1
  17. package/dist/pageComponents/LoginPage.js +7 -1
  18. package/dist/pageComponents/LoginPage.js.map +1 -1
  19. package/dist/pageComponents/LogoutPage.d.ts +1 -0
  20. package/dist/pageComponents/LogoutPage.d.ts.map +1 -1
  21. package/dist/pageComponents/LogoutPage.js +7 -1
  22. package/dist/pageComponents/LogoutPage.js.map +1 -1
  23. package/dist/pageComponents/components/SelectChurchModal.d.ts +1 -0
  24. package/dist/pageComponents/components/SelectChurchModal.d.ts.map +1 -1
  25. package/dist/pageComponents/components/SelectChurchModal.js +9 -1
  26. package/dist/pageComponents/components/SelectChurchModal.js.map +1 -1
  27. package/package.json +2 -2
  28. package/src/helpers/ApiHelper.ts +119 -119
  29. package/src/helpers/ErrorHelper.ts +2 -2
  30. package/src/helpers/FileHelper.ts +3 -6
  31. package/src/helpers/UserHelper.ts +64 -57
  32. package/src/pageComponents/LoginPage.tsx +223 -217
  33. package/src/pageComponents/LogoutPage.tsx +23 -18
  34. package/src/pageComponents/components/SelectChurchModal.tsx +40 -32
@@ -3,124 +3,124 @@ import { ErrorHelper } from "./ErrorHelper";
3
3
 
4
4
  export class ApiHelper {
5
5
 
6
- static apiConfigs: ApiConfig[] = [];
7
- static isAuthenticated = false;
8
-
9
- static getConfig(keyName: string) {
10
- let result: ApiConfig;
11
- this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
12
- //if (result === null) throw new Error("Unconfigured API: " + keyName);
13
- return result;
14
- }
15
-
16
- static setDefaultPermissions(jwt: string) {
17
- this.apiConfigs.forEach(config => {
18
- config.jwt = jwt;
19
- config.permisssions = [];
20
- });
21
- this.isAuthenticated = true;
22
- }
23
-
24
- static setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
25
- this.apiConfigs.forEach(config => {
26
- if (config.keyName === keyName) {
27
- config.jwt = jwt;
28
- config.permisssions = permissions;
29
- }
30
- });
31
- this.isAuthenticated = true;
32
- }
33
-
34
- static clearPermissions() {
35
- this.apiConfigs.forEach(config => { config.jwt = ""; config.permisssions = []; });
36
- this.isAuthenticated = false;
37
- }
38
-
39
- static async get(path: string, apiName: ApiListType) {
40
- const config = this.getConfig(apiName);
41
- const requestOptions = {
42
- method: "GET",
43
- headers: { Authorization: "Bearer " + config.jwt },
44
- cache: "no-store"
45
- };
46
-
47
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
48
- }
49
-
50
- static async getAnonymous(path: string, apiName: ApiListType, tags?: string[]) {
51
- const config = this.getConfig(apiName);
52
- const requestOptions: RequestInit = { method: "GET" };
53
- if (tags?.length > 0) (requestOptions as any).next = { tags: tags };
54
- else requestOptions.cache = "no-store";
55
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
56
- }
57
-
58
- static async post(path: string, data: any[] | {}, apiName: ApiListType) {
59
- const config = this.getConfig(apiName);
60
- const requestOptions = {
61
- method: "POST",
62
- headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
63
- body: JSON.stringify(data)
64
- };
65
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
66
- }
67
-
68
- static async patch(path: string, data: any[] | {}, apiName: ApiListType) {
69
- const config = this.getConfig(apiName);
70
- const requestOptions = {
71
- method: "PATCH",
72
- headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
73
- body: JSON.stringify(data)
74
- };
75
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
76
- }
77
-
78
- static async delete(path: string, apiName: ApiListType) {
79
- const config = this.getConfig(apiName);
80
- const requestOptions = {
81
- method: "DELETE",
82
- headers: { Authorization: "Bearer " + config.jwt }
83
- };
84
- try {
85
- const response = await fetch(config.url + path, requestOptions);
86
- if (!response.ok) await this.throwApiError(response);
87
- } catch (e) {
88
- throw (e);
89
- }
90
- }
91
-
92
- static async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
93
- const config = this.getConfig(apiName);
94
- const requestOptions = {
95
- method: "POST",
96
- headers: { "Content-Type": "application/json" },
97
- body: JSON.stringify(data)
98
- };
99
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
100
- }
101
-
102
- static async fetchWithErrorHandling(url: string, requestOptions: any) {
103
- try {
104
- const response = await fetch(url, requestOptions);
105
- if (!response.ok) await this.throwApiError(response);
106
- else {
107
- if (response.status !== 204 ) {
108
- return response.json();
109
- }
110
- }
111
- } catch (e) {
112
- throw (e);
113
- }
114
- }
115
-
116
- private static async throwApiError(response: Response) {
117
- let msg = response.statusText;
118
- try {
119
- const json = await response.json();
120
- msg = json.errors[0];
121
- } catch { }
122
- ErrorHelper.logError(response.status.toString(), response.url, msg);
123
- throw new Error(msg || "Error");
124
- }
6
+ static apiConfigs: ApiConfig[] = [];
7
+ static isAuthenticated = false;
8
+
9
+ static getConfig(keyName: string) {
10
+ let result: ApiConfig;
11
+ this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
12
+ //if (result === null) throw new Error("Unconfigured API: " + keyName);
13
+ return result;
14
+ }
15
+
16
+ static setDefaultPermissions(jwt: string) {
17
+ this.apiConfigs.forEach(config => {
18
+ config.jwt = jwt;
19
+ config.permissions = [];
20
+ });
21
+ this.isAuthenticated = true;
22
+ }
23
+
24
+ static setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
25
+ this.apiConfigs.forEach(config => {
26
+ if (config.keyName === keyName) {
27
+ config.jwt = jwt;
28
+ config.permissions = permissions;
29
+ }
30
+ });
31
+ this.isAuthenticated = true;
32
+ }
33
+
34
+ static clearPermissions() {
35
+ this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
36
+ this.isAuthenticated = false;
37
+ }
38
+
39
+ static async get(path: string, apiName: ApiListType) {
40
+ const config = this.getConfig(apiName);
41
+ const requestOptions = {
42
+ method: "GET",
43
+ headers: { Authorization: "Bearer " + config.jwt },
44
+ cache: "no-store"
45
+ };
46
+
47
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
48
+ }
49
+
50
+ static async getAnonymous(path: string, apiName: ApiListType, tags?: string[]) {
51
+ const config = this.getConfig(apiName);
52
+ const requestOptions: RequestInit = { method: "GET" };
53
+ if (tags?.length > 0) (requestOptions as any).next = { tags: tags };
54
+ else requestOptions.cache = "no-store";
55
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
56
+ }
57
+
58
+ static async post(path: string, data: any[] | {}, apiName: ApiListType) {
59
+ const config = this.getConfig(apiName);
60
+ const requestOptions = {
61
+ method: "POST",
62
+ headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
63
+ body: JSON.stringify(data)
64
+ };
65
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
66
+ }
67
+
68
+ static async patch(path: string, data: any[] | {}, apiName: ApiListType) {
69
+ const config = this.getConfig(apiName);
70
+ const requestOptions = {
71
+ method: "PATCH",
72
+ headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
73
+ body: JSON.stringify(data)
74
+ };
75
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
76
+ }
77
+
78
+ static async delete(path: string, apiName: ApiListType) {
79
+ const config = this.getConfig(apiName);
80
+ const requestOptions = {
81
+ method: "DELETE",
82
+ headers: { Authorization: "Bearer " + config.jwt }
83
+ };
84
+ try {
85
+ const response = await fetch(config.url + path, requestOptions);
86
+ if (!response.ok) await this.throwApiError(response);
87
+ } catch (e) {
88
+ throw (e);
89
+ }
90
+ }
91
+
92
+ static async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
93
+ const config = this.getConfig(apiName);
94
+ const requestOptions = {
95
+ method: "POST",
96
+ headers: { "Content-Type": "application/json" },
97
+ body: JSON.stringify(data)
98
+ };
99
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
100
+ }
101
+
102
+ static async fetchWithErrorHandling(url: string, requestOptions: any) {
103
+ try {
104
+ const response = await fetch(url, requestOptions);
105
+ if (!response.ok) await this.throwApiError(response);
106
+ else {
107
+ if (response.status !== 204) {
108
+ return response.json();
109
+ }
110
+ }
111
+ } catch (e) {
112
+ throw (e);
113
+ }
114
+ }
115
+
116
+ private static async throwApiError(response: Response) {
117
+ let msg = response.statusText;
118
+ try {
119
+ const json = await response.json();
120
+ msg = json.errors[0];
121
+ } catch { }
122
+ ErrorHelper.logError(response.status.toString(), response.url, msg);
123
+ throw new Error(msg || "Error");
124
+ }
125
125
 
126
126
  }
@@ -1,4 +1,4 @@
1
- import { ErrorLogInterface, ErrrorAppDataInterface } from "@churchapps/helpers";
1
+ import { ErrorLogInterface, ErrorAppDataInterface } from "@churchapps/helpers";
2
2
  import { ApiHelper } from "./ApiHelper";
3
3
 
4
4
 
@@ -7,7 +7,7 @@ export class ErrorHelper {
7
7
  static getAppData: () => { churchId: string, userId: string, originUrl: string, application: string };
8
8
  static customErrorHandler: (errorLog: ErrorLogInterface) => void;
9
9
 
10
- static init = (getAppData: () => ErrrorAppDataInterface, customErrorHandler: (errorLog: ErrorLogInterface) => void) => {
10
+ static init = (getAppData: () => ErrorAppDataInterface, customErrorHandler: (errorLog: ErrorLogInterface) => void) => {
11
11
  ErrorHelper.getAppData = getAppData;
12
12
  ErrorHelper.customErrorHandler = customErrorHandler;
13
13
  }
@@ -1,8 +1,8 @@
1
1
  import axios from "axios";
2
2
 
3
3
  export class FileHelper {
4
- static postPresignedFile = (presigned: any, uploadedFile: File, progressCallback: (percent: number) => void, axiosInstance?: any) => {
5
- const axiosToUse = axiosInstance || axios;
4
+
5
+ static postPresignedFile = (presigned: any, uploadedFile: File, progressCallback: (percent: number) => void) => {
6
6
  const formData = new FormData();
7
7
  //formData.append("key", presigned.key); //no longer needed with new aws sdk
8
8
  formData.append("acl", "public-read");
@@ -17,10 +17,7 @@ export class FileHelper {
17
17
  }
18
18
  };
19
19
 
20
- console.log("#10");
21
- console.log("axios: ", axiosToUse);
22
- console.log("axios tyeof: ", typeof axiosToUse.post);
23
- return axiosToUse.post(presigned.url, formData, axiosConfig);
20
+ return axios.post(presigned.url, formData, axiosConfig);
24
21
  };
25
22
 
26
23
  static dataURLtoBlob(dataurl: string) {
@@ -2,70 +2,77 @@ import { ApiHelper } from "./ApiHelper"
2
2
  import { UserInterface, UserContextInterface, IApiPermission, PersonInterface, LoginUserChurchInterface } from "@churchapps/helpers";
3
3
 
4
4
  export class UserHelper {
5
- static currentUserChurch: LoginUserChurchInterface;
6
- static userChurches: LoginUserChurchInterface[];
7
- static user: UserInterface;
8
- static churchChanged: boolean = false;
9
- static person: PersonInterface;
5
+ static currentUserChurch: LoginUserChurchInterface;
6
+ static userChurches: LoginUserChurchInterface[];
7
+ static user: UserInterface;
8
+ static churchChanged: boolean = false;
9
+ static person: PersonInterface;
10
10
 
11
- static selectChurch = async (context?: UserContextInterface, churchId?: string, keyName?: string) => {
12
- let userChurch = null;
11
+ static selectChurch = async (context?: UserContextInterface, churchId?: string, keyName?: string) => {
12
+ let userChurch = null;
13
13
 
14
- if (churchId) {
15
- UserHelper.userChurches.forEach(uc => {
16
- if (uc.church.id === churchId) userChurch = uc;
17
- });
18
- }
19
- else if (keyName) UserHelper.userChurches.forEach(uc => { if (uc.church.subDomain === keyName) userChurch = uc; });
20
- else userChurch = UserHelper.userChurches[0];
21
- if (!userChurch) return;
22
- else {
23
- UserHelper.currentUserChurch = userChurch;
24
- UserHelper.setupApiHelper(UserHelper.currentUserChurch);
25
- // TODO - remove context code from here and perform the logic in the component itself.
26
- if (context) {
27
- if (context.userChurch !== null) UserHelper.churchChanged = true;
28
- context.setUserChurch(UserHelper.currentUserChurch);
29
- }
30
- }
31
- }
14
+ if (churchId) {
15
+ UserHelper.userChurches.forEach(uc => {
16
+ if (uc.church.id === churchId) userChurch = uc;
17
+ });
18
+ }
19
+ else if (keyName) UserHelper.userChurches.forEach(uc => { if (uc.church.subDomain === keyName) userChurch = uc; });
20
+ else userChurch = UserHelper.userChurches[0];
21
+ if (!userChurch) return;
22
+ else {
23
+ UserHelper.currentUserChurch = userChurch;
24
+ UserHelper.setupApiHelper(UserHelper.currentUserChurch);
25
+ // TODO - remove context code from here and perform the logic in the component itself.
26
+ if (context) {
27
+ if (context.userChurch !== null) UserHelper.churchChanged = true;
28
+ context.setUserChurch(UserHelper.currentUserChurch);
29
+ }
30
+ }
31
+ }
32
32
 
33
- static setupApiHelper(userChurch: LoginUserChurchInterface) {
34
- ApiHelper.setDefaultPermissions(userChurch.jwt);
35
- userChurch.apis.forEach(api => { ApiHelper.setPermissions(api.keyName, api.jwt, api.permissions); });
36
- }
33
+ static setupApiHelper(userChurch: LoginUserChurchInterface) {
34
+ ApiHelper.setDefaultPermissions(userChurch.jwt);
35
+ userChurch.apis.forEach(api => { ApiHelper.setPermissions(api.keyName, api.jwt, api.permissions); });
36
+ }
37
37
 
38
- static setupApiHelperNoChurch(user: LoginUserChurchInterface) {
39
- ApiHelper.setDefaultPermissions(user.jwt);
40
- }
38
+ static setupApiHelperNoChurch(user: LoginUserChurchInterface) {
39
+ ApiHelper.setDefaultPermissions(user.jwt);
40
+ }
41
41
 
42
- static checkAccess({ api, contentType, action }: IApiPermission): boolean {
43
- const permissions = ApiHelper.getConfig(api)?.permisssions || [];
42
+ static checkAccess({ api, contentType, action }: IApiPermission): boolean {
43
+ const permissions = ApiHelper.getConfig(api)?.permissions || [];
44
44
 
45
- let result = false;
46
- if (permissions !== undefined) {
47
- permissions.forEach(element => {
48
- if (element.contentType === contentType && element.action === action) result = true;
49
- });
50
- }
51
- return result;
52
- }
45
+ let result = false;
46
+ if (permissions !== undefined) {
47
+ permissions.forEach(element => {
48
+ if (element.contentType === contentType && element.action === action) result = true;
49
+ });
50
+ }
51
+ return result;
52
+ }
53
53
 
54
- static createAppUrl(appUrl: string, returnUrl: string) {
55
- const jwt = ApiHelper.getConfig("MembershipApi")?.jwt;
54
+ static createAppUrl(appUrl: string, returnUrl: string) {
55
+ const jwt = ApiHelper.getConfig("MembershipApi")?.jwt;
56
56
 
57
- if (jwt) {
58
- return `${appUrl}/login/?jwt=${jwt}&returnUrl=${encodeURIComponent(returnUrl)}`;
59
- } else {
60
- return `${appUrl}/login/?returnUrl=${encodeURIComponent(returnUrl)}`;
61
- }
62
- }
57
+ if (jwt) {
58
+ return `${appUrl}/login/?jwt=${jwt}&returnUrl=${encodeURIComponent(returnUrl)}`;
59
+ } else {
60
+ return `${appUrl}/login/?returnUrl=${encodeURIComponent(returnUrl)}`;
61
+ }
62
+ }
63
63
 
64
- static redirectToLogin(returnUrl?: string) {
65
- if (typeof window !== "undefined") {
66
- const currentUrl = returnUrl || window.location.pathname + window.location.search;
67
- const encodedReturnUrl = encodeURIComponent(currentUrl);
68
- window.location.href = `/login?returnUrl=${encodedReturnUrl}`;
69
- }
70
- }
64
+ static redirectToLogin(returnUrl?: string, handleRedirect?: (url: string) => void) {
65
+ if (typeof window !== "undefined") {
66
+ const currentUrl = returnUrl || window.location.pathname + window.location.search;
67
+ const encodedReturnUrl = encodeURIComponent(currentUrl);
68
+ const loginUrl = `/login?returnUrl=${encodedReturnUrl}`;
69
+
70
+ // Use handleRedirect function if available, otherwise fallback to window.location
71
+ if (handleRedirect) {
72
+ handleRedirect(loginUrl);
73
+ } else {
74
+ window.location.href = loginUrl;
75
+ }
76
+ }
77
+ }
71
78
  }