-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sc-4887] upload questions answers (#817)
* Replace our custom CSV parser by PapaParse - Add unit tests for our CSV parser - rename utils to csv-parser * Upload Q&A flow * Fix conversation style + support <br>
- Loading branch information
1 parent
3eb493c
commit accb778
Showing
27 changed files
with
382 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
10 changes: 7 additions & 3 deletions
10
libs/common/src/lib/resources/upload-button/upload-button.component.ts
This file contains 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,17 +1,21 @@ | ||
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; | ||
import { UploadDialogService } from './upload-dialog.service'; | ||
import { UploadDialogService, UploadType } from './upload-dialog.service'; | ||
import { FeatureFlagService } from '@flaps/core'; | ||
import { shareReplay } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'stf-upload-button', | ||
templateUrl: './upload-button.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class UploadButtonComponent implements OnInit { | ||
constructor(private uploadService: UploadDialogService) {} | ||
isQnAEnabled = this.feature.isFeatureEnabled('upload-q-and-a').pipe(shareReplay(1)); | ||
|
||
constructor(private uploadService: UploadDialogService, private feature: FeatureFlagService) {} | ||
|
||
ngOnInit(): void {} | ||
|
||
upload(type: 'files' | 'folder' | 'link' | 'csv') { | ||
upload(type: UploadType) { | ||
this.uploadService.upload(type); | ||
} | ||
} |
This file contains 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 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 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,41 @@ | ||
import { parseCsv } from '@flaps/common'; | ||
|
||
describe('csv-parser', () => { | ||
describe('parseCsv', () => { | ||
it('should parse simple csv string into a string[][]', () => { | ||
const simpleCsv = `row1 column1,row1 column2\nrow2 column1,row2 column2`; | ||
const simpleCsvParsed = [ | ||
['row1 column1', 'row1 column2'], | ||
['row2 column1', 'row2 column2'], | ||
]; | ||
expect(parseCsv(simpleCsv)).toEqual(simpleCsvParsed); | ||
}); | ||
|
||
it('should parse csv with quotes (quotes are doubled: "")', () => { | ||
const csvWithQuotes = `"row1 ""column1""",row1 column2\n"row2 ""column1""",row2 column2`; | ||
const csvWithQuotesParsed = [ | ||
['row1 "column1"', 'row1 column2'], | ||
['row2 "column1"', 'row2 column2'], | ||
]; | ||
expect(parseCsv(csvWithQuotes)).toEqual(csvWithQuotesParsed); | ||
}); | ||
|
||
it('should parse csv with special characters (cell containing comma or line breaks are wrapped into double quotes)', () => { | ||
const csvWithSpecialChars = `"row1, column1","row1\ncolumn2"\n"row2, column1","row2\ncolumn2"`; | ||
const csvWithSpecialCharsParsed = [ | ||
['row1, column1', 'row1\ncolumn2'], | ||
['row2, column1', 'row2\ncolumn2'], | ||
]; | ||
expect(parseCsv(csvWithSpecialChars)).toEqual(csvWithSpecialCharsParsed); | ||
}); | ||
|
||
it('should parse csv where a cell is entirely wrapped into quotes (in that case double quotes are tripled)', () => { | ||
const csvWrappedInQuotes = `row1 column1,"""row1 column2"""\nrow2 column1,row2 column2`; | ||
const csvWrappedInQuotesParsed = [ | ||
['row1 column1', '"row1 column2"'], | ||
['row2 column1', 'row2 column2'], | ||
]; | ||
expect(parseCsv(csvWrappedInQuotes)).toEqual(csvWrappedInQuotesParsed); | ||
}); | ||
}); | ||
}); |
This file contains 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,22 @@ | ||
import { Classification } from '@nuclia/core'; | ||
import { parse } from 'papaparse'; | ||
|
||
const SLUG_REGEX = /^[a-zA-Z0-9-_]+$/; | ||
|
||
// CSV parser following RFC 4180: https://github.com/mholt/PapaParse | ||
export function parseCsv(content: string): string[][] { | ||
const parseResult = parse<string[]>(content, {}); | ||
return parseResult.data; | ||
} | ||
|
||
// Parse labels like: 'labelset1/label1|labelset2/label2' | ||
export function parseCsvLabels(labels: string): Classification[] | null { | ||
if (labels.length === 0) return []; | ||
let isValid = true; | ||
const parsedLabels = labels.split('|').map((label) => { | ||
const items = label.split('/'); | ||
isValid &&= items.length === 2 && SLUG_REGEX.test(items[0].trim()) && items[1].trim().length > 0; | ||
return { labelset: items[0]?.trim(), label: items[1]?.trim() }; | ||
}); | ||
return isValid ? parsedLabels : null; | ||
} |
This file contains 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 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
54 changes: 54 additions & 0 deletions
54
libs/common/src/lib/upload/upload-qna/upload-qna.component.html
This file contains 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,54 @@ | ||
<pa-modal-advanced fitContent> | ||
<pa-modal-title> | ||
{{ 'upload.q_and_a' | translate }} | ||
</pa-modal-title> | ||
<pa-modal-content> | ||
<div class="upload-qna-content"> | ||
<ng-container *ngIf="standalone"> | ||
<div | ||
*ngIf="(hasValidKey | async) === false" | ||
class="warning body-s" | ||
[innerHTML]="'standalone.upload-no-nua-key' | translate"></div> | ||
</ng-container> | ||
|
||
<p | ||
class="body-m" | ||
[innerHTML]="'upload.qna_csv_description' | translate"></p> | ||
|
||
<pa-input [formControl]="resourceTitle">Resource title</pa-input> | ||
|
||
<app-csv-select | ||
[buttonKind]="qna.length === 0 ? 'primary' : 'secondary'" | ||
[fields]="2" | ||
(select)="displayCsv($event)"></app-csv-select> | ||
|
||
<pa-table | ||
*ngIf="qna.length > 0" | ||
class="csv-preview" | ||
columns="repeat(2,1fr)"> | ||
<pa-table-header> | ||
<pa-table-cell header>{{ 'upload.qna_csv_preview.question' | translate }}</pa-table-cell> | ||
<pa-table-cell header>{{ 'upload.qna_csv_preview.answer' | translate }}</pa-table-cell> | ||
</pa-table-header> | ||
<pa-table-row *ngFor="let row of qna"> | ||
<pa-table-cell>{{ row[0] }}</pa-table-cell> | ||
<pa-table-cell>{{ row[1] }}</pa-table-cell> | ||
</pa-table-row> | ||
</pa-table> | ||
</div> | ||
</pa-modal-content> | ||
<pa-modal-footer> | ||
<pa-button | ||
aspect="basic" | ||
(click)="close()"> | ||
{{ 'generic.cancel' | translate }} | ||
</pa-button> | ||
<pa-button | ||
type="submit" | ||
kind="primary" | ||
(click)="upload()" | ||
[disabled]="resourceTitle.invalid || qna.length === 0 || isUploading"> | ||
{{ 'generic.add' | translate }} | ||
</pa-button> | ||
</pa-modal-footer> | ||
</pa-modal-advanced> |
7 changes: 7 additions & 0 deletions
7
libs/common/src/lib/upload/upload-qna/upload-qna.component.scss
This file contains 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,7 @@ | ||
@import 'apps/dashboard/src/variables'; | ||
|
||
.upload-qna-content { | ||
display: flex; | ||
flex-direction: column; | ||
gap: rhythm(3); | ||
} |
Oops, something went wrong.