Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #419 formatting numbers in plans and jobs #515

Merged
merged 9 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions mw-webapp/src/component/editableText/EditableText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {HTMLInputTypeAttribute, useState} from "react";
import clsx from "clsx";
import {renderSpan} from "src/component/editableText/renderSpan";
import {FormatterInputValue} from "src/component/input/formatters";
import {Input} from "src/component/input/Input";
import {KeySymbols} from "src/utils/KeySymbols";
import styles from "src/component/editableText/EditableText.module.scss";
Expand All @@ -11,9 +12,9 @@ import styles from "src/component/editableText/EditableText.module.scss";
interface EditableTextProps<T> {

/**
* Cell item's text
* Cell item's value
*/
text: T;
value: T;

/**
* Function that update element on Enter click or unfocused
Expand Down Expand Up @@ -47,15 +48,15 @@ interface EditableTextProps<T> {
/**
* Render Input or span depend on client actions
*/
export const EditableText = <T extends string | number>(props: EditableTextProps<T>) => {
export const EditableValue = <T extends string | number>(props: EditableTextProps<T>) => {
const [isEditing, setIsEditing] = useState(false);
const [text, setText] = useState<T>(props.text);
const [value, setValue] = useState(props.value);

/**
* HandleChangeFinish
*/
const handleChangeFinish = () => {
props.onChangeFinish(text);
props.onChangeFinish(value);
setIsEditing(false);
};

Expand All @@ -69,24 +70,23 @@ export const EditableText = <T extends string | number>(props: EditableTextProps
};

/**
* Check type of coming value and convert it to Number if need to use input with type "number"
* Update value
*/
const setValue = (value: string) => {
const number = Number(value) ?? 0;
const updatedValue = props.type === "number" ? number : value;
setText(updatedValue as T);
const updateValue = (updatedValue: string | number) => {
setValue(updatedValue as T);
};

/**
* Render input
*/
const renderInput = () => (
<Input
formatter={FormatterInputValue.withNoFirstZero}
type={props.type ?? "text"}
max={props.max}
value={text}
value={value}
Ekaterina1994 marked this conversation as resolved.
Show resolved Hide resolved
autoFocus={true}
onChange={setValue}
onChange={updateValue}
/>
);

Expand All @@ -101,7 +101,7 @@ export const EditableText = <T extends string | number>(props: EditableTextProps
>
{isEditing
? renderInput()
: renderSpan(text)
: renderSpan(value)
}
</div>
);
Expand Down
40 changes: 34 additions & 6 deletions mw-webapp/src/component/input/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {HTMLInputTypeAttribute} from "react";
import clsx from "clsx";
import {InputMode} from "src/component/input/InputMode";
import {ParserInputValue} from "src/component/input/parsers";
import styles from "src/component/input/Input.module.scss";

/**
* Input's props
*/
interface InputProps {
interface InputProps<T extends string | number> {

/**
* Input's value
*/
value: string | number;
value: T;

/**
* Input's type (what type of value is expected)
Expand Down Expand Up @@ -61,25 +62,52 @@ interface InputProps {
/**
* Tracks the value entered into the input
*/
onChange: (value: string) => void;
onChange: (value: T) => void;

/**
* Formatting value
*/
formatter?: (value: T) => T;

/**
* Parsing formatted value
*/
parser?: (value: string) => T;

}

/**
* Input component
*/
export const Input = (props: InputProps) => {
export const Input = <T extends string | number>(props: InputProps<T>) => {

/**
* Event handler for the input change event
*/
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
props.onChange(event.target.value);
let parsedValue: T;

switch (props.type) {
case "number": {
parsedValue = props.parser
? props.parser(event.target.value)
: ParserInputValue.defaultNumberParser(event.target.value);
break;
}
case "string":
default: {
Ekaterina1994 marked this conversation as resolved.
Show resolved Hide resolved
parsedValue = props.parser
? props.parser(event.target.value)
: ParserInputValue.defaultTextParser(event.target.value);
break;
}
}
props.onChange(parsedValue);
};

return (
<input
value={props.value}
value={props.formatter ? props.formatter(props.value) : props.value}
type={props.type ?? "text"}
max={props.max}
placeholder={props.placeholder}
Expand Down
28 changes: 28 additions & 0 deletions mw-webapp/src/component/input/formatters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Formatters
*/
export class FormatterInputValue {

/**
* Delete first zero from input number
*/
public static withNoFirstZero(value: string | number) {
Ekaterina1994 marked this conversation as resolved.
Show resolved Hide resolved
const SECOND_INDEX = 1;
const valueStringified = value.toString();

const formattedValue = valueStringified.startsWith("0")
? valueStringified.slice(SECOND_INDEX)
: value;

return formattedValue;
Ekaterina1994 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Default string formatter
*/
public static defaultStringFormatter(value: string) {
return value;
}

}

20 changes: 20 additions & 0 deletions mw-webapp/src/component/input/parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Parsers
*/
export class ParserInputValue {

/**
* Default text parser
*/
public static defaultTextParser<T = string>(value: string): T {
return value as T;
}

/**
* Default number parser
*/
public static defaultNumberParser<T = number>(value: string): T {
return Number(value) as T;
}

}
2 changes: 1 addition & 1 deletion mw-webapp/src/component/userCard/UserCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const UserCard = (props: UserCardProps) => {
const navigate = useNavigate();

return (
<a onClick={() => navigate(pages.way.getPath({uuid: props.userPreview.uuid}))}>
<a onClick={() => navigate(pages.user.getPath({uuid: props.userPreview.uuid}))}>
<VerticalContainer className={styles.userCardContainer}>
<VerticalContainer className={styles.mainInfo}>
<HorizontalContainer className={styles.nameLikes}>
Expand Down
2 changes: 1 addition & 1 deletion mw-webapp/src/component/wayCard/WayCard.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $wayCardAdditionalHeight: 105px;

.wayTags {
display: -webkit-box;
overflow: hidden;
align-items: center;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
}
Expand Down
2 changes: 1 addition & 1 deletion mw-webapp/src/component/wayCard/wayTag/WayTag.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ $WayTagBorderRadius: 1000px;
padding: $WayTagPadding;
border-radius: $WayTagBorderRadius;
margin-right: $marginMedium;
background-color: var(--secondaryBackgroundColor);
background-color: var(--primaryBackgroundColor);
color: var(--fourthBackgroundColor);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TrashIcon} from "@radix-ui/react-icons";
import {Checkbox} from "src/component/checkbox/Сheckbox";
import {Confirm} from "src/component/confirm/Confirm";
import {EditableText} from "src/component/editableText/EditableText";
import {EditableValue} from "src/component/editableText/EditableText";
import {HorizontalContainer} from "src/component/horizontalContainer/HorizontalContainer";
import {Tooltip} from "src/component/tooltip/Tooltip";
import {Metric} from "src/model/businessModel/Metric";
Expand Down Expand Up @@ -58,8 +58,8 @@ export const GoalMetricItem = (props: SingleGoalMetricProps) => {
onChange={(isDone) => props.updateMetric({...props.metric, isDone, doneDate: new Date()})}
/>
<Tooltip content={tooltipContent}>
<EditableText
text={props.metric.description ?? ""}
<EditableValue
value={props.metric.description ?? ""}
onChangeFinish={(description) => props.updateMetric({...props.metric, description})}
isEditable={props.isEditable}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {clsx} from "clsx";
import {Button} from "src/component/button/Button";
import {Checkbox} from "src/component/checkbox/Сheckbox";
import {Confirm} from "src/component/confirm/Confirm";
import {EditableText} from "src/component/editableText/EditableText";
import {EditableValue} from "src/component/editableText/EditableText";
import {EditableTextarea} from "src/component/editableTextarea/editableTextarea";
import {HorizontalContainer} from "src/component/horizontalContainer/HorizontalContainer";
import {Link} from "src/component/link/Link";
Expand Down Expand Up @@ -274,12 +274,12 @@ export const Columns = (props: ColumnsProps) => {
position={PositionTooltip.RIGHT}
content={`Time${Symbols.NO_BREAK_SPACE}spent on job`}
>
<EditableText
text={jobDone.time}
<EditableValue
value={jobDone.time}
type="number"
max={MAX_TIME}
onChangeFinish={(time) =>
updateJobDoneTime(jobDone, getValidatedTime(time))}
updateJobDoneTime(jobDone, getValidatedTime(Number(time)))}
className={styles.editableTime}
isEditable={isUserOwnerOrMentor}
/>
Expand Down Expand Up @@ -460,11 +460,11 @@ export const Columns = (props: ColumnsProps) => {
position={PositionTooltip.RIGHT}
content={`Estimated${Symbols.NO_BREAK_SPACE}time for the plan`}
>
<EditableText
text={plan.estimationTime}
<EditableValue
value={plan.estimationTime}
type="number"
max={MAX_TIME}
onChangeFinish={(estimationTime) => updatePlanTime(plan, getValidatedTime(estimationTime))}
onChangeFinish={(estimationTime) => updatePlanTime(plan, getValidatedTime(Number(estimationTime)))}
className={styles.editableTime}
isEditable={plan.ownerUuid === user?.uuid}
/>
Expand Down