@churchapps/apphelper 0.6.0 → 0.6.2

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,22 +1,20 @@
1
1
  "use client";
2
2
 
3
- import {
4
- Button,
5
- TextField,
6
- Paper,
7
- Box,
8
- Typography,
9
- Stack,
10
- IconButton,
11
- List,
12
- ListItem,
13
- ListItemAvatar,
14
- ListItemText,
3
+ import {
4
+ Button,
5
+ TextField,
6
+ Paper,
7
+ Box,
8
+ Typography,
9
+ Stack,
10
+ IconButton,
11
+ List,
12
+ ListItemAvatar,
13
+ ListItemText,
15
14
  ListItemButton,
16
15
  InputAdornment,
17
16
  Divider,
18
- Skeleton,
19
- useTheme
17
+ Skeleton
20
18
  } from "@mui/material";
21
19
  import {
22
20
  ArrowBack as ArrowBackIcon,
@@ -90,7 +88,6 @@ export const NewPrivateMessage: React.FC<Props> = (props) => {
90
88
  }, [props.selectedPerson]);
91
89
 
92
90
 
93
- const theme = useTheme();
94
91
  const [isSearching, setIsSearching] = useState(false);
95
92
 
96
93
  const handleSearchSubmit = async (e: React.MouseEvent) => {
@@ -2,22 +2,20 @@
2
2
 
3
3
  import React, { useState } from "react";
4
4
  import { ApiHelper } from "@churchapps/helpers";
5
- import {
6
- Box,
7
- Icon,
8
- Stack,
9
- Typography,
10
- Paper,
11
- List,
12
- ListItem,
13
- ListItemAvatar,
14
- ListItemText,
15
- Avatar,
16
- Chip,
17
- Divider,
18
- IconButton,
19
- Skeleton,
20
- useTheme
5
+ import {
6
+ Box,
7
+ Stack,
8
+ Typography,
9
+ Paper,
10
+ List,
11
+ ListItem,
12
+ ListItemAvatar,
13
+ ListItemText,
14
+ Avatar,
15
+ Chip,
16
+ Divider,
17
+ IconButton,
18
+ Skeleton
21
19
  } from "@mui/material";
22
20
  import {
23
21
  Notifications as NotificationsIcon,
@@ -38,7 +36,6 @@ interface Props {
38
36
  export const Notifications: React.FC<Props> = (props) => {
39
37
  const [notifications, setNotifications] = useState<NotificationInterface[]>([]);
40
38
  const [isLoading, setIsLoading] = useState(true);
41
- const theme = useTheme();
42
39
 
43
40
  const loadData = async () => {
44
41
  setIsLoading(true);
@@ -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(() => {
@@ -2,20 +2,19 @@
2
2
 
3
3
  import React, { useState, useEffect } from "react";
4
4
  import { ApiHelper } from "@churchapps/helpers";
5
- import {
6
- Box,
7
- Stack,
8
- List,
9
- ListItem,
10
- ListItemAvatar,
11
- ListItemText,
12
- Typography,
13
- IconButton,
5
+ import {
6
+ Box,
7
+ Stack,
8
+ List,
9
+ ListItem,
10
+ ListItemAvatar,
11
+ ListItemText,
12
+ Typography,
13
+ IconButton,
14
14
  Chip,
15
15
  Divider,
16
16
  Paper,
17
- Skeleton,
18
- useTheme
17
+ Skeleton
19
18
  } from "@mui/material";
20
19
  import { Add as AddIcon, ChatBubbleOutline as ChatIcon } from "@mui/icons-material";
21
20
  import { PersonAvatar } from "../PersonAvatar";
@@ -458,8 +457,6 @@ export const PrivateMessages: React.FC<Props> = React.memo((props) => {
458
457
  loadData();
459
458
  }
460
459
 
461
- const theme = useTheme();
462
-
463
460
  if (inAddMode) return <NewPrivateMessage context={props.context} onSelectMessage={(pm: PrivateMessageInterface) => { privateMessagesStateStore.setSelectedMessage(pm); privateMessagesStateStore.setInAddMode(false); }} onBack={handleBack} />
464
461
  if (selectedMessage) return <PrivateMessageDetails privateMessage={selectedMessage} context={props.context} onBack={handleBack} refreshKey={props.refreshKey} onMessageRead={loadData} />
465
462
 
@@ -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
+ }