Skip to content

Commit

Permalink
refactor: vueify stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Dec 18, 2023
1 parent 9abac89 commit 7db485c
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 94 deletions.
17 changes: 2 additions & 15 deletions apps/admin/src/components/OrderListItem/ShipmentLabel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
<div :class="config?.cssUtilities?.displayFlex">
<DeliveryOptionsPackageType
:class="config?.cssUtilities?.flexGrow"
:delivery-options="data.deliveryOptions" />

<div :class="config?.cssUtilities?.flexGrow">
<DigitalStampWeightRange
v-if="data.deliveryOptions.packageType === PackageTypeName.DigitalStamp"
:class="config?.cssUtilities?.flexGrow"
:weight-range="dpzRangeWeight" />
</div>
:shipment-or-order="data" />

<div :class="config?.cssUtilities?.flexGrow">
<ShipmentStatus :shipment-id="shipmentId" />
Expand All @@ -34,18 +27,13 @@
</PdkShipmentLabelWrapper>
</template>

<script
lang="ts"
setup>
<script lang="ts" setup>
import {toRefs} from 'vue';
import ShipmentStatus from '../common/ShipmentStatus.vue';
import ShipmentBarcode from '../common/ShipmentBarcode.vue';
import DeliveryOptionsPackageType from '../common/DeliveryOptionsPackageType.vue';
import {useQueryStore} from '../../stores';
import {useAdminConfig, useShipmentData} from '../../composables';
import {PackageTypeName} from '@myparcel/constants';
import {getDigitalStampRange} from '../../forms/helpers/getDigitalStampRange';
import DigitalStampWeightRange from '../common/DigitalStampWeightRange.vue';
const props = defineProps<{shipmentId: number}>();
Expand All @@ -55,5 +43,4 @@ useQueryStore().registerShipmentQueries(shipmentId);
const {loading, actions, shipment: data} = useShipmentData(shipmentId);
const config = useAdminConfig();
const dpzRangeWeight = getDigitalStampRange();
</script>
15 changes: 4 additions & 11 deletions apps/admin/src/components/common/DeliveryOptionsExcerpt.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-if="deliveryOptions">
<div v-if="data && deliveryOptions">
<span :class="config.cssUtilities?.displayFlex">
<CarrierLogo
v-if="carrier"
Expand All @@ -9,11 +9,7 @@

<ul :class="config.cssUtilities?.marginYAuto">
<li>
<DeliveryOptionsPackageType :delivery-options="deliveryOptions" />
</li>

<li v-if="deliveryOptions.packageType === PackageTypeName.DigitalStamp">
<DigitalStampWeightRange :weight-range="dpzRangeWeight" />
<DeliveryOptionsPackageType :shipment-or-order="data" />
</li>

<li>
Expand All @@ -37,20 +33,17 @@ import DeliveryOptionsPackageType from './DeliveryOptionsPackageType.vue';
import DeliveryOptionsDeliveryType from './DeliveryOptionsDeliveryType.vue';
import DateRelative from './DateRelative.vue';
import CarrierLogo from './CarrierLogo.vue';
import {PackageTypeName} from '@myparcel/constants';
import {getDigitalStampRange} from '../../forms/helpers/getDigitalStampRange';
import DigitalStampWeightRange from './DigitalStampWeightRange.vue';
const {query} = useOrderData();
const deliveryOptions = computed(() => get(query.data)?.deliveryOptions);
const data = computed(() => get(query.data));
const deliveryOptions = computed(() => data.value?.deliveryOptions);
const carrier = computed(() => {
const carriersQuery = useFetchCarrier(deliveryOptions.value?.carrier?.name);
return get(carriersQuery.data);
});
const dpzRangeWeight = getDigitalStampRange();
const config = useAdminConfig();
</script>
30 changes: 23 additions & 7 deletions apps/admin/src/components/common/DeliveryOptionsPackageType.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
<template>
<span
v-if="deliveryOptions.packageType"
v-test="[$.type.__name, deliveryOptions.packageType]">
<PdkIcon :icon="deliveryOptions.packageType" />
v-if="resolved.deliveryOptions?.packageType"
v-test="[$.type.__name, resolved.deliveryOptions.packageType]">
<PdkIcon :icon="resolved.deliveryOptions.packageType" />

<span
:title="translate(getPackageTypeTranslation())"
v-text="translate(getPackageTypeTranslation(deliveryOptions.packageType))" />
v-text="translate(getPackageTypeTranslation(resolved.deliveryOptions.packageType))" />

<DigitalStampWeightRange
v-if="resolved.deliveryOptions?.packageType === PackageTypeName.DigitalStamp"
:weight="weight" />
</span>
</template>

<script lang="ts" setup>
import {computed} from 'vue';
import {get, type MaybeRef} from '@vueuse/core';
import {type Shipment} from '@myparcel-pdk/common';
import {type Plugin} from '@myparcel-pdk/admin';
import {isOfType} from '@myparcel/ts-utils';
import {PackageTypeName} from '@myparcel/constants';
import {getPackageTypeTranslation} from '../../utils';
import {useLanguage} from '../../composables';
import DigitalStampWeightRange from './DigitalStampWeightRange.vue';
defineProps<{
deliveryOptions: Shipment.ModelDeliveryOptions;
}>();
const props = defineProps<{shipmentOrOrder: MaybeRef<Shipment.ModelShipment | Plugin.ModelPdkOrder>}>();
const {translate} = useLanguage();
const resolved = computed(() => get(props.shipmentOrOrder));
const weight = computed(() => {
return isOfType<Shipment.ModelPhysicalProperties>(resolved.value.physicalProperties, 'weight')
? resolved.value.physicalProperties.weight
: resolved.value.physicalProperties?.totalWeight ?? 0;
});
</script>
20 changes: 7 additions & 13 deletions apps/admin/src/components/common/DigitalStampWeightRange.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<template>
<span
v-if="weightRange"
v-test="[$.type.__name, weightRange]">

<span
:title="translate('digital_stamp_weight_range', {weightRange})"
v-text="`${weightRange.min}g - ${weightRange.max}g`" />
</span>
<span v-text="defaultRange" />
</template>

<script lang="ts" setup>
import {useLanguage} from '../../composables';
import {toRefs} from 'vue';
import {useDigitalStampRanges} from '../../composables';
const props = defineProps<{weight: number}>();
defineProps<{
weightRange: {min: number; max: number; average: number};
}>();
const propRefs = toRefs(props);
const {translate} = useLanguage();
const {defaultRange} = useDigitalStampRanges(propRefs.weight);
</script>
1 change: 1 addition & 0 deletions apps/admin/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './useAdminInstance';
export * from './useAssetUrl';
export * from './useBulkSelectCheckbox';
export * from './useCheckboxGroupContext';
export * from './useDigitalStampRanges';
export * from './useDropOffInputContext';
export * from './useDropdownData';
export * from './useElementContext';
Expand Down
38 changes: 38 additions & 0 deletions apps/admin/src/composables/useDigitalStampRanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {computed, type ComputedRef} from 'vue';
import {get, type MaybeRef} from '@vueuse/core';
import {type Plugin} from '@myparcel-pdk/common';
import {type SelectOptionWithPlainLabel} from '../types';
import {useOrderData} from './orders';

interface UseDigitalStampRanges {
ranges: ComputedRef<SelectOptionWithPlainLabel<number, string>[]>;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments
currentRange: ComputedRef<SelectOptionWithPlainLabel<number, string>>;
}

export const useDigitalStampRanges = (weight: MaybeRef<number>): UseDigitalStampRanges => {
// todo: move this to global context
const {order} = useOrderData();

const allRanges = computed<Plugin.DigitalStampRange[]>(() => get(order).digitalStampRanges ?? []);

const ranges = computed<SelectOptionWithPlainLabel<number, string>[]>(() => {
return allRanges.value.map((range) => ({
plainLabel: `${range.min}g – ${range.max}g`,
value: range.average,
}));
});

const currentRange = computed(() => {
const resolvedWeight = get(weight);

const matchingRange = allRanges.value.find((range) => range.min <= resolvedWeight && range.max >= resolvedWeight);

return ranges.value.find((range) => range.value === matchingRange?.average) ?? ranges.value[0];
});

return {
ranges,
currentRange,
};
};
23 changes: 0 additions & 23 deletions apps/admin/src/forms/helpers/getDigitalStampRange.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
import {ref} from 'vue';
import {first, get, last} from 'lodash-unified';
import {get} from 'lodash-unified';
import {get as vuGet} from '@vueuse/core';
import {type Plugin} from '@myparcel-pdk/common';
import {type InteractiveElementConfiguration} from '@myparcel/vue-form-builder';
import {PackageTypeName} from '@myparcel/constants';
import {type ShipmentOptionsRefs} from '../types';
import {FIELD_MANUAL_WEIGHT, FIELD_PACKAGE_TYPE} from '../field';
import {createDefaultOption, defineFormField, resolveFormComponent} from '../../helpers';
import {type GlobalFieldProps, type OptionsProp, type SelectOption} from '../../../types';
import {type GlobalFieldProps, type OptionsProp} from '../../../types';
import {AdminComponent, TriState} from '../../../data';
import {useOrderData, usePluginSettings} from '../../../composables';
import {useDigitalStampRanges, useOrderData, usePluginSettings} from '../../../composables';

export const createDigitalStampRangeField = (refs: ShipmentOptionsRefs): InteractiveElementConfiguration => {
const pluginSettings = usePluginSettings();
const {order} = useOrderData();

const orderData = vuGet(order);

const {digitalStampRanges} = orderData;
const {initialWeight, manualWeight} = orderData?.physicalProperties ?? {};

const totalWeight = Number(initialWeight) + Number(pluginSettings.order.emptyDigitalStampWeight ?? 0);

const ranges: Plugin.ModelContextOrderDataContext['digitalStampRanges'] = digitalStampRanges ?? [];

const rangeOptions = ranges.map((range) => ({
plainLabel: `${range.min}g – ${range.max}g`,
value: range.average,
})) satisfies SelectOption[];

const defaultRange = ranges.find((range) => {
return (range.min <= totalWeight && range.max >= totalWeight) ?? first(rangeOptions);
});
const {ranges, currentRange} = useDigitalStampRanges(orderData);

const selectedValue =
TriState.Inherit === manualWeight
? TriState.Inherit
: (get(refs, FIELD_MANUAL_WEIGHT, manualWeight) as number) ?? defaultRange?.average;

const defaultOption = rangeOptions.find((option) => option.value === defaultRange?.average) ?? last(rangeOptions);
: (get(refs, FIELD_MANUAL_WEIGHT, manualWeight) as number) ?? currentRange.value?.value;

return defineFormField({
name: FIELD_MANUAL_WEIGHT,
Expand All @@ -55,7 +42,7 @@ export const createDigitalStampRangeField = (refs: ShipmentOptionsRefs): Interac
n: totalWeight,
},
},
options: [createDefaultOption(defaultOption?.plainLabel), ...rangeOptions],
options: [createDefaultOption(currentRange.value?.plainLabel), ...ranges.value],
} satisfies OptionsProp & GlobalFieldProps,
});
};
3 changes: 2 additions & 1 deletion apps/admin/src/services/createNotification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type Variant} from '@myparcel-pdk/common';
import {type PdkNotification, NotificationCategory} from '../types';
import {type PdkNotification} from '../types';
import {NotificationCategory} from '../data';
import {useLanguage} from '../composables';

const PREFIX = 'notification_';
Expand Down
12 changes: 7 additions & 5 deletions libs/common/src/types/php-pdk.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,14 @@ export namespace Plugin {
shop: Account.ModelShop;
};

export interface DigitalStampRange {
min: number;
max: number;
average: number;
}

export type ModelContextOrderDataContext = ModelPdkOrder & {
digitalStampRanges: {
min: number;
max: number;
average: number;
}[];
digitalStampRanges: DigitalStampRange[];
inheritedDeliveryOptions: Record<string, Shipment.ModelDeliveryOptions>;
};

Expand Down

0 comments on commit 7db485c

Please sign in to comment.