diff --git a/.babelrc b/.babelrc index bcb6ee8de..eaf32387b 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,3 @@ { "presets": ["es2015", "stage-0"] -} \ No newline at end of file +} diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index 845c343b3..000000000 --- a/.eslintrc +++ /dev/null @@ -1,39 +0,0 @@ -{ - "env": { - "browser": true - }, - "extends": ["standard"], - "parser": "babel-eslint", - "rules": { - "eol-last": 0, - "eqeqeq": [1, "smart"], - "indent" : [2, "tab", {"SwitchCase": 1 }], - "no-multiple-empty-lines": [2, {"max": 2}], - "no-unused-vars": [1, "all"], - "quotes": [0, "single"], - "semi": [2, "always"], - "space-before-function-paren": [2, "never"], - "spaced-comment": [0, "always"], - "radix": 0 - }, - "globals": { - "angular": true, - "_": true, - "moment": true, - "$": true, - "jQuery": true, - "d3": true, - "describe": true, - "xdescribe": true, - "expect": true, - "spyOn": true, - "beforeEach": true, - "afterEach": true, - "inject": true, - "it": true, - "jasmine": true - }, - "plugins": [ - "jasmine" - ] -} \ No newline at end of file diff --git a/package.json b/package.json index 9ed64d20f..da152fc1f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ }, "homepage": "https://github.com/mike-allison/angular-webpack-workflow", "dependencies": { - "angular": "^1.5.8" + "angular": "^1.5.8", + "angular-ui-router": "^0.3.1", + "bootstrap": "^3.3.7" }, "devDependencies": { "angular-mocks": "^1.5.8", @@ -30,9 +32,9 @@ "babel-core": "^6.14.0", "babel-eslint": "^6.0.4", "babel-loader": "^6.2.5", - "babel-runtime": "^6.11.6", "babel-preset-es2015": "^6.1.18", "babel-preset-stage-0": "^6.1.18", + "babel-runtime": "^6.11.6", "browser-sync": "^2.15.0", "browser-sync-webpack-plugin": "^1.1.2", "connect-modrewrite": "^0.9.0", @@ -49,6 +51,7 @@ "html-webpack-plugin": "^2.22.0", "isparta-instrumenter-loader": "^1.0.1", "jasmine-core": "^2.5.0", + "json-loader": "^0.5.4", "karma": "^1.2.0", "karma-coverage": "^1.1.1", "karma-jasmine": "^1.0.2", diff --git a/src/app.js b/src/app.js new file mode 100644 index 000000000..f04e2d79f --- /dev/null +++ b/src/app.js @@ -0,0 +1,27 @@ +import angular from 'angular'; +import uirouter from 'angular-ui-router'; +import products from './products/products.component'; +import auth from './auth/auth.component'; +import AuthService from './auth/auth.service'; + +import 'bootstrap/dist/css/bootstrap.css'; + +function assignServicesToRootScope($rootScope, AuthService){ + $rootScope.authService = AuthService; +} +assignServicesToRootScope.$inject = ['$rootScope', 'AuthService']; + +let App = angular.module('app', [uirouter, auth, products]); +App.config(function($urlRouterProvider, $locationProvider) { + $locationProvider.html5Mode(true); + $urlRouterProvider.otherwise('/products'); +}); +App.run(assignServicesToRootScope); +App.run(function() { + let jsonData = ['ownerships', 'products', 'users', 'testRequests']; + jsonData.forEach(function(item) { + let data = require('./json/' + item + '.json'); + localStorage.setItem(item, JSON.stringify(data)); + }); +}) +export default App.name; diff --git a/src/app.module.js b/src/app.module.js deleted file mode 100644 index 73fd97e0a..000000000 --- a/src/app.module.js +++ /dev/null @@ -1,5 +0,0 @@ -import angular from 'angular'; - -import home from './home/home.component'; - -export default angular.module('app', [home]).name; \ No newline at end of file diff --git a/src/auth/auth.component.js b/src/auth/auth.component.js new file mode 100644 index 000000000..d45d72ae2 --- /dev/null +++ b/src/auth/auth.component.js @@ -0,0 +1,24 @@ +import uirouter from 'angular-ui-router'; +import AuthController from './auth.controller'; +import AuthService from './auth.service'; + +export default angular.module('app.auth', [uirouter]) + .controller('AuthController', AuthController) + .service('AuthService', AuthService) + .config(function($stateProvider) { + $stateProvider + .state('login', { + url: '/login', + controller: AuthController, + controllerAs: '$ctrl', + templateUrl: 'views/login.html', + }) + + .state('register', { + url: '/register', + controller: AuthController, + controllerAs: '$ctrl', + templateUrl: 'views/register.html', + }); + }) + .name; diff --git a/src/auth/auth.controller.js b/src/auth/auth.controller.js new file mode 100644 index 000000000..9004aadd9 --- /dev/null +++ b/src/auth/auth.controller.js @@ -0,0 +1,30 @@ +export default class AuthController { + constructor($state, $window, AuthService) { + this.$state = $state + this.authService = AuthService; + this.$window = $window; + this.errorRegister = this.errorLogin = null; + this.$inject = ['$state', '$window', 'AuthService']; + } + + submitRegister () { + let userRegister = this.authService.registerUser(this.formData); + if (userRegister) { + if (this.authService.logIn(userRegister)) { + this.$state.go('products'); + } + } else { + this.errorRegister = "Ce nom est déjà utilisé, try again !" + } + } + + submitLogin() { + let userLogged = this.authService.logIn(this.formData.username); + if (userLogged) { + this.$state.go('products'); + } else { + this.errorLogin = "Trompé de nom d'utilisateur ?" + } + } + +} diff --git a/src/auth/auth.service.js b/src/auth/auth.service.js new file mode 100644 index 000000000..2948fef22 --- /dev/null +++ b/src/auth/auth.service.js @@ -0,0 +1,37 @@ +export default class AuthService { + constructor() { + this.currentUser = null; + } + + getConnectedUser() { + return localStorage.getItem('connectedUser'); + } + + logIn(userName) { + if (this.userExists(userName)) { + localStorage.setItem('connectedUser', userName); + return userName; + } + return false; + } + + logOut() { + localStorage.removeItem('connectedUser'); + } + + registerUser(userData) { + if (!this.userExists(userData.username)) { + let users = JSON.parse(localStorage.getItem('users')); + users.push(userData); + localStorage.setItem('users', JSON.stringify(users)); + return userData.username; + } + return false; + } + + userExists(userName) { + let users = JSON.parse(localStorage.getItem('users')) + var userExists = users.filter((user) => user.username === userName); + return (userExists.length === 1); + } +} diff --git a/src/home/home.component.js b/src/home/home.component.js deleted file mode 100644 index 991dc0c11..000000000 --- a/src/home/home.component.js +++ /dev/null @@ -1,13 +0,0 @@ -class HomeController { - constructor() { - this.welcomeMessage = 'Hello World'; - } -} -const Home = { - template: require('./home.html'), - controller: HomeController -}; - -export default angular.module('app.home', []) - .component('home', Home) - .name; \ No newline at end of file diff --git a/src/home/home.html b/src/home/home.html deleted file mode 100644 index c3564ee76..000000000 --- a/src/home/home.html +++ /dev/null @@ -1,3 +0,0 @@ -
- {{$ctrl.welcomeMessage}} -
\ No newline at end of file diff --git a/src/index.html b/src/index.html index dfc4b318b..18a53ffd1 100644 --- a/src/index.html +++ b/src/index.html @@ -1,11 +1,31 @@ - + Angular App + + + - +
+
+ +
+ +
- \ No newline at end of file + diff --git a/src/json/ownerships.json b/src/json/ownerships.json new file mode 100644 index 000000000..ae961beeb --- /dev/null +++ b/src/json/ownerships.json @@ -0,0 +1,34 @@ +[ + { + "username": "Ludo", + "productName": "Tesla Model X" + }, + { + "username": "Ludo", + "productName": "Playstation 4" + }, + { + "username": "Bob", + "productName": "IPhone 7" + }, + { + "username": "Bob", + "productName": "Playstation 4" + }, + { + "username": "Marie", + "productName": "IPhone 7" + }, + { + "username": "Marie", + "productName": "Magimix Cook Expert" + }, + { + "username": "Kevin", + "productName": "IPhone 7 plus" + }, + { + "username": "Kevin", + "productName": "Playstation 4" + } +] diff --git a/src/json/products.json b/src/json/products.json new file mode 100644 index 000000000..af7c26614 --- /dev/null +++ b/src/json/products.json @@ -0,0 +1,17 @@ +[ +{ + "name": "IPhone 7" +}, +{ + "name": "IPhone 7 plus" +}, +{ + "name": "Magimix Cook Expert" +}, +{ + "name": "Playstation 4" +}, +{ + "name": "Tesla Model X" +} +] diff --git a/src/json/testRequests.json b/src/json/testRequests.json new file mode 100644 index 000000000..bea7edd94 --- /dev/null +++ b/src/json/testRequests.json @@ -0,0 +1,22 @@ +[ + { + "username": "Ludo", + "productName": "Magimix Cook Expert" + }, + { + "username": "Bob", + "productName": "Tesla Model X" + }, + { + "username": "Marie", + "productName": "Iphone 7 plus" + }, + { + "username": "Marie", + "productName": "Playstation 4" + }, + { + "username": "Kevin", + "productName": "Tesla Model X" + } +] diff --git a/src/json/users.json b/src/json/users.json new file mode 100644 index 000000000..1396c00e0 --- /dev/null +++ b/src/json/users.json @@ -0,0 +1,22 @@ +[ + { + "username": "Ludo", + "phone": "0616490302", + "city": "Toulouse" + }, + { + "username": "Bob", + "phone": "0612345678", + "city": "Paris" + }, + { + "username": "Marie", + "phone": "0687654321", + "city": "Bordeaux" + }, + { + "username": "Kevin", + "phone": "0612312312", + "city": "Bayonne" + } +] diff --git a/src/products/products.component.js b/src/products/products.component.js new file mode 100644 index 000000000..1ad94fe35 --- /dev/null +++ b/src/products/products.component.js @@ -0,0 +1,22 @@ +import uirouter from 'angular-ui-router'; +import ProductsService from './products.service'; +import ProductsController from './products.controller'; +import AuthService from '../auth/auth.service'; + +export default angular.module('app.products', [uirouter]) + .service('ProductsService', ProductsService) + .service('AuthService', AuthService) + .controller('ProductsController', ProductsController) + .config(function($stateProvider) { + $stateProvider.state('products', { + url: '/products', + views: { + '@': { + controller: ProductsController, + controllerAs: 'products', + templateUrl: 'views/products.html', + } + } + }); + }) + .name; diff --git a/src/products/products.controller.js b/src/products/products.controller.js new file mode 100644 index 000000000..9a1010590 --- /dev/null +++ b/src/products/products.controller.js @@ -0,0 +1,34 @@ +export default class ProductsController { + constructor($scope, $state, ProductsService, AuthService) { + this.authService = AuthService; + this.productsService = ProductsService; + this.$state = $state; + this.loadProducts(); + this.$inject = ['$scope', '$state', 'ProductsService', 'AuthService']; + } + + loadProducts() { + this.productList = this.productsService.getProductList(); + this.productList.forEach(product => { + product.owners = this.productsService.getProductOwners(product.name); + product.requesters = this.productsService.getProductRequesters(product.name); + }); + } + + requestProduct(product) { + this.productsService.addProductRequester(product.name, this.authService.getConnectedUser()); + this.$state.reload(); + } + + haveProduct(product) { + this.productsService.addProductOwner(product.name, this.authService.getConnectedUser()); + this.$state.reload(); + } + + alreadyOwnerOrRequester(product) { + return ( + this.productsService.userAlreadyRequestProduct(product.name, this.authService.getConnectedUser()) || + this.productsService.userAlreadyHasProduct(product.name, this.authService.getConnectedUser()) + ); + } +} diff --git a/src/products/products.service.js b/src/products/products.service.js new file mode 100644 index 000000000..8d229a8e2 --- /dev/null +++ b/src/products/products.service.js @@ -0,0 +1,53 @@ +export default class ProductsService { + constructor() { + } + + getProductList() { + return JSON.parse(localStorage.getItem('products')); + } + + getProductOwners(product) { + let owners = JSON.parse(localStorage.getItem('ownerships')); + return owners.filter(owner => + owner.productName === product + ); + } + + getProductRequesters(product) { + let requesters = JSON.parse(localStorage.getItem('testRequests')); + return requesters.filter(requester => + requester.productName === product + ); + } + + addProductRequester(product, user) { + this.setUserToProduct('testRequests', product, user); + } + + addProductOwner(product, user) { + this.setUserToProduct('ownerships', product, user); + } + + setUserToProduct(mode, product, user) { + let items = JSON.parse(localStorage.getItem(mode)); + items.push({ + productName: product, + username: user + }); + localStorage.setItem(mode, JSON.stringify(items)); + } + + userAlreadyRequestProduct(product, user) { + let requesters = this.getProductRequesters(product); + return (requesters.filter(requester => + requester.username === user + ).length === 1); + } + + userAlreadyHasProduct(product, user) { + let owners = this.getProductOwners(product); + return (owners.filter(owner => + owner.username === user + ).length === 1); + } +} diff --git a/webpack.make.js b/webpack.make.js index b25aa70a5..f9ede4f25 100644 --- a/webpack.make.js +++ b/webpack.make.js @@ -35,7 +35,7 @@ module.exports = function makeWebpackConfig(options) { config.entry = {} } else { config.entry = { - app: './src/app.module.js' + app: './src/app.js' } } @@ -128,6 +128,9 @@ module.exports = function makeWebpackConfig(options) { },{ test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" + },{ + test: /\.json$/, + loader: "json" }] }; @@ -215,17 +218,6 @@ module.exports = function makeWebpackConfig(options) { }) ]; - if (!TEST && !BUILD) { - config.eslint = { - parser: 'babel-eslint' - }; - config.module.loaders.push({ - test: /\.js$/, - exclude: /node_modules|bower_components|vendor/, - loaders: ['eslint'] - }); - } - // Skip rendering index.html in test mode if (!TEST) { // Reference: https://github.com/ampedandwired/html-webpack-plugin @@ -280,4 +272,4 @@ module.exports = function makeWebpackConfig(options) { }; return config; -}; \ No newline at end of file +};