diff --git a/src/components/GoalTimeline/GoalsActions.tsx b/src/components/GoalTimeline/GoalsActions.tsx index d21741a336..d738f21e4c 100644 --- a/src/components/GoalTimeline/GoalsActions.tsx +++ b/src/components/GoalTimeline/GoalsActions.tsx @@ -10,8 +10,10 @@ import { StoreState } from "../../types"; import { ActionWithPayload, StoreStateDispatch } from "../../types/actions"; import { Goal, GoalHistoryState, GoalType } from "../../types/goals"; import { goalTypeToGoal } from "../../types/goalUtilities"; +import { Project } from "../../types/project"; import { User } from "../../types/user"; import { Edit } from "../../types/userEdit"; +import { saveChangesToProject } from "../Project/ProjectActions"; export enum GoalsActions { LOAD_USER_EDITS = "LOAD_USER_EDITS", @@ -221,3 +223,33 @@ async function addStepToGoal(goal: Goal, goalIndex: number) { } } } + +export async function saveChanges( + goal: Goal, + history: Goal[], + project: Project, + dispatch: StoreStateDispatch +) { + await saveChangesToGoal(goal, history, dispatch); + await saveChangesToProject(project, dispatch); +} + +async function saveChangesToGoal( + updatedGoal: Goal, + history: Goal[], + dispatch: StoreStateDispatch +) { + const user = LocalStorage.getCurrentUser(); + if (user) { + const userEditId = getUserEditId(user); + if (userEditId !== undefined) { + const goalIndex = getIndexInHistory(history, updatedGoal); + dispatch(updateGoal(updatedGoal)); + await Backend.addStepToGoal( + userEditId, + goalIndex, + updatedGoal + ).catch((err) => console.error(err)); + } + } +} diff --git a/src/goals/CharInventoryCreation/CharacterInventoryActions.tsx b/src/goals/CharInventoryCreation/CharacterInventoryActions.tsx index 94657de510..d0b03e84ce 100644 --- a/src/goals/CharInventoryCreation/CharacterInventoryActions.tsx +++ b/src/goals/CharInventoryCreation/CharacterInventoryActions.tsx @@ -1,16 +1,9 @@ import * as backend from "../../backend"; -import { getCurrentUser } from "../../backend/localStorage"; -import { - getIndexInHistory, - getUserEditId, - updateGoal, -} from "../../components/GoalTimeline/GoalsActions"; -import { saveChangesToProject } from "../../components/Project/ProjectActions"; +import { saveChanges } from "../../components/GoalTimeline/GoalsActions"; import { StoreState } from "../../types"; import { StoreStateDispatch } from "../../types/actions"; import { Goal } from "../../types/goals"; import { Project } from "../../types/project"; -import { User } from "../../types/user"; import { CreateCharInv } from "../CreateCharInv/CreateCharInv"; import { CharacterSetEntry, @@ -130,11 +123,11 @@ export function setCharacterStatus(character: string, status: characterStatus) { export function uploadInventory() { return async (dispatch: StoreStateDispatch, getState: () => StoreState) => { const state = getState(); - const project = updateCurrentProject(state); const updatedGoal = updateCurrentGoal(state); - const history = state.goalsState.historyState.history; + const goalHistory = state.goalsState.historyState.history; + const updatedProject = updateCurrentProject(state); - await saveChanges(updatedGoal, history, project, dispatch); + await saveChanges(updatedGoal, goalHistory, updatedProject, dispatch); }; } @@ -147,13 +140,13 @@ export function fetchWords() { export function getAllCharacters() { return async (dispatch: StoreStateDispatch, getState: () => StoreState) => { - let state = getState(); - let words = await backend.getFrontierWords(); - let characters: string[] = []; - words.forEach((word) => characters.push(...word.vernacular)); - characters = [...new Set(characters)]; + const state = getState(); + const words = await backend.getFrontierWords(); + const charactersWithDuplicates: string[] = []; + words.forEach((word) => charactersWithDuplicates.push(...word.vernacular)); + const characters = [...new Set(charactersWithDuplicates)]; - let characterSet: CharacterSetEntry[] = []; + const characterSet: CharacterSetEntry[] = []; characters.forEach((letter) => { characterSet.push({ character: letter, @@ -172,6 +165,8 @@ export function getAllCharacters() { }; } +// Helper Functions + function countCharacterOccurences(char: string, words: string[]) { let count = 0; for (let word of words) { @@ -194,44 +189,16 @@ export function getCharacterStatus( return "undecided"; } -async function saveChanges( - goal: Goal, - history: Goal[], - project: Project, - dispatch: StoreStateDispatch -) { - await saveChangesToGoal(goal, history, dispatch); - await saveChangesToProject(project, dispatch); -} - -async function saveChangesToGoal( - updatedGoal: Goal, - history: Goal[], - dispatch: StoreStateDispatch -) { - const user: User | null = getCurrentUser(); - if (user) { - const userEditId: string | undefined = getUserEditId(user); - if (userEditId !== undefined) { - const goalIndex = getIndexInHistory(history, updatedGoal); - dispatch(updateGoal(updatedGoal)); - await backend - .addStepToGoal(userEditId, goalIndex, updatedGoal) - .catch((err: string) => console.log(err)); - } - } -} - function updateCurrentProject(state: StoreState): Project { - let project = state.currentProject; + const project = state.currentProject; project.validCharacters = state.characterInventoryState.validCharacters; project.rejectedCharacters = state.characterInventoryState.rejectedCharacters; return project; } function updateCurrentGoal(state: StoreState): Goal { - let history: Goal[] = state.goalsState.historyState.history; - let currentGoal: CreateCharInv = history[history.length - 1] as CreateCharInv; + const history = state.goalsState.historyState.history; + const currentGoal = history[history.length - 1] as CreateCharInv; // Nothing stored as goal data for now return currentGoal;