Skip to content

Commit

Permalink
remove usecase to get email regex from keycloack
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Dec 11, 2024
1 parent 36b6fed commit bf50208
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 77 deletions.
1 change: 0 additions & 1 deletion api/src/rpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ export function createRouter(params: {
currentUser: user
})
),
"getAllowedEmailRegexp": loggedProcedure.query(() => coreContext.userApi.getAllowedEmailRegexp()),
"getAllOrganizations": loggedProcedure.query(() => dbApi.agent.getAllOrganizations()),
"updateEmail": loggedProcedure
.input(
Expand Down
3 changes: 0 additions & 3 deletions web/src/core/adapter/sillApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ export function createSillApi(params: {

return out;
},
"getAllowedEmailRegexp": memoize(() => trpcClient.getAllowedEmailRegexp.query(), {
"promise": true
}),
"getAllOrganizations": memoize(() => trpcClient.getAllOrganizations.query(), {
"promise": true
}),
Expand Down
1 change: 0 additions & 1 deletion web/src/core/adapter/sillApiMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ export const sillApi: SillApi = {
"updateEmail": async ({ newEmail }) => {
console.log(`Update email ${newEmail}`);
},
"getAllowedEmailRegexp": memoize(async () => "/gouv.fr$/", { "promise": true }),
"getAllOrganizations": memoize(async () => ["DINUM", "CNRS", "ESR"], {
"promise": true
}),
Expand Down
4 changes: 0 additions & 4 deletions web/src/core/ports/SillApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ export type SillApi = {
params: TrpcRouterInput["updateEmail"]
) => Promise<TrpcRouterOutput["updateEmail"]>;

getAllowedEmailRegexp: {
(): Promise<TrpcRouterOutput["getAllowedEmailRegexp"]>;
clear: () => void;
};
getAllOrganizations: {
(params: TrpcRouterInput["getAllOrganizations"]): Promise<
TrpcRouterOutput["getAllOrganizations"]
Expand Down
4 changes: 0 additions & 4 deletions web/src/core/usecases/userAccountManagement/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace State {
export type Ready = {
stateDescription: "ready";
accountManagementUrl: string | undefined;
allowedEmailRegexpStr: string;
allOrganizations: string[];
organization: {
value: string | null;
Expand Down Expand Up @@ -63,7 +62,6 @@ export const { reducer, actions } = createUsecaseActions({
}: {
payload: {
accountManagementUrl: string | undefined;
allowedEmailRegexpStr: string;
organization: string | null;
email: string;
allOrganizations: string[];
Expand All @@ -74,7 +72,6 @@ export const { reducer, actions } = createUsecaseActions({
) => {
const {
accountManagementUrl,
allowedEmailRegexpStr,
organization,
email,
allOrganizations,
Expand All @@ -85,7 +82,6 @@ export const { reducer, actions } = createUsecaseActions({
return {
"stateDescription": "ready",
accountManagementUrl,
allowedEmailRegexpStr,
allOrganizations,
"organization": {
"value": organization,
Expand Down
9 changes: 1 addition & 8 deletions web/src/core/usecases/userAccountManagement/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@ export const thunks = {

const user = await getUser();

const [
{ keycloakParams },
allowedEmailRegexpStr,
allOrganizations,
{ agent }
] = await Promise.all([
const [{ keycloakParams }, allOrganizations, { agent }] = await Promise.all([
sillApi.getOidcParams(),
sillApi.getAllowedEmailRegexp(),
sillApi.getAllOrganizations(),
sillApi.getAgent({ "email": user.email })
]);
Expand All @@ -39,7 +33,6 @@ export const thunks = {

dispatch(
actions.initialized({
allowedEmailRegexpStr,
"email": user.email,
"organization": organization,
"accountManagementUrl":
Expand Down
64 changes: 8 additions & 56 deletions web/src/ui/pages/account/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Equals } from "tsafe";
import { declareComponentKeys } from "i18nifty";
import { useCore, useCoreState } from "core";
import { Input } from "@codegouvfr/react-dsfr/Input";
import { z } from "zod";
import { Button } from "@codegouvfr/react-dsfr/Button";
import { OrganizationField } from "../../shared/PromptForOrganization";
import type { PageRoute } from "./route";
Expand Down Expand Up @@ -55,40 +54,19 @@ function AccountReady(props: { className?: string }) {

const { t } = useTranslation({ Account });

const {
email,
organization,
aboutAndIsPublic,
doSupportAccountManagement,
allowedEmailRegExp
} = (function useClosure() {
const state = useCoreState("userAccountManagement", "main");
const { email, aboutAndIsPublic, doSupportAccountManagement } =
(function useClosure() {
const state = useCoreState("userAccountManagement", "main");

assert(state !== undefined);
assert(state !== undefined);

const { allowedEmailRegexpStr, ...rest } = state;

const allowedEmailRegExp = useMemo(
() => new RegExp(allowedEmailRegexpStr),
[allowedEmailRegexpStr]
);

return {
...rest,
allowedEmailRegExp
};
})();
return state;
})();

const { isDark } = useIsDark();

const { userAccountManagement } = useCore().functions;

const [emailInputValue, setEmailInputValue] = useState(email.value);

const [organizationInputValue, setOrganizationInputValue] = useState(
organization.value ?? null
);

const evtAboutInputValue = useConst(() => Evt.create(aboutAndIsPublic.about));

/* prettier-ignore */
Expand Down Expand Up @@ -116,22 +94,6 @@ function AccountReady(props: { className?: string }) {
evtAboutInputValue.state = text;
}, []);

const emailInputValueErrorMessage = (() => {
try {
z.string().email().parse(emailInputValue);
} catch {
return t("not a valid email");
}

if (!allowedEmailRegExp.test(emailInputValue)) {
return t("email domain not allowed", {
"domain": emailInputValue.split("@")[1]
});
}

return undefined;
})();

const { classes, cx, css } = useStyles();

return (
Expand All @@ -142,21 +104,11 @@ function AccountReady(props: { className?: string }) {
<Input
label={t("mail")}
nativeInputProps={{
"onChange": event => setEmailInputValue(event.target.value),
"value": emailInputValue,
"value": email.value,
"name": "email",
"type": "email",
"id": "email",
"onKeyDown": event => {
if (event.key === "Escape") {
setEmailInputValue(email.value);
}
}
"id": "email"
}}
state={
emailInputValueErrorMessage === undefined ? undefined : "error"
}
stateRelatedMessage={emailInputValueErrorMessage}
disabled={true}
/>

Expand Down

0 comments on commit bf50208

Please sign in to comment.