|
| 1 | +# @rowslint/importer-angular |
| 2 | + |
| 3 | +The Rowslint Angular package provides access to the prebuilt importer component. The package interacts directly with the Rowslint APIs using your API key. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +``` |
| 8 | +npm install @rowslint/importer-angular |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Module |
| 14 | + |
| 15 | +Retrieve the API key from the organization page and import `RowslintModule` with the organization API key in your module |
| 16 | + |
| 17 | +```ts |
| 18 | +import { RowslintModule } from '@rowslint/importer-angular'; |
| 19 | + |
| 20 | +@NgModule({ |
| 21 | + imports: [RowslintModule.forRoot({ apiKey: 'ORGANIZATION_API_KEY' })] |
| 22 | +}) |
| 23 | +``` |
| 24 | + |
| 25 | +### Component |
| 26 | + |
| 27 | +Call the `launch()` method of the `RowslintService` service with the template config to display the importer UI. |
| 28 | + |
| 29 | +```ts |
| 30 | +import { Component } from '@angular/core'; |
| 31 | +import { RowslintTemplateConfig, RowslintService } from '@rowslint/importer-angular'; |
| 32 | + |
| 33 | +@Component({ |
| 34 | + selector: 'app-root', |
| 35 | + template: `<button (click)="import()">Import</button>`, |
| 36 | +}) |
| 37 | +export class AppComponent { |
| 38 | + config: RowslintTemplateConfig = { |
| 39 | + // Your template key here. |
| 40 | + templateKey: 'TEMPLATE_KEY', |
| 41 | + }; |
| 42 | + |
| 43 | + constructor(private rowslintService: RowslintService) {} |
| 44 | + |
| 45 | + import() { |
| 46 | + this.rowslintService.launch(this.config); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +#### Headless UI |
| 52 | + |
| 53 | +You can also use your custom upload UI and then use Rowslint importer only to format and validate data. To do so, call the `launch()` method with the uploaded file (in `file` type). |
| 54 | + |
| 55 | +```ts |
| 56 | +this.rowslintService.launch(this.config, this.file); |
| 57 | +``` |
| 58 | + |
| 59 | +### Events |
| 60 | + |
| 61 | +RowslintService provides an event to listen to the closing of the importer modal. You can use it to handle the closing of the importer after the import has been completed. |
| 62 | + |
| 63 | +``` |
| 64 | +constructor(private rowslintService: RowslintService) { |
| 65 | + this.rowslintService.onImport.subscribe((result) => { |
| 66 | + switch (result.status) { |
| 67 | + case RowslintImportStatus.SUCCESS: |
| 68 | + // Handle spreadsheet import success. |
| 69 | + break; |
| 70 | + case RowslintImportStatus.ERROR: |
| 71 | + // Handle spreadsheet import error. |
| 72 | + break; |
| 73 | + case RowslintImportStatus.CANCELLED: |
| 74 | + ¨// Handle spreadsheet import cancel. |
| 75 | + break; |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | +``` |
| 80 | + |
| 81 | +## Interfaces |
| 82 | + |
| 83 | +### `RowslintOptions` |
| 84 | + |
| 85 | +#### Description |
| 86 | + |
| 87 | +Represents the global options. Used to configure RowslintModule at the top level of the application. |
| 88 | + |
| 89 | +#### Properties |
| 90 | + |
| 91 | +| name | type | required | default | description | |
| 92 | +| ------ | -------- | -------- | ------- | ---------------------------------------------------------------- | |
| 93 | +| apiKey | `string` | required | | The organization API key. Can be found in the organization page. | |
| 94 | + |
| 95 | +### `RowslintTemplateConfig` |
| 96 | + |
| 97 | +#### Description |
| 98 | + |
| 99 | +Represents the configuration of each template importer. |
| 100 | + |
| 101 | +#### Properties |
| 102 | + |
| 103 | +| name | type | required | default | description | |
| 104 | +| ---------------- | --------------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
| 105 | +| templateKey | `string` | required | | The template key. Can be found in the template detail/edit page. | |
| 106 | +| language | `'en' \| 'fr'` | optional | 'en' | The language to be used in the importer. | |
| 107 | +| returnType | `'json' \| 'xslx' \| 'csv'` | optional | 'json' | the data format that the import will return. | |
| 108 | +| metadata | `unknown` | optional | | Additional data if you would like to send specific data to your server according to the configuration of the destination template (e.g. logged-in user data: `user_id`...). Can hold any type of data. | |
| 109 | +| customValidators | `RowslintTemplateCustomValidator` | optional | | Object to handle custom validations from your code. | |
| 110 | + |
| 111 | +### `RowslintImportResult` |
| 112 | + |
| 113 | +#### Description |
| 114 | + |
| 115 | +Represents the return of the importer after the modal is closed. |
| 116 | + |
| 117 | +#### Properties |
| 118 | + |
| 119 | +| name | type | required | default | description | |
| 120 | +| -------- | --------------------------------------------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 121 | +| status | `'success' \| 'error' \| 'cancelled'` | required | | `'success'` if import completed successfully, else `'error'`. `'cancelled'` if the user quits without completing the import steps. | |
| 122 | +| data | `{ file?: File; rows?: Array<Record<string, unknown>>; }` | optional | | Return the imported file in the `file` property or the JSON data format in the `row` property according to the `returnType` property of the `RowslintTemplateConfig` interface. | |
| 123 | +| metadata | `{ is_valid: boolean; file_name: string; sheet_name: string; }` | optional | | Holds the uploaded file information and the `is_valide` propperty which is `true` when the file is imported respecting all template validations, otherwise `else`. | |
| 124 | + |
| 125 | +## Examples |
| 126 | + |
| 127 | +### Importer UI |
| 128 | + |
| 129 | +This example demonstrates how to use the importer by displaying the importer UI and listening on the event when the import process is complete. |
| 130 | + |
| 131 | +```ts |
| 132 | +import { Component } from '@angular/core'; |
| 133 | +import { take } from 'rxjs'; |
| 134 | + |
| 135 | +import { RowslintService, RowslintTemplateConfig } from '@rowslint/importer-angular'; |
| 136 | + |
| 137 | +@Component({ |
| 138 | + selector: 'demo-root', |
| 139 | + template: `<button (click)="launch()">Launch</button>`, |
| 140 | +}) |
| 141 | +export class AppComponent { |
| 142 | + config: RowslintTemplateConfig = { |
| 143 | + templateKey: 'TEMPLATE_KEY', |
| 144 | + returnType: 'json', |
| 145 | + }; |
| 146 | + |
| 147 | + constructor(private rowslintService: RowslintService) {} |
| 148 | + |
| 149 | + launch() { |
| 150 | + this.rowslintService.launch(this.config); |
| 151 | + this.rowslintService.onImport.pipe(take(1)).subscribe((result) => { |
| 152 | + if (result.status === 'success' && result.metadata?.is_valid) { |
| 153 | + // Continue the import process. |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Headless importer UI |
| 161 | + |
| 162 | +In this example, we will use the importer in the headless mode. To do so, we will use our custom file input field to upload the file, then we will set it to the importer. |
| 163 | + |
| 164 | +```ts |
| 165 | +import { Component } from '@angular/core'; |
| 166 | +import { take } from 'rxjs'; |
| 167 | + |
| 168 | +import { RowslintService, RowslintTemplateConfig } from '@rowslint/importer-angular'; |
| 169 | + |
| 170 | +@Component({ |
| 171 | + selector: 'demo-root', |
| 172 | + template: ` |
| 173 | + <input id="file" type="file" /> |
| 174 | + <button (click)="launch()">Launch</button> |
| 175 | + `, |
| 176 | +}) |
| 177 | +export class AppComponent { |
| 178 | + config: RowslintTemplateConfig = { |
| 179 | + templateKey: 'TEMPLATE_KEY', |
| 180 | + returnType: 'xslx', |
| 181 | + }; |
| 182 | + |
| 183 | + constructor(private rowslintService: RowslintService) {} |
| 184 | + |
| 185 | + launch() { |
| 186 | + const inputFile = <HTMLInputElement>document.getElementById('file'); |
| 187 | + this.rowslintService.launch(this.config, inputFile?.files?.[0]); |
| 188 | + this.rowslintService.onImport.pipe(take(1)).subscribe((result) => { |
| 189 | + // Continue the import process. |
| 190 | + }); |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +### Custom validations |
| 196 | + |
| 197 | +We can define a custom configuration of validations from the code. Let's say our template has a "firstname" column. In this example, we will customize the validation of this column to accept only values starting with the letter "A". |
| 198 | + |
| 199 | +```ts |
| 200 | +import { Component } from '@angular/core'; |
| 201 | +import { take } from 'rxjs'; |
| 202 | + |
| 203 | +import { RowslintService, RowslintTemplateConfig } from '@rowslint/importer-angular'; |
| 204 | + |
| 205 | +@Component({ |
| 206 | + selector: 'demo-root', |
| 207 | + template: ` <button (click)="launch()">Launch</button> `, |
| 208 | +}) |
| 209 | +export class AppComponent { |
| 210 | + config: RowslintTemplateConfig = { |
| 211 | + templateKey: 'TEMPLATE_KEY', |
| 212 | + customValidators: { |
| 213 | + firstname: (columnValue) => { |
| 214 | + if (typeof columnValue === 'string' && columnValue.startsWith('A')) { |
| 215 | + // Return `true` if the value is valid. |
| 216 | + return true; |
| 217 | + } |
| 218 | + // Return custom message if the value is invalid. |
| 219 | + return { |
| 220 | + message: 'Must start with the letter "A".', |
| 221 | + }; |
| 222 | + }, |
| 223 | + }, |
| 224 | + }; |
| 225 | + |
| 226 | + constructor(private rowslintService: RowslintService) {} |
| 227 | + |
| 228 | + launch() { |
| 229 | + this.rowslintService.launch(this.config); |
| 230 | + this.rowslintService.onImport.pipe(take(1)).subscribe((result) => { |
| 231 | + // Continue the import process. |
| 232 | + }); |
| 233 | + } |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +Note that: |
| 238 | + |
| 239 | +- `firstname` property is the column name and must already be added to the template columns. |
| 240 | +- By defining a column custom validation, this will override the column validation type already defined in the template edition page. |
| 241 | + |
| 242 | +We can also return a list of valid values to be displayed in a select field. |
| 243 | + |
| 244 | +```ts |
| 245 | +import { Component } from '@angular/core'; |
| 246 | +import { take } from 'rxjs'; |
| 247 | + |
| 248 | +import { RowslintService, RowslintTemplateConfig } from '@rowslint/importer-angular'; |
| 249 | + |
| 250 | +@Component({ |
| 251 | + selector: 'demo-root', |
| 252 | + template: ` <button (click)="launch()">Launch</button> `, |
| 253 | +}) |
| 254 | +export class AppComponent { |
| 255 | + validValues = ['A', 'B']; |
| 256 | + config: RowslintTemplateConfig = { |
| 257 | + templateKey: 'TEMPLATE_KEY', |
| 258 | + customValidators: { |
| 259 | + firstname: (columnValue) => { |
| 260 | + if (typeof columnValue === 'string' && validValues.includes(columnValue)) { |
| 261 | + // Return `true` if the value is valid. |
| 262 | + return true; |
| 263 | + } |
| 264 | + // Return the `this.validValues` array that will be displayed in a select field. |
| 265 | + return { |
| 266 | + message: 'Must be "A" or "B".', |
| 267 | + validationType: 'choiceList', |
| 268 | + validationOptions: { |
| 269 | + list: this.validValues, |
| 270 | + }, |
| 271 | + }; |
| 272 | + }, |
| 273 | + }, |
| 274 | + }; |
| 275 | + |
| 276 | + constructor(private rowslintService: RowslintService) {} |
| 277 | + |
| 278 | + launch() { |
| 279 | + this.rowslintService.launch(this.config); |
| 280 | + this.rowslintService.onImport.pipe(take(1)).subscribe((result) => { |
| 281 | + // Continue the import process. |
| 282 | + }); |
| 283 | + } |
| 284 | +} |
| 285 | +``` |
| 286 | + |
| 287 | +## Documentation |
| 288 | + |
| 289 | +To see the latest documentation, [please click here](https://docs.rowslint.dev) |
0 commit comments