From 3e0efc356221cb44e8922478b7b2d76366431ff0 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Sun, 7 Apr 2024 08:49:15 +0300 Subject: [PATCH 01/10] added locales for saveButton --- frontend/src/locales/en.json | 3 ++- frontend/src/locales/ru.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.json index 3fd1df6f..9a892d43 100644 --- a/frontend/src/locales/en.json +++ b/frontend/src/locales/en.json @@ -337,7 +337,8 @@ "openOnRunIT": "Edit on RunIT", "rename": "Renane", "run": "Run", - "share": "Share" + "share": "Share", + "save": "Save" }, "toasts": { "duplicateSnippet": { diff --git a/frontend/src/locales/ru.json b/frontend/src/locales/ru.json index c39817da..9dbfcffb 100644 --- a/frontend/src/locales/ru.json +++ b/frontend/src/locales/ru.json @@ -468,7 +468,8 @@ "openOnRunIT": "Редактировать в RunIT", "rename": "Переименовать", "run": "Запустить", - "share": "Поделиться" + "share": "Поделиться", + "save": "Сохранить" }, "toasts": { "duplicateSnippet": { From 079d7a6ae84c5848a6e58cfe7768c1098fb82025 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Sun, 7 Apr 2024 08:54:21 +0300 Subject: [PATCH 02/10] created useSaveButton hook --- frontend/src/hooks/useSaveButton.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 frontend/src/hooks/useSaveButton.js diff --git a/frontend/src/hooks/useSaveButton.js b/frontend/src/hooks/useSaveButton.js new file mode 100644 index 00000000..b7b4b518 --- /dev/null +++ b/frontend/src/hooks/useSaveButton.js @@ -0,0 +1,20 @@ +// 1. сохранить код +// 2 перенаправить в мои снипеты + +import { useSelector } from 'react-redux' +import routes from "../routes.js"; + +const useSaveButton = () => { + const moveTo = () => { + const username = useSelector((state) => state.user.userInfo.username) + routes.profilePagePath(username) + } + const saveCode = '' + + return { + moveTo, + saveCode + } +} + +export default useSaveButton \ No newline at end of file From 0e1ddf5724ba8377600f8b665d851efd3c6423b3 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Sun, 7 Apr 2024 08:54:38 +0300 Subject: [PATCH 03/10] added layout of saveButton in toolbar --- frontend/src/pages/snippet/ActionsToolbar.jsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/frontend/src/pages/snippet/ActionsToolbar.jsx b/frontend/src/pages/snippet/ActionsToolbar.jsx index 6d6406c5..f6127942 100644 --- a/frontend/src/pages/snippet/ActionsToolbar.jsx +++ b/frontend/src/pages/snippet/ActionsToolbar.jsx @@ -5,11 +5,12 @@ import Button from 'react-bootstrap/Button'; import Col from 'react-bootstrap/Col'; import { BoxArrowUp, Files, PlayFill } from 'react-bootstrap-icons'; import { actions } from '../../slices'; -import { useAuth, useRunButton } from '../../hooks'; +import { useAuth, useRunButton, useSaveButton } from '../../hooks'; function ActionsToolbar({ snippet }) { const { t } = useTranslation(); const { onClick, disabled } = useRunButton(); + const { moveTo, saveCode } = useSaveButton(); const dispatch = useDispatch(); const { snippetData, code } = snippet; const { name: snippetName, ownerUsername } = snippetData; @@ -76,6 +77,14 @@ function ActionsToolbar({ snippet }) { {t('snippetActions.run')} + ); } From 5e742dee46af49ba112d77b2290c8b66d75cd6b6 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Sun, 7 Apr 2024 11:00:17 +0300 Subject: [PATCH 04/10] make fix --- frontend/src/hooks/index.js | 3 ++- frontend/src/hooks/useSaveButton.js | 26 ++++++++++--------- frontend/src/pages/snippet/ActionsToolbar.jsx | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/frontend/src/hooks/index.js b/frontend/src/hooks/index.js index df4ce974..cb13f5d6 100644 --- a/frontend/src/hooks/index.js +++ b/frontend/src/hooks/index.js @@ -3,8 +3,9 @@ import { useContext } from 'react'; import { AuthContext, SnippetsContext } from '../contexts'; import useLanguage from './useLanguage.js'; import useRunButton from './useRunButton.js'; +import useSaveButton from './useSaveButton.js'; const useAuth = () => useContext(AuthContext); const useSnippets = () => useContext(SnippetsContext); -export { useAuth, useLanguage, useRunButton, useSnippets }; +export { useAuth, useLanguage, useRunButton, useSnippets, useSaveButton }; diff --git a/frontend/src/hooks/useSaveButton.js b/frontend/src/hooks/useSaveButton.js index b7b4b518..bd07164b 100644 --- a/frontend/src/hooks/useSaveButton.js +++ b/frontend/src/hooks/useSaveButton.js @@ -1,20 +1,22 @@ // 1. сохранить код // 2 перенаправить в мои снипеты -import { useSelector } from 'react-redux' -import routes from "../routes.js"; +import { useSelector, useDispatch } from 'react-redux'; +import routes from '../routes.js'; const useSaveButton = () => { - const moveTo = () => { - const username = useSelector((state) => state.user.userInfo.username) - routes.profilePagePath(username) + const { editor, user } = useSelector((state) => state); + const saveCode = () => { + if (editor.isAllSaved) { + console.log(user, 'user'); + routes.profilePagePath(user.userInfo.username); } - const saveCode = '' + }; - return { - moveTo, - saveCode - } -} + return { + moveTo, + saveCode, + }; +}; -export default useSaveButton \ No newline at end of file +export default useSaveButton; diff --git a/frontend/src/pages/snippet/ActionsToolbar.jsx b/frontend/src/pages/snippet/ActionsToolbar.jsx index f6127942..764051d8 100644 --- a/frontend/src/pages/snippet/ActionsToolbar.jsx +++ b/frontend/src/pages/snippet/ActionsToolbar.jsx @@ -80,8 +80,8 @@ function ActionsToolbar({ snippet }) { From 7317db15fa2e107c128a975c40326407db7204e5 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Sun, 7 Apr 2024 11:23:35 +0300 Subject: [PATCH 05/10] added save code button --- frontend/src/hooks/useSaveButton.js | 14 ++++++++------ frontend/src/pages/snippet/ActionsToolbar.jsx | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/frontend/src/hooks/useSaveButton.js b/frontend/src/hooks/useSaveButton.js index bd07164b..2b1d8679 100644 --- a/frontend/src/hooks/useSaveButton.js +++ b/frontend/src/hooks/useSaveButton.js @@ -2,19 +2,21 @@ // 2 перенаправить в мои снипеты import { useSelector, useDispatch } from 'react-redux'; +import { useNavigate } from 'react-router-dom'; import routes from '../routes.js'; +import { actions } from '../slices/editorSlice.js'; const useSaveButton = () => { - const { editor, user } = useSelector((state) => state); + const currentCode = useSelector((state) => state.editor.code); + const username = useSelector((state) => state.user.userInfo.username); + const dispatch = useDispatch(); + const navigate = useNavigate(); const saveCode = () => { - if (editor.isAllSaved) { - console.log(user, 'user'); - routes.profilePagePath(user.userInfo.username); - } + dispatch(actions.setCodeAndSavedCode(currentCode)); + navigate(routes.profilePagePath(username)); }; return { - moveTo, saveCode, }; }; diff --git a/frontend/src/pages/snippet/ActionsToolbar.jsx b/frontend/src/pages/snippet/ActionsToolbar.jsx index 764051d8..85d01b29 100644 --- a/frontend/src/pages/snippet/ActionsToolbar.jsx +++ b/frontend/src/pages/snippet/ActionsToolbar.jsx @@ -10,7 +10,7 @@ import { useAuth, useRunButton, useSaveButton } from '../../hooks'; function ActionsToolbar({ snippet }) { const { t } = useTranslation(); const { onClick, disabled } = useRunButton(); - const { moveTo, saveCode } = useSaveButton(); + const { saveCode } = useSaveButton(); const dispatch = useDispatch(); const { snippetData, code } = snippet; const { name: snippetName, ownerUsername } = snippetData; From 7b78162606cda167f9665a95eab2155cd1a35d63 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Mon, 15 Apr 2024 18:04:21 +0300 Subject: [PATCH 06/10] removeNavigation --- frontend/src/hooks/useSaveButton.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frontend/src/hooks/useSaveButton.js b/frontend/src/hooks/useSaveButton.js index 2b1d8679..c7a83adf 100644 --- a/frontend/src/hooks/useSaveButton.js +++ b/frontend/src/hooks/useSaveButton.js @@ -3,17 +3,14 @@ import { useSelector, useDispatch } from 'react-redux'; import { useNavigate } from 'react-router-dom'; -import routes from '../routes.js'; import { actions } from '../slices/editorSlice.js'; const useSaveButton = () => { const currentCode = useSelector((state) => state.editor.code); - const username = useSelector((state) => state.user.userInfo.username); const dispatch = useDispatch(); - const navigate = useNavigate(); const saveCode = () => { dispatch(actions.setCodeAndSavedCode(currentCode)); - navigate(routes.profilePagePath(username)); + }; return { From 5b7074b3ce40417dd0f53df61ae0ac99c7ce9666 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Mon, 15 Apr 2024 18:04:44 +0300 Subject: [PATCH 07/10] removed useNavigate import --- frontend/src/hooks/useSaveButton.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frontend/src/hooks/useSaveButton.js b/frontend/src/hooks/useSaveButton.js index c7a83adf..1f33b7f8 100644 --- a/frontend/src/hooks/useSaveButton.js +++ b/frontend/src/hooks/useSaveButton.js @@ -2,7 +2,6 @@ // 2 перенаправить в мои снипеты import { useSelector, useDispatch } from 'react-redux'; -import { useNavigate } from 'react-router-dom'; import { actions } from '../slices/editorSlice.js'; const useSaveButton = () => { @@ -10,7 +9,7 @@ const useSaveButton = () => { const dispatch = useDispatch(); const saveCode = () => { dispatch(actions.setCodeAndSavedCode(currentCode)); - + }; return { From 9c2a3005a0e71b371289bdde4e5426326f5602df Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Mon, 15 Apr 2024 18:40:24 +0300 Subject: [PATCH 08/10] added locales ru, eng for toast --- frontend/src/locales/en.json | 4 ++++ frontend/src/locales/ru.json | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.json index 27ff0892..53d6ef67 100644 --- a/frontend/src/locales/en.json +++ b/frontend/src/locales/en.json @@ -333,6 +333,10 @@ "toasts": { "duplicateSnippet": { "success": "Snippet successfully copied!" + }, + "saveCode": { + "success": "Snippet successfully saved!", + "error" : "Snippet was not saved" } } } diff --git a/frontend/src/locales/ru.json b/frontend/src/locales/ru.json index 1daa1b44..348a4171 100644 --- a/frontend/src/locales/ru.json +++ b/frontend/src/locales/ru.json @@ -464,6 +464,10 @@ "toasts": { "duplicateSnippet": { "success": "Сниппет успешно скопирован!" + }, + "saveCode": { + "success": "Сниппет успешно сохранен!", + "error" : "Не удалось сохранить сниппет" } } } From 94f2726ae4ebd855ddd17d0737c5f7b400802b4f Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Mon, 15 Apr 2024 18:40:50 +0300 Subject: [PATCH 09/10] remove commentary from useSaveButton --- frontend/src/hooks/useSaveButton.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/frontend/src/hooks/useSaveButton.js b/frontend/src/hooks/useSaveButton.js index 1f33b7f8..11d20b91 100644 --- a/frontend/src/hooks/useSaveButton.js +++ b/frontend/src/hooks/useSaveButton.js @@ -1,6 +1,3 @@ -// 1. сохранить код -// 2 перенаправить в мои снипеты - import { useSelector, useDispatch } from 'react-redux'; import { actions } from '../slices/editorSlice.js'; @@ -9,7 +6,6 @@ const useSaveButton = () => { const dispatch = useDispatch(); const saveCode = () => { dispatch(actions.setCodeAndSavedCode(currentCode)); - }; return { From 191c50465cb04f63a88411b03f1ea349d761a3c8 Mon Sep 17 00:00:00 2001 From: Daniil Bobrov Date: Mon, 15 Apr 2024 18:41:51 +0300 Subject: [PATCH 10/10] import toast to ActionsToolbar. Added handleSaveCode --- frontend/src/pages/snippet/ActionsToolbar.jsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/frontend/src/pages/snippet/ActionsToolbar.jsx b/frontend/src/pages/snippet/ActionsToolbar.jsx index 85d01b29..a0c43b02 100644 --- a/frontend/src/pages/snippet/ActionsToolbar.jsx +++ b/frontend/src/pages/snippet/ActionsToolbar.jsx @@ -4,15 +4,17 @@ import { useDispatch } from 'react-redux'; import Button from 'react-bootstrap/Button'; import Col from 'react-bootstrap/Col'; import { BoxArrowUp, Files, PlayFill } from 'react-bootstrap-icons'; +import { toast } from 'react-toastify'; import { actions } from '../../slices'; import { useAuth, useRunButton, useSaveButton } from '../../hooks'; +import 'react-toastify/dist/ReactToastify.css'; function ActionsToolbar({ snippet }) { const { t } = useTranslation(); const { onClick, disabled } = useRunButton(); const { saveCode } = useSaveButton(); const dispatch = useDispatch(); - const { snippetData, code } = snippet; + const { snippetData, code, isAllSaved } = snippet; const { name: snippetName, ownerUsername } = snippetData; const { isLoggedIn } = useAuth(); @@ -50,6 +52,15 @@ function ActionsToolbar({ snippet }) { } }; + const handleSaveCode = () => { + if (isAllSaved) { + saveCode(); + toast.success(t('toasts.saveCode.success')); + return; + } + toast.error(t('toasts.saveCode.error')); + }; + return ( -