Skip to content

Commit 46cee2e

Browse files
authored
max items for settings and login refactor (#16)
1 parent c9b42fc commit 46cee2e

File tree

6 files changed

+71
-16
lines changed

6 files changed

+71
-16
lines changed

src/api/api.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const BASE_URL = process.env.REACT_APP_SERVER_URL;
66
const axiosInstance = axios.create({
77
baseURL: BASE_URL,
88
headers: {
9-
'Content-Type': 'application/json',
10-
'Origin': window.location.origin
9+
'Content-Type': 'application/json'
1110
},
1211
});
1312

@@ -32,7 +31,6 @@ axiosInstance.interceptors.response.use(
3231
(error) => {
3332
if (error.response && error.response.status === 401) {
3433
localStorage.removeItem('authToken');
35-
return Promise.reject((window.location.href = ROUTES.login));
3634
}
3735
return Promise.reject(error);
3836
},
@@ -53,7 +51,6 @@ export const axiosRequestHandler = async ({ path, method, body }: AxiosRequestCo
5351
});
5452
return response.data;
5553
} catch (error) {
56-
console.error('api request error', error);
5754
throw error;
5855
}
5956
};

src/api/settings.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
SetShipmentCarrierPriceResponse,
99
SetSiteAvailabilityRequest,
1010
SetSiteAvailabilityResponse,
11+
SetMaxOrderItemsRequest,
1112
} from './proto-http/admin';
1213

1314
export function setShipmentCarrier(
@@ -33,3 +34,9 @@ export function setSiteAvailability(
3334
): Promise<SetSiteAvailabilityResponse> {
3435
return adminService.SetSiteAvailability(request);
3536
}
37+
38+
export function setMaxOrderItems(
39+
request: SetMaxOrderItemsRequest,
40+
): Promise<SetSiteAvailabilityResponse> {
41+
return adminService.SetMaxOrderItems(request);
42+
}

src/components/login/login.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const LoginBlock: FC = () => {
1818
}
1919
}, [navigate]);
2020

21-
const handlePasswordSubmit = async (e: FormEvent<HTMLFormElement>) => {
21+
const handleLoginSubmit = async (e: FormEvent<HTMLFormElement>) => {
2222
e.preventDefault();
2323

2424
try {
@@ -38,24 +38,26 @@ export const LoginBlock: FC = () => {
3838

3939
navigate({ to: ROUTES.main, replace: true });
4040
} catch (error) {
41-
console.error(error);
41+
setErrorMessage('error occured during login process');
4242
}
4343
};
4444

4545
const handlePasswordChange = (e: ChangeEvent<HTMLInputElement>) => {
4646
setPassword(e.target.value);
47+
setErrorMessage('');
4748
};
4849

4950
const handleUsernameChange = (e: ChangeEvent<HTMLInputElement>) => {
5051
setUsername(e.target.value);
52+
setErrorMessage('');
5153
};
5254

5355
return (
5456
<div className={styles.login_wrapper}>
5557
<div className={styles.logo}></div>
5658
<div className={styles.card_body}>
5759
{errorMessage && <p className={styles.error}>{errorMessage}</p>}
58-
<form className={styles.form} onSubmit={handlePasswordSubmit}>
60+
<form className={styles.form} onSubmit={handleLoginSubmit}>
5961
<div className={styles.user_container}>
6062
<input
6163
className={styles.input}
@@ -74,7 +76,7 @@ export const LoginBlock: FC = () => {
7476
placeholder='PASSWORD'
7577
/>
7678
</div>
77-
<button type='submit'>Login</button>
79+
<button type='submit' disabled={!username || !password}>Login</button>
7880
</form>
7981
</div>
8082
</div>

src/components/managers/media/mediaManager.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const MediaManager: FC = () => {
5656
const authToken = localStorage.getItem('authToken');
5757

5858
if (!authToken) {
59-
console.log('fuck off');
6059
alert('Authentication token not found');
6160
return;
6261
}

src/components/managers/settings/settings.tsx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ import {
77
common_ShipmentCarrierInsert,
88
} from 'api/proto-http/admin';
99
import {
10+
setMaxOrderItems,
1011
setPaymentMethod,
1112
setShipmentCarrier,
1213
setShipmentCarrierPrice,
1314
setSiteAvailability,
1415
} from 'api/settings';
1516
import styles from 'styles/settings.scss';
17+
import { useNavigate } from '@tanstack/react-location';
18+
import { ROUTES } from 'constants/routes';
19+
1620

1721
export const Settings: FC = () => {
1822
const [payment, setPayment] = useState<common_PaymentMethod[] | undefined>([]);
1923
const [carrier, setCarrier] = useState<common_ShipmentCarrierInsert[]>([]);
2024
const [price, setPrice] = useState<{ [name: string]: string }>({});
25+
const [maxItems, setMaxItems] = useState<number>();
2126
const [siteEnabled, setSiteEnabled] = useState<boolean>();
27+
const navigate = useNavigate();
2228

2329
useEffect(() => {
2430
const fetchDictionary = async () => {
@@ -30,6 +36,7 @@ export const Settings: FC = () => {
3036
).filter((c): c is common_ShipmentCarrierInsert => c !== undefined);
3137

3238
setCarrier(carrierData);
39+
setMaxItems(response.dictionary?.maxOrderItems)
3340
setSiteEnabled(response.dictionary?.siteEnabled);
3441
} catch (error) {
3542
console.error(error);
@@ -39,12 +46,21 @@ export const Settings: FC = () => {
3946
}, []);
4047

4148
const handlerPaymentMethod = async (paymentMethod: string | undefined, allow: boolean) => {
49+
if (paymentMethod) {
50+
setPayment(prevPayment => {
51+
return prevPayment?.map(method => {
52+
if (method.name === paymentMethod) {
53+
return { ...method, allowed: allow };
54+
}
55+
return method;
56+
});
57+
});
58+
}
4259
try {
43-
const response = await setPaymentMethod({
60+
await setPaymentMethod({
4461
paymentMethod: paymentMethod as common_PaymentMethodNameEnum,
4562
allow,
4663
});
47-
console.log(response);
4864
} catch (error) {
4965
console.error(error);
5066
}
@@ -70,11 +86,10 @@ export const Settings: FC = () => {
7086
const newPrice = price[carrier];
7187
if (newPrice) {
7288
try {
73-
const response = await setShipmentCarrierPrice({
89+
await setShipmentCarrierPrice({
7490
carrier: carrier,
7591
price: { value: newPrice },
7692
});
77-
console.log(response);
7893
} catch (error) {
7994
console.error(error);
8095
}
@@ -89,12 +104,25 @@ export const Settings: FC = () => {
89104
}
90105
};
91106

107+
const handlerMaxOrderItems = async (e: string) => {
108+
const maxOrderItemsParsed = parseInt(e, 10);
109+
setMaxItems(maxOrderItemsParsed);
110+
111+
try {
112+
await setMaxOrderItems({ maxOrderItems: maxItems });
113+
} catch (error) {
114+
console.error(error);
115+
}
116+
};
117+
118+
119+
92120
const handleSiteAvailability = async (available: boolean) => {
121+
setSiteEnabled(available);
93122
try {
94-
const response = await setSiteAvailability({
123+
await setSiteAvailability({
95124
available,
96125
});
97-
console.log(response);
98126
} catch (error) {
99127
console.error(error);
100128
}
@@ -113,6 +141,7 @@ export const Settings: FC = () => {
113141
<h3>{cutUnusedPartOfPaymentName(m.name)}</h3>
114142
<input
115143
type='checkbox'
144+
checked={m.allowed}
116145
onChange={(e) => handlerPaymentMethod(m.name, e.target.checked)}
117146
/>
118147
</div>
@@ -138,13 +167,22 @@ export const Settings: FC = () => {
138167
onChange={(e) => handlePriceChange(c.carrier, e.target.value)}
139168
/>
140169
<button type='button' onClick={() => handleShipmentCarrierPrice(c.carrier)}>
141-
upload
170+
update price
142171
</button>
143172
</div>
144173
</div>
145174
))}
146175
</div>
147176

177+
<div className={styles.max_items_container}>
178+
<h2>max items</h2>
179+
<input
180+
type='number'
181+
defaultValue={maxItems}
182+
onChange={(e) => handlerMaxOrderItems(e.target.value)}
183+
/>
184+
</div>
185+
148186
<div className={styles.site}>
149187
<h2>site available</h2>
150188
<input

src/styles/settings.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
}
7777
}
7878

79+
.max_items_container {
80+
display: grid;
81+
gap: 10px;
82+
83+
input {
84+
width: 100px;
85+
font-size: 20px;
86+
font-weight: bold;
87+
border: 2px solid black;
88+
}
89+
}
90+
7991
.site {
8092
display: flex;
8193
align-items: center;

0 commit comments

Comments
 (0)