-
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(console,phrases): check duplicated emails when inviting members
- Loading branch information
1 parent
961fd8e
commit 29aa2b7
Showing
19 changed files
with
118 additions
and
87 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
packages/console/src/pages/TenantSettings/TenantMembers/InviteEmailsInput/hooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { emailRegEx } from '@logto/core-kit'; | ||
import { conditional, conditionalArray, conditionalString } from '@silverhand/essentials'; | ||
import { useCallback, useContext } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import useSWR from 'swr'; | ||
|
||
import { useAuthedCloudApi } from '@/cloud/hooks/use-cloud-api'; | ||
import { type TenantMemberResponse } from '@/cloud/types/router'; | ||
import { TenantsContext } from '@/contexts/TenantsProvider'; | ||
import { type RequestError } from '@/hooks/use-api'; | ||
|
||
import { type InviteeEmailItem } from '../types'; | ||
|
||
const useEmailInputUtils = () => { | ||
const cloudApi = useAuthedCloudApi(); | ||
const { currentTenantId } = useContext(TenantsContext); | ||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); | ||
|
||
const { data: existingMembers = [] } = useSWR<TenantMemberResponse[], RequestError>( | ||
`api/tenant/${currentTenantId}/members`, | ||
async () => | ||
cloudApi.get('/api/tenants/:tenantId/members', { params: { tenantId: currentTenantId } }) | ||
); | ||
|
||
/** | ||
* Find duplicated and invalid formatted email addresses. | ||
* | ||
* @param emails Array of email emails. | ||
* @returns | ||
*/ | ||
const findDuplicatedOrInvalidEmails = useCallback( | ||
(emails: string[] = []) => { | ||
const duplicatedEmails = new Set<string>(); | ||
const invalidEmails = new Set<string>(); | ||
const validEmails = new Set<string>( | ||
existingMembers.map(({ primaryEmail }) => primaryEmail ?? '').filter(Boolean) | ||
); | ||
|
||
for (const email of emails) { | ||
if (!emailRegEx.test(email)) { | ||
invalidEmails.add(email); | ||
} | ||
|
||
if (validEmails.has(email)) { | ||
duplicatedEmails.add(email); | ||
} else { | ||
validEmails.add(email); | ||
} | ||
} | ||
|
||
return { | ||
duplicatedEmails, | ||
invalidEmails, | ||
}; | ||
}, | ||
[existingMembers] | ||
); | ||
|
||
const parseEmailOptions = useCallback( | ||
( | ||
inputValues: InviteeEmailItem[] | ||
): { | ||
values: InviteeEmailItem[]; | ||
errorMessage?: string; | ||
} => { | ||
const { duplicatedEmails, invalidEmails } = findDuplicatedOrInvalidEmails( | ||
inputValues.map((email) => email.value) | ||
); | ||
// Show error message and update the inputs' status for error display. | ||
if (duplicatedEmails.size > 0 || invalidEmails.size > 0) { | ||
return { | ||
values: inputValues.map(({ status, ...rest }) => ({ | ||
...rest, | ||
...conditional( | ||
(duplicatedEmails.has(rest.value) || invalidEmails.has(rest.value)) && { | ||
status: 'info', | ||
} | ||
), | ||
})), | ||
errorMessage: conditionalArray( | ||
conditionalString(duplicatedEmails.size > 0 && t('tenant_members.errors.user_exists')), | ||
conditionalString(invalidEmails.size > 0 && t('tenant_members.errors.invalid_email')) | ||
).join(' '), | ||
}; | ||
} | ||
|
||
return { values: inputValues }; | ||
}, | ||
[findDuplicatedOrInvalidEmails] | ||
Check warning on line 89 in packages/console/src/pages/TenantSettings/TenantMembers/InviteEmailsInput/hooks.ts GitHub Actions / ESLint Report Analysispackages/console/src/pages/TenantSettings/TenantMembers/InviteEmailsInput/hooks.ts#L89
|
||
); | ||
|
||
return { | ||
parseEmailOptions, | ||
}; | ||
}; | ||
|
||
export default useEmailInputUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
packages/console/src/pages/TenantSettings/TenantMembers/InviteEmailsInput/utils.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters