Skip to content

Commit 858397a

Browse files
committed
fix: problems not getting readded when the dateselect is updated
1 parent 8a343e3 commit 858397a

File tree

1 file changed

+41
-74
lines changed

1 file changed

+41
-74
lines changed

src/problems-by-company/company.ts

+41-74
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Create a new variable to store all solutions
2+
const allSolutions = [] as { id: number, title: string, url: string }[];
13
const solutions = [] as { id: number, title: string, url: string }[];
24

35
let companyName = 'Amazon';
@@ -72,6 +74,25 @@ function addNavbarLinks() {
7274
});
7375
}
7476

77+
interface Company {
78+
name: string;
79+
}
80+
81+
interface Question {
82+
title: string;
83+
frontend_id: number;
84+
companies?: Company[];
85+
}
86+
87+
interface LeetcodeProblems {
88+
questions: Question[];
89+
}
90+
91+
let minFrequency = Number.MAX_SAFE_INTEGER;
92+
let maxFrequency = 0;
93+
94+
95+
7596
async function updateFrequency(selectedFrequency: string) {
7697
// Clear the existing table
7798
const table = document.getElementById('solutionTable') as HTMLTableElement;
@@ -83,6 +104,9 @@ async function updateFrequency(selectedFrequency: string) {
83104
minFrequency = Number.MAX_SAFE_INTEGER;
84105
maxFrequency = 0;
85106

107+
// Create a new array to hold the solutions with the selected frequency
108+
const updatedSolutions = [];
109+
86110
// Update the frequency values in the solutions array
87111
const data = await new Promise<{ companyProblems: any }>((resolve) => {
88112
chrome.storage.local.get('companyProblems', function (data) {
@@ -92,7 +116,7 @@ async function updateFrequency(selectedFrequency: string) {
92116

93117
const companyProblems = data.companyProblems[companyName];
94118
if (Array.isArray(companyProblems)) {
95-
solutions.forEach((solution, index) => {
119+
allSolutions.forEach((solution, index) => { // Iterate over allSolutions
96120
// Check if the selected frequency value exists for the problem
97121
if (companyProblems[index].hasOwnProperty(selectedFrequency)) {
98122
const freqValue = companyProblems[index][selectedFrequency];
@@ -101,32 +125,22 @@ async function updateFrequency(selectedFrequency: string) {
101125
// Update min and max frequency for the selected range
102126
if (freqValue < minFrequency) minFrequency = freqValue;
103127
if (freqValue > maxFrequency) maxFrequency = freqValue;
128+
129+
// Add the solution to the updated solutions array
130+
updatedSolutions.push(solution);
104131
}
105132
});
106133
}
107134

135+
// Replace the solutions array with the updated solutions
136+
solutions.length = 0;
137+
solutions.push(...updatedSolutions);
138+
108139
// Rebuild the table with updated frequency values
109140
rebuildTable();
110141
}
111142

112143

113-
interface Company {
114-
name: string;
115-
}
116-
117-
interface Question {
118-
title: string;
119-
frontend_id: number;
120-
companies?: Company[];
121-
}
122-
123-
interface LeetcodeProblems {
124-
questions: Question[];
125-
}
126-
127-
let minFrequency = Number.MAX_SAFE_INTEGER;
128-
let maxFrequency = 0;
129-
130144
function addCompanyProblems(sortMethod: string) {
131145
chrome.storage.local.get(['companyProblems', 'leetcodeProblems'], function (data) {
132146
const companyProblems = data.companyProblems[companyName];
@@ -139,14 +153,15 @@ function addCompanyProblems(sortMethod: string) {
139153
// Check if companyProblems is an array before proceeding
140154
if (Array.isArray(companyProblems)) {
141155
companyProblems.forEach((problem) => {
142-
const correspondingLeetcodeProblem = leetcodeProblems.find(q => q.frontend_id === problem.id);
143-
solutions.push({
156+
const correspondingLeetcodeProblem = leetcodeProblems.find(q => q.frontend_id === problem.id); // Find the corresponding problem
157+
// Populate allSolutions instead of solutions
158+
allSolutions.push({
144159
id: problem.id,
145160
title: problem.title,
146161
url: `https://leetcode.com/problems/${problem.title.replace(/\s/g, '-')}/`,
147162
frequency: problem.freq_alltime,
148-
difficulty: correspondingLeetcodeProblem?.difficulty_lvl, // Add difficulty
149-
acceptance: correspondingLeetcodeProblem?.acceptance, // Add acceptance
163+
difficulty: correspondingLeetcodeProblem?.difficulty_lvl, // Use the defined variable
164+
acceptance: correspondingLeetcodeProblem?.acceptance, // Use the defined variable
150165
});
151166

152167
// Update min and max frequency
@@ -155,64 +170,17 @@ function addCompanyProblems(sortMethod: string) {
155170
});
156171
}
157172

173+
// Initialize solutions with all problems initially
174+
solutions.length = 0;
175+
solutions.push(...allSolutions);
176+
158177
console.log(solutions);
159178

160179
// Rebuild the table with sorted solutions
161180
rebuildTable();
162-
163181
});
164182
}
165183

166-
async function addCompaniesToSelect() {
167-
const companySearch = document.getElementById('companySearch') as HTMLInputElement;
168-
const companyList = document.getElementById('companyList') as HTMLDataListElement;
169-
let companies = [];
170-
171-
const data = await new Promise<{ companyProblems: any }>((resolve) => {
172-
chrome.storage.local.get('companyProblems', function (data) {
173-
resolve(data);
174-
});
175-
});
176-
177-
const companyProblems = data.companyProblems;
178-
// add all the keys to the set
179-
Object.keys(companyProblems).forEach((company) => {
180-
if (company) {
181-
console.log(company);
182-
companies.push(company);
183-
}
184-
});
185-
186-
// Event when the "Enter" key is pressed or an option is selected from the dropdown
187-
const handleSelection = () => {
188-
const inputValue = companySearch.value;
189-
// Find the selected company in a case-insensitive manner
190-
const selectedCompany = Array.from(companies).find(
191-
(company) => company.toLowerCase() === inputValue.toLowerCase()
192-
);
193-
if (selectedCompany) {
194-
chrome.storage.local.set({ clickedCompany: selectedCompany }, () => {
195-
location.reload();
196-
});
197-
}
198-
};
199-
200-
companySearch.addEventListener('keydown', (event) => {
201-
if (event.key === 'Enter') {
202-
handleSelection();
203-
}
204-
});
205-
206-
companySearch.addEventListener('change', handleSelection);
207-
208-
const sortedCompanies = companies.sort();
209-
210-
sortedCompanies.forEach((company) => {
211-
const option = document.createElement('option');
212-
option.value = company;
213-
companyList.appendChild(option);
214-
});
215-
}
216184

217185
// Function to rebuild the table with sorted solutions
218186
function rebuildTable() {
@@ -312,7 +280,6 @@ function sortBy(column: string) {
312280

313281
// Rebuild the table with sorted solutions
314282
rebuildTable();
315-
316283
}
317284

318285
/* Run the script */

0 commit comments

Comments
 (0)