Skip to content

Commit

Permalink
remove not needed state
Browse files Browse the repository at this point in the history
  • Loading branch information
Quanmuito committed Jan 25, 2024
1 parent f5dcad5 commit 09c61af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
35 changes: 10 additions & 25 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { useForm, SubmitHandler } from 'react-hook-form';
import { Input } from 'components';
import { getDeliveryFee } from 'calculator';
import { AppState, InputDetails } from 'types';
import { InputFields, InputDetails } from 'types';
import {
TYPE_DATETIME,
TYPE_NUMBER,
Expand Down Expand Up @@ -38,30 +38,11 @@ const INPUT_DETAILS: InputDetails[] = [
},
];

const initialState: AppState = {
cartValue: 0,
deliveryDistance: 0,
numberOfItems: 0,
orderTime: new Date().toISOString().slice(0, 16),
};

export default function App() {
const { register, handleSubmit, setError, formState: { errors } } = useForm<AppState>();
const [appState, setAppState] = useState<AppState>(initialState);
const { register, handleSubmit, setError, formState: { errors } } = useForm<InputFields>();
const [fee, setFee] = useState<number>(0);

const getResult = (): number => {
if (isZero(appState.cartValue) && isZero(appState.numberOfItems)) {
return 0;
}
return getDeliveryFee(
appState.cartValue,
appState.deliveryDistance,
appState.numberOfItems,
appState.orderTime
);
};

const onSubmit: SubmitHandler<AppState> = (data) => {
const onSubmit: SubmitHandler<InputFields> = (data) => {
if (isNaN(data.cartValue)) {
setError('cartValue', {
type: 'manual',
Expand Down Expand Up @@ -94,7 +75,11 @@ export default function App() {
return;
}

setAppState({ ...data });
if (isZero(data.cartValue) && isZero(data.numberOfItems)) {
setFee(0);
} else {
setFee(getDeliveryFee(data.cartValue, data.deliveryDistance, data.numberOfItems, data.orderTime));
}
};

return (
Expand Down Expand Up @@ -122,7 +107,7 @@ export default function App() {
</button>
<div className={ style.result }>
<h3>Total delivery fee (€)</h3>
<h3 data-test-id="fee">{ getResult().toFixed(2) }</h3>
<h3 data-test-id="fee">{ fee.toFixed(2) }</h3>
</div>
</form>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import React from 'react';
import { UseFormRegister, RegisterOptions } from 'react-hook-form';
import { AppState } from 'types';
import { InputFields } from 'types';
import { isEmptyString } from 'utils';
import style from 'style/style.module.css';

type InputPropsType = {
id: keyof AppState,
id: keyof InputFields,
label: string,
type: string,
description: string,
errorMessage: string
datetime: boolean,
register: UseFormRegister<AppState>
register: UseFormRegister<InputFields>
}

export const Input = ({
Expand All @@ -23,7 +23,7 @@ export const Input = ({
datetime = false,
register,
}: InputPropsType) => {
const options: RegisterOptions<AppState, keyof AppState> = {
const options: RegisterOptions<InputFields, keyof InputFields> = {
required: true,
};

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export type AppState = {
export type InputFields = {
cartValue: number,
deliveryDistance: number,
numberOfItems: number,
orderTime: string,
}

export type InputDetails = {
id: keyof AppState,
id: keyof InputFields,
label: string,
type: string,
description: string,
Expand Down

0 comments on commit 09c61af

Please sign in to comment.