forked from VictorDanilov/andersen-js-course
-
Notifications
You must be signed in to change notification settings - Fork 1
Практическая работа №4 #4
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
Open
rick-nice
wants to merge
14
commits into
client-server-app
Choose a base branch
from
client-server-app-done
base: client-server-app
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16a58c4
changing project structure, adding modules
rick-nice ec0f4af
Client: adding html, css and images
rick-nice 4da27b1
Client: adding EventEmitter
rick-nice af7809b
Client: adding controller
rick-nice 55b089a
Client: adding View
rick-nice a37d857
Client: adding constants and start js
rick-nice bb84988
Server: adding env and config script for env
rick-nice 546cb3b
Server: adding model
rick-nice 85fdc1b
Server: adding routes
rick-nice 918ee4c
Server: adding main script
rick-nice c846fc3
better formatting
rick-nice 2cbb8bb
beaty corrections
rick-nice 43f4a85
prefixes css
rick-nice 3238777
some style fixes: colors, sizes
rick-nice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "presets": ["@babel/preset-env"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| PORT=8080 | ||
| DATABASE_URL="mongodb+srv://testcluster-2m2lf.mongodb.net/test?retryWrites=true&w=majority" | ||
| DB_USER="recipelist-user" | ||
| DB_PASSWORD="JVOEEWe8R4Tu8vSi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| # Andersen JS Course | ||
| # Recipe client\server app | ||
|
|
||
| Репозиторий с задачами для курса Andersen JS. Вам необходимо сделать fork этого репозитория, чтобы начать работу над задачами в рамках курса. | ||
| Cooking is easy! | ||
|
|
||
| ## Перед запуском | ||
| 1. Установите NodeJS (версию Current) - https://nodejs.org/en/ | ||
| 2. Установите Yarn - https://yarnpkg.com/ru/ | ||
| 3. Если вы еще не поставили себе редактор кода, то можете поставить VSCode - https://code.visualstudio.com/ | ||
| 4. Для удобства разработки вам понадобятся плагины для ESlint (https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) и Prettier (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) | ||
| ## Start app | ||
|
|
||
| ## Подготовка к запуску | ||
| * Находясь в папке с проектом выполните в консоле команду `yarn`. Эта команда установит все необходимые пакеты зависимостей. | ||
| - Run `yarn start-server` to start server. Server needs Internet to connect Database. Server works on 8080 port. | ||
| - Run `yarn start-client` to launch client. It will connect to server automaticly, so make sure to start server first. Client works on 3030 port. | ||
|
|
||
| ## Запуск приложения | ||
| * Выполните команду `yarn start`. После этого у вас должен подняться локальный сервер, открыться новая вкладка в браузере, и вы можете приступать к работе. | ||
| ## How to use | ||
|
|
||
| You can make new recipes with "Add new recipe" button. You can edit, favor or delete existing recipes. You can filter recipes by favorites with "Favorites" button. If you need to show all recipes, click on "Recipe" button. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,62 @@ | ||
| { | ||
| "name": "andersen-js-course", | ||
| "name": "client-server-app", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "repository": "https://github.com/VictorDanilov/andersen-js-course.git", | ||
| "author": "Viktor Danilov <wikslayer@gmail.com>", | ||
| "repository": "https://github.com/rick-nice/andersen-js-course.git", | ||
| "author": "rick-nice <rickman.nice@gmail.com>", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "start": "webpack-dev-server --open", | ||
| "build": "webpack" | ||
| "start-server": "nodemon --exec babel-node src/server/server.js", | ||
| "start-client": "webpack-dev-server --open", | ||
| "build": "webpack", | ||
| "lint": "eslint --debug src/", | ||
| "lint:write": "eslint --debug src/ --fix", | ||
| "prettier": "prettier --write src/**/*.js" | ||
| }, | ||
| "husky": { | ||
| "hooks": { | ||
| "pre-commit": "lint-staged" | ||
| } | ||
| }, | ||
| "lint-staged": { | ||
| "*.js": [ | ||
| "yarn lint:write", | ||
| "git add" | ||
| ] | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.4.4", | ||
| "@babel/core": "^7.4.5", | ||
| "@babel/node": "^7.4.5", | ||
| "@babel/preset-env": "^7.4.5", | ||
| "babel-eslint": "^10.0.1", | ||
| "babel-loader": "^8.0.6", | ||
| "babel-polyfill": "^6.26.0", | ||
| "css-loader": "^2.1.1", | ||
| "eslint": "5.3.0", | ||
| "eslint-config-airbnb-base": "13.1.0", | ||
| "eslint-config-prettier": "^4.3.0", | ||
| "eslint-loader": "^2.1.2", | ||
| "eslint-plugin-import": "^2.14.0", | ||
| "eslint-plugin-prettier": "^3.1.0", | ||
| "file-loader": "^4.0.0", | ||
| "html-loader": "^0.5.5", | ||
| "html-webpack-plugin": "^3.2.0", | ||
| "husky": "^2.4.0", | ||
| "lint-staged": "^8.2.0", | ||
| "nodemon": "^1.19.1", | ||
| "prettier": "^1.17.1", | ||
| "style-loader": "^0.23.1", | ||
| "webpack": "^4.32.2", | ||
| "webpack-cli": "^3.3.2", | ||
| "webpack-dev-server": "^3.5.0" | ||
| }, | ||
| "dependencies": { | ||
| "body-parser": "^1.19.0", | ||
| "bootstrap": "^4.3.1", | ||
| "cors": "^2.8.5", | ||
| "dotenv": "^8.0.0", | ||
| "express": "^4.17.1", | ||
| "mongoose": "^5.6.3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* eslint-disable no-undef */ | ||
| import * as EVENTS from './constants/events'; | ||
| import * as URL from './constants/url'; | ||
|
|
||
| class Controller { | ||
| constructor(view) { | ||
| this.view = view; | ||
|
|
||
| this.initializeEvents(); | ||
|
|
||
| this.getAll(); | ||
| } | ||
|
|
||
| initializeEvents() { | ||
| this.view.on(EVENTS.ADD, this.addRecipe.bind(this)); | ||
| this.view.on(EVENTS.DELETE, this.deleteRecipe.bind(this)); | ||
| this.view.on(EVENTS.EDIT, this.editRecipe.bind(this)); | ||
| this.view.on(EVENTS.ALL, this.getAll.bind(this)); | ||
| this.view.on(EVENTS.FAVORITES, this.getFavorites.bind(this)); | ||
| } | ||
|
|
||
| async getAll() { | ||
| try { | ||
| const response = await window.fetch(URL.RECIPES); | ||
| const recipes = await response.json(); | ||
| this.view.showRecipes(recipes); | ||
| return response; | ||
| } catch (err) { | ||
| throw new Error(err); | ||
| } | ||
| } | ||
|
|
||
| async getFavorites() { | ||
| try { | ||
| const response = await window.fetch(URL.FAVORITES, { | ||
| mode: 'cors', | ||
| }); | ||
| const favorites = await response.json(); | ||
| this.view.showRecipes(favorites); | ||
| return response; | ||
| } catch (err) { | ||
| throw new Error(err); | ||
| } | ||
| } | ||
|
|
||
| async addRecipe(recipe) { | ||
| try { | ||
| const response = await window.fetch(URL.RECIPES, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(recipe), | ||
| }); | ||
| const { message } = await response.json(); | ||
| const newRecipe = { ...recipe, _id: message._id }; | ||
| this.view.addRecipe(newRecipe); | ||
| return response; | ||
| } catch (err) { | ||
| throw new Error(err); | ||
| } | ||
| } | ||
|
|
||
| async deleteRecipe({ id }) { | ||
| try { | ||
| const response = await window.fetch(`${URL.RECIPES}/${id}`, { | ||
| method: 'DELETE', | ||
| }); | ||
| this.view.removeRecipe(id); | ||
| return response; | ||
| } catch (err) { | ||
| throw new Error(err); | ||
| } | ||
| } | ||
|
|
||
| async editRecipe(recipe) { | ||
| try { | ||
| const { id, ...recipeBody } = recipe; | ||
| const response = await window.fetch(`${URL.RECIPES}/${id}`, { | ||
| method: 'PATCH', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(recipeBody), | ||
| }); | ||
| // Проверка на добавление рецепта в избранное, изменять данные рецепта не нужно | ||
| if (Object.keys(recipeBody).length !== 1) { | ||
| this.view.editRecipe(recipe); | ||
| } | ||
| return response; | ||
| } catch (err) { | ||
| throw new Error(err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default Controller; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class EventEmitter { | ||
| constructor() { | ||
| this.events = {}; | ||
| } | ||
|
|
||
| on(type, listener) { | ||
| this.events[type] = this.events[type] || []; | ||
| this.events[type].push(listener); | ||
| } | ||
|
|
||
| emit(type, arg) { | ||
| if (this.events[type]) { | ||
| this.events[type].forEach(listener => listener(arg)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default EventEmitter; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
просто используй fetch вместо window.fetch.