generated from sripwoud/ts-template
-
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: define stats atom family, subsribe to stats
- Loading branch information
Showing
16 changed files
with
167 additions
and
65 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use client' | ||
import { useSubscribeStats } from 'client/h/useSubscribeStats' | ||
import type { ReactNode } from 'react' | ||
|
||
export default function QuestionLayout( | ||
{ children, params: { groupId, questionId } }: { | ||
children: ReactNode | ||
params: { groupId: string; questionId: string } | ||
}, | ||
) { | ||
useSubscribeStats({ groupId, questionId: Number.parseInt(questionId) }) | ||
return ( | ||
<> | ||
{children} | ||
</> | ||
) | ||
} |
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
import { trpc } from 'client/l/trpc' | ||
import { questionTypeAtom } from 'client/state/questions/atom' | ||
import * as actions from 'client/state/stats/actions' | ||
import { statsAtomFamily } from 'client/state/stats/atom' | ||
import { useAtomValue, useSetAtom } from 'jotai' | ||
import { useEffect } from 'react' | ||
|
||
export const useSubscribeStats = ({ groupId, questionId }: { groupId: string; questionId: number }) => { | ||
const dispatch = useSetAtom(statsAtomFamily(questionId)) | ||
const questionType = useAtomValue(questionTypeAtom)({ groupId, questionId }) | ||
const { data: stats } = trpc.questions.stats.useQuery({ questionId, type: questionType }) | ||
|
||
useEffect(() => { | ||
if (stats === undefined) return | ||
dispatch(actions.init(stats)) | ||
}, [stats]) | ||
|
||
trpc.feedbacks.onInsert.useSubscription({ questionId }, { | ||
onData: ({ data: { feedback, question_id } }) => { | ||
dispatch(actions.update({ feedback, question_id, type: questionType })) | ||
}, | ||
onError: (error) => { | ||
console.error(error) | ||
}, | ||
}) | ||
} |
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,12 +1,12 @@ | ||
import { type Questions, type QuestionsAction, QuestionsActionType } from 'client/state/questions/types' | ||
import type { Question } from 'server/questions/entities' | ||
import { type Questions, type QuestionsAction, QuestionsActionType } from './types' | ||
|
||
export const init = (payload: Questions): QuestionsAction => ({ | ||
type: QuestionsActionType.FIND_ALL, | ||
type: QuestionsActionType.INIT, | ||
payload, | ||
}) | ||
|
||
export const update = (payload: Question): QuestionsAction => ({ | ||
type: QuestionsActionType.ON_CHANGE, | ||
type: QuestionsActionType.UPDATE, | ||
payload, | ||
}) |
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,12 +1,20 @@ | ||
import { initialQuestionsState, questionsReducer } from 'client/state/questions/reducer' | ||
import type { Questions, QuestionsAction } from 'client/state/questions/types' | ||
import deepEqual from 'fast-deep-equal' | ||
import { atom, type WritableAtom } from 'jotai' | ||
import { atomFamily, atomWithReducer } from 'jotai/utils' | ||
import { initialQuestionsState, questionsReducer } from './reducer' | ||
import type { Questions, QuestionsAction } from './types' | ||
|
||
export const questionsAtomFamily = atomFamily<string, WritableAtom<Questions, [QuestionsAction], void>>( | ||
(_groupId: string) => atomWithReducer<Questions, QuestionsAction>(initialQuestionsState, questionsReducer), | ||
(_groupId) => atomWithReducer<Questions, QuestionsAction>(initialQuestionsState, questionsReducer), | ||
deepEqual, | ||
) | ||
|
||
export const questionsByGroupAtom = atom((get) => (groupId: string) => Object.values(get(questionsAtomFamily(groupId)))) | ||
|
||
export const questionTypeAtom = atom((get) => ({ groupId, questionId }: { groupId: string; questionId: number }) => | ||
get(questionsAtomFamily(groupId))[questionId]!.type | ||
) | ||
|
||
export const questionAtom = atom(get => ({ groupId, questionId }: { groupId: string; questionId: number }) => | ||
get(questionsAtomFamily(groupId))[questionId]! | ||
) |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { type Stats, type StatsAction, StatsActionType, type StatsUpdatePayload } from 'client/state/stats/types' | ||
|
||
export const init = (payload: Stats): StatsAction => ({ | ||
type: StatsActionType.INIT, | ||
payload, | ||
}) | ||
|
||
export const update = (payload: StatsUpdatePayload): StatsAction => ({ | ||
type: StatsActionType.UPDATE, | ||
payload, | ||
}) |
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,12 @@ | ||
import { initialStatsState, statsReducer } from 'client/state/stats/reducer' | ||
import type { Stats, StatsAction } from 'client/state/stats/types' | ||
import deepEqual from 'fast-deep-equal' | ||
import { atom, type WritableAtom } from 'jotai' | ||
import { atomFamily, atomWithReducer } from 'jotai/utils' | ||
|
||
export const statsAtomFamily = atomFamily<number, WritableAtom<Stats, [StatsAction], void>>( | ||
(_questionId) => atomWithReducer<Stats, StatsAction>(initialStatsState, statsReducer), | ||
deepEqual, | ||
) | ||
|
||
export const statsByQuestionAtom = atom((get) => (questionId: number) => get(statsAtomFamily(questionId))) |
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,26 @@ | ||
import { type Stats, type StatsAction, StatsActionType } from 'client/state/stats/types' | ||
|
||
export const initialStatsState: Stats = { no: 0, yes: 0 } | ||
|
||
export const statsReducer = (state: Stats, { type, payload }: StatsAction): Stats => { | ||
switch (type) { | ||
case StatsActionType.INIT: | ||
return payload | ||
case StatsActionType.UPDATE: | ||
switch (payload.type) { | ||
case 'boolean': | ||
switch (payload.feedback) { | ||
case 'yes': | ||
return { ...state, yes: state.yes + 1 } | ||
case 'no': | ||
return { ...state, no: state.no + 1 } | ||
default: | ||
throw new Error('feedback value incompatible with question type') | ||
} | ||
default: | ||
throw new Error('Unsupported question type') | ||
} | ||
default: | ||
return state | ||
} | ||
} |
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,21 @@ | ||
import type { inferRouterOutputs } from '@trpc/server' | ||
import type { Feedback } from 'server/feedbacks/entities' | ||
import type { Question } from 'server/questions/entities' | ||
import type { Router } from 'server/trpc/trpc.router' | ||
|
||
export type Stats = inferRouterOutputs<Router>['questions']['stats'] | ||
|
||
export enum StatsActionType { | ||
INIT = 'INIT', | ||
UPDATE = 'UPDATE', | ||
} | ||
|
||
export type StatsUpdatePayload = Pick<Feedback, 'question_id' | 'feedback'> & { type: Question['type'] } | ||
|
||
export type StatsAction = { | ||
type: StatsActionType.INIT | ||
payload: Stats | ||
} | { | ||
type: StatsActionType.UPDATE | ||
payload: StatsUpdatePayload | ||
} |
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,7 @@ | ||
import { z } from 'zod' | ||
|
||
export const SubscribeToFeedbacksDto = z.object({ | ||
questionId: z.number().int().positive(), | ||
}) | ||
|
||
export type SubscribeToFeedbacksDto = z.infer<typeof SubscribeToFeedbacksDto> |
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