Browse Source

feat(admin-ui): Can delete TaxRate via list view

Relates to #262
Michael Bromley 6 years ago
parent
commit
ee02aa2a9a

+ 27 - 0
packages/admin-ui/src/app/common/generated-types.ts

@@ -1809,10 +1809,14 @@ export type Mutation = {
   createTaxCategory: TaxCategory,
   /** Update an existing TaxCategory */
   updateTaxCategory: TaxCategory,
+  /** Deletes a TaxCategory */
+  deleteTaxCategory: DeletionResponse,
   /** Create a new TaxRate */
   createTaxRate: TaxRate,
   /** Update an existing TaxRate */
   updateTaxRate: TaxRate,
+  /** Delete a TaxRate */
+  deleteTaxRate: DeletionResponse,
   /** Create a new Zone */
   createZone: Zone,
   /** Update an existing Zone */
@@ -2169,6 +2173,11 @@ export type MutationUpdateTaxCategoryArgs = {
 };
 
 
+export type MutationDeleteTaxCategoryArgs = {
+  id: Scalars['ID']
+};
+
+
 export type MutationCreateTaxRateArgs = {
   input: CreateTaxRateInput
 };
@@ -2179,6 +2188,11 @@ export type MutationUpdateTaxRateArgs = {
 };
 
 
+export type MutationDeleteTaxRateArgs = {
+  id: Scalars['ID']
+};
+
+
 export type MutationCreateZoneArgs = {
   input: CreateZoneInput
 };
@@ -4332,6 +4346,13 @@ export type UpdateTaxRateMutationVariables = {
 
 export type UpdateTaxRateMutation = ({ __typename?: 'Mutation' } & { updateTaxRate: ({ __typename?: 'TaxRate' } & TaxRateFragment) });
 
+export type DeleteTaxRateMutationVariables = {
+  id: Scalars['ID']
+};
+
+
+export type DeleteTaxRateMutation = ({ __typename?: 'Mutation' } & { deleteTaxRate: ({ __typename?: 'DeletionResponse' } & Pick<DeletionResponse, 'result' | 'message'>) });
+
 export type ChannelFragment = ({ __typename?: 'Channel' } & Pick<Channel, 'id' | 'createdAt' | 'updatedAt' | 'code' | 'token' | 'pricesIncludeTax' | 'currencyCode' | 'defaultLanguageCode'> & { defaultShippingZone: Maybe<({ __typename?: 'Zone' } & Pick<Zone, 'id' | 'name'>)>, defaultTaxZone: Maybe<({ __typename?: 'Zone' } & Pick<Zone, 'id' | 'name'>)> });
 
 export type GetChannelsQueryVariables = {};
@@ -5326,6 +5347,12 @@ export namespace UpdateTaxRate {
   export type UpdateTaxRate = TaxRateFragment;
 }
 
+export namespace DeleteTaxRate {
+  export type Variables = DeleteTaxRateMutationVariables;
+  export type Mutation = DeleteTaxRateMutation;
+  export type DeleteTaxRate = DeleteTaxRateMutation['deleteTaxRate'];
+}
+
 export namespace Channel {
   export type Fragment = ChannelFragment;
   export type DefaultShippingZone = (NonNullable<ChannelFragment['defaultShippingZone']>);

+ 9 - 0
packages/admin-ui/src/app/data/definitions/settings-definitions.ts

@@ -256,6 +256,15 @@ export const UPDATE_TAX_RATE = gql`
     ${TAX_RATE_FRAGMENT}
 `;
 
+export const DELETE_TAX_RATE = gql`
+    mutation DeleteTaxRate($id: ID!) {
+        deleteTaxRate(id: $id) {
+            result
+            message
+        }
+    }
+`;
+
 export const CHANNEL_FRAGMENT = gql`
     fragment Channel on Channel {
         id

+ 8 - 0
packages/admin-ui/src/app/data/providers/settings-data.service.ts

@@ -15,6 +15,7 @@ import {
     CreateZoneInput,
     DeleteChannel,
     DeleteCountry,
+    DeleteTaxRate,
     GetActiveChannel,
     GetAllJobs,
     GetAvailableCountries,
@@ -59,6 +60,7 @@ import {
     CREATE_ZONE,
     DELETE_CHANNEL,
     DELETE_COUNTRY,
+    DELETE_TAX_RATE,
     GET_ACTIVE_CHANNEL,
     GET_ALL_JOBS,
     GET_AVAILABLE_COUNTRIES,
@@ -228,6 +230,12 @@ export class SettingsDataService {
         });
     }
 
+    deleteTaxRate(id: string) {
+        return this.baseDataService.mutate<DeleteTaxRate.Mutation, DeleteTaxRate.Variables>(DELETE_TAX_RATE, {
+            id,
+        });
+    }
+
     getChannels() {
         return this.baseDataService.query<GetChannels.Query>(GET_CHANNELS);
     }

+ 21 - 0
packages/admin-ui/src/app/settings/components/tax-rate-list/tax-rate-list.component.html

@@ -21,6 +21,7 @@
     <vdr-dt-column>{{ 'settings.zone' | translate }}</vdr-dt-column>
     <vdr-dt-column>{{ 'settings.tax-rate' | translate }}</vdr-dt-column>
     <vdr-dt-column></vdr-dt-column>
+    <vdr-dt-column></vdr-dt-column>
     <ng-template let-taxRate="item">
         <td class="left align-middle">{{ taxRate.name }}</td>
         <td class="left align-middle">{{ taxRate.category.name }}</td>
@@ -33,5 +34,25 @@
                 [linkTo]="['./', taxRate.id]"
             ></vdr-table-row-action>
         </td>
+        <td class="right align-middle">
+            <vdr-dropdown>
+                <button type="button" class="btn btn-link btn-sm" vdrDropdownTrigger>
+                    {{ 'common.actions' | translate }}
+                    <clr-icon shape="caret down"></clr-icon>
+                </button>
+                <vdr-dropdown-menu vdrPosition="bottom-right">
+                    <button
+                        type="button"
+                        class="delete-button"
+                        (click)="deleteTaxRate(taxRate)"
+                        [disabled]="!('DeleteSettings' | hasPermission)"
+                        vdrDropdownItem
+                    >
+                        <clr-icon shape="trash" class="is-danger"></clr-icon>
+                        {{ 'common.delete' | translate }}
+                    </button>
+                </vdr-dropdown-menu>
+            </vdr-dropdown>
+        </td>
     </ng-template>
 </vdr-data-table>

+ 48 - 2
packages/admin-ui/src/app/settings/components/tax-rate-list/tax-rate-list.component.ts

@@ -1,9 +1,14 @@
 import { ChangeDetectionStrategy, Component } from '@angular/core';
 import { ActivatedRoute, Router } from '@angular/router';
+import { EMPTY } from 'rxjs';
+import { map, switchMap } from 'rxjs/operators';
 
 import { BaseListComponent } from '../../../common/base-list.component';
-import { GetTaxRateList } from '../../../common/generated-types';
+import { DeletionResult, GetTaxRateList } from '../../../common/generated-types';
+import { _ } from '../../../core/providers/i18n/mark-for-extraction';
+import { NotificationService } from '../../../core/providers/notification/notification.service';
 import { DataService } from '../../../data/providers/data.service';
+import { ModalService } from '../../../shared/providers/modal/modal.service';
 
 @Component({
     selector: 'vdr-tax-rate-list',
@@ -12,11 +17,52 @@ import { DataService } from '../../../data/providers/data.service';
     changeDetection: ChangeDetectionStrategy.OnPush,
 })
 export class TaxRateListComponent extends BaseListComponent<GetTaxRateList.Query, GetTaxRateList.Items> {
-    constructor(private dataService: DataService, router: Router, route: ActivatedRoute) {
+    constructor(
+        private modalService: ModalService,
+        private notificationService: NotificationService,
+        private dataService: DataService,
+        router: Router,
+        route: ActivatedRoute,
+    ) {
         super(router, route);
         super.setQueryFn(
             (...args: any[]) => this.dataService.settings.getTaxRates(...args),
             data => data.taxRates,
         );
     }
+
+    deleteTaxRate(taxRate: GetTaxRateList.Items) {
+        return this.modalService
+            .dialog({
+                title: _('settings.confirm-delete-tax-rate'),
+                body: taxRate.name,
+                buttons: [
+                    { type: 'secondary', label: _('common.cancel') },
+                    { type: 'danger', label: _('common.delete'), returnValue: true },
+                ],
+            })
+            .pipe(
+                switchMap(res => (res ? this.dataService.settings.deleteTaxRate(taxRate.id) : EMPTY)),
+                map(res => res.deleteTaxRate),
+            )
+            .subscribe(
+                res => {
+                    if (res.result === DeletionResult.DELETED) {
+                        this.notificationService.success(_('common.notify-delete-success'), {
+                            entity: 'TaxRate',
+                        });
+                        this.refresh();
+                    } else {
+                        this.notificationService.error(res.message || _('common.notify-delete-error'), {
+                            entity: 'TaxRate',
+                        });
+                    }
+                },
+                err => {
+                    this.notificationService.error(_('common.notify-delete-error'), {
+                        entity: 'TaxRate',
+                    });
+                },
+            );
+    }
 }

+ 1 - 0
packages/admin-ui/src/i18n-messages/en.json

@@ -560,6 +560,7 @@
     "channel": "Channel",
     "channel-token": "Channel token",
     "confirm-delete-role": "Delete role?",
+    "confirm-delete-tax-rate": "Delete tax rate?",
     "create": "Create",
     "create-new-channel": "Create new channel",
     "create-new-country": "Create new country",