Skip to content

Commit

Permalink
common import container (#1734)
Browse files Browse the repository at this point in the history
* common import container

* added comment for example use

* resolved review cmts

---------

Co-authored-by: danoswaltCL <[email protected]>
  • Loading branch information
Yagnik56 and danoswaltCL authored Jul 9, 2024
1 parent 3c746a2 commit e9c7029
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,17 @@
[primaryActionBtnLabel]="data.primaryActionBtnLabel"
[primaryActionBtnColor]="data.primaryActionBtnColor"
[primaryActionBtnDisabled]="isImportActionBtnDisabled | async"
(primaryActionBtnClicked)="importFiles()"
>
<div class="drag-drop-container">
<div
class="input-container"
(dragover)="onDragOver($event)"
(dragleave)="onDragLeave($event)"
(drop)="onDrop($event)"
[ngClass]="{ 'drag-over': (isDragOver | async) }"
<app-common-import-container
fileType=".json"
buttonLabel="Import Json"
(filesSelected)="handleFilesSelected($event)"
class="full-width"
>
<div class="input-container-header">
<mat-icon>close</mat-icon>
</div>
<div class="input-container-content" *ngIf="fileName | async as fileName; else uploadPrompt">
<mat-icon>insert_drive_file</mat-icon>
<p>{{ fileName }}</p>
</div>
<ng-template #uploadPrompt>
<mat-icon>file_upload</mat-icon>
<p>Drag & drop or</p>
<button mat-flat-button color="primary" (click)="fileInput.click()">
<span>Choose JSON</span>
</button>
<input type="file" #fileInput (change)="onFileSelected($event)" style="display: none" />
</ng-template>
</div>
<p>
</app-common-import-container>
<p class="ft-12-600 import-message">
{{ 'feature-flags.import-feature-flag.message.text' | translate }}
<a href="">Learn More</a>
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,14 @@
height: 100%;
color: grey;

mat-icon {
height: 70px;
width: 70px;
font-size: 70px;
color: grey;
.full-width {
width: 100%;
}
}

.input-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border: 1px dashed grey;
border-radius: 4px;
height: 206px;
width: 592px;
position: relative;

button {
font-size: 14px;
}
}

.input-container-header {
position: absolute;
top: 5px;
right: 5px;

mat-icon {
height: 24px;
width: 24px;
font-size: 24px;
}
}

.input-container-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

p {
.import-message {
margin-top: 5px;
color: grey;
font-size: 12px;
}

a {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ import { ChangeDetectionStrategy, Component, ElementRef, Inject, OnInit, ViewChi
import { CommonModalComponent } from '../../../../../shared-standalone-component-lib/components';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { CommonModalConfig } from '../../../../../shared-standalone-component-lib/components/common-modal/common-modal-config';
import { FeatureFlagsService } from '../../../../../core/feature-flags/feature-flags.service';
import { BehaviorSubject } from 'rxjs';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../../../../../shared/shared.module';
import { CommonImportContainerComponent } from '../../../../../shared-standalone-component-lib/components/common-import-container/common-import-container.component';

@Component({
selector: 'app-import-feature-flag-modal',
standalone: true,
imports: [CommonModalComponent, CommonModule, SharedModule],
imports: [CommonModalComponent, CommonModule, SharedModule, CommonImportContainerComponent],
templateUrl: './import-feature-flag-modal.component.html',
styleUrls: ['./import-feature-flag-modal.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ImportFeatureFlagModalComponent {
@ViewChild('fileInput') fileInput: ElementRef<HTMLInputElement>;

isImportActionBtnDisabled = new BehaviorSubject<boolean>(true);
isDragOver = new BehaviorSubject<boolean>(false);
fileName = new BehaviorSubject<string | null>(null);

constructor(
@Inject(MAT_DIALOG_DATA)
Expand All @@ -29,46 +26,12 @@ export class ImportFeatureFlagModalComponent {
public dialogRef: MatDialogRef<ImportFeatureFlagModalComponent>
) {}

onDragOver(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
this.isDragOver.next(true);
}

onDragLeave(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
this.isDragOver.next(false);
}

onDrop(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
this.isDragOver.next(false);

const files = event.dataTransfer?.files;
if (files && files.length > 0) {
this.processFile(files[0]);
}
}

onFileSelected(event: Event) {
const input = event.target as HTMLInputElement;
if (input.files && input.files.length > 0) {
this.processFile(input.files[0]);
}
}

processFile(file: File) {
if (file.type === 'application/json') {
this.fileName.next(file.name);
handleFilesSelected(files: File[]) {
if(files.length>0) {
this.isImportActionBtnDisabled.next(false);
this.handleFileInput(file);
} else {
alert('Please upload a valid JSON file.');
this.fileName.next(null);
this.isImportActionBtnDisabled.next(true);
}
console.log('Selected files:', files);
//Send files to validation endpoint to receive data for table
}

handleFileInput(file: File) {
Expand All @@ -80,6 +43,10 @@ export class ImportFeatureFlagModalComponent {
reader.readAsText(file);
}

importFiles() {
console.log('Import feature flags');
}

closeModal() {
this.dialogRef.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div
class="input-container"
(dragover)="onDragOver($event)"
(dragleave)="onDragLeave($event)"
(drop)="onDrop($event)"
[ngClass]="{ 'drag-over': (isDragOver | async) }"
>
<mat-icon>file_upload</mat-icon>
<p class="ft-14-600 drag-text">Drag & drop or</p>
<button mat-flat-button color="primary" (click)="fileInput.click()">
<span>{{ buttonLabel }}</span>
</button>
<input [accept]="fileType" type="file" multiple #fileInput style="display: none" (change)="onFileSelected($event)" />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.input-container {
/* Add your styles here */
height: 210px;
width: 592px;
border: 2px dashed #ccc;
padding: 20px;
text-align: center;
border-radius: 10px;
transition: background-color 0.3s;

&.drag-over {
background-color: #f0f0f0;
}

.drag-text {
margin-bottom: 0.7rem;
}

mat-icon {
height: 70px;
width: 70px;
font-size: 70px;
color: grey;
}

button {
font-size: 14px;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../../../shared/shared.module';
import { CommonModalComponent } from '../common-modal/common-modal.component';
import { BehaviorSubject } from 'rxjs';
import { FILE_TYPE } from 'upgrade_types';

/**
* A reusable component for drag-and-drop file import functionality.
* This component allows users to drag and drop files or select them via a file input.
* It supports specifying a file type and emits the selected files to the parent component.
*
* The component accepts the following inputs:
* - `fileType`: A string representing the accepted file type (e.g., '.json'). Only files with this extension can be selected or dropped.
* - `buttonLabel`: A string representing the label text of the button. Defaults to 'Upload File'.
*
* The component emits the following outputs:
* - `filesSelected`: An event that emits the selected files as an array of `File` objects.
*
* Example usage:
*
* ```
* <app-common-drag-and-drop-file-import
* fileType=".json"
* buttonLabel="Import JSON Files"
* (filesSelected)="handleFilesSelected($event)"
* ></app-common-drag-and-drop-file-import>
* ```
*/
@Component({
selector: 'app-common-import-container',
standalone: true,
imports: [CommonModalComponent, CommonModule, SharedModule],
templateUrl: './common-import-container.component.html',
styleUrls: ['./common-import-container.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CommonImportContainerComponent {
@Input() fileType!: FILE_TYPE;
@Input() buttonLabel!: string;
@Output() filesSelected = new EventEmitter<File[]>();

isDragOver = new BehaviorSubject<boolean>(false);

onDragOver(event: DragEvent) {
this.handleDragState(event, true);
}

onDragLeave(event: DragEvent) {
this.handleDragState(event, false);
}

onDrop(event: DragEvent) {
this.handleDragState(event, false);
this.handleFileSelection(event.dataTransfer?.files);
}

private handleDragState(event: DragEvent, isOver: boolean) {
event.preventDefault();
event.stopPropagation();
this.isDragOver.next(isOver);
}

onFileSelected(event: Event) {
const input = event.target as HTMLInputElement;
this.handleFileSelection(input.files);
}

private handleFileSelection(files: FileList | null) {
if (files && files.length > 0) {
const validFiles = Array.from(files).filter(file => file.name.endsWith(this.fileType));
if (validFiles.length > 0) {
this.filesSelected.emit(validFiles);
} else {
console.error('Invalid file types...');
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { MatButtonModule } from '@angular/material/button';
import { MatDialogActions, MatDialogClose, MatDialogContent, MatDialogTitle } from '@angular/material/dialog';
import { CommonModule, NgTemplateOutlet } from '@angular/common';
import { MatIcon } from '@angular/material/icon';
import { Observable } from 'rxjs';

@Component({
selector: 'app-common-dialog',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class DialogService {
const config: MatDialogConfig = {
data: commonModalConfig,
width: '670px',
height: '450px',
height: '460px',
autoFocus: 'input',
disableClose: true,
};
Expand Down
5 changes: 5 additions & 0 deletions types/src/Experiment/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,8 @@ export enum FEATURE_FLAG_PARTICIPANT_LIST_KEY {
INCLUDE = 'featureFlagSegmentInclusion',
EXCLUDE = 'featureFlagSegmentExclusion',
}

export enum FILE_TYPE {
JSON = '.json',
CSV = '.csv',
}
1 change: 1 addition & 0 deletions types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export {
FLAG_SEARCH_KEY,
FEATURE_FLAG_STATUS,
STATUS_INDICATOR_CHIP_TYPE,
FILE_TYPE,
} from './Experiment/enums';
export {
IEnrollmentCompleteCondition,
Expand Down

0 comments on commit e9c7029

Please sign in to comment.