Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Impeqq committed Nov 18, 2021
1 parent 78de9ce commit 1e05d06
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 68 deletions.
2 changes: 1 addition & 1 deletion backend/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface IMutation {
sendMessage(input?: SendMessageInput): Message | Promise<Message>;
updatePassword(input?: UpdatePasswordInput): string | Promise<string>;
updateUser(input?: UpdateInput, file?: Upload): string | Promise<string>;
signUp(input?: SignUpInput, file?: Upload): string | Promise<string>;
signUp(input?: SignUpInput): string | Promise<string>;
signIn(input?: SignInInput): string | Promise<string>;
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/user/user.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Query {
type Mutation {
updatePassword(input: UpdatePasswordInput): String
updateUser(input: UpdateInput, file: Upload): String
signUp(input: SignUpInput, file: Upload): String
signUp(input: SignUpInput): String
signIn(input: SignInInput): String
}

Expand Down
15 changes: 4 additions & 11 deletions backend/src/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class UserResolver {

@Query()
@UseGuards(new AuthGuard())
searchUser(@Args('name') name: string) {
return this.userService.findByName(name);
searchUser(@Args('name') name: string, @CurrentUser() user: UserEntity) {
return this.userService.findByName(name, user.id);
}

@Mutation()
Expand All @@ -37,15 +37,8 @@ export class UserResolver {
}

@Mutation()
async signUp(
@Args('input') signUpInput: SignUpInput,
@Args('file') file: { file: FileUpload },
) {
const _file = await file;
const user = await this.userService.createUser(
{ ...signUpInput },
_file.file,
);
async signUp(@Args('input') signUpInput: SignUpInput) {
const user = await this.userService.createUser({ ...signUpInput });
pubsub.publish('userRegistred', { userRegistred: user });
return this.userService.createToken(user);
}
Expand Down
25 changes: 12 additions & 13 deletions backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { UserEntity } from './user.entity';
import { ILike, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import * as jwt from 'jsonwebtoken';
import * as bcrypt from 'bcryptjs';
import { SignInInput, SignUpInput } from './dto/auth.inputs';
Expand Down Expand Up @@ -34,28 +34,27 @@ export class UserService {
);
}

async findByName(name: string) {
async findByName(name: string, id: string) {
if (!name) return [];
return await this.userRepo.find({
relations: ['avatar'],
where: [
{ firstName: ILike(`%${name}%`) },
{ lastName: ILike(`%${name}%`) },
],
});

return await this.userRepo
.createQueryBuilder('user')
.innerJoinAndSelect('user.avatar', 'file')
.where('user.firstName ILike :name', { name })
.orWhere('user.lastName ILike :name', { name })
.andWhere('user.id != :id', { id })
.getMany();
}

async findOne(id: string) {
return await this.userRepo.findOne({ id });
}

async createUser(user: SignUpInput, file: FileUpload) {
async createUser(user: SignUpInput) {
const password = await bcrypt.hash(user.password, 10);

const avatar = await this.fileService.uploadDatabaseFile(file);

return this.userRepo
.create({ ...user, email: user.email.toLowerCase(), password, avatar })
.create({ ...user, email: user.email.toLowerCase(), password })
.save()
.catch((e) => {
if (/(email)[\s\S]+(already exists)/.test(e.detail)) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ export const Chat = () => {
/>
);
})}
<div className={styles.date}>
{/* <div className={styles.date}>
<span>Today</span>
</div>
</div> */}
</ScrollList>
</div>
<Typing />
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/sidebar/search-list/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
top: calc(100% + 10px);
z-index: 1;

& > div > div:last-child {
align-self: center;
}

&.not-found {
padding: 16px;
color: $c_text_primary;
Expand Down
41 changes: 9 additions & 32 deletions frontend/src/pages/Auth/register-block/register-block.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { BaseForm } from "@components";
import {
Button,
ButtonStyle,
ButtonType,
AlertMessage,
IconDirection,
Input,
InputGroup,
} from "@ui";
import GoogleLogoIcon from "@assets/svg/google-logo.svg";
import { Button, ButtonType, AlertMessage, Input } from "@ui";
import styles from "./styles.scss";
import { useForm, UseFormOptions } from "react-hook-form";
import { registerModel } from "@features/models";
Expand All @@ -28,7 +19,7 @@ export const RegisterBlock = () => {
fetchPolicy: "network-only",
onCompleted: ({ signUp }) => {
setItem(AUTH_TOKEN, signUp);
history.push(routePath.main.path);
history.push(routePath.profile.path);
},
});

Expand All @@ -44,15 +35,10 @@ export const RegisterBlock = () => {
password: data?.[registerModel.password],
firstName: data?.[registerModel.firstName],
lastName: data?.[registerModel.lastName],
avatar: data?.[registerModel.avatar].item(0),
},
});
};

const handleRegisterGoogle = () => {
console.log("google");
};

return (
<BaseForm
formMethods={formMethods}
Expand All @@ -61,7 +47,6 @@ export const RegisterBlock = () => {
>
<span className={styles.title}>New to the messenger?</span>
<span className={styles.description}>Instant registration</span>
<Input placeholder={"Avatar"} type="file" name={registerModel.avatar} />
<Input placeholder={"First name"} name={registerModel.firstName} />
<Input placeholder={"Last name"} name={registerModel.lastName} />
<Input placeholder={"Email"} name={registerModel.email} />
Expand All @@ -75,21 +60,13 @@ export const RegisterBlock = () => {
className={styles.errorMessage}
message={error?.message}
/>
<InputGroup>
<Button
text={"Sign Up"}
type={ButtonType.SUBMIT}
loading={loading}
withDisable={true}
/>
<Button
text={"Sign Up"}
iconDirection={IconDirection.RIGHT}
icon={<GoogleLogoIcon />}
style={ButtonStyle.OUTLINE}
onClick={handleRegisterGoogle}
/>
</InputGroup>
<Button
className={styles.button}
text={"Sign Up"}
type={ButtonType.SUBMIT}
loading={loading}
withDisable={true}
/>
</BaseForm>
);
};
4 changes: 4 additions & 0 deletions frontend/src/pages/Auth/register-block/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
.error-message {
margin-bottom: 30px;
}

.button {
width: 100%;
}
}
2 changes: 0 additions & 2 deletions frontend/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ export const SEND_REGISTER = gql`
$password: String!
$firstName: String!
$lastName: String!
$avatar: Upload
) {
signUp(
input: {
Expand All @@ -155,7 +154,6 @@ export const SEND_REGISTER = gql`
email: $email
password: $password
}
file: $avatar
)
}
`;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/ui/atoms/alert-message/alert-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export const AlertMessage = ({ message, className, type }: TProps) => {
}, [message]);

useEffect(() => {
setTimeout(() => {
const timeout = setTimeout(() => {
if (seconds !== 0) {
setSeconds((seconds) => seconds - 1);
}
}, 1000);
return () => clearTimeout(timeout);
}, [seconds]);

if (!message || seconds === 0) return null;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/ui/atoms/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ export const Button = ({
}, [loading, withDisable]);

useEffect(() => {
setTimeout(() => {
const timeout = setTimeout(() => {
if (seconds !== 0) {
setSeconds((seconds) => seconds - 1);
}
}, 1000);
return () => clearTimeout(timeout);
}, [seconds]);

return (
Expand Down
1 change: 1 addition & 0 deletions frontend/src/ui/atoms/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const Input = ({
placeholder={placeholder}
name={name}
onChange={onChange}
autoComplete="off"
className={cn({ [styles.error]: errors[name] })}
/>
{hidden && (
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/ui/templates/main/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ export const Main: React.FC = ({ children }) => {
},
});

const isGuest = getItem(AUTH_TOKEN);
const isGuest = !getItem(AUTH_TOKEN);

useEffect(() => {
fetchMe();
}, [fetchMe, isGuest]);
if (!isGuest) {
history.push(routePath.main.path);
}
}, [fetchMe, isGuest, history]);

return (
<>
Expand All @@ -43,8 +46,8 @@ export const Main: React.FC = ({ children }) => {
lastName={me?.lastName}
avatarId={me?.avatar?.id}
/>
<div className={cn(styles.flex, { [styles.guest]: !me })}>
{me && <Sidebar />}
<div className={cn(styles.flex, { [styles.guest]: isGuest })}>
{!isGuest && <Sidebar />}
{children}
</div>
</>
Expand Down

0 comments on commit 1e05d06

Please sign in to comment.