Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: adiciona uma descrição ao usuário #1470

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.up = async (pgm) => {
await pgm.addColumns('users', {
description: {
type: 'text',
check: 'length(description) <= 5000',
notNull: true,
default: '',
},
});
};

exports.down = false;
4 changes: 4 additions & 0 deletions models/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function filterInput(user, feature, input) {
username: input.username,
email: input.email,
password: input.password,
description: input.description,
notifications: input.notifications,
};
}
Expand Down Expand Up @@ -175,6 +176,7 @@ function filterOutput(user, feature, output) {
filteredOutputValues = {
id: output.id,
username: output.username,
description: output.description,
features: output.features,
tabcoins: output.tabcoins,
tabcash: output.tabcash,
Expand All @@ -189,6 +191,7 @@ function filterOutput(user, feature, output) {
id: output.id,
username: output.username,
email: output.email,
description: output.description,
notifications: output.notifications,
features: output.features,
tabcoins: output.tabcoins,
Expand All @@ -203,6 +206,7 @@ function filterOutput(user, feature, output) {
filteredOutputValues = output.map((user) => ({
id: user.id,
username: user.username,
description: user.description,
features: user.features,
tabcoins: user.tabcoins,
tabcash: user.tabcash,
Expand Down
5 changes: 4 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ async function update(username, postedUserData, options = {}) {
username = $2,
email = $3,
password = $4,
notifications = $5,
description = $5,
notifications = $6,
updated_at = (now() at time zone 'utc')
WHERE
id = $1
Expand All @@ -261,6 +262,7 @@ async function update(username, postedUserData, options = {}) {
userWithUpdatedValues.username,
userWithUpdatedValues.email,
userWithUpdatedValues.password,
userWithUpdatedValues.description,
userWithUpdatedValues.notifications,
],
};
Expand All @@ -286,6 +288,7 @@ async function validatePatchSchema(postedUserData) {
username: 'optional',
email: 'optional',
password: 'optional',
description: 'optional',
notifications: 'optional',
});

Expand Down
19 changes: 19 additions & 0 deletions models/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ const schemas = {
});
},

description: function () {
return Joi.object({
description: Joi.string()
.replace(/(\s|\p{C}|\u2800|\u034f|\u115f|\u1160|\u17b4|\u17b5|\u3164|\uffa0)+$|\u0000/gsu, '')
.max(5000)
.invalid(null)
.allow('')
.when('$required.description', { is: 'required', then: Joi.required(), otherwise: Joi.optional() })
.messages({
'any.required': `"description" é um campo obrigatório.`,
'string.base': `"description" deve ser do tipo String.`,
'string.max': `"description" deve conter no máximo {#limit} caracteres.`,
'any.invalid': `"description" possui o valor inválido "null".`,
}),
});
},

notifications: function () {
return Joi.object({
notifications: Joi.boolean()
Expand Down Expand Up @@ -867,6 +884,8 @@ const reservedUsernames = [
'dados',
'dashboard',
'desconectar',
'descricao',
'description',
'deslogar',
'diretrizes',
'discussao',
Expand Down
23 changes: 19 additions & 4 deletions pages/[username]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Label,
LabelGroup,
Pagehead,
Viewer,
useConfirm,
} from '@/TabNewsUI';
import { KebabHorizontalIcon, TrashIcon } from '@primer/octicons-react';
Expand Down Expand Up @@ -86,12 +87,11 @@ export default function Home({ contentListFound, pagination, userFound: userFoun
function OptionsMenu() {
return (
<Box sx={{ position: 'relative' }}>
<Box sx={{ position: 'absolute', right: 0 }}>
<Box sx={{ position: 'absolute', right: 0, top: 2 }}>
<ActionMenu>
<ActionMenu.Anchor>
<IconButton size="small" icon={KebabHorizontalIcon} aria-label="Editar usuário" />
</ActionMenu.Anchor>

<ActionMenu.Overlay>
<ActionList>
{!userFound?.features?.includes('nuked') && (
Expand Down Expand Up @@ -129,13 +129,28 @@ export default function Home({ contentListFound, pagination, userFound: userFoun
</Flash>
)}

<Box sx={{ width: '100%', display: 'flex', alignItems: 'flex-start' }}>
<Pagehead as="h1" sx={{ width: '100%', mt: 0, pt: 0, pb: 3, display: 'flex', alignItems: 'center' }}>
<Box sx={{ width: '100%', display: 'flex' }}>
<Pagehead as="h1" sx={{ width: '100%', mt: 0, pt: 0, pb: 3, mb: 3 }}>
{userFound.username} <UserFeatures />
</Pagehead>
{user?.features?.includes('ban:user') && OptionsMenu()}
</Box>

{userFound.description && (
<Box
sx={{
borderWidth: 1,
borderStyle: 'solid',
borderColor: 'border.default',
borderRadius: '6px',
width: '100%',
p: 3,
mb: 3,
}}>
<Viewer value={userFound.description} />
</Box>
)}

<ContentList
contentList={contentListFound}
pagination={pagination}
Expand Down
1 change: 1 addition & 0 deletions pages/api/v1/users/[username]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function patchValidationHandler(request, response, next) {
username: 'optional',
email: 'optional',
password: 'optional',
description: 'optional',
notifications: 'optional',
});

Expand Down
1 change: 1 addition & 0 deletions pages/interface/hooks/useUser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function UserProvider({ children }) {
const cachedUserProperties = {
id: responseBody.id,
username: responseBody.username,
description: responseBody.description,
features: responseBody.features,
tabcoins: responseBody.tabcoins,
tabcash: responseBody.tabcash,
Expand Down
27 changes: 25 additions & 2 deletions pages/perfil/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Button,
Checkbox,
DefaultLayout,
Editor,
Flash,
FormControl,
Heading,
Expand All @@ -16,11 +17,10 @@ import { useEffect, useRef, useState } from 'react';

export default function EditProfile() {
return (
<DefaultLayout containerWidth="small" metadata={{ title: 'Editar Perfil' }}>
<DefaultLayout containerWidth="medium" metadata={{ title: 'Editar Perfil' }}>
<Heading as="h1" sx={{ mb: 3 }}>
Editar Perfil
</Heading>

<EditProfileForm />
</DefaultLayout>
);
Expand Down Expand Up @@ -56,6 +56,7 @@ function EditProfileForm() {
const [isLoading, setIsLoading] = useState(false);
const [errorObject, setErrorObject] = useState(undefined);
const [emailDisabled, setEmailDisabled] = useState(false);
const [description, setDescription] = useState(user?.description || '');

function clearErrors() {
setErrorObject(undefined);
Expand Down Expand Up @@ -106,6 +107,10 @@ function EditProfileForm() {
payload.email = email;
}

if (user.description !== description) {
payload.description = description;
}

if (user.notifications !== notifications) {
payload.notifications = notifications;
}
Expand Down Expand Up @@ -235,6 +240,24 @@ function EditProfileForm() {
)}
</FormControl>

<FormControl id="description">
<FormControl.Label>Descrição</FormControl.Label>

<Editor
onChange={(value) => {
clearErrors();
setDescription(value);
}}
value={description}
isValid={errorObject?.key === 'description'}
compact={true}
/>

{errorObject?.key === 'description' && errorObject?.type === 'string.max' && (
<FormControl.Validation variant="error">{errorObject.message}</FormControl.Validation>
)}
</FormControl>

<FormControl id="notifications" sx={{ gap: 2, alignItems: 'center' }}>
<FormControl.Label>Receber notificações por email</FormControl.Label>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe('Use case: Registration Flow (all successfully)', () => {
id: postUserResponseBody.id,
username: postUserResponseBody.username,
email: '[email protected]',
description: '',
notifications: true,
features: [
'create:session',
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/api/v1/user/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('GET /api/v1/user', () => {
expect(responseBody).toStrictEqual({
id: defaultUser.id,
username: defaultUser.username,
description: defaultUser.description,
email: defaultUser.email,
notifications: defaultUser.notifications,
features: defaultUser.features,
Expand Down Expand Up @@ -238,6 +239,7 @@ describe('GET /api/v1/user', () => {
expect(responseBody).toStrictEqual({
id: defaultUser.id,
username: defaultUser.username,
description: defaultUser.description,
email: defaultUser.email,
notifications: defaultUser.notifications,
features: defaultUser.features,
Expand Down Expand Up @@ -291,6 +293,7 @@ describe('GET /api/v1/user', () => {
expect(responseBody).toStrictEqual({
id: defaultUser.id,
username: defaultUser.username,
description: defaultUser.description,
email: defaultUser.email,
notifications: defaultUser.notifications,
features: defaultUser.features,
Expand Down Expand Up @@ -343,6 +346,7 @@ describe('GET /api/v1/user', () => {
expect(responseBody).toStrictEqual({
id: defaultUser.id,
username: defaultUser.username,
description: defaultUser.description,
email: defaultUser.email,
notifications: defaultUser.notifications,
features: defaultUser.features,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/api/v1/users/[username]/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ describe('DELETE /api/v1/users/[username]', () => {
expect(nukeResponseBody).toStrictEqual({
id: secondUser.id,
username: secondUser.username,
description: secondUser.description,
features: ['nuked'],
tabcoins: 0,
tabcash: 0,
Expand Down Expand Up @@ -550,6 +551,7 @@ describe('DELETE /api/v1/users/[username]', () => {
expect(nuke1ResponseBody).toStrictEqual({
id: secondUser.id,
username: secondUser.username,
description: secondUser.description,
features: ['nuked'],
tabcoins: 0,
tabcash: 0,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/api/v1/users/[username]/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('GET /api/v1/users/[username]', () => {
expect(responseBody).toStrictEqual({
id: userCreated.id,
username: 'userNameToBeFound',
description: userCreated.description,
features: userCreated.features,
tabcoins: userCreated.tabcoins,
tabcash: userCreated.tabcash,
Expand Down Expand Up @@ -117,6 +118,7 @@ describe('GET /api/v1/users/[username]', () => {
expect(responseBody).toStrictEqual({
id: userCreated.id,
username: 'userNameToBeFoundCAPS',
description: userCreated.description,
features: userCreated.features,
tabcoins: userCreated.tabcoins,
tabcash: userCreated.tabcash,
Expand Down
Loading