@jsb188/graphql 1.1.4 → 1.1.6

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 (49) hide show
  1. package/cache/config.ts +13 -7
  2. package/cache/index.ts +355 -144
  3. package/gql/fragments/accountAuthFragments.ts +3 -9
  4. package/gql/fragments/accountFragments.ts +11 -0
  5. package/gql/fragments/actionFragments.ts +16 -0
  6. package/gql/fragments/logFragments.ts +75 -6
  7. package/gql/fragments/organizationFragments.ts +70 -4
  8. package/gql/fragments/otherFragments.ts +10 -8
  9. package/gql/fragments/productFragments.ts +63 -0
  10. package/gql/fragments/reportFragments.ts +47 -0
  11. package/gql/mutations/accountMutations.ts +31 -0
  12. package/gql/mutations/aiChatMutations.ts +1 -0
  13. package/gql/mutations/authMutations.ts +80 -22
  14. package/gql/mutations/index.ts +8 -0
  15. package/gql/mutations/logMutations.ts +56 -1
  16. package/gql/mutations/organizationMutations.ts +42 -0
  17. package/gql/mutations/otherMutations.ts +0 -17
  18. package/gql/mutations/userAuthMutations.ts +0 -116
  19. package/gql/queries/aiChatQueries.ts +4 -0
  20. package/gql/queries/appQueries.ts +2 -1
  21. package/gql/queries/index.ts +6 -0
  22. package/gql/queries/logQueries.ts +60 -11
  23. package/gql/queries/organizationQueries.ts +62 -1
  24. package/gql/queries/productQueries.ts +70 -0
  25. package/gql/queries/reportQueries.ts +60 -0
  26. package/hooks/index.ts +259 -113
  27. package/hooks/use-account-mtn.ts +68 -0
  28. package/hooks/use-aiChat-mtn.ts +112 -0
  29. package/hooks/use-aiChat-qry.ts +81 -10
  30. package/hooks/use-auth-mtn.ts +114 -63
  31. package/hooks/use-log-mtn.ts +85 -0
  32. package/hooks/use-log-qry.ts +71 -18
  33. package/hooks/use-organization-mtn.ts +105 -0
  34. package/hooks/use-organization-qry.ts +119 -15
  35. package/hooks/use-product-qry.ts +109 -0
  36. package/hooks/use-report-qry.ts +69 -0
  37. package/package.json +15 -25
  38. package/sse.ts +11 -0
  39. package/types.d.ts +1 -0
  40. package/gql/fragments/cardFragments.ts +0 -15
  41. package/gql/fragments/chatFragments.ts +0 -98
  42. package/gql/fragments/userAuthFragments.ts +0 -8
  43. package/gql/fragments/userFragments.ts +0 -69
  44. package/gql/mutations/chatMutations.ts +0 -116
  45. package/gql/mutations/userMutations.ts +0 -89
  46. package/gql/queries/cardQueries.ts +0 -24
  47. package/gql/queries/chatQueries.ts +0 -86
  48. package/gql/queries/otherQueries.ts +0 -12
  49. package/gql/queries/userQueries.ts +0 -104
@@ -0,0 +1,112 @@
1
+ import { resetQuery, updateQuery } from '../cache/';
2
+ import { useMutation } from '../client';
3
+ import { updateAIChats } from './use-aiChat-qry';
4
+ import { sendAIChatMessageMtn, startAIChatMtn, stopAIChatMessageMtn } from '../gql/mutations/aiChatMutations';
5
+ import type { UseMutationParams } from '../types.d';
6
+ import { useOpenModalPopUp } from '@jsb188/react/states';
7
+ import { useEffect, useRef } from 'react';
8
+
9
+ /**
10
+ * Start AI chat
11
+ */
12
+
13
+ export function useStartAIChat(params: UseMutationParams = {}) {
14
+
15
+ const { onCompleted, onError } = params;
16
+ const openModalPopUp = useOpenModalPopUp();
17
+
18
+ const [startAIChat, mtnValues,, updateObservers] = useMutation(
19
+ startAIChatMtn,
20
+ {
21
+ openModalPopUp,
22
+ onCompleted: (data: any, err: any, variables: any) => {
23
+ onCompleted?.(data, err, variables);
24
+
25
+ const aiChat = data.startAIChat?.node;
26
+ updateAIChats(aiChat, updateObservers);
27
+ },
28
+ onError,
29
+ },
30
+ );
31
+
32
+ return {
33
+ startAIChat,
34
+ ...mtnValues,
35
+ };
36
+ }
37
+
38
+ /**
39
+ * Send a new message to AI Chat
40
+ */
41
+
42
+ export function useAIChatTextarea(variablesKey: string, params: UseMutationParams = {}) {
43
+
44
+ const { onCompleted, onError } = params;
45
+ const didSendRef = useRef(false);
46
+ const openModalPopUp = useOpenModalPopUp();
47
+
48
+ // Send AI chat message
49
+
50
+ const [sendAIChatMessage, mtnValues1,, updateObservers] = useMutation(
51
+ sendAIChatMessageMtn,
52
+ {
53
+ openModalPopUp,
54
+ onCompleted: (data: any, err: any, variables: any) => {
55
+ didSendRef.current = true;
56
+
57
+ onCompleted?.(data, err, variables);
58
+
59
+ const aiChatId = variables.aiChatId;
60
+ const messageId = data.sendAIChatMessage?.node?.id;
61
+
62
+ if (messageId && aiChatId && variablesKey) {
63
+ updateQuery(`#aiChat:${variablesKey}`, (currentData: any) => {
64
+ if (Array.isArray(currentData.messages?.data)) {
65
+ currentData.messages.data.push([`$aiChatMessageFragment:${messageId}`]);
66
+ }
67
+ return currentData;
68
+ }, false, updateObservers);
69
+ } else if (data && !err) {
70
+ console.dev(`Unknown error: {${messageId ? 'aiChatId' : 'messageId'}} was not found.`, 'warning');
71
+ }
72
+ },
73
+ onError,
74
+ },
75
+ );
76
+
77
+ // Stop AI chat message
78
+
79
+ const [stopAIChatMessage, mtnValues2] = useMutation(
80
+ stopAIChatMessageMtn,
81
+ {
82
+ openModalPopUp,
83
+ onCompleted: (data: any, err: any, variables: any) => {
84
+ onCompleted?.(data, err, variables);
85
+ },
86
+ onError,
87
+ },
88
+ );
89
+
90
+ // Reset query on unmount if a message was sent;
91
+ // This fixes an issue where in-progress messages that are stopped midway remain broken
92
+ // as a result of the SSE pipe breaking at unmount.
93
+
94
+ useEffect(() => {
95
+ return () => {
96
+ if (didSendRef.current) {
97
+ resetQuery(`#aiChat:${variablesKey}`, true, updateObservers);
98
+ }
99
+ };
100
+ }, []);
101
+
102
+ return {
103
+ send: {
104
+ sendAIChatMessage,
105
+ ...mtnValues1,
106
+ },
107
+ stop: {
108
+ stopAIChatMessage,
109
+ ...mtnValues2,
110
+ }
111
+ };
112
+ }
@@ -1,19 +1,20 @@
1
1
  import { getENVVariable } from '@jsb188/app';
2
2
  import { getAuthToken } from '@jsb188/app/utils/api';
3
+ import { makeVariablesKey } from '@jsb188/app/utils/logic';
3
4
  import SSE from '@jsb188/sse';
4
5
  import { useEffect, useState } from 'react';
5
- import { loadFragment } from '../cache/';
6
+ import { loadFragment, loadQuery, resetQuery, updateQuery } from '../cache/';
6
7
  import { useQuery } from '../client';
7
8
  import { aiChatMessagesQry, aiChatQry, aiChatsQry } from '../gql/queries/aiChatQueries';
8
9
  import handleSSEData, { type PublishPayload } from '../sse';
9
- import type { UseQueryParams } from '../types.d';
10
+ import type { UpdateObserversFn, UseQueryParams } from '../types.d';
10
11
 
11
12
  /**
12
13
  * Constants
13
14
  */
14
15
 
15
- const AI_CHATS_LIMIT = 100; // Must match server code exactly
16
- const AI_CHAT_MESSAGES_LIMIT = 100; // Must match server code exactly
16
+ const AI_CHATS_LIMIT = 70; // Must match server code exactly
17
+ const AI_CHAT_MESSAGES_LIMIT = 55; // Must match server code exactly
17
18
 
18
19
  /**
19
20
  * Helper; get generating (status) key from SSE data
@@ -49,7 +50,7 @@ function getGeneratingFragmentId(sseData: PublishPayload) {
49
50
 
50
51
  export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string | null, params: UseQueryParams = {}) {
51
52
 
52
- const { data, ...other } = useQuery(aiChatQry, {
53
+ const { data, ...rest } = useQuery(aiChatQry, {
53
54
  variables: {
54
55
  aiChatId
55
56
  },
@@ -57,7 +58,7 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
57
58
  ...params,
58
59
  });
59
60
 
60
- const { updateObservers } = other;
61
+ const { updateObservers } = rest;
61
62
  const [sessionKey, setSessionKey] = useState<string | null>(initialSessionKey || null);
62
63
  const [generatingFragmentId, setGeneratingFragmentId] = useState<string | null>(null);
63
64
  const aiChat = data?.aiChat || loadFragment(`$aiChatFragment:${aiChatId}`);
@@ -95,7 +96,6 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
95
96
  };
96
97
 
97
98
  connection.connect(connectParams);
98
-
99
99
  // const removeNetworkListeners = reconnectOnBrowserNetwork(connection, connectParams);
100
100
 
101
101
  return () => {
@@ -105,12 +105,53 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
105
105
  }
106
106
  }, [dataAIChatId, sessionKey]);
107
107
 
108
+ useEffect(() => {
109
+ // Every time AIChat is fetched, check if it exists in lists cache
110
+ if (dataAIChatId) {
111
+ const variablesKey = makeVariablesKey({
112
+ filter: aiChat.calDate ? 'CAL_DATE' : 'CHATS',
113
+ cursor: null,
114
+ after: false,
115
+ limit: AI_CHATS_LIMIT
116
+ });
117
+
118
+ const queryKey = `#aiChats:${variablesKey}`;
119
+ const queryCache = loadQuery(queryKey);
120
+
121
+ if (queryCache) {
122
+ const notFound = queryCache.every((a: any) => a.id !== dataAIChatId);
123
+
124
+ if (notFound) {
125
+ // If not found, add to cache
126
+ updateQuery(queryKey, (prev: any) => {
127
+ return [{
128
+ cursor: aiChat.cursor,
129
+ __flat: {
130
+ __cache: true,
131
+ data: [`$aiChatFragment:${dataAIChatId}`],
132
+ }
133
+ }, ...prev];
134
+ }, false, updateObservers);
135
+ }
136
+ }
137
+ }
138
+ }, [dataAIChatId]);
139
+
140
+ useEffect(() => {
141
+ // Force reset if chat message is in progress
142
+ if (aiChat?.inProgress) {
143
+ return () => {
144
+ resetQuery(rest.queryKey, true, updateObservers);
145
+ };
146
+ }
147
+ }, [aiChat?.inProgress]);
148
+
108
149
  return {
109
150
  aiChat,
110
151
  generatingFragmentId,
111
152
  sessionKey,
112
153
  setSessionKey,
113
- ...other
154
+ ...rest
114
155
  };
115
156
  }
116
157
 
@@ -138,19 +179,49 @@ export function useAIChatMessages(aiChatId?: string, params: UseQueryParams = {}
138
179
  };
139
180
  }
140
181
 
182
+ /**
183
+ * Update sidebar AI chats query
184
+ */
185
+
186
+ export function updateAIChats(aiChat: any, updateObservers: UpdateObserversFn) {
187
+ if (aiChat) {
188
+ const isCalDate = !!aiChat.calDate;
189
+ const queryKey = `#aiChats:$after:false$filter:${isCalDate ? 'cal_date' : 'chats'}$limit:${AI_CHATS_LIMIT}`;
190
+
191
+ updateQuery(
192
+ queryKey,
193
+ (res) => {
194
+ return [
195
+ {
196
+ cursor: aiChat.cursor,
197
+ __flat: {
198
+ __cache: true,
199
+ data: [`$aiChatFragment:${aiChat.id}`]
200
+ }
201
+ },
202
+ ...res
203
+ ];
204
+ },
205
+ false,
206
+ updateObservers
207
+ );
208
+ }
209
+ }
210
+
141
211
  /**
142
212
  * Fetch AI chats list
143
213
  */
144
214
 
145
- export function useAIChats(filter?: 'CAL_DATE' | 'CHATS') {
215
+ export function useAIChats(organizationId: string, filter?: 'CAL_DATE' | 'CHATS') {
146
216
  const { data, ...other } = useQuery(aiChatsQry, {
147
217
  variables: {
218
+ organizationId,
148
219
  filter,
149
220
  cursor: null,
150
221
  after: false,
151
222
  limit: AI_CHATS_LIMIT
152
223
  },
153
- skip: false,
224
+ skip: !organizationId,
154
225
  });
155
226
 
156
227
  return {
@@ -2,10 +2,10 @@ import i18n from '@jsb188/app/i18n';
2
2
  import { deleteAuthToken } from '@jsb188/app/platform';
3
3
  import type { UseMutationParams } from '@jsb188/graphql/types.d';
4
4
  import type { OpenModalPopUpFn } from '@jsb188/react/states';
5
- import { AppContext, onAccountLogin, onGuestInit, useOpenModalPopUp } from '@jsb188/react/states';
5
+ import { AppContext, onGuestInit, useOpenModalPopUp } from '@jsb188/react/states';
6
6
  import React, { useContext, useEffect, useState } from 'react';
7
- import { sendEmailVerificationMtn, signUpWithEmailMtn } from '../gql/mutations/authMutations';
8
- import { changePasswordMtn, checkUsernameOrEmailMtn, requestTokenizedEmailMtn, signInMtn, signOutMtn } from '../gql/mutations/userAuthMutations';
7
+ import { changeAccountPasswordMtn, checkUsernameOrEmailMtn, confirmPasswordMtn, confirmPhoneVerificationCodeMtn, requestTokenizedEmailMtn, sendPhoneVerificationCodeMtn, signUpWithEmailMtn } from '../gql/mutations/authMutations';
8
+ import { signOutMtn } from '../gql/mutations/userAuthMutations';
9
9
  import { useMutation } from './index';
10
10
 
11
11
  /**
@@ -21,20 +21,31 @@ interface AuthHookParams extends UseMutationParams {
21
21
  * Change password (while logged in)
22
22
  */
23
23
 
24
- export function useChangePassword(params: UseMutationParams = {}) {
24
+ interface UseChangeAccountPasswordParams extends UseMutationParams {
25
+ doNotFinishForm?: boolean;
26
+ }
25
27
 
26
- const { dispatchApp, appState: { hasPassword, account } } = useContext(AppContext);
27
- const openModalPopUp = useOpenModalPopUp();
28
- const { onCompleted, onError } = params;
28
+ export function useChangeAccountPassword(params: UseChangeAccountPasswordParams = {}, openModalPopUp?: OpenModalPopUpFn) {
29
29
 
30
- const onCompleted_ = (data: any, err: any, variables: any) => {
31
- if (onCompleted) {
32
- onCompleted(data, err, variables);
30
+ const { dispatchApp, appState: { hasPassword, account } } = useContext(AppContext);
31
+ const { onCompleted, onError, doNotFinishForm } = params;
32
+
33
+ const onFinishChangeAccountPassword = () => {
34
+ if (!hasPassword) {
35
+ dispatchApp({
36
+ __type: 'APP_EDIT_AUTH',
37
+ data: {
38
+ id: account?.id,
39
+ hasPassword: true,
40
+ }
41
+ });
33
42
  }
43
+ };
34
44
 
35
- const success = !!data?.changePassword;
45
+ const onCompleted_ = (data: any, err: any, variables: any) => {
46
+ const success = !!data?.changeAccountPassword;
36
47
  if (success) {
37
- openModalPopUp({
48
+ openModalPopUp?.({
38
49
  name: 'password_changed',
39
50
  props: {
40
51
  title: i18n.t('form.success'),
@@ -43,20 +54,18 @@ export function useChangePassword(params: UseMutationParams = {}) {
43
54
  }
44
55
  });
45
56
 
46
- if (!hasPassword) {
47
- dispatchApp({
48
- __type: 'APP_EDIT_AUTH',
49
- data: {
50
- id: account?.id,
51
- hasPassword: true,
52
- }
53
- });
57
+ if (!doNotFinishForm) {
58
+ onFinishChangeAccountPassword();
59
+ }
60
+
61
+ if (onCompleted) {
62
+ onCompleted(success, err, variables);
54
63
  }
55
64
  }
56
65
  };
57
66
 
58
- const [changePassword, mtnValues] = useMutation(
59
- changePasswordMtn,
67
+ const [changeAccountPassword, mtnValues, mtnHandlers] = useMutation(
68
+ changeAccountPasswordMtn,
60
69
  {
61
70
  openModalPopUp,
62
71
  onCompleted: onCompleted_,
@@ -66,8 +75,11 @@ export function useChangePassword(params: UseMutationParams = {}) {
66
75
 
67
76
  return {
68
77
  openModalPopUp,
69
- changePassword,
78
+ changeAccountPassword,
79
+ onFinishChangeAccountPassword,
80
+ account,
70
81
  ...mtnValues,
82
+ ...mtnHandlers,
71
83
  };
72
84
  }
73
85
 
@@ -75,7 +87,7 @@ export function useChangePassword(params: UseMutationParams = {}) {
75
87
  * Reset password (from 30m email link)
76
88
  */
77
89
 
78
- export function useChangePasswordForm(params: UseMutationParams = {}) {
90
+ export function useChangeAccountPasswordForm(params: UseMutationParams = {}) {
79
91
 
80
92
  const { dispatchApp, appState: { hasPassword, account } } = useContext(AppContext);
81
93
  const { onCompleted, onError } = params;
@@ -85,7 +97,7 @@ export function useChangePasswordForm(params: UseMutationParams = {}) {
85
97
  onCompleted(data, err, variables);
86
98
  }
87
99
 
88
- const success = !!data?.changePassword;
100
+ const success = !!data?.changeAccountPassword;
89
101
  if (success) {
90
102
  // It's possible for user to use this hook without being logged in
91
103
  // So we have to check for ID first
@@ -101,8 +113,8 @@ export function useChangePasswordForm(params: UseMutationParams = {}) {
101
113
  }
102
114
  };
103
115
 
104
- const [changePassword, mtnValues] = useMutation(
105
- changePasswordMtn,
116
+ const [changeAccountPassword, mtnValues, mtnHandlers] = useMutation(
117
+ changeAccountPasswordMtn,
106
118
  {
107
119
  checkMountedBeforeCallback: true,
108
120
  onCompleted: onCompleted_,
@@ -111,8 +123,9 @@ export function useChangePasswordForm(params: UseMutationParams = {}) {
111
123
  );
112
124
 
113
125
  return {
114
- changePassword,
126
+ changeAccountPassword,
115
127
  ...mtnValues,
128
+ ...mtnHandlers,
116
129
  };
117
130
  }
118
131
 
@@ -121,9 +134,8 @@ export function useChangePasswordForm(params: UseMutationParams = {}) {
121
134
  */
122
135
 
123
136
  export function useRequestTokenizedEmail(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
124
-
125
137
  const { onCompleted, onError } = params;
126
- const [requestTokenizedEmail, mtnValues] = useMutation(
138
+ const [requestTokenizedEmail, mtnValues, mtnHandlers] = useMutation(
127
139
  requestTokenizedEmailMtn,
128
140
  {
129
141
  checkMountedBeforeCallback: true,
@@ -136,6 +148,54 @@ export function useRequestTokenizedEmail(params: UseMutationParams = {}, openMod
136
148
  return {
137
149
  requestTokenizedEmail,
138
150
  ...mtnValues,
151
+ ...mtnHandlers,
152
+ };
153
+ }
154
+
155
+ /**
156
+ * Send verification code to phone number
157
+ */
158
+
159
+ export function useSendPhoneVerificationCode(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
160
+ const { onCompleted, onError } = params;
161
+ const [sendPhoneVerificationCode, mtnValues, mtnHandlers] = useMutation(
162
+ sendPhoneVerificationCodeMtn,
163
+ {
164
+ checkMountedBeforeCallback: true,
165
+ openModalPopUp,
166
+ onCompleted,
167
+ onError
168
+ },
169
+ );
170
+
171
+ return {
172
+ sendPhoneVerificationCode,
173
+ ...mtnValues,
174
+ ...mtnHandlers,
175
+ };
176
+ }
177
+
178
+ /**
179
+ * Confirm verification code
180
+ */
181
+
182
+ export function useConfirmPhoneVerificationCode(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
183
+ const { onCompleted, onError } = params;
184
+
185
+ const [confirmPhoneVerificationCode, mtnValues, mtnHandlers] = useMutation(
186
+ confirmPhoneVerificationCodeMtn,
187
+ {
188
+ checkMountedBeforeCallback: true,
189
+ openModalPopUp,
190
+ onCompleted,
191
+ onError
192
+ },
193
+ );
194
+
195
+ return {
196
+ confirmPhoneVerificationCode,
197
+ ...mtnValues,
198
+ ...mtnHandlers,
139
199
  };
140
200
  }
141
201
 
@@ -143,9 +203,8 @@ export function useRequestTokenizedEmail(params: UseMutationParams = {}, openMod
143
203
  * Sign in
144
204
  */
145
205
 
146
- export function useSignIn(params: AuthHookParams = {}, openModalPopUp?: OpenModalPopUpFn) {
206
+ export function useCheckUsernameOrEmail(params: AuthHookParams = {}, openModalPopUp?: OpenModalPopUpFn) {
147
207
  const { onError, onCompleted, initialError, initialFormData } = params;
148
- const { dispatchApp } = useContext<any>(AppContext);
149
208
 
150
209
  const [dataForSchema, setDataForSchema] = useState({
151
210
  hasInput: !!initialFormData?.usernameOrEmail,
@@ -153,7 +212,7 @@ export function useSignIn(params: AuthHookParams = {}, openModalPopUp?: OpenModa
153
212
  hidePassword: !initialFormData?.usernameOrEmail,
154
213
  });
155
214
 
156
- const [checkUsernameOrEmail, step1MtnValues, step1MtnHandlers] = useMutation(
215
+ const [checkUsernameOrEmail, mtnValues, mtnHandlers] = useMutation(
157
216
  checkUsernameOrEmailMtn,
158
217
  {
159
218
  openModalPopUp,
@@ -163,40 +222,10 @@ export function useSignIn(params: AuthHookParams = {}, openModalPopUp?: OpenModa
163
222
  },
164
223
  );
165
224
 
166
- const [signIn, step2MtnValues, step2MtnHandlers] = useMutation(
167
- signInMtn,
168
- {
169
- openModalPopUp,
170
- initialError,
171
- onError,
172
- onCompleted: (data, error) => {
173
- const result = data?.signIn;
174
- if (result?.token && result.user?.id) {
175
- onAccountLogin(result, dispatchApp);
176
-
177
- if (onCompleted) {
178
- onCompleted(data, error);
179
- }
180
- }
181
- },
182
- },
183
- );
184
-
185
- let mtnValues;
186
- let mtnHandlers;
187
- if (dataForSchema.hidePassword) {
188
- mtnValues = step1MtnValues;
189
- mtnHandlers = step1MtnHandlers;
190
- } else {
191
- mtnValues = step2MtnValues;
192
- mtnHandlers = step2MtnHandlers;
193
- }
194
-
195
225
  return {
196
226
  ...mtnValues,
197
227
  ...mtnHandlers,
198
228
  checkUsernameOrEmail,
199
- signIn,
200
229
  dataForSchema,
201
230
  setDataForSchema,
202
231
  };
@@ -226,6 +255,28 @@ export function useSignUpWithEmail(params: AuthHookParams = {}, openModalPopUp?:
226
255
  };
227
256
  }
228
257
 
258
+ /**
259
+ * Confirm password
260
+ * NOTE: *must* be signed in already
261
+ */
262
+
263
+ export function useConfirmPassword(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
264
+ const [confirmPassword, mtnValues, mtnHandlers] = useMutation(
265
+ confirmPasswordMtn,
266
+ {
267
+ checkMountedBeforeCallback: true,
268
+ openModalPopUp,
269
+ ...params,
270
+ },
271
+ );
272
+
273
+ return {
274
+ confirmPassword,
275
+ ...mtnValues,
276
+ ...mtnHandlers,
277
+ };
278
+ }
279
+
229
280
  // Before are deprecated HOC that needs to be migrated to hooks
230
281
  // Before are deprecated HOC that needs to be migrated to hooks
231
282
  // Before are deprecated HOC that needs to be migrated to hooks
@@ -0,0 +1,85 @@
1
+ import { checkACLPermission } from '@jsb188/mday/utils/organization';
2
+ import type { UseMutationParams } from '@jsb188/graphql/types.d';
3
+ import { OpenModalPopUpFn } from '@jsb188/react/states';
4
+ import { useMemo } from 'react';
5
+ import { updateFragment } from '../cache/index';
6
+ import { deleteLogEntryMtn, editLogEntryMtn } from '../gql/mutations/logMutations';
7
+ import { useMutation } from './index';
8
+ import { useReactiveLogFragment } from './use-log-qry';
9
+ import { useOrgRelFromMyOrganizations } from './use-organization-qry';
10
+
11
+ /**
12
+ * Fetch a single log entry,
13
+ * from cache first; only check server if cache is not found.
14
+ */
15
+
16
+ export function useEditLogEntry(
17
+ viewerAccountId: string,
18
+ logEntryId: string,
19
+ organizationId: string,
20
+ params: UseMutationParams = {},
21
+ openModalPopUp?: OpenModalPopUpFn
22
+ ) {
23
+
24
+ const { organizationRelationship } = useOrgRelFromMyOrganizations(organizationId);
25
+ const notReady = !organizationRelationship;
26
+
27
+ const [editLogEntry, mtnValues, mtnHandlers] = useMutation(
28
+ editLogEntryMtn,
29
+ {
30
+ // checkMountedBeforeCallback: true,
31
+ openModalPopUp,
32
+ ...params,
33
+ },
34
+ );
35
+
36
+ const logEntry = useReactiveLogFragment(logEntryId, null, mtnValues.mutationCount);
37
+ // const logEntry = useReactiveFragment(
38
+ // null,
39
+ // [`$logEntryFragment:${logEntryId}`, [`$logArableFragment:${logEntryId}`, 'details']],
40
+ // mtnValues.mutationCount,
41
+ // );
42
+
43
+ const isMyDocument = !!viewerAccountId && logEntry?.accountId === viewerAccountId;
44
+ const allowEdit = useMemo(() => {
45
+ return checkACLPermission(organizationRelationship, 'logs', isMyDocument ? 'WRITE' : 'MANAGE');
46
+ }, [organizationRelationship?.acl, organizationRelationship?.role]);
47
+
48
+ return {
49
+ logEntry,
50
+ editLogEntry,
51
+ notReady,
52
+ allowEdit,
53
+ ...mtnValues,
54
+ ...mtnHandlers,
55
+ };
56
+ }
57
+
58
+ /**
59
+ * Delete a single log entry
60
+ * NOTE: Don't check ACL and allow server to handle it.
61
+ */
62
+
63
+ export function useDeleteLogEntry(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
64
+
65
+ const { onCompleted, ...rest } = params;
66
+ const [deleteLogEntry, mtnValues, mtnHandlers, updateObservers] = useMutation(
67
+ deleteLogEntryMtn,
68
+ {
69
+ openModalPopUp,
70
+ onCompleted: (data, dataMtnValues, variables) => {
71
+ if (data?.deleteLogEntry) {
72
+ updateFragment(`$logEntryFragment:${variables.logEntryId}`, { __deleted: true }, null, false, updateObservers);
73
+ }
74
+ onCompleted?.(data, dataMtnValues, variables);
75
+ },
76
+ ...rest,
77
+ },
78
+ );
79
+
80
+ return {
81
+ deleteLogEntry,
82
+ ...mtnValues,
83
+ ...mtnHandlers,
84
+ };
85
+ }