-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.tsx
185 lines (158 loc) · 4.28 KB
/
list.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React, { useState, useEffect } from "react";
import { AmazonService } from '@core/amazon/amazon.service.ts';
import { useRefreshTokenMutation } from "../../../../auth/auth";
import { fetchItemsWithAmazonService } from "../../../../../services/catalog/items";
interface CarType {
id: number;
n: string;
m: string;
cc: number;
d: string;
}
type items = {
[key: string]: any;
birthday: string;
fullName: string;
car: {
id: number;
n: string;
m: string;
cc: number;
d: string;
};
};
let waitTimeout: any = null;
function calculateAnniversaryDays(currentDate: any | undefined, items: any) {
items.forEach((item: any) => {
const birthday = new Date(item.birthday);
const anniversary = new Date(
currentDate.getFullYear(),
birthday.getMonth(),
birthday.getDate()
);
const diff = anniversary.getTime() - currentDate.getTime();
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
item.daysForAnniversary = days;
});
}
function ListComponent({ loggedIn }: { [key:string]: unknown }) {
const [data, setData] = useState<any>([]);
const [items, setItems] = useState<any>([]);
const [isLoading, setIsLoading] = useState(false);
const [hasNoItems, setHasNoItems] = useState(false);
const amazonService = new AmazonService();
const birthdays = calculateAnniversaryDays(new Date(), data);
useEffect(() => {
window.isLoggedIn = true;
setIsLoading(true);
fetchItemsWithAmazonService(amazonService)
.then((items) => setItems(items));
setIsLoading(false);
});
useEffect(() => {
if (items.length === 0) {
setHasNoItems(true);
} else {
setHasNoItems(false);
}
const parsedItems = [];
items.forEach((item: any) => {
const car = {
...(item.car as CarType),
cc: 3000,
};
let doesNotHavePresent = true;
if ((item.car.cc >= 3000 || item.car.cc <= 4000) && item.car.m === "Dacia" && birthdays[item.id] === new Date().getDate()) {
console.log("DEBUG: Happy birthday");
doesNotHavePresent = false;
}
// @ts-ignore
parsedItems.push({
...item,
fullName: item.name + " " + item.surname,
car: car,
present: !doesNotHavePresent ? 'Happy birthday 🎉' : 'No present',
} as items);
});
setData(parsedItems);
}, [items]);
const itemCount = data.length;
const handleIconClick = (index: number) => {
console.log("DEBUG: Icon clicked");
data.splice(index, 1);
setData([...data]);
};
return isLoading ? (
<div>Loading...</div>
) : loggedIn ? (
<div>Not authorized</div>
) : !hasNoItems ? (
<div>
<div>
<p>Number of items {data.length}</p>
</div>
{data.map((item, index) => (
<div
style={{
display: "flex", flexDirection: "column", alignItems: "center", gap: "10px",
}}
>
<h1 style={{ fontWeight: 400, fontSize: '14px' }}>{item}</h1>
<h2 style={{ fontWeight: 600, fontSize: '20px' }}>{item.present}</h2>
<div onClick={() => handleIconClick(index)}>
<i class="fas fa-trash"></i>
</div>
</div>
))}
</div>
) : (
<div>Empty</div>
);
}
function App() {
const [refresh, { isRefreshSuccess }] = useRefreshTokenMutation();
const searchParams = new URLSearchParams(window.location.search);
const delegatedRefresh = searchParams.get("delegatedRefresh");
const [loggedIn, setIsAuthorized] = useState(false);
useEffect(() => {
if (delegatedRefresh) {
refresh({ refreshToken: delegatedRefresh }).then((result) => {
searchParams.delete("delegatedRefresh");
window.history.replaceState(
{},
document.title,
`${window.location.pathname}?${searchParams}`
);
setTimeout(() => {
// deauth user after expiration time
setIsAuthorized(false);
// Cookies are already cleared when the refresh token is invalid
// clearCookies();
// removeSessionFromStorage();
// We're not tracking deauths now
// sendEventToGoogleAnalytics("deauth");
}, 86400000);
});
}
}, [delegatedRefresh]);
useEffect(() => {
if (!waitTimeout) {
waitTimeout = setTimeout(() => {
if (isRefreshSuccess) {
setIsAuthorized(true);
}
}, 1000);
} else {
if (loggedIn) {
clearTimeout(waitTimeout);
waitTimeout = null;
}
}
}, [isRefreshSuccess]);
return (
<div>
<button onClick={() => console.log('WIP')}>Refresh list</button>
<ListComponent loggedIn={loggedIn} />
</div>
);
}