-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
100 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
import { useEffect, useMemo, useState } from 'react'; | ||
// import { useEffect, useMemo, useState } from 'react'; | ||
|
||
import { useLocalContext } from '@graasp/apps-query-client'; | ||
import { PermissionLevel, PermissionLevelCompare } from '@graasp/sdk'; | ||
// import { useLocalContext } from '@graasp/apps-query-client'; | ||
// import { PermissionLevel, PermissionLevelCompare } from '@graasp/sdk'; | ||
|
||
import { | ||
AppDataType, | ||
UserAnswerAppData, | ||
getDefaultUserAnswerAppData, | ||
} from '@/config/appData'; | ||
import { hooks, mutations } from '@/config/queryClient'; | ||
import { UserAnswer } from '@/interfaces/userAnswer'; | ||
// import { | ||
// AppDataType, | ||
// UserAnswerAppData, | ||
// getDefaultUserAnswerAppData, | ||
// } from '@/config/appData'; | ||
// import { hooks, mutations } from '@/config/queryClient'; | ||
// import { UserAnswer } from '@/interfaces/userAnswer'; | ||
|
||
const useUserAnswers = (): { | ||
userAnswer?: UserAnswer; | ||
submitAnswer: (userAnswer: UserAnswer) => void; | ||
deleteAnswer: (id?: UserAnswerAppData['id']) => void; | ||
allAnswersAppData?: UserAnswerAppData[]; | ||
} => { | ||
const { data, isSuccess } = hooks.useAppData(); | ||
const [userAnswerAppData, setUserAnswerData] = useState<UserAnswerAppData>(); | ||
const [allAnswersAppData, setAllAnswersAppData] = | ||
useState<UserAnswerAppData[]>(); | ||
const { mutate: postAppData } = mutations.usePostAppData(); | ||
const { mutate: patchAppData } = mutations.usePatchAppData(); | ||
const { mutate: deleteAppData } = mutations.useDeleteAppData(); | ||
const { permission } = useLocalContext(); | ||
// const useUserAnswers = (): { | ||
// userAnswer?: UserAnswer; | ||
// submitAnswer: (userAnswer: UserAnswer) => void; | ||
// deleteAnswer: (id?: UserAnswerAppData['id']) => void; | ||
// allAnswersAppData?: UserAnswerAppData[]; | ||
// } => { | ||
// const { data, isSuccess } = hooks.useAppData(); | ||
// const [userAnswerAppData, setUserAnswerData] = useState<UserAnswerAppData>(); | ||
// const [allAnswersAppData, setAllAnswersAppData] = | ||
// useState<UserAnswerAppData[]>(); | ||
// const { mutate: postAppData } = mutations.usePostAppData(); | ||
// const { mutate: patchAppData } = mutations.usePatchAppData(); | ||
// const { mutate: deleteAppData } = mutations.useDeleteAppData(); | ||
// const { permission } = useLocalContext(); | ||
|
||
const isAdmin = useMemo( | ||
() => PermissionLevelCompare.gte(permission, PermissionLevel.Admin), | ||
[permission], | ||
); | ||
// const isAdmin = useMemo( | ||
// () => PermissionLevelCompare.gte(permission, PermissionLevel.Admin), | ||
// [permission], | ||
// ); | ||
|
||
const { memberId } = useLocalContext(); | ||
// const { memberId } = useLocalContext(); | ||
|
||
useEffect(() => { | ||
if (isSuccess) { | ||
const allAns = data.filter( | ||
(d) => d.type === AppDataType.UserAnswer, | ||
) as UserAnswerAppData[]; | ||
setAllAnswersAppData(allAns); | ||
setUserAnswerData( | ||
allAns.find((d) => d.member.id === memberId) as UserAnswerAppData, | ||
); | ||
} | ||
}, [isSuccess, data, memberId]); | ||
// useEffect(() => { | ||
// if (isSuccess) { | ||
// const allAns = data.filter( | ||
// (d) => d.type === AppDataType.UserAnswer, | ||
// ) as UserAnswerAppData[]; | ||
// setAllAnswersAppData(allAns); | ||
// setUserAnswerData( | ||
// allAns.find((d) => d.member.id === memberId) as UserAnswerAppData, | ||
// ); | ||
// } | ||
// }, [isSuccess, data, memberId]); | ||
|
||
const submitAnswer = (userAnswer: UserAnswer): void => { | ||
if (userAnswerAppData?.id) { | ||
patchAppData({ | ||
...userAnswerAppData, | ||
data: userAnswer, | ||
}); | ||
} else { | ||
postAppData(getDefaultUserAnswerAppData(userAnswer)); | ||
} | ||
}; | ||
// const submitAnswer = (userAnswer: UserAnswer): void => { | ||
// if (userAnswerAppData?.id) { | ||
// patchAppData({ | ||
// ...userAnswerAppData, | ||
// data: userAnswer, | ||
// }); | ||
// } else { | ||
// postAppData(getDefaultUserAnswerAppData(userAnswer)); | ||
// } | ||
// }; | ||
|
||
const deleteAnswer = (id?: UserAnswerAppData['id']): void => { | ||
if (id) { | ||
deleteAppData({ id }); | ||
} else if (userAnswerAppData) { | ||
deleteAppData({ id: userAnswerAppData?.id }); | ||
} | ||
}; | ||
return { | ||
userAnswer: userAnswerAppData?.data, | ||
submitAnswer, | ||
allAnswersAppData: isAdmin ? allAnswersAppData : undefined, | ||
deleteAnswer, | ||
}; | ||
}; | ||
// const deleteAnswer = (id?: UserAnswerAppData['id']): void => { | ||
// if (id) { | ||
// deleteAppData({ id }); | ||
// } else if (userAnswerAppData) { | ||
// deleteAppData({ id: userAnswerAppData?.id }); | ||
// } | ||
// }; | ||
// return { | ||
// userAnswer: userAnswerAppData?.data, | ||
// submitAnswer, | ||
// allAnswersAppData: isAdmin ? allAnswersAppData : undefined, | ||
// deleteAnswer, | ||
// }; | ||
// }; | ||
|
||
export default useUserAnswers; | ||
// export default useUserAnswers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import { PLAYER_VIEW_CY } from '@/config/selectors'; | ||
|
||
import { UserAnswersProvider } from '../context/UserAnswersContext'; | ||
import QuestionView from '../question-view/QuestionView'; | ||
|
||
const PlayerView = (): JSX.Element => ( | ||
<div data-cy={PLAYER_VIEW_CY}> | ||
<UserAnswersProvider> | ||
<QuestionView /> | ||
</UserAnswersProvider> | ||
<QuestionView /> | ||
</div> | ||
); | ||
export default PlayerView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters