-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,25 @@ | ||
'use client' | ||
|
||
import { Card, CardBody, CardHeader, Tab, Tabs } from '@nextui-org/react' | ||
import NextLink from 'next/link' | ||
import { usePathname } from 'next/navigation' | ||
import { ReactNode } from 'react' | ||
|
||
import SettingsTemplate from '@/components/settings-template/settings-template' | ||
|
||
const TABS = [ | ||
{ | ||
target: 'profile', | ||
title: 'Perfil', | ||
}, | ||
] | ||
|
||
export default function Layout({ children }: { children: ReactNode }) { | ||
const pathname = usePathname() | ||
|
||
return ( | ||
<> | ||
<Card className="flex flex-col gap-2 w-full p-1"> | ||
<CardHeader> | ||
<div className="h1 font-bold text-2xl">Ajustes de usuario</div> | ||
</CardHeader> | ||
<CardBody className="overflow-hidden"> | ||
<Tabs | ||
size="lg" | ||
aria-label="Options" | ||
selectedKey={pathname} | ||
classNames={{ | ||
tab: 'max-w-fit', | ||
tabList: 'w-full', | ||
}} | ||
> | ||
<Tab | ||
key="/settings/profile" | ||
href="/settings/profile" | ||
title="Perfil" | ||
as={NextLink} | ||
/> | ||
</Tabs> | ||
<div className="mt-4">{children}</div> | ||
</CardBody> | ||
</Card> | ||
<SettingsTemplate pathname={pathname} tabs={TABS}> | ||
{children} | ||
</SettingsTemplate> | ||
</> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { err, ok, Result } from 'neverthrow' | ||
|
||
import NotFoundError from '@/core/common/domain/errors/application/not-found-error' | ||
import ApplicationError from '@/core/common/domain/errors/application-error' | ||
import DomainError from '@/core/common/domain/errors/domain-error' | ||
import Email from '@/core/common/domain/value-objects/email' | ||
import FullName from '@/core/common/domain/value-objects/fullname' | ||
import User from '@/core/user/domain/model/user.entity' | ||
import Users from '@/core/user/domain/services/users.repository' | ||
import UpdateSettingRequest from '@/core/user/dto/requests/update-setting.request' | ||
|
||
export default class UpdateSettingUseCase { | ||
constructor(private readonly users: Users) {} | ||
|
||
async with( | ||
command: UpdateSettingRequest, | ||
): Promise<Result<void, NotFoundError | DomainError>> { | ||
return Email.create(command.email) | ||
.asyncAndThen((email) => this.users.findByEmail(email)) | ||
.andThen((user) => this.updateUser(user, command)) | ||
.andThen((user) => this.users.save(user)) | ||
} | ||
|
||
private updateUser(user: User, command: UpdateSettingRequest) { | ||
switch (command.field) { | ||
case 'name': { | ||
return this.updateFullName(command, user) | ||
} | ||
default: { | ||
return err( | ||
new ApplicationError( | ||
`Field ${command.field} does not exists in User entity`, | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
private updateFullName(command: UpdateSettingRequest, user: User) { | ||
return FullName.create(command.value).andThen((fullName) => { | ||
user.name = fullName | ||
|
||
return ok(user) | ||
}) | ||
} | ||
} |
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,13 @@ | ||
import { DeepReadonly } from 'ts-essentials' | ||
|
||
type UpdateSettingRequest = DeepReadonly<{ | ||
email: string | ||
field: string | ||
value: string | ||
}> | ||
|
||
const UpdateSettingRequest = { | ||
with: (properties: UpdateSettingRequest): UpdateSettingRequest => properties, | ||
} | ||
|
||
export default UpdateSettingRequest |
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,75 @@ | ||
'use server' | ||
|
||
import { revalidateTag } from 'next/cache' | ||
import { z } from 'zod' | ||
|
||
import UpdateSettingRequest from '@/core/user/dto/requests/update-setting.request' | ||
import me from '@/core/user/infrastructure/actions/me' | ||
import container from '@/lib/container' | ||
import FormResponse from '@/lib/zod/form-response' | ||
|
||
export type UpdatableFields = 'name' | 'email' | ||
|
||
interface UpdateSettingForm { | ||
field: UpdatableFields | ||
value: string | ||
} | ||
|
||
const UpdateSettingMap = { | ||
name: z.object({ | ||
field: z.string(), | ||
value: z.string().min(1).max(64), | ||
}), | ||
} | ||
|
||
type UpdateSettingMapKeys = keyof typeof UpdateSettingMap | ||
|
||
export async function updateSetting( | ||
previousState: FormResponse<UpdateSettingForm>, | ||
formData: FormData, | ||
): Promise<FormResponse<UpdateSettingForm>> { | ||
const user = await me() | ||
const { field } = previousState.data | ||
|
||
if (!user) { | ||
return FormResponse.custom( | ||
[field], | ||
'Error en la sesión del usuario', | ||
previousState.data, | ||
) | ||
} | ||
|
||
const { email } = user | ||
|
||
if (!(field in UpdateSettingMap)) { | ||
return FormResponse.custom<UpdateSettingForm>( | ||
[field], | ||
'El campo no es válido', | ||
previousState.data, | ||
) | ||
} | ||
|
||
const result = UpdateSettingMap[field as UpdateSettingMapKeys].safeParse( | ||
Object.fromEntries(formData), | ||
) | ||
|
||
if (!result.success) { | ||
return FormResponse.withError<UpdateSettingForm>( | ||
result.error, | ||
previousState.data, | ||
) | ||
} | ||
|
||
const { value } = result.data | ||
|
||
await container.updateSetting.with( | ||
UpdateSettingRequest.with({ email, field, value }), | ||
) | ||
|
||
revalidateTag(`role-for-${email}`) | ||
|
||
return FormResponse.success<UpdateSettingForm>( | ||
result.data as UpdateSettingForm, | ||
'Perfil actualizado.', | ||
) | ||
} |
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