-
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.
feat: show chips and submit/reset buttons
- Loading branch information
Showing
10 changed files
with
369 additions
and
160 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
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,3 +1,9 @@ | ||
export enum UserAnswerStatus { | ||
Saved = 'saved', | ||
Submitted = 'submitted', | ||
} | ||
|
||
export type UserAnswer = { | ||
answer?: string; | ||
status?: UserAnswerStatus; | ||
}; |
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 |
---|---|---|
|
@@ -2,23 +2,16 @@ import type { Database, LocalContext } from '@graasp/apps-query-client'; | |
import { | ||
AppItemFactory, | ||
CompleteMember, | ||
Context, | ||
MemberFactory, | ||
PermissionLevel, | ||
} from '@graasp/sdk'; | ||
|
||
import { API_HOST } from '@/config/env'; | ||
|
||
export const defaultMockContext: LocalContext = { | ||
apiHost: API_HOST, | ||
permission: PermissionLevel.Admin, | ||
context: 'builder', | ||
itemId: '1234-1234-123456-8123-123456', | ||
memberId: 'mock-member-id', | ||
}; | ||
|
||
export const mockMembers: CompleteMember[] = [ | ||
MemberFactory({ | ||
id: defaultMockContext.memberId || '', | ||
id: '1', | ||
name: 'current-member', | ||
email: '[email protected]', | ||
type: 'individual', | ||
|
@@ -36,33 +29,23 @@ export const mockMembers: CompleteMember[] = [ | |
]; | ||
|
||
export const mockItem = AppItemFactory({ | ||
id: defaultMockContext.itemId, | ||
name: 'app-short-answer', | ||
creator: mockMembers[0], | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}); | ||
|
||
export const defaultMockContext: LocalContext = { | ||
apiHost: API_HOST, | ||
permission: PermissionLevel.Admin, | ||
context: Context.Builder, | ||
itemId: mockItem.id, | ||
memberId: mockMembers[0].id, | ||
}; | ||
|
||
const buildDatabase = (members?: CompleteMember[]): Database => ({ | ||
appData: [], | ||
appActions: [ | ||
{ | ||
id: 'cecc1671-6c9d-4604-a3a2-6d7fad4a5996', | ||
type: 'admin-action', | ||
member: mockMembers[0], | ||
createdAt: new Date().toISOString(), | ||
item: mockItem, | ||
data: { content: 'hello' }, | ||
}, | ||
{ | ||
id: '0c11a63a-f333-47e1-8572-b8f99fe883b0', | ||
type: 'other-action', | ||
member: mockMembers[1], | ||
createdAt: new Date().toISOString(), | ||
item: mockItem, | ||
data: { content: 'other member' }, | ||
}, | ||
], | ||
appActions: [], | ||
members: members ?? mockMembers, | ||
appSettings: [], | ||
items: [mockItem], | ||
|
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 |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { | ||
FC, | ||
ReactElement, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
import { useLocalContext } from '@graasp/apps-query-client'; | ||
import { AppData, PermissionLevel, PermissionLevelCompare } from '@graasp/sdk'; | ||
|
||
import sortBy from 'lodash.sortby'; | ||
|
||
import { | ||
AppDataType, | ||
UserAnswerAppData, | ||
getDefaultUserAnswerAppData, | ||
} from '@/config/appData'; | ||
import { hooks, mutations } from '@/config/queryClient'; | ||
import { UserAnswer, UserAnswerStatus } from '@/interfaces/userAnswer'; | ||
|
||
import { useSettings } from './SettingsContext'; | ||
|
||
type UserAnswersContextType = { | ||
userAnswer?: UserAnswer; | ||
setAnswer: (answer: string) => void; | ||
submitAnswer: () => void; | ||
deleteAnswer: (id?: UserAnswerAppData['id']) => void; | ||
allAnswersAppData?: UserAnswerAppData[]; | ||
}; | ||
|
||
const defaultContextValue: UserAnswersContextType = { | ||
setAnswer: () => null, | ||
submitAnswer: () => null, | ||
deleteAnswer: () => null, | ||
}; | ||
|
||
const UserAnswersContext = | ||
createContext<UserAnswersContextType>(defaultContextValue); | ||
|
||
export const UserAnswersProvider: FC<{ | ||
children: ReactElement | ReactElement[]; | ||
}> = ({ children }) => { | ||
const { data, isSuccess } = hooks.useAppData<UserAnswer>({ | ||
type: AppDataType.UserAnswer, | ||
}); | ||
const [userAnswerAppData, setUserAnswerAppData] = | ||
useState<UserAnswerAppData>(); | ||
const [allAnswersAppData, setAllAnswersAppData] = | ||
useState<UserAnswerAppData[]>(); | ||
const { mutate: postAppData } = mutations.usePostAppData(); | ||
const { mutate: patchAppData } = mutations.usePatchAppData(); | ||
const { mutate: deleteAppData } = mutations.useDeleteAppData(); | ||
const { permission, memberId } = useLocalContext(); | ||
|
||
const { general } = useSettings(); | ||
const { autosubmit } = general; | ||
|
||
const isAdmin = useMemo( | ||
() => PermissionLevelCompare.gte(permission, PermissionLevel.Admin), | ||
[permission], | ||
); | ||
useEffect(() => { | ||
if (isSuccess) { | ||
const allAns = data.filter( | ||
(d: AppData) => d.type === AppDataType.UserAnswer, | ||
) as UserAnswerAppData[]; | ||
setAllAnswersAppData(allAns); | ||
setUserAnswerAppData( | ||
sortBy(allAns, ['createdAt']) | ||
.reverse() | ||
.find((d) => d.member.id === memberId), | ||
); | ||
} | ||
}, [isSuccess, data, memberId]); | ||
|
||
const setAnswer = useMemo( | ||
() => | ||
(answer: string): void => { | ||
const payloadData = { | ||
answer, | ||
status: autosubmit | ||
? UserAnswerStatus.Submitted | ||
: UserAnswerStatus.Saved, | ||
}; | ||
if (userAnswerAppData?.id) { | ||
patchAppData({ | ||
...userAnswerAppData, | ||
data: payloadData, | ||
}); | ||
} else { | ||
postAppData(getDefaultUserAnswerAppData(payloadData)); | ||
} | ||
}, | ||
[autosubmit, patchAppData, postAppData, userAnswerAppData], | ||
); | ||
|
||
const submitAnswer = useMemo( | ||
() => (): void => { | ||
if (userAnswerAppData?.id) { | ||
const payloadData = { | ||
...userAnswerAppData.data, | ||
status: UserAnswerStatus.Submitted, | ||
}; | ||
patchAppData({ | ||
...userAnswerAppData, | ||
data: payloadData, | ||
}); | ||
} else { | ||
throw new Error('No answer to submit.'); | ||
} | ||
}, | ||
[patchAppData, userAnswerAppData], | ||
); | ||
|
||
const deleteAnswer = useMemo( | ||
() => | ||
(id?: UserAnswerAppData['id']): void => { | ||
if (id) { | ||
deleteAppData({ id }); | ||
} else if (userAnswerAppData) { | ||
deleteAppData({ id: userAnswerAppData?.id }); | ||
} | ||
}, | ||
[deleteAppData, userAnswerAppData], | ||
); | ||
const contextValue = useMemo( | ||
() => ({ | ||
userAnswer: userAnswerAppData?.data, | ||
setAnswer, | ||
submitAnswer, | ||
allAnswersAppData: isAdmin ? allAnswersAppData : undefined, | ||
deleteAnswer, | ||
}), | ||
[ | ||
allAnswersAppData, | ||
deleteAnswer, | ||
isAdmin, | ||
setAnswer, | ||
submitAnswer, | ||
userAnswerAppData?.data, | ||
], | ||
); | ||
|
||
return ( | ||
<UserAnswersContext.Provider value={contextValue}> | ||
{children} | ||
</UserAnswersContext.Provider> | ||
); | ||
}; | ||
|
||
const useUserAnswers = (): UserAnswersContextType => | ||
useContext(UserAnswersContext); | ||
|
||
export default useUserAnswers; |
Oops, something went wrong.