-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsesSlice.ts
44 lines (38 loc) · 1.07 KB
/
responsesSlice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { createSlice } from '@reduxjs/toolkit'
import { FormId } from './form'
import { responsesData, statisticsData } from './data'
import { RootState } from './store'
import { QuestionStatistics } from './statistics'
type ResponsesState = { [key: FormId]: FormResponses }
export type FormResponses = {
header: Array<string>
responses: Array<Array<string>>
statistics: Array<QuestionStatistics>
}
const initialState: ResponsesState = Object.fromEntries(
Object.entries(responsesData).map(([key, rows]) => [
key,
{
// First row contains header
header: rows.length > 0 ? rows[0] : [],
responses: rows.slice(1),
statistics: statisticsData[key],
},
])
)
export const reponsesSlice = createSlice({
name: 'responses',
initialState,
reducers: {},
})
export const selectFormResponses = (
state: RootState,
id: FormId
): FormResponses => {
const result = state.responses[id]
if (!result) {
throw new Error(`Expected to find responses for form with id ${id}`)
}
return result
}
export const responsesReducer = reponsesSlice.reducer