├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── frontend ├── .gitignore ├── .vscode │ └── settings.json ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── api │ │ └── apiCalls.js │ ├── assets │ │ ├── hoaxify.png │ │ └── profile.png │ ├── bootstrap-override.scss │ ├── components │ │ ├── AutoUploadImage.css │ │ ├── AutoUploadImage.js │ │ ├── ButtonWithProgress.js │ │ ├── HoaxFeed.js │ │ ├── HoaxSubmit.js │ │ ├── HoaxView.js │ │ ├── Input.js │ │ ├── LanguageSelector.js │ │ ├── Modal.js │ │ ├── ProfileCard.js │ │ ├── ProfileImageWithDefault.js │ │ ├── Spinner.js │ │ ├── TopBar.js │ │ ├── UserList.js │ │ └── UserListItem.js │ ├── container │ │ └── App.js │ ├── i18n.js │ ├── index.css │ ├── index.js │ ├── pages │ │ ├── HomePage.js │ │ ├── LoginPage.js │ │ ├── UserPage.js │ │ └── UserSignupPage.js │ ├── redux │ │ ├── Constants.js │ │ ├── authActions.js │ │ ├── authReducer.js │ │ └── configureStore.js │ ├── serviceWorker.js │ ├── setupTests.js │ └── shared │ │ └── ApiProgress.js └── yarn.lock ├── readme.md └── ws ├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── hoaxify │ │ └── ws │ │ ├── WsApplication.java │ │ ├── auth │ │ ├── AuthController.java │ │ ├── AuthException.java │ │ ├── AuthResponse.java │ │ ├── AuthService.java │ │ ├── Credentials.java │ │ ├── Token.java │ │ └── TokenRepository.java │ │ ├── configuration │ │ ├── AppConfiguration.java │ │ ├── AuthEntryPoint.java │ │ ├── SecurityConfiguration.java │ │ ├── TokenFilter.java │ │ └── WebConfiguration.java │ │ ├── error │ │ ├── ApiError.java │ │ ├── ErrorHandler.java │ │ └── NotFoundException.java │ │ ├── file │ │ ├── FileAttachment.java │ │ ├── FileAttachmentRepository.java │ │ ├── FileController.java │ │ ├── FileService.java │ │ └── vm │ │ │ └── FileAttachmentVM.java │ │ ├── hoax │ │ ├── Hoax.java │ │ ├── HoaxController.java │ │ ├── HoaxRepository.java │ │ ├── HoaxSecurityService.java │ │ ├── HoaxService.java │ │ └── vm │ │ │ ├── HoaxSubmitVM.java │ │ │ └── HoaxVM.java │ │ ├── shared │ │ ├── CurrentUser.java │ │ ├── FileType.java │ │ ├── FileTypeValidator.java │ │ └── GenericResponse.java │ │ └── user │ │ ├── UniqueUsername.java │ │ ├── UniqueUsernameValidator.java │ │ ├── User.java │ │ ├── UserController.java │ │ ├── UserRepository.java │ │ ├── UserService.java │ │ └── vm │ │ ├── UserUpdateVM.java │ │ └── UserVM.java └── resources │ ├── ValidationMessages.properties │ ├── ValidationMessages_tr.properties │ └── application.yaml └── test └── java └── com └── hoaxify └── ws └── WsApplicationTests.java /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Hoaxify Deploy 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | 7 | jobs: 8 | build: 9 | 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Use Node.js 12.x 15 | uses: actions/setup-node@v1 16 | with: 17 | node-version: 12.x 18 | - name: Setup Java JDK 19 | uses: actions/setup-java@v1.4.3 20 | with: 21 | # The Java version to make available on the path. Takes a whole or semver Java version, or 1.x syntax (e.g. 1.8 => Java 8.x). Early access versions can be specified in the form of e.g. 14-ea, 14.0.0-ea, or 14.0.0-ea.28 22 | java-version: 1.8 23 | - run: npm ci 24 | working-directory: ./frontend 25 | - run: npm run build --if-present 26 | working-directory: ./frontend 27 | 28 | - name: copy react to spring static resources 29 | run: | 30 | mkdir -p ws/src/main/resources/static 31 | cp -a frontend/build/. ws/src/main/resources/static/ 32 | 33 | - name: Build spring 34 | run: mvn -B package --file pom.xml 35 | working-directory: ./ws 36 | 37 | - name: SFTP Deploy 38 | uses: wlixcc/SFTP-Deploy-Action@v1.0 39 | with: 40 | username: ${{secrets.USERNAME}} 41 | server: ${{secrets.IP}} 42 | ssh_private_key: ${{secrets.SSH}} 43 | local_path: ws/target/ws-0.0.1-SNAPSHOT.jar 44 | remote_path: /home/basarbk 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ws/picture-storage 2 | ws/storage-* -------------------------------------------------------------------------------- /frontend/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /frontend/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "git.decorations.enabled": false, 3 | "scm.diffDecorations": "none", 4 | "files.exclude": { 5 | ".vscode": true, 6 | "node_modules": true, 7 | "package-lock.json": true, 8 | "yarn.lock": true, 9 | ".gitignore": true, 10 | "README.md": true, 11 | ".prettierrc": true 12 | }, 13 | "editor.formatOnType": true, 14 | "breadcrumbs.filePath": "off", 15 | "breadcrumbs.symbolPath": "off", 16 | "breadcrumbs.enabled": false, 17 | "workbench.statusBar.visible": false, 18 | "editor.wordBasedSuggestions": false, 19 | "editor.suggest.snippetsPreventQuickSuggestions": false, 20 | "editor.suggest.filterGraceful": false, 21 | "editor.lineNumbers": "off", 22 | "problems.decorations.enabled": false, 23 | "workbench.activityBar.visible": false, 24 | "editor.formatOnPaste": true, 25 | "editor.formatOnSave": true, 26 | "zenMode.centerLayout": false, 27 | "editor.find.addExtraSpaceOnTop": false, 28 | "debug.showInlineBreakpointCandidates": false, 29 | "editor.glyphMargin": false 30 | } 31 | -------------------------------------------------------------------------------- /frontend/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /frontend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frontend", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "axios": "^0.19.2", 10 | "bootstrap": "^4.4.1", 11 | "i18next": "^19.1.0", 12 | "node-sass": "^4.13.1", 13 | "react": "^16.12.0", 14 | "react-dom": "^16.12.0", 15 | "react-i18next": "^11.3.2", 16 | "react-redux": "^7.2.0", 17 | "react-router-dom": "^5.1.2", 18 | "react-scripts": "3.4.0", 19 | "redux": "^4.0.5", 20 | "redux-thunk": "^2.3.0", 21 | "secure-ls": "^1.2.6", 22 | "timeago.js": "^4.0.2" 23 | }, 24 | "scripts": { 25 | "start": "react-scripts start", 26 | "build": "react-scripts build", 27 | "test": "react-scripts test", 28 | "eject": "react-scripts eject" 29 | }, 30 | "eslintConfig": { 31 | "extends": "react-app" 32 | }, 33 | "browserslist": { 34 | "production": [ 35 | ">0.2%", 36 | "not dead", 37 | "not op_mini all" 38 | ], 39 | "development": [ 40 | "last 1 chrome version", 41 | "last 1 firefox version", 42 | "last 1 safari version" 43 | ] 44 | }, 45 | "proxy": "http://localhost:8080" 46 | } 47 | -------------------------------------------------------------------------------- /frontend/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/basarbk/spring-react-tr-course/f1004a8314d41498d1a7321ca3f83faab44ab91c/frontend/public/favicon.ico -------------------------------------------------------------------------------- /frontend/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | 24 | 25 | React App 26 | 27 | 28 | 29 |
30 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /frontend/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/basarbk/spring-react-tr-course/f1004a8314d41498d1a7321ca3f83faab44ab91c/frontend/public/logo192.png -------------------------------------------------------------------------------- /frontend/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/basarbk/spring-react-tr-course/f1004a8314d41498d1a7321ca3f83faab44ab91c/frontend/public/logo512.png -------------------------------------------------------------------------------- /frontend/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /frontend/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /frontend/src/api/apiCalls.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | 3 | export const signup = body => { 4 | return axios.post('/api/1.0/users', body); 5 | }; 6 | 7 | export const login = creds => { 8 | return axios.post('/api/1.0/auth', creds); 9 | }; 10 | 11 | export const logout = () => { 12 | return axios.post('/api/1.0/logout'); 13 | } 14 | 15 | export const changeLanguage = language => { 16 | axios.defaults.headers['accept-language'] = language; 17 | }; 18 | 19 | export const getUsers = (page = 0, size = 3) => { 20 | return axios.get(`/api/1.0/users?page=${page}&size=${size}`); 21 | }; 22 | 23 | export const setAuthorizationHeader = ({ isLoggedIn, token }) => { 24 | if (isLoggedIn) { 25 | const authorizationHeaderValue = `Bearer ${token}`; 26 | axios.defaults.headers['Authorization'] = authorizationHeaderValue; 27 | } else { 28 | delete axios.defaults.headers['Authorization']; 29 | } 30 | }; 31 | 32 | export const getUser = username => { 33 | return axios.get(`/api/1.0/users/${username}`); 34 | }; 35 | 36 | export const updateUser = (username, body) => { 37 | return axios.put(`/api/1.0/users/${username}`, body); 38 | }; 39 | 40 | export const postHoax = hoax => { 41 | return axios.post('/api/1.0/hoaxes', hoax); 42 | }; 43 | 44 | export const getHoaxes = (username, page = 0) => { 45 | const path = username ? `/api/1.0/users/${username}/hoaxes?page=` : '/api/1.0/hoaxes?page='; 46 | return axios.get(path + page); 47 | }; 48 | 49 | export const getOldHoaxes = (id, username) => { 50 | const path = username ? `/api/1.0/users/${username}/hoaxes/${id}` : `/api/1.0/hoaxes/${id}`; 51 | return axios.get(path); 52 | }; 53 | 54 | export const getNewHoaxCount = (id, username) => { 55 | const path = username ? `/api/1.0/users/${username}/hoaxes/${id}?count=true` : `/api/1.0/hoaxes/${id}?count=true`; 56 | return axios.get(path); 57 | }; 58 | 59 | export const getNewHoaxes = (id, username) => { 60 | const path = username ? `/api/1.0/users/${username}/hoaxes/${id}?direction=after` : `/api/1.0/hoaxes/${id}?direction=after`; 61 | return axios.get(path); 62 | }; 63 | 64 | export const postHoaxAttachment = attachment => { 65 | return axios.post('/api/1.0/hoax-attachments', attachment); 66 | }; 67 | 68 | export const deleteHoax = id => { 69 | return axios.delete(`/api/1.0/hoaxes/${id}`); 70 | }; 71 | 72 | export const deleteUser = username => { 73 | return axios.delete(`/api/1.0/users/${username}`); 74 | }; 75 | -------------------------------------------------------------------------------- /frontend/src/assets/hoaxify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/basarbk/spring-react-tr-course/f1004a8314d41498d1a7321ca3f83faab44ab91c/frontend/src/assets/hoaxify.png -------------------------------------------------------------------------------- /frontend/src/assets/profile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/basarbk/spring-react-tr-course/f1004a8314d41498d1a7321ca3f83faab44ab91c/frontend/src/assets/profile.png -------------------------------------------------------------------------------- /frontend/src/bootstrap-override.scss: -------------------------------------------------------------------------------- 1 | $primary: #818; 2 | 3 | @import '~bootstrap/scss/bootstrap.scss'; 4 | 5 | .btn-delete-link { 6 | color: $gray-400; 7 | padding: 0; 8 | } 9 | 10 | .btn-delete-link:hover { 11 | color: $danger; 12 | } 13 | -------------------------------------------------------------------------------- /frontend/src/components/AutoUploadImage.css: -------------------------------------------------------------------------------- 1 | .overlay { 2 | position: absolute; 3 | top: 0; 4 | bottom: 0; 5 | left: 0; 6 | right: 0; 7 | height: 100%; 8 | width: 100%; 9 | opacity: 0; 10 | transition: 0.5s ease; 11 | background-color: #00000099; 12 | } 13 | -------------------------------------------------------------------------------- /frontend/src/components/AutoUploadImage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './AutoUploadImage.css'; 3 | 4 | const AutoUploadImage = ({ image, uploading }) => { 5 | return ( 6 |
7 | hoax-attachment 8 |
9 |
10 |
11 | Loading... 12 |
13 |
14 |
15 |
16 | ); 17 | }; 18 | 19 | export default AutoUploadImage; 20 | -------------------------------------------------------------------------------- /frontend/src/components/ButtonWithProgress.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | const ButtonWithProgress = props => { 4 | const { onClick, pendingApiCall, disabled, text, className } = props; 5 | 6 | return ( 7 | 10 | ); 11 | }; 12 | 13 | export default ButtonWithProgress; 14 | -------------------------------------------------------------------------------- /frontend/src/components/HoaxFeed.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | import { getHoaxes, getOldHoaxes, getNewHoaxCount, getNewHoaxes } from '../api/apiCalls'; 3 | import { useTranslation } from 'react-i18next'; 4 | import HoaxView from './HoaxView'; 5 | import { useApiProgress } from '../shared/ApiProgress'; 6 | import Spinner from './Spinner'; 7 | import { useParams } from 'react-router-dom'; 8 | 9 | const HoaxFeed = () => { 10 | const [hoaxPage, setHoaxPage] = useState({ content: [], last: true, number: 0 }); 11 | const [newHoaxCount, setNewHoaxCount] = useState(0); 12 | const { t } = useTranslation(); 13 | const { username } = useParams(); 14 | 15 | const path = username ? `/api/1.0/users/${username}/hoaxes?page=` : '/api/1.0/hoaxes?page='; 16 | const initialHoaxLoadProgress = useApiProgress('get', path); 17 | 18 | let lastHoaxId = 0; 19 | let firstHoaxId = 0; 20 | if (hoaxPage.content.length > 0) { 21 | firstHoaxId = hoaxPage.content[0].id; 22 | 23 | const lastHoaxIndex = hoaxPage.content.length - 1; 24 | lastHoaxId = hoaxPage.content[lastHoaxIndex].id; 25 | } 26 | const oldHoaxPath = username ? `/api/1.0/users/${username}/hoaxes/${lastHoaxId}` : `/api/1.0/hoaxes/${lastHoaxId}`; 27 | const loadOldHoaxesProgress = useApiProgress('get', oldHoaxPath, true); 28 | 29 | const newHoaxPath = username 30 | ? `/api/1.0/users/${username}/hoaxes/${firstHoaxId}?direction=after` 31 | : `/api/1.0/hoaxes/${firstHoaxId}?direction=after`; 32 | 33 | const loadNewHoaxesProgress = useApiProgress('get', newHoaxPath, true); 34 | 35 | useEffect(() => { 36 | const getCount = async () => { 37 | const response = await getNewHoaxCount(firstHoaxId, username); 38 | setNewHoaxCount(response.data.count); 39 | }; 40 | let looper = setInterval(getCount, 5000); 41 | return function cleanup() { 42 | clearInterval(looper); 43 | }; 44 | }, [firstHoaxId, username]); 45 | 46 | useEffect(() => { 47 | const loadHoaxes = async page => { 48 | try { 49 | const response = await getHoaxes(username, page); 50 | setHoaxPage(previousHoaxPage => ({ 51 | ...response.data, 52 | content: [...previousHoaxPage.content, ...response.data.content] 53 | })); 54 | } catch (error) {} 55 | }; 56 | loadHoaxes(); 57 | }, [username]); 58 | 59 | const loadOldHoaxes = async () => { 60 | const response = await getOldHoaxes(lastHoaxId, username); 61 | setHoaxPage(previousHoaxPage => ({ 62 | ...response.data, 63 | content: [...previousHoaxPage.content, ...response.data.content] 64 | })); 65 | }; 66 | 67 | const loadNewHoaxes = async () => { 68 | const response = await getNewHoaxes(firstHoaxId, username); 69 | setHoaxPage(previousHoaxPage => ({ 70 | ...previousHoaxPage, 71 | content: [...response.data, ...previousHoaxPage.content] 72 | })); 73 | setNewHoaxCount(0); 74 | }; 75 | 76 | const onDeleteHoaxSuccess = id => { 77 | setHoaxPage(previousHoaxPage => ({ 78 | ...previousHoaxPage, 79 | content: previousHoaxPage.content.filter(hoax => hoax.id !== id) 80 | })); 81 | }; 82 | 83 | const { content, last } = hoaxPage; 84 | 85 | if (content.length === 0) { 86 | return
{initialHoaxLoadProgress ? : t('There are no hoaxes')}
; 87 | } 88 | 89 | return ( 90 |
91 | {newHoaxCount > 0 && ( 92 |
{} : loadNewHoaxes} 96 | > 97 | {loadNewHoaxesProgress ? : t('There are new hoaxes')} 98 |
99 | )} 100 | {content.map(hoax => { 101 | return ; 102 | })} 103 | {!last && ( 104 |
{} : loadOldHoaxes} 108 | > 109 | {loadOldHoaxesProgress ? : t('Load old hoaxes')} 110 |
111 | )} 112 |
113 | ); 114 | }; 115 | 116 | export default HoaxFeed; 117 | -------------------------------------------------------------------------------- /frontend/src/components/HoaxSubmit.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | import { useSelector } from 'react-redux'; 3 | import ProfileImageWithDefault from './ProfileImageWithDefault'; 4 | import { useTranslation } from 'react-i18next'; 5 | import { postHoax, postHoaxAttachment } from '../api/apiCalls'; 6 | import { useApiProgress } from '../shared/ApiProgress'; 7 | import ButtonWithProgress from './ButtonWithProgress'; 8 | import Input from './Input'; 9 | import AutoUploadImage from './AutoUploadImage'; 10 | 11 | const HoaxSubmit = () => { 12 | const { image } = useSelector(store => ({ image: store.image })); 13 | const [focused, setFocused] = useState(false); 14 | const [hoax, setHoax] = useState(''); 15 | const [errors, setErrors] = useState({}); 16 | const [newImage, setNewImage] = useState(); 17 | const [attachmentId, setAttachmentId] = useState(); 18 | const { t } = useTranslation(); 19 | 20 | useEffect(() => { 21 | if (!focused) { 22 | setHoax(''); 23 | setErrors({}); 24 | setNewImage(); 25 | setAttachmentId(); 26 | } 27 | }, [focused]); 28 | 29 | useEffect(() => { 30 | setErrors({}); 31 | }, [hoax]); 32 | 33 | const pendingApiCall = useApiProgress('post', '/api/1.0/hoaxes', true); 34 | const pendingFileUpload = useApiProgress('post', '/api/1.0/hoax-attachments', true); 35 | 36 | const onClickHoaxify = async () => { 37 | const body = { 38 | content: hoax, 39 | attachmentId: attachmentId 40 | }; 41 | 42 | try { 43 | await postHoax(body); 44 | setFocused(false); 45 | } catch (error) { 46 | if (error.response.data.validationErrors) { 47 | setErrors(error.response.data.validationErrors); 48 | } 49 | } 50 | }; 51 | 52 | const onChangeFile = event => { 53 | if (event.target.files.length < 1) { 54 | return; 55 | } 56 | const file = event.target.files[0]; 57 | const fileReader = new FileReader(); 58 | fileReader.onloadend = () => { 59 | setNewImage(fileReader.result); 60 | uploadFile(file); 61 | }; 62 | fileReader.readAsDataURL(file); 63 | }; 64 | 65 | const uploadFile = async file => { 66 | const attachment = new FormData(); 67 | attachment.append('file', file); 68 | const response = await postHoaxAttachment(attachment); 69 | setAttachmentId(response.data.id); 70 | }; 71 | 72 | let textAreaClass = 'form-control'; 73 | if (errors.content) { 74 | textAreaClass += ' is-invalid'; 75 | } 76 | 77 | return ( 78 |
79 | 80 |
81 |