@churchapps/apphelper 0.6.0 → 0.6.1

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.
@@ -1,145 +1,145 @@
1
- "use client";
2
-
3
- import React, { useState } from "react";
4
- import { LoginUserChurchInterface, UserContextInterface, ArrayHelper } from "@churchapps/helpers";
5
- import { ApiHelper } from "@churchapps/helpers";
6
- import { UserHelper } from "../../helpers/UserHelper";
7
- import { NavItem } from "./NavItem";
8
- import { Locale } from "../../helpers";
9
-
10
- export interface Props { userChurches: LoginUserChurchInterface[], currentUserChurch: LoginUserChurchInterface, context: UserContextInterface, onDelete?: () => void, onChurchChange?: () => void }
11
-
12
- export const ChurchList: React.FC<Props> = props => {
13
- const [userChurches, setUserChurches] = useState(() => {
14
- try {
15
- // If we have currentUserChurch, use it as fallback
16
- if (props.currentUserChurch && (!props.userChurches || !Array.isArray(props.userChurches))) {
17
- return [props.currentUserChurch];
18
- }
19
-
20
- let churches = props.userChurches;
21
-
22
- // Ensure we have an array
23
- if (!Array.isArray(churches)) {
24
- console.warn('ChurchList - Expected array but got:', typeof churches, churches);
25
- // If it's a plain church object and we have currentUserChurch, use that
26
- if (props.currentUserChurch) {
27
- return [props.currentUserChurch];
28
- }
29
- churches = [];
30
- }
31
-
32
- // Filter for valid userChurch objects (should have church property)
33
- const validChurches = churches.filter(uc => {
34
- const isValid = uc && uc.church && uc.church.id;
35
- if (!isValid) {
36
- console.warn('ChurchList - Invalid church structure:', uc);
37
- }
38
- return isValid;
39
- });
40
-
41
- // If no valid churches but we have currentUserChurch, use it
42
- if (validChurches.length === 0 && props.currentUserChurch) {
43
- return [props.currentUserChurch];
44
- }
45
-
46
- return validChurches;
47
- } catch (error) {
48
- console.error('ChurchList - Error processing churches:', error);
49
- // Last resort: if we have currentUserChurch, use it
50
- if (props.currentUserChurch) {
51
- return [props.currentUserChurch];
52
- }
53
- return [];
54
- }
55
- });
56
-
57
- // Update local state when props change
58
- React.useEffect(() => {
59
- try {
60
- // If we have currentUserChurch, use it as fallback
61
- if (props.currentUserChurch && (!props.userChurches || !Array.isArray(props.userChurches))) {
62
- setUserChurches([props.currentUserChurch]);
63
- return;
64
- }
65
-
66
- let churches = props.userChurches;
67
-
68
- // Ensure we have an array
69
- if (!Array.isArray(churches)) {
70
- if (props.currentUserChurch) {
71
- setUserChurches([props.currentUserChurch]);
72
- return;
73
- }
74
- churches = [];
75
- }
76
-
77
- // Filter for valid userChurch objects
78
- const validChurches = churches.filter(uc => uc && uc.church && uc.church.id);
79
-
80
- // If no valid churches but we have currentUserChurch, use it
81
- if (validChurches.length === 0 && props.currentUserChurch) {
82
- setUserChurches([props.currentUserChurch]);
83
- } else {
84
- setUserChurches(validChurches);
85
- }
86
- } catch (error) {
87
- console.error('ChurchList useEffect - Error updating churches:', error);
88
- if (props.currentUserChurch) {
89
- setUserChurches([props.currentUserChurch]);
90
- } else {
91
- setUserChurches([]);
92
- }
93
- }
94
- }, [props.userChurches, props.currentUserChurch]);
95
-
96
- const handleDelete = async (uc: LoginUserChurchInterface) => {
97
- // Helper function to get label with fallback
98
- const getLabel = (key: string, fallback: string) => {
99
- const label = Locale.label(key);
100
- return label && label !== key ? label : fallback;
101
- };
102
-
103
- const confirmMessage = getLabel("wrapper.sureRemoveChurch", "Are you sure you wish to delete this church? You will no longer be a member of {}.").replace("{}", uc.church.name?.toUpperCase());
104
- if (window.confirm(confirmMessage)) {
105
- await ApiHelper.delete(`/userchurch/record/${props.context.user.id}/${uc.church.id}/${uc.person.id}`, "MembershipApi");
106
- await ApiHelper.delete(`/rolemembers/self/${uc.church.id}/${props.context.user.id}`, "MembershipApi");
107
- // remove the same from userChurches
108
- const idx = ArrayHelper.getIndex(UserHelper.userChurches, "church.id", uc.church.id);
109
- if (idx > -1) UserHelper.userChurches.splice(idx, 1);
110
- props?.onDelete();
111
- }
112
- }
113
-
114
- // Helper function to get label with fallback
115
- const getLabel = (key: string, fallback: string) => {
116
- const label = Locale.label(key);
117
- return label && label !== key ? label : fallback;
118
- };
119
-
120
- let result: React.ReactElement[] = [];
121
-
122
- userChurches.forEach(uc => {
123
- const userChurch = uc;
124
- const churchName = uc.church.name;
125
- result.push(<NavItem
126
- key={userChurch.church.id}
127
- selected={(uc.church.id === props.currentUserChurch.church.id) && true}
128
- onClick={async () => {
129
- await UserHelper.selectChurch(props.context, userChurch.church.id, null);
130
-
131
- // Call the onChurchChange callback if provided
132
- if (props.onChurchChange) {
133
- props.onChurchChange();
134
- }
135
- }}
136
- label={churchName || "Unknown"}
137
- icon="church"
138
- deleteIcon={uc.church.id !== props.currentUserChurch.church.id ? "delete" : null}
139
- deleteLabel={getLabel("wrapper.deleteChurch", "Delete")}
140
- deleteFunction={() => { handleDelete(uc); }}
141
- />);
142
- });
143
-
144
- return <div id="church-list">{result}</div>;
145
- };
1
+ "use client";
2
+
3
+ import React, { useState } from "react";
4
+ import { LoginUserChurchInterface, UserContextInterface, ArrayHelper } from "@churchapps/helpers";
5
+ import { ApiHelper } from "@churchapps/helpers";
6
+ import { UserHelper } from "../../helpers/UserHelper";
7
+ import { NavItem } from "./NavItem";
8
+ import { Locale } from "../../helpers";
9
+
10
+ export interface Props { userChurches: LoginUserChurchInterface[], currentUserChurch: LoginUserChurchInterface, context: UserContextInterface, onDelete?: () => void, onChurchChange?: () => void }
11
+
12
+ export const ChurchList: React.FC<Props> = props => {
13
+ const [userChurches, setUserChurches] = useState(() => {
14
+ try {
15
+ // If we have currentUserChurch, use it as fallback
16
+ if (props.currentUserChurch && (!props.userChurches || !Array.isArray(props.userChurches))) {
17
+ return [props.currentUserChurch];
18
+ }
19
+
20
+ let churches = props.userChurches;
21
+
22
+ // Ensure we have an array
23
+ if (!Array.isArray(churches)) {
24
+ console.warn('ChurchList - Expected array but got:', typeof churches, churches);
25
+ // If it's a plain church object and we have currentUserChurch, use that
26
+ if (props.currentUserChurch) {
27
+ return [props.currentUserChurch];
28
+ }
29
+ churches = [];
30
+ }
31
+
32
+ // Filter for valid userChurch objects (should have church property)
33
+ const validChurches = churches.filter(uc => {
34
+ const isValid = uc && uc.church && uc.church.id;
35
+ if (!isValid) {
36
+ console.warn('ChurchList - Invalid church structure:', uc);
37
+ }
38
+ return isValid;
39
+ });
40
+
41
+ // If no valid churches but we have currentUserChurch, use it
42
+ if (validChurches.length === 0 && props.currentUserChurch) {
43
+ return [props.currentUserChurch];
44
+ }
45
+
46
+ return validChurches;
47
+ } catch (error) {
48
+ console.error('ChurchList - Error processing churches:', error);
49
+ // Last resort: if we have currentUserChurch, use it
50
+ if (props.currentUserChurch) {
51
+ return [props.currentUserChurch];
52
+ }
53
+ return [];
54
+ }
55
+ });
56
+
57
+ // Update local state when props change
58
+ React.useEffect(() => {
59
+ try {
60
+ // If we have currentUserChurch, use it as fallback
61
+ if (props.currentUserChurch && (!props.userChurches || !Array.isArray(props.userChurches))) {
62
+ setUserChurches([props.currentUserChurch]);
63
+ return;
64
+ }
65
+
66
+ let churches = props.userChurches;
67
+
68
+ // Ensure we have an array
69
+ if (!Array.isArray(churches)) {
70
+ if (props.currentUserChurch) {
71
+ setUserChurches([props.currentUserChurch]);
72
+ return;
73
+ }
74
+ churches = [];
75
+ }
76
+
77
+ // Filter for valid userChurch objects
78
+ const validChurches = churches.filter(uc => uc && uc.church && uc.church.id);
79
+
80
+ // If no valid churches but we have currentUserChurch, use it
81
+ if (validChurches.length === 0 && props.currentUserChurch) {
82
+ setUserChurches([props.currentUserChurch]);
83
+ } else {
84
+ setUserChurches(validChurches);
85
+ }
86
+ } catch (error) {
87
+ console.error('ChurchList useEffect - Error updating churches:', error);
88
+ if (props.currentUserChurch) {
89
+ setUserChurches([props.currentUserChurch]);
90
+ } else {
91
+ setUserChurches([]);
92
+ }
93
+ }
94
+ }, [props.userChurches, props.currentUserChurch]);
95
+
96
+ const handleDelete = async (uc: LoginUserChurchInterface) => {
97
+ // Helper function to get label with fallback
98
+ const getLabel = (key: string, fallback: string) => {
99
+ const label = Locale.label(key);
100
+ return label && label !== key ? label : fallback;
101
+ };
102
+
103
+ const confirmMessage = getLabel("wrapper.sureRemoveChurch", "Are you sure you wish to delete this church? You will no longer be a member of {}.").replace("{}", uc.church.name?.toUpperCase());
104
+ if (window.confirm(confirmMessage)) {
105
+ await ApiHelper.delete(`/userchurch/record/${props.context.user.id}/${uc.church.id}/${uc.person.id}`, "MembershipApi");
106
+ await ApiHelper.delete(`/rolemembers/self/${uc.church.id}/${props.context.user.id}`, "MembershipApi");
107
+ // remove the same from userChurches
108
+ const idx = ArrayHelper.getIndex(UserHelper.userChurches, "church.id", uc.church.id);
109
+ if (idx > -1) UserHelper.userChurches.splice(idx, 1);
110
+ props?.onDelete();
111
+ }
112
+ }
113
+
114
+ // Helper function to get label with fallback
115
+ const getLabel = (key: string, fallback: string) => {
116
+ const label = Locale.label(key);
117
+ return label && label !== key ? label : fallback;
118
+ };
119
+
120
+ let result: React.ReactElement[] = [];
121
+
122
+ userChurches.forEach(uc => {
123
+ const userChurch = uc;
124
+ const churchName = uc.church.name;
125
+ result.push(<NavItem
126
+ key={userChurch.church.id}
127
+ selected={(uc.church.id === props.currentUserChurch.church.id) && true}
128
+ onClick={async () => {
129
+ await UserHelper.selectChurch(props.context, userChurch.church.id, null);
130
+
131
+ // Call the onChurchChange callback if provided
132
+ if (props.onChurchChange) {
133
+ props.onChurchChange();
134
+ }
135
+ }}
136
+ label={churchName || "Unknown"}
137
+ icon="church"
138
+ deleteIcon={uc.church.id !== props.currentUserChurch.church.id ? "delete" : null}
139
+ deleteLabel={getLabel("wrapper.deleteChurch", "Delete")}
140
+ deleteFunction={() => { handleDelete(uc); }}
141
+ />);
142
+ });
143
+
144
+ return <div id="church-list">{result}</div>;
145
+ };
@@ -1,13 +1,12 @@
1
1
  "use client";
2
2
 
3
3
  import React from "react";
4
- import {
5
- Paper,
6
- Box,
7
- Typography,
8
- Stack,
9
- IconButton,
10
- useTheme
4
+ import {
5
+ Paper,
6
+ Box,
7
+ Typography,
8
+ Stack,
9
+ IconButton
11
10
  } from "@mui/material";
12
11
  import { ArrowBack as ArrowBackIcon } from "@mui/icons-material";
13
12
  import { PrivateMessageInterface, UserContextInterface } from "@churchapps/helpers";
@@ -24,7 +23,6 @@ interface Props {
24
23
  }
25
24
 
26
25
  export const PrivateMessageDetails: React.FC<Props> = (props) => {
27
- const theme = useTheme();
28
26
 
29
27
  // Clear notification when conversation is opened
30
28
  React.useEffect(() => {
@@ -1,41 +1,41 @@
1
- import { ErrorLogInterface, ErrorAppDataInterface } from "@churchapps/helpers";
2
- import { ApiHelper } from "@churchapps/helpers";
3
-
4
-
5
- export class ErrorHelper {
6
-
7
- static getAppData: () => { churchId: string, userId: string, originUrl: string, application: string };
8
- static customErrorHandler: (errorLog: ErrorLogInterface) => void;
9
-
10
- static init = (getAppData: () => ErrorAppDataInterface, customErrorHandler: (errorLog: ErrorLogInterface) => void) => {
11
- ErrorHelper.getAppData = getAppData;
12
- ErrorHelper.customErrorHandler = customErrorHandler;
13
- }
14
-
15
- static logError = (errorType: string, message: string, details: string) => {
16
- if (this.getAppData) {
17
- const data = this.getAppData();
18
- const log: ErrorLogInterface = {
19
- application: data.application,
20
- errorTime: new Date(),
21
- userId: data.userId,
22
- churchId: data.churchId,
23
- originUrl: data.originUrl,
24
- errorType: errorType,
25
- message: message,
26
- details: details
27
- }
28
-
29
-
30
- if (log.errorType === "401" && log.message.indexOf("/users/login") > -1) return;
31
- if (log.message.indexOf("clientErrors") > -1) return;
32
- try {
33
- // Error posting to /errors endpoint disabled
34
- // ApiHelper.postAnonymous("/clientErrors", [log], "MembershipApi");
35
- } catch (error) {
36
- }
37
- if (ErrorHelper.customErrorHandler) ErrorHelper.customErrorHandler(log);
38
- }
39
- }
40
-
41
- }
1
+ import { ErrorLogInterface, ErrorAppDataInterface } from "@churchapps/helpers";
2
+ import { ApiHelper } from "@churchapps/helpers";
3
+
4
+
5
+ export class ErrorHelper {
6
+
7
+ static getAppData: () => { churchId: string, userId: string, originUrl: string, application: string };
8
+ static customErrorHandler: (errorLog: ErrorLogInterface) => void;
9
+
10
+ static init = (getAppData: () => ErrorAppDataInterface, customErrorHandler: (errorLog: ErrorLogInterface) => void) => {
11
+ ErrorHelper.getAppData = getAppData;
12
+ ErrorHelper.customErrorHandler = customErrorHandler;
13
+ }
14
+
15
+ static logError = (errorType: string, message: string, details: string) => {
16
+ if (this.getAppData) {
17
+ const data = this.getAppData();
18
+ const log: ErrorLogInterface = {
19
+ application: data.application,
20
+ errorTime: new Date(),
21
+ userId: data.userId,
22
+ churchId: data.churchId,
23
+ originUrl: data.originUrl,
24
+ errorType: errorType,
25
+ message: message,
26
+ details: details
27
+ }
28
+
29
+
30
+ if (log.errorType === "401" && log.message.indexOf("/users/login") > -1) return;
31
+ if (log.message.indexOf("clientErrors") > -1) return;
32
+ try {
33
+ // Error posting to /errors endpoint disabled
34
+ // ApiHelper.postAnonymous("/clientErrors", [log], "MembershipApi");
35
+ } catch (error) {
36
+ }
37
+ if (ErrorHelper.customErrorHandler) ErrorHelper.customErrorHandler(log);
38
+ }
39
+ }
40
+
41
+ }