# Upgrading from v7 to v8

In order to migrate a prestation from prestations-ng v7 to v8, please follow
the steps described below.

An example of a prestation that has been migrated is available on the
[Skeleton prestation](https://portail.etat-de-vaud.ch/outils/git/projects/CYBSDK/repos/skeleton/browse/).

## Changes of package name and dependencies

Dependencies are now peer-dependencies. Prestations must install them explicitly.

- @fortawesome/angular-fontawesome
- @fortawesome/fontawesome-free
- @fortawesome/fontawesome-svg-core
- @fortawesome/free-solid-svg-icons
- @dsivd/foehn

```bash
yarn add @dsivd/foehn \
    @fortawesome/angular-fontawesome \
    @fortawesome/fontawesome-free \
    @fortawesome/fontawesome-svg-core \
    @fortawesome/free-solid-svg-icons
```

## Upgrade Angular

Prestations-ng v8 requires Angular 7 on your prestation.

To upgrade to Angular 7, please follow the instructions on the [official Angular upgrade page](https://update.angular.io/).

During the `ng update @angular/core` step, Angular CLI will complain about `prestations-ng` and `ng-http-loader` having
dependencies to an outdated version of rxjs. You can manually upgrade the version of `@dsivd/prestations-ng` to `^8.0.0` and
`ng-http-loader` to `^4.0.0`.

Please note that `prestations-ng` is now called `@dsivd/prestations-ng`.

```javascript
"dependencies": {
    ...
    "ng-http-loader": "^4.0.0",
    "@dsivd/prestations-ng": "^8.0.0",
    ...
}
```

You can run the following commands:

```bash
yarn remove prestations-ng
yarn global add @angular/cli
yarn add @angular/cli@7
yarn add ng-http-loader@5.0.1 @dsivd/prestations-ng@8.40.4
ng update @angular/core@7
rxjs-5-to-6-migrate -p src/tsconfig.app.json
```

If you're using [ncu](https://github.com/tjunnone/npm-check-updates), you can also upgrade all the other dependencies with:

```bash
ncu -u -a
yarn
yarn upgrade
```

## Upgrade ng-http-loader

If your prestation is using [ng-http-loader](https://github.com/mpalourdio/ng-http-loader), you need to upgrade this
library to version `4.0.0` and do the following modifications:

### `app.component.html`

`ng-http-loader` has its selected changed from `spinner` to `ng-http-loader`.

```diff
-<spinner
+<ng-http-loader
    id="spinner"
    [backgroundColor]="'#273c46'"
    [spinner]="spinkit.skCubeGrid"
-></spinner>
+></ng-http-loader>
```

### `app.component.ts`

Spinkit import must be updated.

```diff
-import { Spinkit } from 'ng-http-loader/spinkits';
+import { Spinkit } from 'ng-http-loader';
```

### `app.module.ts`

NgHttpLoaderModule import must be updated.

```diff
-import { NgHttpLoaderModule } from 'ng-http-loader/ng-http-loader.module';
+import { NgHttpLoaderModule } from 'ng-http-loader';
```

```diff
imports: [
    BrowserModule,
    FormsModule,
    PrestationsNgModule,
    HttpClientModule,
    AppRoutingModule,
-    NgHttpLoaderModule,
+    NgHttpLoaderModule.forRoot(),
],
```

## Upgrade other dependencies

Optionally, you can upgrade other dependencies to your prestation. You can use
[npm-check-updates](https://github.com/tjunnone/npm-check-updates) to do it easily.

## Add a browserlist

Add in the `front/src` folder a new file called `browserslist` to help Angular CLI auto prefix your css.

```text
# This file is currently used by autoprefixer to adjust CSS to support the below specified browsers
# For additional information regarding the format and rule options, please see:
# https://github.com/browserslist/browserslist#queries
# For IE 9-11 support, please uncomment the last line of the file and adjust as needed
> 0.5%
last 2 versions
Firefox ESR
not dead
IE 11
```

## Use the Foehn stylesheet

Update `style.css` to include the stylesheet from `@dsivd/foehn` directly.

```diff
-@import '~prestations-ng/styles.css';
+@import '~@dsivd/foehn/dist/assets/styles/main.css';
```

## Upgrade imports

All the imports are now on the `@dsivd/prestations-ng` package.

You must replace ALL your imports to follow the example below.

```diff
-import { PrestationsNgModule } from 'prestations-ng/prestations-ng.module';
+import { PrestationsNgModule } from '@dsivd/prestations-ng';
```

You can use the regex `from 'prestations-ng/(.*)` and replace it with `from '@dsivd/prestations-ng';` in
global search and replace in your IDE.

You can also use `find` and `sed` with UNIX:

```bash
find . -name \*.ts -print0 | xargs -0 sed -i'' -e "s/from 'prestations-ng\/\(.*\)/from '@dsivd\/prestations-ng';/g"
```

## Import the right modules

In order to improve tree shaking capabilities, prestations must now include some optional modules
in order to support some features.

```diff
imports: [
     ...
-    PrestationsNgModule,
+    // Core module
+    PrestationsNgCoreModule,
+    // Optional modules, only include if needed
+    FoehnAddressModule,
+    FoehnBreadcrumbModule,
+    FoehnPageModule,
+    FoehnInputModule,
+    FoehnValidationAlertsModule,
+    FoehnIconsModule,
+    FoehnNavigationModule,
+    SdkDictionaryModule,
+    FoehnNotFoundModule,
+    GesdemErrorModule,
+    SdkRedirectModule,
+    PipeModule,
+    FoehnFormModule
     ...
],
```

`PrestationsNgModule` is still available but will soon be deprecated.

More informations on modules on the dedicated page: [MODULES.md](MODULES.md).

## Update `angular.json` and `package.json`

If you're using `--preserve-symlink` in `npm run start`, remove it.

```diff
-"start": "ng serve --proxy-config proxy.conf.json --preserve-symlink --host 0.0.0.0",
+"start": "ng serve --proxy-config proxy.conf.json --host 0.0.0.0",
```

Instead, add this in `angular.json`:

```diff
"architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "dist",
        "index": "src/index.html",
        "main": "src/main.ts",
        "tsConfig": "src/tsconfig.app.json",
        "polyfills": "src/polyfills.ts",
        "assets": [
          "src/assets",
          "src/favicon.ico"
        ],
        "styles": [
          "src/styles.css"
        ],
        "scripts": [],
+       "preserveSymlinks": true
      },
```

### Prestation Title and Support Alerts

In the component that uses `foehn-page`, ensure that the title of the
prestation is taken from CatPrest:

```javascript
// page.component.ts
ngOnInit() {
    // In most cases, the etape handled by the prestation is the first one
    this.applicationInfoService.firstEtapeInfo.subscribe(etapeInfo => {
        const { titreEtape, idMetierPrestation, idMetierEtape } = etapeInfo;

        this.breadcrumbService.setConfiguration({
            prestationCode: idMetierPrestation,
        });

        // Title of the prestation
        this.appTitle = titreEtape;

        // Id of the etape for support alerts
        this.etapeId = idMetierEtape;
    });
}
```

```html
<!-- page.component.html -->
<foehn-page [appTitle]="appTitle" [etapeId]="etapeId">
    <router-outlet></router-outlet>
</foehn-page>
```
