Skip to content

Commit

Permalink
Merge pull request #40 from voscarmv/loadAllLabelsFromSelectedLanguag…
Browse files Browse the repository at this point in the history
…eOnly

Load All labels from selected language only
  • Loading branch information
shadmanhere authored Dec 10, 2022
2 parents 0fd3777 + 84a379e commit eb2de6d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,39 @@ export const rawLabels = async (repos, dispatch) => {
return rawdata;
};

export const getLabels = (repos) => async (dispatch, getState) => {
export const getLabels = (repos, language) => async (dispatch, getState) => {
if (repos === undefined) return;
dispatch({
type: label.GET_LABELS_REQUEST,
loadingPercentage: 0
});
try {
// get date for list of labels, saved in browser's local strorage
const localDataDate = localStorage.getItem('date');
const localStorageObj = JSON.parse(localStorage.getItem(language + '_labelslist'));
const localDataDate = localStorageObj?.date;
let today = new Date();
const dd = String(today.getDate()).padStart(2, '0');
const mm = String(today.getMonth() + 1).padStart(2, '0');
const yyyy = today.getFullYear();
today = (mm + dd + yyyy).toString();
// if date, when data was saved, is more than a day, clear it.
if (localDataDate !== today) {
localStorage.removeItem('labelslist');
localStorage.removeItem('date');
localStorage.removeItem(language + '_labelslist');
}

// get label list from local storage
const localData = localStorage.getItem('labelslist');
const localData = localStorageObj?.labels;
let data = '';
if (localData) data = JSON.parse(localData);
if (localData) data = localData;
else data = await rawLabels(repos, dispatch); // if label list is not present in local storage than get fresh list of labels.
dispatch({
type: label.GET_LABELS_SUCCESS,
payload: data
});
localStorage.setItem('labelslist', JSON.stringify(getState().labelsStore.labelslist));
localStorage.setItem('date', today);
localStorage.setItem(
language + '_labelslist',
JSON.stringify({ language: language, date: today, labels: getState().labelsStore.labelslist })
);
} catch (e) {
console.error(`getlabels error ${e.message}`);
dispatch({
Expand Down
2 changes: 1 addition & 1 deletion src/components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const Home = () => {
</Select>
<Button
variant={`${darkMode ? 'light' : 'dark'} button button-green w-full h-15`}
disabled={loading || (menu === 2 && label === 'All')}
disabled={loading}
onClick={() => findIssues()}>
{label === 'All' ? 'Load Labels' : 'Find Issues'}
</Button>
Expand Down
10 changes: 8 additions & 2 deletions src/components/SearchEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import { getLabels } from '../actions';
export const SearchEngine = () => {
const dispatch = useDispatch();
const repos = useSelector((state) => state.reposStore);
const { language } = useSelector((state) => state.issuesStore);

useEffect(
() => {
dispatch(getLabels(repos.reposlist));
const filteredRepolist = repos.reposlist.filter(
(repo) =>
repo.language.trim() === language ||
repo.topics?.trim().toLowerCase().split(' ').includes(language.toLowerCase())
);
dispatch(getLabels(filteredRepolist, language));
},
// eslint-disable-next-line
[repos]
[repos, language]
);
};

0 comments on commit eb2de6d

Please sign in to comment.