Skip to content

Commit

Permalink
Change Boolean product attribute to Select (#4997)
Browse files Browse the repository at this point in the history
* change boolean attr to select

* json messages

---------

Co-authored-by: Paweł Chyła <chyla1988@gmail.com>
  • Loading branch information
Cloud11PL and poulch committed Jul 8, 2024
1 parent e4b9c5a commit c3dd08c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-hairs-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

The boolean attribute has been changed from a toggle to a select. This change helps visualize when the attribute has not been set.
12 changes: 12 additions & 0 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,10 @@
"context": "checkbox gift cards label",
"string": "Automatically fulfill non shippable gift cards"
},
"7WEeNq": {
"context": "select label",
"string": "True"
},
"7WzUxn": {
"context": "staff member status",
"string": "Inactive"
Expand Down Expand Up @@ -5427,6 +5431,10 @@
"b+jcaN": {
"string": "There are still fulfillments created for this order. Cancel the fulfillments first before you cancel the order."
},
"b1j4K6": {
"context": "select label",
"string": "False"
},
"b1t9bM": {
"context": "empty headers text",
"string": "No custom request headers created for this webhook. Use the button below to add new custom request header."
Expand Down Expand Up @@ -6533,6 +6541,10 @@
"context": "app data privacy link",
"string": "Learn more about data privacy"
},
"k62BKw": {
"context": "select label",
"string": "Unset"
},
"k6WDZl": {
"context": "attribute is visible",
"string": "Visible"
Expand Down
13 changes: 1 addition & 12 deletions src/attributes/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
SearchProductsQuery,
SelectedVariantAttributeFragment,
UploadErrorFragment,
VariantAttributeFragment,
} from "@dashboard/graphql";
import { FormsetData } from "@dashboard/hooks/useFormset";
import { RelayToFlat } from "@dashboard/types";
Expand Down Expand Up @@ -159,16 +158,6 @@ export function getAttributeData(
}
}

export function getDefaultAttributeValues(attribute: VariantAttributeFragment) {
switch (attribute.inputType) {
case AttributeInputTypeEnum.BOOLEAN:
return ["false"];

default:
return [];
}
}

export function getSelectedAttributeValues(
attribute:
| PageSelectedAttributeFragment
Expand All @@ -189,7 +178,7 @@ export function getSelectedAttributeValues(
return [attribute.values[0]?.name];

case AttributeInputTypeEnum.BOOLEAN:
return [attribute.values[0]?.boolean ?? "false"];
return [attribute.values[0]?.boolean];

case AttributeInputTypeEnum.DATE:
return [attribute.values[0]?.date];
Expand Down
2 changes: 1 addition & 1 deletion src/attributes/utils/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function getFileInput(
function getBooleanInput(attribute: AttributeInput) {
return {
id: attribute.id,
boolean: JSON.parse(attribute.value[0] ?? "false"),
boolean: attribute.value[0] ? JSON.parse(attribute.value[0]) : null,
};
}

Expand Down
57 changes: 37 additions & 20 deletions src/components/Attributes/AttributeRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ExtendedAttributeRow from "@dashboard/components/Attributes/ExtendedAttri
import { attributeRowMessages } from "@dashboard/components/Attributes/messages";
import { SwatchRow } from "@dashboard/components/Attributes/SwatchRow";
import {
booleanAttrValueToValue,
getBooleanDropdownOptions,
getErrorMessage,
getFileChoice,
getMultiChoices,
Expand All @@ -21,7 +23,7 @@ import SortableChipsField from "@dashboard/components/SortableChipsField";
import { AttributeInputTypeEnum } from "@dashboard/graphql";
import { commonMessages } from "@dashboard/intl";
import { TextField } from "@material-ui/core";
import { Box, Checkbox, Input, Text } from "@saleor/macaw-ui/next";
import { Box, Input, Select, Text } from "@saleor/macaw-ui/next";
import React from "react";
import { useIntl } from "react-intl";

Expand Down Expand Up @@ -200,24 +202,39 @@ const AttributeRow: React.FC<AttributeRowProps> = ({
);
case AttributeInputTypeEnum.BOOLEAN:
return (
<Box as="li" display="flex" gap={2} alignItems="center" padding={1}>
<Box data-test-id="attribute-value">
<Box
display="flex"
gap={0.5}
flexDirection="column"
alignItems="flex-end"
>
<Checkbox
name={`attribute:${attribute.label}`}
onCheckedChange={checked => onChange(attribute.id, checked)}
checked={JSON.parse(attribute.value[0] ?? "false")}
error={!!error}
id={`attribute:${attribute.label}`}
/>
<Text variant="caption" color="textCriticalDefault">
{getErrorMessage(error, intl)}
</Text>
<BasicAttributeRow label={attribute.label}>
<Box
as="li"
display="flex"
gap={2}
alignItems="center"
justifyContent="flex-end"
padding={1}
>
<Box data-test-id="attribute-value">
<Box
display="flex"
gap={0.5}
flexDirection="column"
alignItems="flex-end"
>
<Select
name={`attribute:${attribute.label}`}
value={booleanAttrValueToValue(attribute.value[0])}
onChange={value =>
onChange(
attribute.id,
value === "unset" ? undefined : value === "true",
)
}
options={getBooleanDropdownOptions(intl)}
id={`attribute:${attribute.label}`}
disabled={disabled}
/>
<Text fontSize="bodySmall" color="textCriticalDefault">
{getErrorMessage(error, intl)}
</Text>
</Box>
</Box>
</Box>
<Box
Expand All @@ -230,7 +247,7 @@ const AttributeRow: React.FC<AttributeRowProps> = ({
>
<Text>{attribute.label}</Text>
</Box>
</Box>
</BasicAttributeRow>
);
case AttributeInputTypeEnum.DATE:
return (
Expand Down
37 changes: 37 additions & 0 deletions src/components/Attributes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,40 @@ export function getErrorMessage(
return getPageErrorMessage(err, intl);
}
}

export function booleanAttrValueToValue(value: unknown | undefined): string {
if (typeof value !== "boolean") {
return "unset";
}

return value ? "true" : "false";
}

export function getBooleanDropdownOptions(intl: IntlShape) {
return [
{
label: intl.formatMessage({
defaultMessage: "True",
id: "7WEeNq",
description: "select label",
}),
value: "true",
},
{
label: intl.formatMessage({
defaultMessage: "False",
id: "b1j4K6",
description: "select label",
}),
value: "false",
},
{
label: intl.formatMessage({
defaultMessage: "Unset",
id: "k62BKw",
description: "select label",
}),
value: "unset",
},
];
}
3 changes: 1 addition & 2 deletions src/products/utils/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-strict-ignore
import {
getDefaultAttributeValues,
getSelectedAttributeValues,
mergeChoicesWithValues,
} from "@dashboard/attributes/utils/data";
Expand Down Expand Up @@ -97,7 +96,7 @@ export function getAttributeInputFromAttributes(
},
id: attribute.id,
label: attribute.name,
value: getDefaultAttributeValues(attribute),
value: [],
}));
}

Expand Down

0 comments on commit c3dd08c

Please sign in to comment.