416
416
<script >
417
417
import clone from ' lodash.clone'
418
418
import draggable from ' vuedraggable'
419
+ import Axios from ' axios'
419
420
420
421
import constants from ' @/constants'
421
422
import helpers from ' @/helpers/helpers'
@@ -431,7 +432,8 @@ const OPERATOR_VALUE_TO_LABEL_MAP = operators.reduce((acc, el) => {
431
432
432
433
const {
433
434
sum ,
434
- pluck
435
+ pluck ,
436
+ handleErr
435
437
} = helpers
436
438
437
439
const {
@@ -482,8 +484,17 @@ export default {
482
484
dialogDeleteFlagVisible: false ,
483
485
dialogEditDistributionOpen: false ,
484
486
dialogCreateSegmentOpen: false ,
485
- flag: {},
486
- flagId: this .$route .params .id ,
487
+ flag: {
488
+ createdBy: ' ' ,
489
+ dataRecordsEnabled: false ,
490
+ description: ' ' ,
491
+ enabled: false ,
492
+ id: 0 ,
493
+ key: ' ' ,
494
+ segments: [],
495
+ updatedAt: ' ' ,
496
+ variants: []
497
+ },
487
498
newSegment: clone (DEFAULT_SEGMENT ),
488
499
newVariant: clone (DEFAULT_VARIANT ),
489
500
selectedSegment: null ,
@@ -499,42 +510,37 @@ export default {
499
510
newDistributionIsValid () {
500
511
const percentageSum = sum (pluck (Object .values (this .newDistributions ), ' percent' ))
501
512
return percentageSum === 100
513
+ },
514
+ flagId () {
515
+ return this .$route .params .flagId
502
516
}
503
517
},
504
518
methods: {
505
519
deleteFlag () {
506
- const {flagId } = this .$route .params
507
- this .$http .delete (` ${ API_URL } /flags/${ flagId} ` )
508
- .then (() => {
509
- this .$router .replace ({name: ' home' })
510
- this .$message .success (` You deleted flag ${ flagId} ` )
511
- }, err => {
512
- this .$message .error (err .body .message )
513
- })
520
+ Axios .delete (
521
+ ` ${ API_URL } /flags/${ this .flagId } `
522
+ ).then (() => {
523
+ this .$router .replace ({name: ' home' })
524
+ this .$message .success (` You deleted flag ${ this .flagId } ` )
525
+ }, handleErr .bind (this ))
514
526
},
515
527
putFlag (flag ) {
516
- const flagId = this .$route .params .flagId
517
- this .$http .put (` ${ API_URL } /flags/${ flagId} ` , {
528
+ Axios .put (` ${ API_URL } /flags/${ this .flagId } ` , {
518
529
description: flag .description ,
519
530
dataRecordsEnabled: flag .dataRecordsEnabled ,
520
531
key: flag .key || ' '
521
532
}).then (() => {
522
533
this .$message .success (` You've updated flag` )
523
- }, err => {
524
- this .$message .error (err .body .message )
525
- })
534
+ }, handleErr .bind (this ))
526
535
},
527
536
setFlagEnabled (checked ) {
528
- const flagId = this .$route .params .flagId
529
- this .$http .put (
530
- ` ${ API_URL } /flags/${ flagId} /enabled` ,
537
+ Axios .put (
538
+ ` ${ API_URL } /flags/${ this .flagId } /enabled` ,
531
539
{enabled: checked}
532
540
).then (() => {
533
541
const checkedStr = checked ? ' on' : ' off'
534
542
this .$message .success (` You turned ${ checkedStr} this feature flag` )
535
- }, err => {
536
- this .$message .error (err .body .message )
537
- })
543
+ }, handleErr .bind (this ))
538
544
},
539
545
selectVariant ($event , variant ) {
540
546
const checked = $event
@@ -560,39 +566,30 @@ export default {
560
566
this .dialogEditDistributionOpen = true
561
567
},
562
568
saveDistribution (segment ) {
563
- const flagId = this .$route .params .flagId
564
-
565
569
const distributions = Object .values (this .newDistributions ).filter (distribution => distribution .percent !== 0 )
566
570
567
- this . $http .put (
568
- ` ${ API_URL } /flags/${ flagId} /segments/${ segment .id } /distributions` ,
571
+ Axios .put (
572
+ ` ${ API_URL } /flags/${ this . flagId } /segments/${ segment .id } /distributions` ,
569
573
{distributions}
570
574
).then (response => {
571
- let distributions = response .body
575
+ let distributions = response .data
572
576
this .selectedSegment .distributions = distributions
573
577
this .dialogEditDistributionOpen = false
574
578
this .$message .success (' distributions updated' )
575
- }, err => {
576
- this .$message .error (err .body .message )
577
- })
579
+ }, handleErr .bind (this ))
578
580
},
579
581
createVariant () {
580
- const flagId = this .$route .params .flagId
581
- this .$http .post (
582
- ` ${ API_URL } /flags/${ flagId} /variants` ,
582
+ Axios .post (
583
+ ` ${ API_URL } /flags/${ this .flagId } /variants` ,
583
584
this .newVariant
584
585
).then (response => {
585
- let variant = response .body
586
+ let variant = response .data
586
587
this .newVariant = clone (DEFAULT_VARIANT )
587
588
this .flag .variants .push (variant)
588
589
this .$message .success (' new variant created' )
589
- }, err => {
590
- this .$message .error (err .body .message )
591
- })
590
+ }, handleErr .bind (this ))
592
591
},
593
592
deleteVariant (variant ) {
594
- const {flagId } = this .$route .params
595
-
596
593
const isVariantInUse = this .flag .segments .some (segment => (
597
594
segment .distributions .some (distribution => distribution .variantID === variant .id )
598
595
))
@@ -606,140 +603,108 @@ export default {
606
603
return
607
604
}
608
605
609
- this . $http .delete (
610
- ` ${ API_URL } /flags/${ flagId} /variants/${ variant .id } `
606
+ Axios .delete (
607
+ ` ${ API_URL } /flags/${ this . flagId } /variants/${ variant .id } `
611
608
).then (() => {
612
609
this .$message .success (' variant deleted' )
613
610
this .fetchFlag ()
614
- }, err => {
615
- this .$message .error (err .body .message )
616
- })
611
+ }, handleErr .bind (this ))
617
612
},
618
613
putVariant (variant ) {
619
- const flagId = this .$route .params .flagId
620
614
variant .attachment = JSON .parse (variant .attachmentStr )
621
- this . $http .put (
622
- ` ${ API_URL } /flags/${ flagId} /variants/${ variant .id } ` ,
615
+ Axios .put (
616
+ ` ${ API_URL } /flags/${ this . flagId } /variants/${ variant .id } ` ,
623
617
variant
624
618
).then (() => {
625
619
this .$message .success (' variant updated' )
626
- }, err => {
627
- this .$message .error (err .body .message )
628
- })
620
+ }, handleErr .bind (this ))
629
621
},
630
622
createConstraint (segment ) {
631
- const {flagId } = this .$route .params
632
- this .$http .post (
633
- ` ${ API_URL } /flags/${ flagId} /segments/${ segment .id } /constraints` ,
623
+ Axios .post (
624
+ ` ${ API_URL } /flags/${ this .flagId } /segments/${ segment .id } /constraints` ,
634
625
segment .newConstraint
635
626
).then (response => {
636
- let constraint = response .body
627
+ let constraint = response .data
637
628
segment .constraints .push (constraint)
638
629
segment .newConstraint = clone (DEFAULT_CONSTRAINT )
639
630
this .$message .success (' new constraint created' )
640
- }, err => {
641
- this .$message .error (err .body .message )
642
- })
631
+ }, handleErr .bind (this ))
643
632
},
644
633
putConstraint (segment , constraint ) {
645
- const {flagId } = this .$route .params
646
-
647
- this .$http .put (
648
- ` ${ API_URL } /flags/${ flagId} /segments/${ segment .id } /constraints/${ constraint .id } ` ,
634
+ Axios .put (
635
+ ` ${ API_URL } /flags/${ this .flagId } /segments/${ segment .id } /constraints/${ constraint .id } ` ,
649
636
constraint
650
637
).then (() => {
651
638
this .$message .success (' constraint updated' )
652
- }, err => {
653
- this .$message .error (err .body .message )
654
- })
639
+ }, handleErr .bind (this ))
655
640
},
656
641
deleteConstraint (segment , constraint ) {
657
- const {flagId } = this .$route .params
658
-
659
642
if (! confirm (' Are you sure you want to delete this constraint?' )) {
660
643
return
661
644
}
662
645
663
- this . $http .delete (
664
- ` ${ API_URL } /flags/${ flagId} /segments/${ segment .id } /constraints/${ constraint .id } `
646
+ Axios .delete (
647
+ ` ${ API_URL } /flags/${ this . flagId } /segments/${ segment .id } /constraints/${ constraint .id } `
665
648
).then (() => {
666
649
const index = segment .constraints .findIndex (constraint => constraint .id === constraint .id )
667
650
segment .constraints .splice (index, 1 )
668
651
this .$message .success (' constraint deleted' )
669
- }, err => {
670
- this .$message .error (err .body .message )
671
- })
652
+ }, handleErr .bind (this ))
672
653
},
673
654
putSegment (segment ) {
674
- const flagId = this .$route .params .flagId
675
- this .$http .put (
676
- ` ${ API_URL } /flags/${ flagId} /segments/${ segment .id } ` ,
655
+ Axios .put (
656
+ ` ${ API_URL } /flags/${ this .flagId } /segments/${ segment .id } ` ,
677
657
{
678
658
description: segment .description ,
679
659
rolloutPercent: parseInt (segment .rolloutPercent , 10 )
680
660
}
681
661
).then (() => {
682
662
this .$message .success (' segment updated' )
683
- }, err => {
684
- this .$message .error (err .body .message )
685
- })
663
+ }, handleErr .bind (this ))
686
664
},
687
665
putSegmentsReorder (segments ) {
688
- const flagId = this .$route .params .flagId
689
- this .$http .put (
690
- ` ${ API_URL } /flags/${ flagId} /segments/reorder` ,
666
+ Axios .put (
667
+ ` ${ API_URL } /flags/${ this .flagId } /segments/reorder` ,
691
668
{ segmentIDs: pluck (segments, ' id' ) }
692
669
).then (() => {
693
670
this .$message .success (' segment reordered' )
694
- }, err => {
695
- this .$message .error (err .body .message )
696
- })
671
+ }, handleErr .bind (this ))
697
672
},
698
673
deleteSegment (segment ) {
699
- const {flagId } = this .$route .params
700
-
701
674
if (! confirm (' Are you sure you want to delete this segment?' )) {
702
675
return
703
676
}
704
677
705
- this . $http .delete (
706
- ` ${ API_URL } /flags/${ flagId} /segments/${ segment .id } `
678
+ Axios .delete (
679
+ ` ${ API_URL } /flags/${ this . flagId } /segments/${ segment .id } `
707
680
).then (() => {
708
681
const index = this .flag .segments .findIndex (el => el .id === segment .id )
709
682
this .flag .segments .splice (index, 1 )
710
683
this .$message .success (' segment deleted' )
711
- }, err => {
712
- this .$message .error (err .body .message )
713
- })
684
+ }, handleErr .bind (this ))
714
685
},
715
686
createSegment () {
716
- const flagId = this .$route .params .flagId
717
- this .$http .post (
718
- ` ${ API_URL } /flags/${ flagId} /segments` ,
687
+ Axios .post (
688
+ ` ${ API_URL } /flags/${ this .flagId } /segments` ,
719
689
this .newSegment
720
690
).then (response => {
721
- let segment = response .body
691
+ let segment = response .data
722
692
processSegment (segment)
723
693
segment .constraints = []
724
694
this .newSegment = clone (DEFAULT_SEGMENT )
725
695
this .flag .segments .push (segment)
726
696
this .$message .success (' new segment created' )
727
697
this .dialogCreateSegmentOpen = false
728
- }, err => {
729
- this .$message .error (err .body .message )
730
- })
698
+ }, handleErr .bind (this ))
731
699
},
732
700
fetchFlag () {
733
- const flagId = this .$route .params .flagId
734
- this .$http .get (` ${ API_URL } /flags/${ flagId} ` ).then (response => {
735
- let flag = response .body
701
+ Axios .get (` ${ API_URL } /flags/${ this .flagId } ` ).then (response => {
702
+ let flag = response .data
736
703
flag .segments .forEach (segment => processSegment (segment))
737
704
flag .variants .forEach (variant => processVariant (variant))
738
705
this .flag = flag
739
706
this .loaded = true
740
- }, err => {
741
- this .$message .error (err .body .message )
742
- })
707
+ }, handleErr .bind (this ))
743
708
}
744
709
},
745
710
mounted () {
0 commit comments