Skip to content

Commit

Permalink
fix(widget-field): replace deprecated widgetField to new widgetField
Browse files Browse the repository at this point in the history
Signed-off-by: samuel.park <[email protected]>
  • Loading branch information
piggggggggy committed Dec 16, 2024
1 parent 919cd60 commit 4efb92b
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { sortWidgetTableFields } from '@/common/modules/widgets/_helpers/widget-
import { useWidgetGenerateStore } from '@/common/modules/widgets/_store/widget-generate-store';
import { widgetValidatorRegistry } from '@/common/modules/widgets/_widget-field-value-manager/constant/validator-registry';
import type {
_FormatRulesValue, _FormatRulesOptions, ThresholdValue, _FormatRulesType,
FormatRulesValue, FormatRulesOptions, ThresholdValue, FormatRulesType,
} from '@/common/modules/widgets/_widget-fields/advanced-format-rules/type';
import type {
Expand All @@ -28,17 +28,17 @@ import type {
const FIELD_KEY = 'formatRules';
const props = defineProps<_WidgetFieldComponentProps<_FormatRulesOptions>>();
const props = defineProps<_WidgetFieldComponentProps<FormatRulesOptions>>();
const widgetGenerateStore = useWidgetGenerateStore();
const widgetGenerateGetters = widgetGenerateStore.getters;
const validator = widgetValidatorRegistry[FIELD_KEY];
const state = reactive({
fieldValue: computed<_FormatRulesValue>(() => props.fieldManager.data[FIELD_KEY].value),
fieldValue: computed<FormatRulesValue>(() => props.fieldManager.data[FIELD_KEY].value),
// TODO: remove options type assertion
type: computed<_FormatRulesType>(() => props.widgetFieldSchema?.options?.formatRulesType as _FormatRulesType),
invalid: computed(() => validator(state.fieldValue, props.widgetConfig)),
type: computed<FormatRulesType>(() => props.widgetFieldSchema?.options?.formatRulesType as FormatRulesType),
invalid: computed(() => !validator(state.fieldValue, props.widgetConfig)),
selectedField: computed<string|undefined>(() => {
if (props.widgetFieldSchema?.options?.useField) {
return state.fieldValue.field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const state = reactive({
name: key,
label: key,
}))),
invalid: computed(() => validator(state.fieldValue, props.widgetConfig)),
invalid: computed(() => !validator(state.fieldValue, props.widgetConfig)),
});
const handleClickColor = (color: ColorSchemaMenuItem) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<script lang="ts" setup>
import {
computed, reactive,
} from 'vue';
import type { TranslateResult } from 'vue-i18n';
import {
PFieldTitle, PToggleButton, PFieldGroup, PSelectDropdown, PI, PTooltip,
} from '@cloudforet/mirinae';
import type { SelectDropdownMenuItem } from '@cloudforet/mirinae/types/inputs/dropdown/select-dropdown/type';
import { i18n } from '@/translations';
import ColorInput from '@/common/components/inputs/ColorInput.vue';
import { isIncludingDateField } from '@/common/modules/widgets/_helpers/widget-field-helper';
import {
widgetFieldDefaultValueMap,
} from '@/common/modules/widgets/_widget-field-value-manager/constant/default-value-registry';
import type { ComparisonFormat, ComparisonValue, ComparisonOptions } from '@/common/modules/widgets/_widget-fields/comparison/type';
import type { GroupByValue } from '@/common/modules/widgets/_widget-fields/group-by/type';
import type {
_WidgetFieldComponentProps,
} from '@/common/modules/widgets/types/widget-field-type';
const FIELD_KEY = 'comparison';
const props = defineProps<_WidgetFieldComponentProps<ComparisonOptions>>();
const state = reactive({
fieldValue: computed<ComparisonValue>(() => props.fieldManager.data[FIELD_KEY].value),
disabled: computed<boolean>(() => { // NOTE: EXCEPTION FOR ONLY TABLE WIDGET
// TODO: add disable case with PIVOT DT
if (props.widgetConfig.widgetName !== 'table') return false;
const groupByField = props.fieldManager.data.groupBy.value as GroupByValue;
return Array.isArray(groupByField?.value) && isIncludingDateField(groupByField.value);
}),
infoText: computed<TranslateResult>(() => {
if (props.widgetConfig.widgetName !== 'table') return i18n.t('COMMON.WIDGETS.COMPARISON.INFO_TOOLTIP_TABLE');
return i18n.t('COMMON.WIDGETS.COMPARISON.INFO_TOOLTIP');
}),
initialValue: computed<ComparisonValue>(() => widgetFieldDefaultValueMap.comparison),
formatMenu: computed<SelectDropdownMenuItem[]>(() => [
{ label: i18n.t('COMMON.WIDGETS.COMPARISON.ALL'), name: 'all' },
{ label: `${i18n.t('COMMON.WIDGETS.COMPARISON.PERCENT')}(%)`, name: 'percent' },
{ label: i18n.t('COMMON.WIDGETS.COMPARISON.FIXED'), name: 'fixed' },
]),
});
const handleUpdateColor = (key:'decreaseColor'|'increaseColor', color:string) => {
props.fieldManager.setFieldValue(FIELD_KEY, {
...state.fieldValue,
[key]: color,
});
};
const handleUpdateToggle = (value: boolean) => {
if (value) {
props.fieldManager.setFieldValue(FIELD_KEY, state.initialValue);
} else {
props.fieldManager.setFieldValue(FIELD_KEY, {
toggleValue: false,
});
}
};
const handleUpdateFormat = (format: ComparisonFormat) => {
props.fieldManager.setFieldValue(FIELD_KEY, {
...state.fieldValue,
format,
});
};
</script>

<template>
<div class="widget-field-total">
<div class="field-header">
<p-field-title>
{{ $t('COMMON.WIDGETS.COMPARISON.COMPARISON') }}
<p-tooltip :contents="state.infoText">
<p-i name="ic_info-circle"
width="0.875rem"
height="0.875rem"
/>
</p-tooltip>
</p-field-title>
<p-toggle-button :value="state.fieldValue?.toggleValue"
:disabled="state.disabled"
@change-toggle="handleUpdateToggle"
/>
</div>
<template v-if="state.fieldValue?.toggleValue">
<div class="compare-with-wrapper">
<p-field-group :label="$t('COMMON.WIDGETS.COMPARISON.COMPARE_WITH')"
class="left-part"
style-type="secondary"
required
>
<span class="previous-period-text">{{ $t('COMMON.WIDGETS.COMPARISON.PREVIOUS_PERIOD') }}</span>
</p-field-group>
</div>
<div class="compare-input-wrapper">
<div class="left-part">
<p-field-group :label="$t('COMMON.WIDGETS.COMPARISON.DECREASE')"
style-type="secondary"
required
>
<color-input :value="state.fieldValue.decreaseColor"
@update:value="(color) => handleUpdateColor('decreaseColor', color)"
/>
</p-field-group>
<p-field-group :label="$t('COMMON.WIDGETS.COMPARISON.INCREASE')"
style-type="secondary"
required
>
<color-input :value="state.fieldValue.increaseColor"
@update:value="(color) => handleUpdateColor('increaseColor', color)"
/>
</p-field-group>
</div>
<p-field-group :label="$t('COMMON.WIDGETS.COMPARISON.FORMAT')"
class="w-full"
style-type="secondary"
required
>
<p-select-dropdown :menu="state.formatMenu"
use-fixed-menu-style
:selected="state.fieldValue.format"
block
@update:selected="(format) => handleUpdateFormat(format)"
/>
</p-field-group>
</div>
</template>
</div>
</template>

<style lang="postcss" scoped>
.widget-field-total {
.field-header {
@apply flex items-center gap-1 justify-between;
}
.compare-with-wrapper {
@apply flex gap-2;
.left-part {
flex-shrink: 0;
}
.previous-period-text {
@apply text-label-md text-gray-800;
}
}
.compare-input-wrapper {
@apply flex gap-2;
.left-part {
@apply flex gap-3;
flex-shrink: 0;
}
}
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<script lang="ts" setup>
import {
computed, reactive, watch,
computed, reactive,
} from 'vue';

import { PSelectDropdown, PFieldGroup } from '@cloudforet/mirinae';
Expand All @@ -25,7 +25,7 @@ const widgetGenerateGetters = widgetGenerateStore.getters;
const validator = widgetValidatorRegistry[FIELD_KEY];

const state = reactive({
fieldValue: computed<DataFieldValue>(() => props.fieldManager.data?.[FIELD_KEY]?.value),
fieldValue: computed<DataFieldValue>(() => props.fieldManager.data[FIELD_KEY]?.value),
multiselectable: computed(() => props.widgetFieldSchema?.options?.multiSelectable),
menuItems: computed<MenuItem[]>(() => {
const dataInfoList = Object.keys(widgetGenerateGetters.selectedDataTable?.data_info ?? {}) ?? [];
Expand All @@ -34,7 +34,7 @@ const state = reactive({
label: d,
}));
}),
invalid: computed<boolean>(() => validator(state.fieldValue, props.widgetConfig)),
invalid: computed<boolean>(() => !validator(state.fieldValue, props.widgetConfig)),
selectedItem: computed<MenuItem[]|string|undefined>(() => {
if (!state.menuItems.length) return undefined;
if (state.multiselectable) {
Expand All @@ -55,22 +55,6 @@ const convertToMenuItem = (data: string[]) => data.map((d) => ({
label: d,
}));

/* Watcher */
// initial field value
watch(() => state.selectedItem, (menuItem) => {
if (menuItem?.length && !state.fieldValue.data) {
if (state.multiselectable) {
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.fieldValue, data: [menuItem[0]] });
} else {
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.fieldValue, field: menuItem[0].name });
}
}
}, { immediate: true });

watch(() => state.fieldValue, (newValue) => {
console.debug('DataField - fieldValue', newValue);
});

</script>

<template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const state = reactive({
const { dateRange } = useWidgetDateRange({
dateRangeFieldValue: computed(() => state.fieldValue),
baseOnDate: computed(() => (state.fieldValue?.inherit ? state.baseDateRange?.end : undefined)),
granularity: computed(() => state.granularity),
granularity: computed(() => props.fieldManager.data.granularity?.value),
usePreviewFormat: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const validator = widgetValidatorRegistry[FIELD_KEY];
const props = defineProps<_WidgetFieldComponentProps<undefined>>();
const state = reactive({
fieldValue: computed<DisplayAnnotationValue>(() => props.fieldManager.data[FIELD_KEY].value),
invalid: computed(() => validator(state.fieldValue, props.widgetConfig)),
invalid: computed(() => !validator(state.fieldValue, props.widgetConfig)),
invalidText: computed(() => {
if (state.invalid) return i18n.t('COMMON.WIDGETS.DISPLAY_ANNOTATION.INVALID_VALUE');
return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,11 @@ const state = reactive({
}
if (state.fixedValue) return state.menuItems.find((d) => d.name === state.fixedValue);
if (state.multiselectable) {
return state.menuItems.filter((d) => state.fieldValue.value?.includes(d.name));
return state.menuItems.filter((d) => state.fieldValue.data?.includes(d.name));
}
return state.fieldValue.value;
}),
invalid: computed(() => validator(state.fieldValue, props.widgetConfig)),
isValid: computed<boolean>(() => {
if (!state.hideCount && !state.proxyValue?.count) return false;
if (state.multiselectable && !state.selectedItem?.length) return false;
if (state.fixedValue) {
if (Array.isArray(state.selectedItem)) return state.selectedItem?.map((d) => d.name).include(state.fixedValue);
return state.selectedItem === state.fixedValue;
}
return !!state.selectedItem;
return state.fieldValue.data;
}),
isValid: computed(() => validator(state.fieldValue, props.widgetConfig)),
max: computed(() => props.widgetFieldSchema?.options?.max),
isMaxValid: computed<boolean>(() => (state.max ? ((state.fieldValue?.count ?? DEFAULT_COUNT) <= state.max) : true)),
tooltipDesc: computed(() => i18n.t('COMMON.WIDGETS.MAX_ITEMS_DESC', {
Expand All @@ -83,16 +74,16 @@ const state = reactive({
/* Event */
const handleUpdateSelect = (val: string|MenuItem[]) => {
state.selectedItem = val;
if (Array.isArray(val)) {
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.proxyValue, value: val.map((item) => item.name) });
if (!val) return;
if (state.multiselectable && Array.isArray(val)) {
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.fieldValue, data: val.map((item) => item.name) });
} else {
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.proxyValue, value: val });
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.fieldValue, data: val });
}
};
const handleUpdateCount = (val: number) => {
if (val === state.fieldValue.count) return;
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.proxyValue, count: val });
props.fieldManager.setFieldValue(FIELD_KEY, { ...state.fieldValue, count: val });
};
</script>
Expand All @@ -112,7 +103,7 @@ const handleUpdateCount = (val: number) => {
:selected="state.selectedItem"
:multi-selectable="state.multiselectable"
:show-select-marker="state.multiselectable"
:invalid="state.invalid"
:invalid="!state.isValid"
:disabled="!!state.fixedValue"
appearance-type="badge"
block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const validator = widgetValidatorRegistry[FIELD_KEY];
const state = reactive({
fieldValue: computed<WidgetHeaderValue>(() => props.fieldManager.data[FIELD_KEY].value),
invalid: computed(() => validator(state.fieldValue, props.widgetConfig)),
invalid: computed(() => !validator(state.fieldValue, props.widgetConfig)),
titleInvalidText: computed(() => {
if (state.invalid) {
return i18n.t('COMMON.WIDGETS.WIDGET_TITLE_REQUIRED');
Expand All @@ -36,6 +36,7 @@ const state = reactive({
/* Event */
const handleToggleWidgetHeader = (value: boolean) => {
console.debug('[[HEADER]] - toggle', value);
if (value) {
props.fieldManager.setFieldValue(FIELD_KEY, {
toggleValue: true,
Expand All @@ -51,6 +52,8 @@ const handleToggleWidgetHeader = (value: boolean) => {
}
};
const handleUpdateValue = (key: string, value: string) => {
console.debug('[[HEADER]] - value', key, value);
props.fieldManager.setFieldValue(FIELD_KEY, {
...state.fieldValue,
[key]: value?.trim() || '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const validator = widgetValidatorRegistry[FIELD_KEY];
const state = reactive({
fieldValue: computed<_IconValue>(() => props.fieldManager.data[FIELD_KEY].value),
visibleMenu: false,
invalid: computed(() => validator(state.fieldValue, props.widgetConfig)),
invalid: computed(() => !validator(state.fieldValue, props.widgetConfig)),
iconList: ICON_FIELD_ITEMS,
defaultColor: widgetFieldDefaultValueMap.icon.color,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { getInitialSelectedMenuItem, isDateField } from '@/common/modules/widgets/_helpers/widget-field-helper';
import { sortWidgetTableFields } from '@/common/modules/widgets/_helpers/widget-helper';
import type { DateRangeValue } from '@/common/modules/widgets/_widget-fields/date-range/type';
import type { GranularityValue } from '@/common/modules/widgets/_widget-fields/granularity/type';
import type { GroupByValue } from '@/common/modules/widgets/_widget-fields/group-by/type';
import type {
TableDataFieldOptions,
Expand Down Expand Up @@ -78,9 +79,9 @@ const {
widgetConfig: toRef(props, 'widgetConfig') as Ref<WidgetConfig>,
});
const { dateRange } = useWidgetDateRange({
dateRangeFieldValue: computed(() => (props.allValueMap?.dateRange as DateRangeValue)),
dateRangeFieldValue: computed(() => (props.allValueMap?.dateRange?.value as DateRangeValue)),
baseOnDate: computed(() => props.dateRange?.end),
granularity: computed<string>(() => props.allValueMap?.granularity as string),
granularity: computed<GranularityValue>(() => props.allValueMap?.granularity?.value),
});
const state = reactive({
Expand Down
Loading

0 comments on commit 4efb92b

Please sign in to comment.