user.ts 1.4 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
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
import {MY_LIST_ACCESSION_ADD, MY_LIST_ACCESSION_INIT, MY_LIST_ACCESSION_REMOVE} from 'constants/user';

const initAccessions = (accessions: string[]) => ({
    type: MY_LIST_ACCESSION_INIT,
    payload: accessions,
});

const addAccession = (accUUID: string) => ({
    type: MY_LIST_ACCESSION_ADD,
    payload: accUUID,
});

const removeAccession = (accUUID: string) => ({
    type: MY_LIST_ACCESSION_REMOVE,
    payload: accUUID,
});

export const addAccessionToMyList = (accUUID: string) => (dispatch, getState) => {
    const myList = getState().user.myList;
    if (!myList.accessions.includes(accUUID)) {
        dispatch(addAccession(accUUID));
        window.localStorage.setItem('myList', JSON.stringify(getState().user.myList));
    } else {
        console.log(`Can't add accession ${accUUID}. Already in myList`);
    }
};

export const removeAccessionFromMyList = (accUUID: string) => (dispatch, getState) => {
    const myList = getState().user.myList;
    if (myList.accessions.includes(accUUID)) {
        dispatch(removeAccession(accUUID));
        window.localStorage.setItem('myList', JSON.stringify(getState().user.myList));
    } else {
        console.log(`Can't remove accession ${accUUID}. Not in myList`);
    }
};

export const initMyList = (myList: any) => (dispatch) => {
    if (myList.accessions) {
       return dispatch(initAccessions(myList.accessions));
    }
    return dispatch(initAccessions([]));
};