Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move goal-general function from CharacterInventory to GoalActions. #923

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/components/GoalTimeline/GoalsActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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));
}
}
}
63 changes: 15 additions & 48 deletions src/goals/CharInventoryCreation/CharacterInventoryActions.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
};
}

Expand All @@ -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,
Expand All @@ -172,6 +165,8 @@ export function getAllCharacters() {
};
}

// Helper Functions

function countCharacterOccurences(char: string, words: string[]) {
let count = 0;
for (let word of words) {
Expand All @@ -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;
Expand Down