Skip to content

Commit

Permalink
feat(chat): init users management container
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Dec 29, 2024
1 parent 4e7d63b commit be1ffc6
Show file tree
Hide file tree
Showing 73 changed files with 1,272 additions and 115 deletions.
1 change: 0 additions & 1 deletion apps/admin/src/helpers/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {

import type { SdkCreateUserAuthMethodsT } from '@llm/sdk';

import { genRandomPassword } from '@llm/commons';
import { Checkbox, FormField, Input } from '@llm/ui';
import { genRandomPassword } from '~/helpers';
import { useI18n } from '~/i18n';

type Props = ValidationErrorsListProps<SdkCreateUserAuthMethodsT>;
Expand Down
2 changes: 1 addition & 1 deletion apps/admin/src/modules/users/form/update/fields/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './user-organization-info-form-field';
export * from './user-organization-settings-form-field';
export * from './user-update-auth-methods-form-field';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
controlled,
useFormValidatorMessages,
type ValidationErrorsListProps,
} from '@under-control/forms';

import type { SdkTableRowWithIdNameT, SdkUpdateUserOrganizationInputT } from '@llm/sdk';

import { FormField } from '@llm/ui';
import { useI18n } from '~/i18n';
import { OrganizationsSearchSelect, UserOrganizationRoleSelect } from '~/modules/organizations';

type Props = ValidationErrorsListProps<SdkUpdateUserOrganizationInputT> & {
organization: SdkTableRowWithIdNameT;
};

export const UserOrganizationSettingsFormField = controlled<SdkUpdateUserOrganizationInputT, Props>((
{
organization,
errors,
control: { bind },
},
) => {
const t = useI18n().pack.modules.users.form.fields.organization;
const validation = useFormValidatorMessages({ errors });

return (
<>
<FormField
className="uk-margin"
label={t.choose.label}
>
<OrganizationsSearchSelect
defaultValue={organization}
disabled
/>
</FormField>

<FormField
className="uk-margin"
label={t.role.label}
{...validation.extract('role')}
>
<UserOrganizationRoleSelect {...bind.path('role')} required />
</FormField>

<hr />
</>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { useMemo } from 'react';

import type { SdkUpdateUserAuthMethodsT } from '@llm/sdk';

import { genRandomPassword } from '@llm/commons';
import { Checkbox, FormField, Input } from '@llm/ui';
import { genRandomPassword } from '~/helpers';
import { useI18n } from '~/i18n';

type Props = ValidationErrorsListProps<SdkUpdateUserAuthMethodsT>;
Expand Down
8 changes: 8 additions & 0 deletions apps/admin/src/modules/users/form/update/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {
SdkTableRowWithIdT,
SdkUpdateUserInputT,
} from '@llm/sdk';

export type UpdateUserFormValue =
SdkTableRowWithIdT &
SdkUpdateUserInputT;
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { type FormHookAttrs, useForm } from '@under-control/forms';
import { flow } from 'fp-ts/lib/function';

import { runTask, tapTaskEither } from '@llm/commons';
import { type SdkTableRowWithIdT, type SdkUpdateUserInputT, useSdkForLoggedIn } from '@llm/sdk';
import { useSdkForLoggedIn } from '@llm/sdk';
import { usePredefinedFormValidators, useSaveTaskEitherNotification } from '@llm/ui';

import type { UpdateUserFormValue } from './types';

import { useUseAuthFormValidator } from '../shared';

type UpdateUserFormHookAttrs =
& Omit<
FormHookAttrs<SdkUpdateUserInputT & SdkTableRowWithIdT>,
FormHookAttrs<UpdateUserFormValue>,
'validation' | 'onSubmit'
>
& {
Expand All @@ -23,9 +25,9 @@ export function useUserUpdateForm(
}: UpdateUserFormHookAttrs,
) {
const { sdks } = useSdkForLoggedIn();
const { emailFormatValidator } = usePredefinedFormValidators<SdkUpdateUserInputT & SdkTableRowWithIdT>();
const { emailFormatValidator } = usePredefinedFormValidators<UpdateUserFormValue>();
const saveNotifications = useSaveTaskEitherNotification();
const authValidator = useUseAuthFormValidator<SdkUpdateUserInputT & SdkTableRowWithIdT>();
const authValidator = useUseAuthFormValidator<UpdateUserFormValue>();

return useForm({
resetAfterSubmit: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useMemo } from 'react';

import type { SdkUserT } from '@llm/sdk';

import {
Expand All @@ -10,8 +12,10 @@ import {
} from '@llm/ui';
import { useI18n } from '~/i18n';

import type { UpdateUserFormValue } from './types';

import { UserSharedFormFields } from '../shared';
import { UserOrganizationInfoField, UserUpdateAuthMethodsFormField } from './fields';
import { UserOrganizationSettingsFormField, UserUpdateAuthMethodsFormField } from './fields';
import { useUserUpdateForm } from './use-user-update-form';

export type UserUpdateFormModalProps =
Expand All @@ -30,14 +34,32 @@ export function UserUpdateFormModal(
}: UserUpdateFormModalProps,
) {
const t = useI18n().pack.modules.users.form;
const { handleSubmitEvent, validator, submitState, bind } = useUserUpdateForm({
defaultValue: {

const defaultValue = useMemo<UpdateUserFormValue>(() => {
const attrs = {
id: user.id,
active: user.active,
archiveProtection: user.archiveProtection,
auth: user.auth,
email: user.email,
},
};

return (
user.role === 'user'
? {
...attrs,
role: 'user',
organization: user.organization,
}
: {
...attrs,
role: 'root',
}
);
}, [user]);

const { handleSubmitEvent, validator, submitState, bind } = useUserUpdateForm({
defaultValue,
onAfterSubmit,
});

Expand All @@ -61,9 +83,12 @@ export function UserUpdateFormModal(
)}
>
{user.role === 'user' && (
<UserOrganizationInfoField user={user} />
<UserOrganizationSettingsFormField
organization={user.organization}
{...validator.errors.extract('organization', { nested: true })}
{...bind.path('organization')}
/>
)}

<UserSharedFormFields
errors={validator.errors.all as unknown as any}
{...bind.merged()}
Expand Down
3 changes: 1 addition & 2 deletions apps/admin/src/modules/users/table/users-table-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { z } from 'zod';

import { pipe } from 'fp-ts/lib/function';

import { tapTaskOption } from '@llm/commons';
import { genRandomPassword, tapTaskOption } from '@llm/commons';
import { useAsyncCallback } from '@llm/commons-front';
import { SdkIdNameUrlEntryV, SdKSearchUsersInputV, serializeSdkIdNameUrlEntry, useSdkForLoggedIn } from '@llm/sdk';
import {
Expand All @@ -14,7 +14,6 @@ import {
ResetFiltersButton,
useDebouncedPaginatedSearch,
} from '@llm/ui';
import { genRandomPassword } from '~/helpers';
import { useI18n } from '~/i18n';
import { OrganizationsSearchSelect } from '~/modules/organizations';

Expand Down
55 changes: 55 additions & 0 deletions apps/chat/src/i18n/packs/i18n-lang-en.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import deepmerge from 'deepmerge';

import type { SdkOrganizationUserRoleT } from '@llm/sdk';

import { I18N_FORWARDED_EN_PACK } from '@llm/ui';

const I18N_USER_ORGANIZATION_ROLES_EN: Record<SdkOrganizationUserRoleT, string> = {
owner: 'Właściciel',
member: 'Użytkownik',
};

export const I18N_PACK_EN = deepmerge(I18N_FORWARDED_EN_PACK, {
navigation: {
links: {
Expand Down Expand Up @@ -389,6 +396,54 @@ export const I18N_PACK_EN = deepmerge(I18N_FORWARDED_EN_PACK, {
remove: 'Remove from favorites',
},
},
organizations: {
userRoles: I18N_USER_ORGANIZATION_ROLES_EN,
},
users: {
form: {
title: {
create: 'Create user',
edit: 'Edit user',
},
fields: {
email: {
label: 'E-Mail',
placeholder: 'Enter e-mail address',
},
flags: {
label: 'Flags',
},
active: {
label: 'Active',
},
organization: {
role: {
label: 'Role in organization',
},
},
auth: {
label: 'Authentication',
email: {
label: 'Email',
placeholder: 'Enter email address',
},
password: {
label: 'Password',
placeholder: 'Enter password',
},
resetPassword: {
label: 'Reset password',
},
},
},
},
row: {
authMethod: {
password: 'Password',
email: 'Email',
},
},
},
footer: {
copyright: 'Open Source AI Platform',
madeWith: 'Made with',
Expand Down
55 changes: 55 additions & 0 deletions apps/chat/src/i18n/packs/i18n-lang-pl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import deepmerge from 'deepmerge';

import type { SdkOrganizationUserRoleT } from '@llm/sdk';

import { I18N_FORWARDED_PL_PACK } from '@llm/ui';

import type { I18nLangPack } from './i18n-packs';

const I18N_USER_ORGANIZATION_ROLES_PL: Record<SdkOrganizationUserRoleT, string> = {
owner: 'Owner',
member: 'Member',
};

export const I18N_PACK_PL: I18nLangPack = deepmerge(I18N_FORWARDED_PL_PACK, {
navigation: {
links: {
Expand Down Expand Up @@ -391,6 +398,54 @@ export const I18N_PACK_PL: I18nLangPack = deepmerge(I18N_FORWARDED_PL_PACK, {
remove: 'Usuń z ulubionych',
},
},
organizations: {
userRoles: I18N_USER_ORGANIZATION_ROLES_PL,
},
users: {
form: {
title: {
create: 'Utwórz użytkownika',
edit: 'Edytuj użytkownika',
},
fields: {
email: {
label: 'E-Mail',
placeholder: 'Wprowadź adres e-mail',
},
flags: {
label: 'Flagi',
},
active: {
label: 'Aktywny',
},
organization: {
role: {
label: 'Rola w organizacji',
},
},
auth: {
label: 'Uwierzytelnianie',
email: {
label: 'Email',
placeholder: 'Wprowadź adres email',
},
password: {
label: 'Hasło',
placeholder: 'Wprowadź hasło',
},
resetPassword: {
label: 'Zresetuj hasło',
},
},
},
},
row: {
authMethod: {
email: 'E-Mail',
password: 'Hasło',
},
},
},
footer: {
copyright: 'Platforma AI Open Source',
madeWith: 'Stworzone z',
Expand Down
Loading

0 comments on commit be1ffc6

Please sign in to comment.