From 51414663fb21166ba09f1852e632c04595821dee Mon Sep 17 00:00:00 2001 From: Hitesh Mandav Date: Mon, 10 Oct 2022 19:01:40 +0530 Subject: [PATCH 1/4] Reorganizing code using single responsibility principle --- README.md | 12 ++++- src/app/controllers.js | 48 ------------------- .../controllers/person-create.controller.js | 17 +++++++ src/app/controllers/person-edit.controller.js | 23 +++++++++ src/app/controllers/person-list.controller.js | 5 ++ src/app/controllers/search.controller.js | 9 ++++ .../card.directive.js} | 10 ---- src/app/directives/spinner.directive.js | 12 +++++ src/app/filters.js | 11 ----- src/app/filters/default-image.filter.js | 12 +++++ src/app/services/contact.resource.js | 13 +++++ .../contact.service.js} | 11 ----- src/index.html | 14 ++++-- 13 files changed, 111 insertions(+), 86 deletions(-) delete mode 100644 src/app/controllers.js create mode 100644 src/app/controllers/person-create.controller.js create mode 100644 src/app/controllers/person-edit.controller.js create mode 100644 src/app/controllers/person-list.controller.js create mode 100644 src/app/controllers/search.controller.js rename src/app/{directives.js => directives/card.directive.js} (71%) create mode 100644 src/app/directives/spinner.directive.js delete mode 100644 src/app/filters.js create mode 100644 src/app/filters/default-image.filter.js create mode 100644 src/app/services/contact.resource.js rename src/app/{services.js => services/contact.service.js} (93%) diff --git a/README.md b/README.md index d93a6f8..fdf17f3 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,14 @@ To run the application we type `npm start` - this loads the application using a local webserver, check the console for the port number to use. -The application is a simple contacts application where you can search, create or edit a contact. \ No newline at end of file +The application is a simple contacts application where you can search, create or edit a contact. + +# Steps for Migration. + +Preparing for migration: + +STEP 1: + +Follow the step 1 of angular style guide https://github.com/johnpapa/angular-styleguide + +Single Responsibility: Organize the code in such a maner that 1 file contains only one component. \ No newline at end of file diff --git a/src/app/controllers.js b/src/app/controllers.js deleted file mode 100644 index f78828c..0000000 --- a/src/app/controllers.js +++ /dev/null @@ -1,48 +0,0 @@ -angular - .module("codecraft") - .controller("PersonCreateController", function( - $scope, - $state, - ContactService - ) { - $scope.contacts = ContactService; - $scope.person = {}; - - $scope.save = function() { - console.log("createContact"); - $scope.contacts.createContact($scope.person).then(function() { - $state.go("list"); - }); - }; - }) - .controller("PersonEditController", function( - $scope, - $stateParams, - $state, - ContactService - ) { - $scope.contacts = ContactService; - $scope.person = $scope.contacts.getPerson($stateParams.email); - - $scope.save = function() { - $scope.contacts.updateContact($scope.person).then(function() { - $state.go("list"); - }); - }; - - $scope.remove = function() { - $scope.contacts.removeContact($scope.person).then(function() { - $state.go("list"); - }); - }; - }) - .controller("PersonListController", function($scope, ContactService) { - $scope.contacts = ContactService; - }) - .controller("SearchController", function($scope, ContactService) { - $scope.contacts = ContactService; - - $scope.loadMore = function() { - $scope.contacts.loadMore(); - }; - }); diff --git a/src/app/controllers/person-create.controller.js b/src/app/controllers/person-create.controller.js new file mode 100644 index 0000000..0b90189 --- /dev/null +++ b/src/app/controllers/person-create.controller.js @@ -0,0 +1,17 @@ +angular + .module("codecraft") + .controller("PersonCreateController", function( + $scope, + $state, + ContactService + ) { + $scope.contacts = ContactService; + $scope.person = {}; + + $scope.save = function() { + console.log("createContact"); + $scope.contacts.createContact($scope.person).then(function() { + $state.go("list"); + }); + }; + }) diff --git a/src/app/controllers/person-edit.controller.js b/src/app/controllers/person-edit.controller.js new file mode 100644 index 0000000..f6d76f7 --- /dev/null +++ b/src/app/controllers/person-edit.controller.js @@ -0,0 +1,23 @@ +angular + .module("codecraft") + .controller("PersonEditController", function( + $scope, + $stateParams, + $state, + ContactService + ) { + $scope.contacts = ContactService; + $scope.person = $scope.contacts.getPerson($stateParams.email); + + $scope.save = function() { + $scope.contacts.updateContact($scope.person).then(function() { + $state.go("list"); + }); + }; + + $scope.remove = function() { + $scope.contacts.removeContact($scope.person).then(function() { + $state.go("list"); + }); + }; + }) \ No newline at end of file diff --git a/src/app/controllers/person-list.controller.js b/src/app/controllers/person-list.controller.js new file mode 100644 index 0000000..258191e --- /dev/null +++ b/src/app/controllers/person-list.controller.js @@ -0,0 +1,5 @@ +angular + .module("codecraft") + .controller("PersonListController", function($scope, ContactService) { + $scope.contacts = ContactService; + }) diff --git a/src/app/controllers/search.controller.js b/src/app/controllers/search.controller.js new file mode 100644 index 0000000..5c70fed --- /dev/null +++ b/src/app/controllers/search.controller.js @@ -0,0 +1,9 @@ +angular + .module("codecraft") + .controller("SearchController", function($scope, ContactService) { + $scope.contacts = ContactService; + + $scope.loadMore = function() { + $scope.contacts.loadMore(); + }; + }); diff --git a/src/app/directives.js b/src/app/directives/card.directive.js similarity index 71% rename from src/app/directives.js rename to src/app/directives/card.directive.js index 5a6d920..b7ccd3a 100644 --- a/src/app/directives.js +++ b/src/app/directives/card.directive.js @@ -18,13 +18,3 @@ angular } }; }) - .directive("ccSpinner", function() { - return { - restrict: "AE", - templateUrl: "templates/spinner.html", - scope: { - isLoading: "=", - message: "@" - } - }; - }); diff --git a/src/app/directives/spinner.directive.js b/src/app/directives/spinner.directive.js new file mode 100644 index 0000000..0d452cc --- /dev/null +++ b/src/app/directives/spinner.directive.js @@ -0,0 +1,12 @@ +angular + .module("codecraft") + .directive("ccSpinner", function() { + return { + restrict: "AE", + templateUrl: "templates/spinner.html", + scope: { + isLoading: "=", + message: "@" + } + }; + }); diff --git a/src/app/filters.js b/src/app/filters.js deleted file mode 100644 index 13193f3..0000000 --- a/src/app/filters.js +++ /dev/null @@ -1,11 +0,0 @@ -angular.module("codecraft").filter("defaultImage", function() { - return function(input, param) { - if (!param) { - param = "/img/avatar.png"; - } - if (!input) { - return param; - } - return input; - }; -}); diff --git a/src/app/filters/default-image.filter.js b/src/app/filters/default-image.filter.js new file mode 100644 index 0000000..3f38243 --- /dev/null +++ b/src/app/filters/default-image.filter.js @@ -0,0 +1,12 @@ +angular.module("codecraft").filter("defaultImage", function() { + return function(input, param) { + if (!param) { + param = "/img/avatar.png"; + } + if (!input) { + return param; + } + return input; + }; + }); + \ No newline at end of file diff --git a/src/app/services/contact.resource.js b/src/app/services/contact.resource.js new file mode 100644 index 0000000..59bb68f --- /dev/null +++ b/src/app/services/contact.resource.js @@ -0,0 +1,13 @@ +angular + .module("codecraft") + .factory("Contact", function($resource) { + return $resource( + "http://localhost:3000/contacts/:id", + {id: "@id"}, + { + update: { + method: "PUT" + } + } + ); + }) diff --git a/src/app/services.js b/src/app/services/contact.service.js similarity index 93% rename from src/app/services.js rename to src/app/services/contact.service.js index dc5196b..7f747cd 100644 --- a/src/app/services.js +++ b/src/app/services/contact.service.js @@ -1,16 +1,5 @@ angular .module("codecraft") - .factory("Contact", function($resource) { - return $resource( - "http://localhost:3000/contacts/:id", - {id: "@id"}, - { - update: { - method: "PUT" - } - } - ); - }) .factory("ContactService", function(Contact, $rootScope, $q, toaster) { var self = { getPerson: function(email) { diff --git a/src/index.html b/src/index.html index 25ff5e7..bd9d833 100644 --- a/src/index.html +++ b/src/index.html @@ -65,13 +65,17 @@ - - - - - + + + + + + + + + From 969b05e0da3ddd6067f068ad25de7805e737b40a Mon Sep 17 00:00:00 2001 From: Hitesh Mandav Date: Wed, 12 Oct 2022 10:37:56 +0530 Subject: [PATCH 2/4] TS and webpack configuration --- .gitignore | 4 +- README.md | 26 ++++- package.json | 28 ++++- src/app/{app.main.js => app.main.ts} | 8 +- src/app/{app.routes.js => app.routes.ts} | 2 +- src/app/controllers/index.ts | 4 + ...troller.js => person-create.controller.ts} | 0 ...ontroller.js => person-edit.controller.ts} | 0 ...ontroller.js => person-list.controller.ts} | 0 ...rch.controller.js => search.controller.ts} | 0 .../{card.directive.js => card.directive.ts} | 0 src/app/directives/index.ts | 2 + ...nner.directive.js => spinner.directive.ts} | 0 ...mage.filter.js => default-image.filter.ts} | 2 +- src/app/filters/index.ts | 1 + src/app/main.ts | 19 ++++ ...ontact.resource.js => contact.resource.ts} | 2 +- ...{contact.service.js => contact.service.ts} | 19 ++-- src/app/services/index.ts | 2 + src/index.html | 29 +---- tsconfig.json | 105 ++++++++++++++++++ webpack.config.js | 20 ++++ 22 files changed, 225 insertions(+), 48 deletions(-) rename src/app/{app.main.js => app.main.ts} (80%) rename src/app/{app.routes.js => app.routes.ts} (92%) create mode 100644 src/app/controllers/index.ts rename src/app/controllers/{person-create.controller.js => person-create.controller.ts} (100%) rename src/app/controllers/{person-edit.controller.js => person-edit.controller.ts} (100%) rename src/app/controllers/{person-list.controller.js => person-list.controller.ts} (100%) rename src/app/controllers/{search.controller.js => search.controller.ts} (100%) rename src/app/directives/{card.directive.js => card.directive.ts} (100%) create mode 100644 src/app/directives/index.ts rename src/app/directives/{spinner.directive.js => spinner.directive.ts} (100%) rename src/app/filters/{default-image.filter.js => default-image.filter.ts} (82%) create mode 100644 src/app/filters/index.ts create mode 100644 src/app/main.ts rename src/app/services/{contact.resource.js => contact.resource.ts} (79%) rename src/app/services/{contact.service.js => contact.service.ts} (84%) create mode 100644 src/app/services/index.ts create mode 100644 tsconfig.json create mode 100644 webpack.config.js diff --git a/.gitignore b/.gitignore index 13ed183..5584cd5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ tmp .DS_Store .idea src/libs -data/db.json \ No newline at end of file +data/db.json +dist/ +src/dist/ \ No newline at end of file diff --git a/README.md b/README.md index fdf17f3..96fa4d8 100644 --- a/README.md +++ b/README.md @@ -28,4 +28,28 @@ STEP 1: Follow the step 1 of angular style guide https://github.com/johnpapa/angular-styleguide -Single Responsibility: Organize the code in such a maner that 1 file contains only one component. \ No newline at end of file +Single Responsibility: Organize the code in such a maner that 1 file contains only one component. + +STEP 2: + +Move all the dependencies from bower to npm with approprate npm naming and version in package.json and run npm install. + +Convert all the JS files to TS files. +Add index.ts files in each directory importing all the entities. + +install following dev dependencies: rimraf, ts-loader, typescript, webpack, @types/angular +npm install rimraf ts-loader typescript webpack @types/angular --save-dev + +run tsc --init in project dir to generate tsconfig.json file and set appropriate ts configuration options. + +Create webpack.config.js file and configure webpack options accordingly. + +add amin.ts file containing all the imports corresponding to the scripts in index.html file and remove scripts from index.html file. + +add bundle.js file in script tag in index.html file. + +add build script in package.json rimraf src/dist && webpack --bail --progress --profile + +run build scipt start the app and verify if everything works file. + +optionally can add dist folders to .gitignore file. \ No newline at end of file diff --git a/package.json b/package.json index 2e9e524..64dec83 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,36 @@ "scripts": { "server": "cp ./data/orig-db.json ./data/db.json && json-server --watch ./data/db.json", "setup": "bower install", - "start": "cd src && serve" + "start": "cd src && serve", + "build": "rimraf src/dist && webpack --bail --progress --profile" }, "author": "", "license": "ISC", - "dependencies": {}, + "dependencies": { + "angular": "1.4.0", + "angular-animate": "1.4.0", + "angular-auto-validate": "^1.19.0", + "angular-ladda": "^0.4.3", + "angular-resource": "1.4.0", + "angular-spinner": "^1.0.1", + "angular-strap": "^2.3.12", + "angular-ui-router": "^0.4.2", + "angularjs-toaster": "^2.1.0", + "bootstrap": "3.3.2", + "bootstrap-additions": "0.3.1", + "font-awesome": "4.3.0", + "jquery": "2.1.3", + "ng-infinite-scroll": "1.2.1" + }, "devDependencies": { + "@types/angular": "^1.8.4", "bower": "^1.8.0", "json-server": "^0.9.6", - "serve": "^5.1.2" + "rimraf": "^3.0.2", + "serve": "^5.1.2", + "ts-loader": "^9.4.1", + "typescript": "^4.8.4", + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0" } } diff --git a/src/app/app.main.js b/src/app/app.main.ts similarity index 80% rename from src/app/app.main.js rename to src/app/app.main.ts index 197434d..5065e73 100644 --- a/src/app/app.main.js +++ b/src/app/app.main.ts @@ -11,10 +11,10 @@ angular "ui.router" ]) .config(function( - $httpProvider, - $resourceProvider, - laddaProvider, - $datepickerProvider + $httpProvider: any, + $resourceProvider: any, + laddaProvider: any, + $datepickerProvider: any ) { laddaProvider.setOption({ style: "expand-right" diff --git a/src/app/app.routes.js b/src/app/app.routes.ts similarity index 92% rename from src/app/app.routes.js rename to src/app/app.routes.ts index 2f98217..6e883b2 100644 --- a/src/app/app.routes.js +++ b/src/app/app.routes.ts @@ -1,6 +1,6 @@ angular .module("codecraft") - .config(function($stateProvider, $urlRouterProvider) { + .config(function($stateProvider: any, $urlRouterProvider: any) { $stateProvider .state("list", { url: "/", diff --git a/src/app/controllers/index.ts b/src/app/controllers/index.ts new file mode 100644 index 0000000..acd2c80 --- /dev/null +++ b/src/app/controllers/index.ts @@ -0,0 +1,4 @@ +import './person-create.controller'; +import './person-edit.controller'; +import './person-list.controller'; +import './search.controller'; diff --git a/src/app/controllers/person-create.controller.js b/src/app/controllers/person-create.controller.ts similarity index 100% rename from src/app/controllers/person-create.controller.js rename to src/app/controllers/person-create.controller.ts diff --git a/src/app/controllers/person-edit.controller.js b/src/app/controllers/person-edit.controller.ts similarity index 100% rename from src/app/controllers/person-edit.controller.js rename to src/app/controllers/person-edit.controller.ts diff --git a/src/app/controllers/person-list.controller.js b/src/app/controllers/person-list.controller.ts similarity index 100% rename from src/app/controllers/person-list.controller.js rename to src/app/controllers/person-list.controller.ts diff --git a/src/app/controllers/search.controller.js b/src/app/controllers/search.controller.ts similarity index 100% rename from src/app/controllers/search.controller.js rename to src/app/controllers/search.controller.ts diff --git a/src/app/directives/card.directive.js b/src/app/directives/card.directive.ts similarity index 100% rename from src/app/directives/card.directive.js rename to src/app/directives/card.directive.ts diff --git a/src/app/directives/index.ts b/src/app/directives/index.ts new file mode 100644 index 0000000..94de91b --- /dev/null +++ b/src/app/directives/index.ts @@ -0,0 +1,2 @@ +import './card.directive'; +import './spinner.directive'; diff --git a/src/app/directives/spinner.directive.js b/src/app/directives/spinner.directive.ts similarity index 100% rename from src/app/directives/spinner.directive.js rename to src/app/directives/spinner.directive.ts diff --git a/src/app/filters/default-image.filter.js b/src/app/filters/default-image.filter.ts similarity index 82% rename from src/app/filters/default-image.filter.js rename to src/app/filters/default-image.filter.ts index 3f38243..0dbfee2 100644 --- a/src/app/filters/default-image.filter.js +++ b/src/app/filters/default-image.filter.ts @@ -1,5 +1,5 @@ angular.module("codecraft").filter("defaultImage", function() { - return function(input, param) { + return function(input: any, param: any) { if (!param) { param = "/img/avatar.png"; } diff --git a/src/app/filters/index.ts b/src/app/filters/index.ts new file mode 100644 index 0000000..b3ecb76 --- /dev/null +++ b/src/app/filters/index.ts @@ -0,0 +1 @@ +import './default-image.filter'; \ No newline at end of file diff --git a/src/app/main.ts b/src/app/main.ts new file mode 100644 index 0000000..43b7a3e --- /dev/null +++ b/src/app/main.ts @@ -0,0 +1,19 @@ +import 'angular'; +import 'angular-resource'; +import 'angular-animate' + +import 'ng-infinite-scroll'; +import 'angular-spinner'; +import 'angular-auto-validate/dist/jcs-auto-validate'; +import 'angular-ladda'; +import 'angular-strap'; +import 'angularjs-toaster'; +import 'angular-ui-router'; + + +import './app.main'; +import './services'; +import './filters'; +import './directives'; +import './controllers'; +import './app.routes'; diff --git a/src/app/services/contact.resource.js b/src/app/services/contact.resource.ts similarity index 79% rename from src/app/services/contact.resource.js rename to src/app/services/contact.resource.ts index 59bb68f..12e6620 100644 --- a/src/app/services/contact.resource.js +++ b/src/app/services/contact.resource.ts @@ -1,6 +1,6 @@ angular .module("codecraft") - .factory("Contact", function($resource) { + .factory("Contact", function($resource : any) { return $resource( "http://localhost:3000/contacts/:id", {id: "@id"}, diff --git a/src/app/services/contact.service.js b/src/app/services/contact.service.ts similarity index 84% rename from src/app/services/contact.service.js rename to src/app/services/contact.service.ts index 7f747cd..7cf64d9 100644 --- a/src/app/services/contact.service.js +++ b/src/app/services/contact.service.ts @@ -1,11 +1,11 @@ angular .module("codecraft") - .factory("ContactService", function(Contact, $rootScope, $q, toaster) { + .factory("ContactService", function(Contact: any, $rootScope: any, $q: any, toaster: any) { var self = { - getPerson: function(email) { + getPerson: function(email: any) { console.log(email); for (var i = 0; i < self.persons.length; i++) { - var obj = self.persons[i]; + var obj: any = self.persons[i]; if (obj.email == email) { return obj; } @@ -15,10 +15,11 @@ angular hasMore: true, isLoading: false, isSaving: false, - persons: [], + persons: [] as any[], search: null, sorting: "name", ordering: "ASC", + isDeleting: false, doSearch: function() { self.hasMore = true; self.page = 1; @@ -42,7 +43,7 @@ angular q: self.search }; - Contact.query(params, function(data) { + Contact.query(params, function(data: any) { console.debug(data); angular.forEach(data, function(person) { self.persons.push(new Contact(person)); @@ -61,7 +62,7 @@ angular self.loadContacts(); } }, - updateContact: function(person) { + updateContact: function(person: any) { var d = $q.defer(); self.isSaving = true; person.$update().then(function() { @@ -71,10 +72,10 @@ angular }); return d.promise; }, - removeContact: function(person) { + removeContact: function(person: any) { var d = $q.defer(); self.isDeleting = true; - name = person.name; + var name = person.name; person.$remove().then(function() { self.isDeleting = false; var index = self.persons.indexOf(person); @@ -84,7 +85,7 @@ angular }); return d.promise; }, - createContact: function(person) { + createContact: function(person: any) { var d = $q.defer(); self.isSaving = true; Contact.save(person).$promise.then(function() { diff --git a/src/app/services/index.ts b/src/app/services/index.ts new file mode 100644 index 0000000..df2669f --- /dev/null +++ b/src/app/services/index.ts @@ -0,0 +1,2 @@ +import './contact.resource'; +import './contact.service'; diff --git a/src/index.html b/src/index.html index bd9d833..37bb54e 100644 --- a/src/index.html +++ b/src/index.html @@ -49,34 +49,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..3671d0b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,105 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "lib": ["es6", "dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "es6", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + "baseUrl": "", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + "typeRoots": [ + "./node_modules/@types" + ], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist/out-tsc", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..ada5a21 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,20 @@ +module.exports = { + entry: './src/app/main.ts', + output: { + filename: '../src/dist/bundle.js', + }, + optimization: { + minimize: false, + }, + resolve: { + extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js'], + }, + module: { + rules: [ + { + test: /\.tsx?$/, + loader: 'ts-loader', + } + ] + } +} From ec5e2d6987868df1f21dd3af924881fd01519d38 Mon Sep 17 00:00:00 2001 From: Hitesh Mandav Date: Wed, 12 Oct 2022 11:05:25 +0530 Subject: [PATCH 3/4] upgrade to latest angular js version --- README.md | 7 ++++++- package.json | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 96fa4d8..4248e8a 100644 --- a/README.md +++ b/README.md @@ -52,4 +52,9 @@ add build script in package.json rimraf src/dist && webpack --bail --progress -- run build scipt start the app and verify if everything works file. -optionally can add dist folders to .gitignore file. \ No newline at end of file +optionally can add dist folders to .gitignore file. + +STEP 3: + +upgrade angularjs version to lates version by updating the version number in package.json file. Run npm i then npm run build and start the app to verify if everything is working if there are any errors refer to angularjs change log to look for breaking anges and fix those. + diff --git a/package.json b/package.json index 64dec83..6ea3f15 100644 --- a/package.json +++ b/package.json @@ -12,11 +12,11 @@ "author": "", "license": "ISC", "dependencies": { - "angular": "1.4.0", - "angular-animate": "1.4.0", + "angular": "1.8.2", + "angular-animate": "1.8.2", "angular-auto-validate": "^1.19.0", "angular-ladda": "^0.4.3", - "angular-resource": "1.4.0", + "angular-resource": "1.8.2", "angular-spinner": "^1.0.1", "angular-strap": "^2.3.12", "angular-ui-router": "^0.4.2", From 8fbb5de5df8b914449f12f37dcead5682c4ccc32 Mon Sep 17 00:00:00 2001 From: Hitesh Mandav Date: Thu, 13 Oct 2022 09:24:26 +0530 Subject: [PATCH 4/4] componentifying the controllers and directives. --- README.md | 7 ++++ src/app/app.routes.ts | 12 ++---- src/app/components/card.component.ts | 28 +++++++++++++ src/app/components/index.ts | 6 +++ src/app/components/person-create.component.ts | 27 +++++++++++++ src/app/components/person-edit.component.ts | 40 +++++++++++++++++++ src/app/components/person-list.component.ts | 18 +++++++++ src/app/components/search.component.ts | 22 ++++++++++ src/app/components/spinner.component.ts | 15 +++++++ src/app/controllers/index.ts | 4 -- .../controllers/person-create.controller.ts | 17 -------- src/app/controllers/person-edit.controller.ts | 23 ----------- src/app/controllers/person-list.controller.ts | 5 --- src/app/controllers/search.controller.ts | 9 ----- src/app/directives/card.directive.ts | 20 ---------- src/app/directives/index.ts | 2 - src/app/directives/spinner.directive.ts | 12 ------ src/app/main.ts | 3 +- src/templates/card.html | 18 ++++----- src/templates/create.html | 4 +- src/templates/edit.html | 8 ++-- src/templates/form.html | 18 ++++----- src/templates/list.html | 10 ++--- src/templates/searchform.html | 12 +++--- src/templates/spinner.html | 4 +- 25 files changed, 205 insertions(+), 139 deletions(-) create mode 100644 src/app/components/card.component.ts create mode 100644 src/app/components/index.ts create mode 100644 src/app/components/person-create.component.ts create mode 100644 src/app/components/person-edit.component.ts create mode 100644 src/app/components/person-list.component.ts create mode 100644 src/app/components/search.component.ts create mode 100644 src/app/components/spinner.component.ts delete mode 100644 src/app/controllers/index.ts delete mode 100644 src/app/controllers/person-create.controller.ts delete mode 100644 src/app/controllers/person-edit.controller.ts delete mode 100644 src/app/controllers/person-list.controller.ts delete mode 100644 src/app/controllers/search.controller.ts delete mode 100644 src/app/directives/card.directive.ts delete mode 100644 src/app/directives/index.ts delete mode 100644 src/app/directives/spinner.directive.ts diff --git a/README.md b/README.md index 4248e8a..1fc486c 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,10 @@ STEP 3: upgrade angularjs version to lates version by updating the version number in package.json file. Run npm i then npm run build and start the app to verify if everything is working if there are any errors refer to angularjs change log to look for breaking anges and fix those. +STEP 4: + +Componentify the angularjs version. + +i.e : convert the controllers and directives i.e anything with a view to angularjs components. +remove the imports of controllers and directives from main.ts and add components import. +change the app.routes to refer to the new components. diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 6e883b2..84771ef 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -6,12 +6,10 @@ angular url: "/", views: { main: { - templateUrl: "templates/list.html", - controller: "PersonListController" + template: '' }, search: { - templateUrl: "templates/searchform.html", - controller: "SearchController" + template: '' } } }) @@ -19,8 +17,7 @@ angular url: "/edit/:email", views: { main: { - templateUrl: "templates/edit.html", - controller: "PersonEditController" + template: '', } } }) @@ -28,8 +25,7 @@ angular url: "/create", views: { main: { - templateUrl: "templates/create.html", - controller: "PersonCreateController" + template: "", } } }); diff --git a/src/app/components/card.component.ts b/src/app/components/card.component.ts new file mode 100644 index 0000000..eae37e1 --- /dev/null +++ b/src/app/components/card.component.ts @@ -0,0 +1,28 @@ +let CardComponent = { + selector: 'ccCard', // + templateUrl: 'templates/card.html', + bindings: { + user: "=" + }, + controller: class CardController { + public isDeleting; + public ContactService; + public user: any; + + constructor(ContactService: any) { + this.ContactService = ContactService; + this.isDeleting = false; + }; + + deleteUser = () => { + this.isDeleting = true; + this.ContactService.removeContact(this.user) + .then( () => {this.isDeleting = false;}); + }; + + } +} + +angular + .module("codecraft") + .component(CardComponent.selector, CardComponent); diff --git a/src/app/components/index.ts b/src/app/components/index.ts new file mode 100644 index 0000000..0d91d84 --- /dev/null +++ b/src/app/components/index.ts @@ -0,0 +1,6 @@ +import './person-create.component'; +import './spinner.component'; +import './card.component'; +import './person-edit.component'; +import './person-list.component'; +import './search.component'; diff --git a/src/app/components/person-create.component.ts b/src/app/components/person-create.component.ts new file mode 100644 index 0000000..9b7d0d0 --- /dev/null +++ b/src/app/components/person-create.component.ts @@ -0,0 +1,27 @@ +import * as angular from 'angular'; + +export let PersonCreateComponent = { + selector: 'personCreate', // + templateUrl: 'templates/create.html', + bindings: {}, + controller: class PersonCreateController { + public contacts: any; + public person: any = {}; + public $state: any; + + constructor($state: any, ContactService: any) { + this.contacts = ContactService; + this.$state = $state; + } + + save() { + console.log("createContact"); + this.contacts.createContact(this.person) + .then(() => {this.$state.go("list");}) + } + } +} + +angular + .module("codecraft") + .component(PersonCreateComponent.selector, PersonCreateComponent); diff --git a/src/app/components/person-edit.component.ts b/src/app/components/person-edit.component.ts new file mode 100644 index 0000000..9c755c2 --- /dev/null +++ b/src/app/components/person-edit.component.ts @@ -0,0 +1,40 @@ +import * as angular from 'angular'; + +export let PersonEditComponent = { + selector: 'personEdit', // + templateUrl: 'templates/edit.html', + bindings: {}, + controller: class PersonEditController { + public $stateParams: any; + public $state: any; + public contacts: any; + public person: any; + + constructor( + $state: any, + $stateParams: any, + ContactService: any + ) { + this.$state = $state; + this.$stateParams = $stateParams; + this.contacts = ContactService; + this.person = this.contacts.getPerson(this.$stateParams.email); + } + + save() { + this.contacts.updateContact(this.person).then(() => { + this.$state.go("list"); + }); + }; + + remove() { + this.contacts.removeContact(this.person).then(() => { + this.$state.go("list"); + }); + }; + }, +} + +angular + .module("codecraft") + .component(PersonEditComponent.selector, PersonEditComponent) diff --git a/src/app/components/person-list.component.ts b/src/app/components/person-list.component.ts new file mode 100644 index 0000000..1426af3 --- /dev/null +++ b/src/app/components/person-list.component.ts @@ -0,0 +1,18 @@ +import * as angular from 'angular'; + +export let PersonListComponent = { + selector: 'personList', // + templateUrl: 'templates/list.html', + bindings: {}, + controller: class PersonListController { + public contacts: any; + + constructor(ContactService: any) { + this.contacts = ContactService; + } + }, +} + +angular + .module("codecraft") + .component(PersonListComponent.selector, PersonListComponent); diff --git a/src/app/components/search.component.ts b/src/app/components/search.component.ts new file mode 100644 index 0000000..e3bd6a4 --- /dev/null +++ b/src/app/components/search.component.ts @@ -0,0 +1,22 @@ +import * as angular from 'angular'; + +export let SearchComponent = { + selector: 'search', + templateUrl: 'templates/searchform.html', + bindings: {}, + controller: class SearchController { + public contacts: any; + + constructor(ContactService: any) { + this.contacts = ContactService; + } + + loadMore() { + this.contacts.loadMore(); + } + } +} + +angular + .module("codecraft") + .component(SearchComponent.selector, SearchComponent); diff --git a/src/app/components/spinner.component.ts b/src/app/components/spinner.component.ts new file mode 100644 index 0000000..0e12856 --- /dev/null +++ b/src/app/components/spinner.component.ts @@ -0,0 +1,15 @@ +import * as angular from 'angular'; + +export let SpinnerComponent = { + selector: "ccSpinner", // + templateUrl: "templates/spinner.html", + bindings: { + isLoading: "=", + message: "@" + }, + controller: class SpinnerController {}, +} + +angular + .module("codecraft") + .component(SpinnerComponent.selector, SpinnerComponent); diff --git a/src/app/controllers/index.ts b/src/app/controllers/index.ts deleted file mode 100644 index acd2c80..0000000 --- a/src/app/controllers/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import './person-create.controller'; -import './person-edit.controller'; -import './person-list.controller'; -import './search.controller'; diff --git a/src/app/controllers/person-create.controller.ts b/src/app/controllers/person-create.controller.ts deleted file mode 100644 index 0b90189..0000000 --- a/src/app/controllers/person-create.controller.ts +++ /dev/null @@ -1,17 +0,0 @@ -angular - .module("codecraft") - .controller("PersonCreateController", function( - $scope, - $state, - ContactService - ) { - $scope.contacts = ContactService; - $scope.person = {}; - - $scope.save = function() { - console.log("createContact"); - $scope.contacts.createContact($scope.person).then(function() { - $state.go("list"); - }); - }; - }) diff --git a/src/app/controllers/person-edit.controller.ts b/src/app/controllers/person-edit.controller.ts deleted file mode 100644 index f6d76f7..0000000 --- a/src/app/controllers/person-edit.controller.ts +++ /dev/null @@ -1,23 +0,0 @@ -angular - .module("codecraft") - .controller("PersonEditController", function( - $scope, - $stateParams, - $state, - ContactService - ) { - $scope.contacts = ContactService; - $scope.person = $scope.contacts.getPerson($stateParams.email); - - $scope.save = function() { - $scope.contacts.updateContact($scope.person).then(function() { - $state.go("list"); - }); - }; - - $scope.remove = function() { - $scope.contacts.removeContact($scope.person).then(function() { - $state.go("list"); - }); - }; - }) \ No newline at end of file diff --git a/src/app/controllers/person-list.controller.ts b/src/app/controllers/person-list.controller.ts deleted file mode 100644 index 258191e..0000000 --- a/src/app/controllers/person-list.controller.ts +++ /dev/null @@ -1,5 +0,0 @@ -angular - .module("codecraft") - .controller("PersonListController", function($scope, ContactService) { - $scope.contacts = ContactService; - }) diff --git a/src/app/controllers/search.controller.ts b/src/app/controllers/search.controller.ts deleted file mode 100644 index 5c70fed..0000000 --- a/src/app/controllers/search.controller.ts +++ /dev/null @@ -1,9 +0,0 @@ -angular - .module("codecraft") - .controller("SearchController", function($scope, ContactService) { - $scope.contacts = ContactService; - - $scope.loadMore = function() { - $scope.contacts.loadMore(); - }; - }); diff --git a/src/app/directives/card.directive.ts b/src/app/directives/card.directive.ts deleted file mode 100644 index b7ccd3a..0000000 --- a/src/app/directives/card.directive.ts +++ /dev/null @@ -1,20 +0,0 @@ -angular - .module("codecraft") - .directive("ccCard", function() { - return { - restrict: "AE", - templateUrl: "templates/card.html", - scope: { - user: "=" - }, - controller: function($scope, ContactService) { - $scope.isDeleting = false; - $scope.deleteUser = function() { - $scope.isDeleting = true; - ContactService.removeContact($scope.user).then(function() { - $scope.isDeleting = false; - }); - }; - } - }; - }) diff --git a/src/app/directives/index.ts b/src/app/directives/index.ts deleted file mode 100644 index 94de91b..0000000 --- a/src/app/directives/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -import './card.directive'; -import './spinner.directive'; diff --git a/src/app/directives/spinner.directive.ts b/src/app/directives/spinner.directive.ts deleted file mode 100644 index 0d452cc..0000000 --- a/src/app/directives/spinner.directive.ts +++ /dev/null @@ -1,12 +0,0 @@ -angular - .module("codecraft") - .directive("ccSpinner", function() { - return { - restrict: "AE", - templateUrl: "templates/spinner.html", - scope: { - isLoading: "=", - message: "@" - } - }; - }); diff --git a/src/app/main.ts b/src/app/main.ts index 43b7a3e..1d5930a 100644 --- a/src/app/main.ts +++ b/src/app/main.ts @@ -14,6 +14,5 @@ import 'angular-ui-router'; import './app.main'; import './services'; import './filters'; -import './directives'; -import './controllers'; +import './components'; import './app.routes'; diff --git a/src/templates/card.html b/src/templates/card.html index 6bf2bb7..00c604a 100644 --- a/src/templates/card.html +++ b/src/templates/card.html @@ -2,36 +2,36 @@
-
-

{{ user.name }} +

{{ $ctrl.user.name }} + ng-class="{'fa-female':$ctrl.user.sex == 'F', 'fa-male': $ctrl.user.sex == 'M'}">

- {{ user.city }}, {{ user.country }} + {{ $ctrl.user.city }}, {{ $ctrl.user.country }}

- {{ user.email }} + {{ $ctrl.user.email }}
- {{ user.birthdate | date:"longDate"}} + {{ $ctrl.user.birthdate | date:"longDate"}}

+ ui-sref="edit({email:$ctrl.user.email})">  Edit + ladda="$ctrl.isDeleting" + ng-click="$ctrl.deleteUser()">  Delete diff --git a/src/templates/create.html b/src/templates/create.html index 2006577..76a447d 100644 --- a/src/templates/create.html +++ b/src/templates/create.html @@ -1,6 +1,6 @@
@@ -9,7 +9,7 @@
diff --git a/src/templates/edit.html b/src/templates/edit.html index 6b200f3..b2abedd 100644 --- a/src/templates/edit.html +++ b/src/templates/edit.html @@ -1,6 +1,6 @@
@@ -9,13 +9,13 @@
diff --git a/src/templates/form.html b/src/templates/form.html index d6c749a..d2a0eb1 100644 --- a/src/templates/form.html +++ b/src/templates/form.html @@ -4,7 +4,7 @@
@@ -14,7 +14,7 @@
@@ -24,7 +24,7 @@ + ng-model="$ctrl.person.photo" />
@@ -33,7 +33,7 @@
@@ -57,7 +57,7 @@ + ng-model="$ctrl.person.phonenumber" />
@@ -67,7 +67,7 @@ + ng-model="$ctrl.person.address" />
@@ -78,7 +78,7 @@ + ng-model="$ctrl.person.city" /> @@ -88,7 +88,7 @@ + ng-model="$ctrl.person.country" /> diff --git a/src/templates/list.html b/src/templates/list.html index fc8020b..4f474b5 100644 --- a/src/templates/list.html +++ b/src/templates/list.html @@ -1,22 +1,22 @@
-
-
+
-

No results found for search term '{{ contacts.search }}'

+

No results found for search term '{{ $ctrl.contacts.search }}'

-
\ No newline at end of file diff --git a/src/templates/searchform.html b/src/templates/searchform.html index f8117da..9e9c03f 100644 --- a/src/templates/searchform.html +++ b/src/templates/searchform.html @@ -4,17 +4,17 @@
@@ -22,8 +22,8 @@
diff --git a/src/templates/spinner.html b/src/templates/spinner.html index 4eb8f10..aa22476 100644 --- a/src/templates/spinner.html +++ b/src/templates/spinner.html @@ -1,6 +1,6 @@
+ ng-show="$ctrl.isLoading"> -

{{ message }}

+

{{ $ctrl.message }}

\ No newline at end of file