Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dzencot committed Jan 20, 2023
1 parent 28ca262 commit f8c8053
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 34 deletions.
12 changes: 5 additions & 7 deletions backend/src/snippets/snippets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { CreateSnippetDto } from './dto/create-snippet.dto';
import { UpdateSnippetDto } from './dto/update-snippet.dto';
import { Snippet } from './interfaces/snippets.interface';
import { SnippetsService } from './snippets.service';
import { UsersService } from '../users/users.service';
import { HttpExceptionFilter } from './exceptions/http-exception.filter';
import { ParseIntPipe } from './pipes/parse-int.pipe';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
Expand All @@ -27,19 +26,18 @@ import { User } from '../users/interfaces/users.interface';
@Controller('snippets')
@UseFilters(new HttpExceptionFilter())
export class SnippetsController {
constructor(
private snippetsService: SnippetsService,
private usersService: UsersService,
) {}
constructor(private snippetsService: SnippetsService) {}

@Get()
async findAll(): Promise<Snippet[]> {
console.log('check')
return this.snippetsService.findAll();
}

@Get(':login/:slug')
async findOneByLoginSlug(@Param('login') login: string, @Param('slug') slug: string): Promise<Snippet> {
async findOneByLoginSlug(
@Param('login') login: string,
@Param('slug') slug: string,
): Promise<Snippet> {
return this.snippetsService.findByLoginSlug(login, slug);
}

Expand Down
4 changes: 1 addition & 3 deletions backend/src/snippets/snippets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export class SnippetsService {
const trimmedName = name.trim();
const extension = path.extname(trimmedName);
const basename = path.basename(trimmedName, extension);
const slug = `${basename
.replace(/\s/g, '-')
.toLowerCase()}_${extension.slice(1)}`;
const slug = `${basename.replace(/\s/g, '-').toLowerCase()}`;
const snippets = await this.snippetManager
.createQueryBuilder(Snippets, 'snippet')
.where('snippet.userId= :id', { id })
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useDispatch, useSelector } from 'react-redux';
// import { useLoaderData } from 'react-router';
import { useParams } from 'react-router-dom';
import { MonacoEditor } from './components/Editor/index.jsx';
import { Button } from './components/Button/index.jsx';
import { SnippetButton } from './components/SnippetButton/index.jsx';
import { Terminal } from './components/Terminal/index.jsx';
import { actions } from './slices/index.js';
import { useSnippets } from './hooks';
Expand Down Expand Up @@ -37,7 +37,7 @@ export function App() {
<main className="container-fluid bg-dark py-5">
<div className="row mb-2">
<div className="col-12">
<Button />
<SnippetButton />
<div className="mt-2 text-center text-muted">
<small>
{isAllSaved ? t('editor.allSaved') : t('editor.unsavedChanges')}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/AppRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function AppRoutes() {
<Route path="/" element={<Layout />}>
<Route index element={<Landing />} />
<Route path="/editor" element={<App />} />
<Route path="/users/:login/snippets/:slug" element={<App />} />
<Route path={routes.aboutPagePath()} element={<About />} />
<Route element={<ProtectedRoute user={isLoggedIn} />}>
<Route path={routes.profilePagePath()} element={<Profile />} />
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/Pages/NotFound.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { useTranslation } from 'react-i18next';

function NotFound() {
const { t } = useTranslation();

return (<div>{t('appRotes.pageNotFound')}</div>);
return <div>{t('appRotes.pageNotFound')}</div>;
}

export default NotFound;
export default NotFound;
4 changes: 2 additions & 2 deletions frontend/src/components/Embed/EmbedRunButton.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { useButton } from '../Button/hooks';
import classes from '../Button/Button.module.css';
import { useButton } from '../SnippetButton/hooks';
import classes from '../SnippetButton/SnippetButton.module.css';

export const EmbedRunButton = memo(() => {
const { onClick, disabled } = useButton();
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Embed/EmbedSnippet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EmbedRunButton } from './EmbedRunButton.jsx';
import { Terminal } from '../Terminal';
import { actions } from '../../slices/index.js';
import { useSnippets } from '../../hooks';
import classes from '../Button/Button.module.css';
import classes from '../SnippetButton/SnippetButton.module.css';

function EmbedSnippet() {
const snippetApi = useSnippets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export const useButton = () => {
() => dispatch(runCode(code)),
[dispatch, runCode, code],
);
const update = async (id) => {
const response = await axios.put(routes.updateSnippetPath(id), { code });
const update = async (id, name) => {
const response = await axios.put(routes.updateSnippetPath(id), {
code,
name,
});
dispatch(actions.updateSavedCode(code));
return response;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useTranslation } from 'react-i18next';
import React, { memo, useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import classes from './Button.module.css';
import { useParams } from 'react-router-dom';
import classes from './SnippetButton.module.css';
import { useButton } from './hooks';
import { useAuth, useSnippets } from '../../hooks';

import { actions as modalActions } from '../../slices/modalSlice.js';

export const Button = memo(() => {
export const SnippetButton = memo(() => {
const { onClick, disabled, update } = useButton();
const [currentSnippetId, setCurrentSnippetId] = useState();
const dispatch = useDispatch();
Expand All @@ -17,16 +18,19 @@ export const Button = memo(() => {
const params = useParams();

useEffect(() => {
const snippetParams = {
login: params.login,
slug: params.slug,
const getSnippetData = async () => {
const snippetParams = {
login: params.login,
slug: params.slug,
};
if (snippetsApi.hasViewSnippetParams(snippetParams)) {
const snippetData = await snippetsApi.getSnippetDataByViewParams(snippetParams);
setCurrentSnippetId(snippetData.id);
} else {
setCurrentSnippetId(false);
}
};
if (snippetsApi.hasViewSnippetParams(snippetParams)) {
const decodedId = snippetsApi.getSnippetIdFromParams();
setCurrentSnippetId(decodedId);
} else {
setCurrentSnippetId(false);
}
getSnippetData();
}, []);

const getTypeOfModal = (isLoggedIn) => {
Expand Down Expand Up @@ -70,7 +74,7 @@ export const Button = memo(() => {
disabled={disabled}
onClick={() => {
onClick();
update(currentSnippetId);
update(currentSnippetId, params.slug);
}}
>
{t('editor.runButton')}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/providers/SnippetsProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function SnippetsProvider({ children }) {
};

const getSnippetDataByViewParams = async ({ login, slug }) => {
const { data } = await axios.get(routes.getSnippetPathByLoginSlug(login, slug));
const { data } = await axios.get(
routes.getSnippetPathByLoginSlug(login, slug),
);
return data;
};

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default {
snippetsPath: () => [apiPath, 'snippets'].join('/'), // get - shows all snippets
createSnippetPath: () => [apiPath, 'snippets'].join('/'), // post - create snippet: { name, code }
getSnippetPath: (id) => [apiPath, 'snippets', `${id}`].join('/'), // get snippet
getSnippetPathByLoginSlug: (login, slug) => [apiPath, 'snippets', login, slug].join('/'),
getSnippetPathByLoginSlug: (login, slug) =>
[apiPath, 'snippets', login, slug].join('/'),
updateSnippetPath: (id) => [apiPath, 'snippets', `${id}`].join('/'), // put - update snippet info: { name, code }
deleteSnippetPath: (id) => [apiPath, 'snippets', `${id}`].join('/'), // delete snippet
homePagePath: () => '/editor',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/slices/terminalSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import axios from 'axios';
export const runCode = createAsyncThunk(
'terminal/runCode',
async (code) => {
const { data, status } = await axios.get(`api/compile`, {
const { data, status } = await axios.get(`/api/compile`, {
params: { code },
});

Expand Down

0 comments on commit f8c8053

Please sign in to comment.