complete pages and response

This commit is contained in:
maxnes3 2024-12-11 14:20:28 +04:00
parent fb64dfa1b7
commit cef3474b43
4 changed files with 60 additions and 21 deletions

View File

@ -32,7 +32,12 @@ const LaptopPredictPage = () => {
const handleGetPredict = async (event: FormEvent) => {
event.preventDefault();
console.log(request);
if (response.predicted_price) {
setResponse({
predicted_price: undefined,
});
return;
}
const newResponse = await apiLaptop.predictPrice(request);
setResponse(newResponse);
};

View File

@ -31,7 +31,12 @@ const TVPredictPage = () => {
const handleGetPredict = async (event: FormEvent) => {
event.preventDefault();
console.log(request);
if (response.predicted_price) {
setResponse({
predicted_price: undefined,
});
return;
}
const newResponse = await apiTV.predictPrice(request);
setResponse(newResponse);
};

View File

@ -1,5 +1,5 @@
import { PredictResponseType } from '@/shared/types/predict';
import { FormEvent } from 'react';
import { FormEvent, useMemo } from 'react';
import classes from './styles.module.scss';
import { Button } from '@/shared/components/button';
import { Selector } from '@/shared/components/selector';
@ -22,26 +22,35 @@ const Form = <T extends Record<string, string[] | number[]>>({
? (Object.keys(selectorsData) as Array<keyof T>)
: [];
const buttonLabel = useMemo(
() => (response.predicted_price ? 'Back to build' : 'Submit'),
[response.predicted_price],
);
return (
<form className={classes.form}>
<div className={classes.selectorList}>
{fields.map((field) => (
<Expander key={field as string} title={field as string}>
<Selector
handleSetValue={updateField}
list={selectorsData ? selectorsData[field] : []}
field={field as string}
/>
</Expander>
))}
</div>
<div className={classes.divider} />
<Button onClick={(e: FormEvent) => handleGetPredict(e)}>Submit</Button>
{response.predicted_price !== undefined && (
<span style={{ color: 'white' }}>
Ответ: {response.predicted_price}
</span>
{!response.predicted_price ? (
<div className={classes.selectorList}>
{fields.map((field) => (
<Expander key={field as string} title={field as string}>
<Selector
handleSetValue={updateField}
list={selectorsData ? selectorsData[field] : []}
field={field as string}
/>
</Expander>
))}
</div>
) : (
<div className={classes.response}>
<h3 className={classes.title}>Estimated price:</h3>
<p className={classes.price}>{response.predicted_price} руб</p>
</div>
)}
<div className={classes.divider} />
<Button onClick={(e: FormEvent) => handleGetPredict(e)}>
{buttonLabel}
</Button>
</form>
);
};

View File

@ -1,4 +1,5 @@
@use '../../shared/assets/styles/adaptive' as adaptive;
@use '@shared/assets/styles/adaptive' as adaptive;
@use '@shared/assets/styles/fonts' as fonts;
.form {
display: flex;
@ -15,6 +16,25 @@
gap: 8px;
}
.response {
display: flex;
flex-direction: column;
padding: 16px;
gap: 16px;
background-color: var(--primary-white);
border-radius: 16px;
.title {
margin: 0;
@include fonts.urbanist-font(28px, 600);
}
.price {
margin: 0;
@include fonts.urbanist-font(20px, 500);
}
}
.divider {
height: 4px;
width: 100%;