Skip to content

Commit

Permalink
Merge branch 'multi-step-2' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
talyaron committed Sep 5, 2024
2 parents 93ec1c5 + d441391 commit a55fd6b
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 315 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@dagrejs/dagre": "^1.0.4",
"@reduxjs/toolkit": "^1.9.5",
"delib-npm": "^1.3.44",
"delib-npm": "^1.3.46",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-sonarjs": "^1.0.3",
"firebase": "^10.0.0",
Expand Down
89 changes: 44 additions & 45 deletions src/controllers/db/multiStageQuestion/getMultiStageStatements.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
import { QuestionStage, Statement } from "delib-npm";
import { Dispatch } from "react";
import { Statement, StatementSchema } from "delib-npm";
import { z } from "zod";
import { isProduction } from "@/controllers/general/helpers";
import { setTempStatementsForPresentation } from "@/model/statements/statementsSlice";
import { setCurrentMultiStepOptions } from "@/model/statements/statementsSlice";
import { store } from "@/model/store";

export async function getMultiStageOptions(

export async function getFirstEvaluationOptions(
statement: Statement,

): Promise<void> {
const dispatch: Dispatch<unknown> = store.dispatch;

try {

const dispatch = store.dispatch;
const urlBase = isProduction() ? "qeesi7aziq-uc.a.run.app" : "http://localhost:5001/synthesistalyaron/us-central1";

const url = isProduction() ? `https://getRandomStatements-${urlBase}` : "http://localhost:5001/synthesistalyaron/us-central1/getRandomStatements";

const response = await fetch(
`${url}?parentId=${statement.statementId}&limit=6`
);
const { randomStatements, error } = await response.json();
if (error) throw new Error(error);
z.array(StatementSchema).parse(randomStatements);


dispatch(setCurrentMultiStepOptions(randomStatements));


} catch (error) {
console.error(error);


}
}

export async function getSecondEvaluationOptions(statement: Statement): Promise<void> {

try {
const dispatch = store.dispatch;
const urlBase = isProduction() ? "qeesi7aziq-uc.a.run.app" : "http://localhost:5001/synthesistalyaron/us-central1";

if (statement.questionSettings?.currentStage === QuestionStage.suggestion) {
const userId = store.getState().user.user?.uid;
if (!userId) throw new Error("User not found");

const url = isProduction() ? `https://getUserOptions-${urlBase}` : "http://localhost:5001/synthesistalyaron/us-central1/getUserOptions";

const response = await fetch(
`${url}?parentId=${statement.statementId}&userId=${userId}`
);
const { statements, error } = await response.json();
if (error) throw new Error(error);

dispatch(setTempStatementsForPresentation(statements));
} else if (
statement.questionSettings?.currentStage === QuestionStage.firstEvaluation
) {
const url = isProduction() ? `https://getRandomStatements-${urlBase}` : "http://localhost:5001/synthesistalyaron/us-central1/getRandomStatements";

const response = await fetch(
`${url}?parentId=${statement.statementId}&limit=6`
);
const { randomStatements, error } = await response.json();
if (error) throw new Error(error);
dispatch(setTempStatementsForPresentation(randomStatements));
} else if (
statement.questionSettings?.currentStage ===
QuestionStage.secondEvaluation
) {
const url = isProduction() ? `https://getTopStatements-${urlBase}` : "http://localhost:5001/synthesistalyaron/us-central1/getTopStatements";
const response = await fetch(
`${url}?parentId=${statement.statementId}&limit=6`
);
const { topSolutions, error } = await response.json();
if (error) throw new Error(error);
dispatch(setTempStatementsForPresentation(topSolutions));
} else {
dispatch(setTempStatementsForPresentation([]));
}
const url = isProduction() ? `https://getTopStatements-${urlBase}` : "http://localhost:5001/synthesistalyaron/us-central1/getTopStatements";
const response = await fetch(
`${url}?parentId=${statement.statementId}&limit=10`
);
const { topSolutions, error } = await response.json();
if (error) throw new Error(error);

z.array(StatementSchema).parse(topSolutions);
dispatch(setCurrentMultiStepOptions(topSolutions));
} catch (error) {
console.error(error);
dispatch(setTempStatementsForPresentation([]));

}
}
86 changes: 61 additions & 25 deletions src/model/statements/statementsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable indent */
import { createSlice, PayloadAction, createSelector } from "@reduxjs/toolkit";
import { RootState } from "../store";
import { RootState, store } from "../store";

// Third party imports

Expand All @@ -15,7 +15,6 @@ import {

// Helpers
import { updateArray } from "../../controllers/general/helpers";
import { sortSubStatements } from "../../view/pages/statement/components/solutions/statementSolutionsCont";

enum StatementScreen {
chat = "chat",
Expand Down Expand Up @@ -206,24 +205,23 @@ export const statementsSlicer = createSlice({
console.error(error);
}
},
setTempStatementsForPresentation: (state, action: PayloadAction<Statement[]>) => {
updateStatementTop: (
state,
action: PayloadAction<{ statementId: string; top: number }[]>,
) => {
try {
const statements = action.payload;
const updates = action.payload;
updates.forEach((update) => {
try {
const statement = state.statements.find(
(statement) => statement.statementId === update.statementId,
);
if (statement) statement.top = update.top;
else throw new Error("statement not found");
} catch (error) {
console.error("On updateStatementTop loop: ", error);
}


//clear all temp statements
state.statements.forEach((statement) => {
statement.isPartOfTempPresentation = false;
});

//set new temp statements
statements.forEach((statement) => {
statement.isPartOfTempPresentation = true;
state.statements = updateArray(
state.statements,
statement,
"statementId",
);
});
} catch (error) {
console.error(error);
Expand All @@ -241,15 +239,15 @@ export const statementsSlicer = createSlice({
action: PayloadAction<{ statement: Statement; screen: Screen }>,
) => {
try {

const { statement, screen } = action.payload;
const _statement = state.statements.find(
(st) => st.statementId === statement.statementId,
);
if (!_statement) throw new Error("statement not found");
const subScreens = _statement?.subScreens;
if (subScreens?.length === 0 || subScreens === undefined)
throw new Error("no subscreens");
throw new Error("no sub screens");
if (subScreens.includes(screen)) {
_statement.subScreens = subScreens.filter(
(subScreen) => subScreen !== screen,
Expand Down Expand Up @@ -297,6 +295,27 @@ export const statementsSlicer = createSlice({
state.statementMembership = [];
state.screen = StatementScreen.chat;
},
setCurrentMultiStepOptions: (state, action: PayloadAction<Statement[]>) => {
try {

const previousInMultiStageOptions = state.statements.filter(statement => statement.isInMultiStage);
previousInMultiStageOptions.forEach((statement) => {
statement.isInMultiStage = false;
});

const newStatements = action.payload;
newStatements.forEach((statement) => {
statement.isInMultiStage = true;
state.statements = updateArray(
state.statements,
statement,
"statementId",
);
});
} catch (error) {
console.error(error);
}
},
},
});

Expand All @@ -305,8 +324,8 @@ export const {
setStatements,
setStatementSubscription,
setStatementsSubscription,
setTempStatementsForPresentation,
deleteStatement,
updateStatementTop,
deleteSubscribedStatement,
setStatementOrder,
setScreen,
Expand All @@ -315,6 +334,7 @@ export const {
setMembership,
removeMembership,
resetStatements,
setCurrentMultiStepOptions
} = statementsSlicer.actions;

// statements
Expand Down Expand Up @@ -371,9 +391,7 @@ export const statementOptionsSelector =
.sort((a, b) => a.createdAt - b.createdAt)
.map((statement) => ({ ...statement }));

const sortedSubStatements = sortSubStatements(subStatements, state.statements.screen);

return sortedSubStatements;
return subStatements;
};

export const questionsSelector = (statementId: string | undefined) => (state: RootState) => state.statements.statements.filter((statement) => statement.parentId === statementId && statement.statementType === StatementType.question).sort((a, b) => a.createdAt - b.createdAt);
Expand Down Expand Up @@ -434,12 +452,30 @@ export const hasTokenSelector =

return statement?.token?.includes(token) || false;
};

export const subscriptionParentStatementSelector = (parentId: string) =>
createSelector(
(state: RootState) => state.statements.statementSubscription,
(statementSubscription) =>
statementSubscription.filter((sub) => sub.statement.topParentId === parentId)
);


export const myStatementsByStatementIdSelector = (statementId: string) => {
const user = store.getState().user.user;

return createSelector(
(state: RootState) => state.statements.statements,
(statements) =>
statements.filter((st) => st.parentId === statementId && st.creatorId === user?.uid)
);
}

export const statementsOfMultiStepSelectorByStatementId =(statementId: string) => createSelector(
(state: RootState) => state.statements.statements,
(statements) => statements.filter((st) => st.isInMultiStage && st.parentId === statementId)
);



export default statementsSlicer.reducer;
8 changes: 4 additions & 4 deletions src/view/components/triangle/Triangle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ interface Props {

const Triangle: FC<Props> = ({ statement }) => {
const {t} = useLanguage();
const subStatements = useSelector(
const subStatements:Statement[] = useSelector(
statementOptionsSelector(statement.statementId)
).filter((s) => s.evaluation?.sumCon !== undefined);
).filter((s:Statement) => s.evaluation?.sumCon !== undefined);

let maxEvaluators = 0;
subStatements.forEach((subStatement) => {
subStatements.forEach((subStatement:Statement) => {
if (subStatement.evaluation?.numberOfEvaluators !== undefined && subStatement.evaluation?.numberOfEvaluators > maxEvaluators) maxEvaluators = subStatement.evaluation.numberOfEvaluators;
});

Expand All @@ -29,7 +29,7 @@ const Triangle: FC<Props> = ({ statement }) => {

</div>
<div className={`${styles.triangle} ${styles["triangle--invisible"]}`}>
{subStatements.map((subStatement) => {
{subStatements.map((subStatement:Statement) => {
return (
<Dot key={subStatement.statementId} subStatement={subStatement} maxEvaluators={maxEvaluators}/>
);
Expand Down
2 changes: 0 additions & 2 deletions src/view/pages/statement/components/SwitchScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default function SwitchScreens({
return (
<StatementEvaluationPage
statement={statement}
subStatements={subStatements}
handleShowTalker={handleShowTalker}
toggleAskNotifications={toggleAskNotifications}
/>
Expand Down Expand Up @@ -83,7 +82,6 @@ export default function SwitchScreens({
return (
<StatementEvaluationPage
statement={statement}
subStatements={subStatements}
handleShowTalker={handleShowTalker}
questions={true}
toggleAskNotifications={toggleAskNotifications}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface CreateStatementModalSwitchProps {
useSimilarStatements: boolean;
setShowModal: React.Dispatch<React.SetStateAction<boolean>>;
isQuestion: boolean;
isMuliStage: boolean;
isMultiStage: boolean;
parentStatement: Statement;
toggleAskNotifications: () => void;
}
Expand All @@ -16,7 +16,7 @@ export default function CreateStatementModalSwitch({
useSimilarStatements,
setShowModal,
isQuestion,
isMuliStage,
isMultiStage,
parentStatement,
toggleAskNotifications,
}: CreateStatementModalSwitchProps) {
Expand All @@ -26,15 +26,15 @@ export default function CreateStatementModalSwitch({
isQuestion={isQuestion}
parentStatement={parentStatement}
toggleAskNotifications={toggleAskNotifications}
isSendToStoreTemp={isMuliStage}
isSendToStoreTemp={isMultiStage}
/>
) : (
<CreateStatementModal
parentStatement={parentStatement}
isOption={!isQuestion}
setShowModal={setShowModal}
toggleAskNotifications={toggleAskNotifications}
isSendToStoreTemp={isMuliStage}
isSendToStoreTemp={isMultiStage}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ const StatementBottomNav: FC<Props> = ({ setShowModal, statement }) => {
) }
</button>
<div className="sort-menu">
{navItems.map((navItem) => (
<div className={`sort-menu__item ${showSorting ? "active" : ""}`}>
{navItems.map((navItem, i) => (
<div key={`item-id-${i}`} className={`sort-menu__item ${showSorting ? "active" : ""}`}>
<Link
className={`open-nav-icon ${showSorting ? "active" : ""}`}
to={navItem.link}
Expand Down
Loading

0 comments on commit a55fd6b

Please sign in to comment.