-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththunks.ts
More file actions
80 lines (69 loc) · 2.47 KB
/
Copy paththunks.ts
File metadata and controls
80 lines (69 loc) · 2.47 KB
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
import { EditorState } from "draft-js";
import { blankCard } from "../utils/constants";
import { SrdType, ValidRichTextKeys } from "../utils/models";
import SpellApiService, { PcClass } from "../utils/SpellApiService";
import { add, edit, removeAll } from "./cardsReducer";
import { RootState } from "./rootReducer";
import { AppThunk } from "./store";
import { reset, set, setSpells, updateActiveCard } from "./uiStateReducer";
// this is a thunk
export const finishEditing: AppThunk = (dispatch, getState) => {
const state: RootState = getState();
const newCardData = state.ui.activeCardData;
const newCardIndex = state.ui.activeCard;
dispatch(edit({ index: newCardIndex, card: newCardData }))
dispatch(reset())
}
export const createNewCard = (): AppThunk => {
return (dispatch, getState) => {
dispatch(add(blankCard));
dispatch(updateActiveCard(blankCard));
dispatch(set(getState().cards.list.length - 1))
}
}
export const setActiveCardCreator = (newIndex: number): AppThunk => {
return (dispatch, getState) => {
const state = getState();
const newActiveCard = state.cards.list[newIndex];
dispatch(set(newIndex));
dispatch(updateActiveCard(newActiveCard));
}
}
export const removeAllCards = (): AppThunk => {
return (dispatch) => {
console.log("hey!!!")
dispatch(removeAll());
dispatch(set(-1));
}
}
export const updateRichTextThunkCreator = (propName: ValidRichTextKeys, newState: EditorState): AppThunk => {
return (dispatch, getState) => {
const activeCard = getState().ui.activeCardData;
const newContent = newState.getCurrentContent();
dispatch(updateActiveCard({ ...activeCard, [propName]: newContent.getPlainText('\n')}))
}
}
export const acceptSuggestionThunk = (suggestion: SrdType): AppThunk => {
return async (dispatch) => {
const newData = await SpellApiService.get(suggestion.index);
dispatch(updateActiveCard(newData));
}
}
export const fetchSpells = (): AppThunk => {
return async function fetchSpellsThunk(dispatch) {
const spells = await SpellApiService.getList();
dispatch(setSpells(spells.results));
}
}
export const getClassSpellsThunk = (c: PcClass, maxLevel: number): AppThunk => {
return async (dispatch) => {
const newData = await SpellApiService.getListByClass(c);
newData.results.map(async (spell) => {
const i = spell.index;
const fullSpell = await SpellApiService.get(i);
if(fullSpell.level <= maxLevel) {
dispatch(add(fullSpell));
}
});
}
}