-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformsSlice.ts
More file actions
115 lines (104 loc) · 2.9 KB
/
Copy pathformsSlice.ts
File metadata and controls
115 lines (104 loc) · 2.9 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { RootState } from './store'
import { formsData } from './data'
import {
Form,
FormId,
FormQuestion,
generateFormId,
generateQuestionId,
QuestionId,
} from './form'
import produce from 'immer'
type FormsState = { [key: FormId]: Form }
const initialState: FormsState = formsData
export const formsSlice = createSlice({
name: 'forms',
initialState,
reducers: {
formAdded: {
reducer: (state, action: PayloadAction<{ id: FormId; name: string }>) => {
const { id, name } = action.payload
const form: Form = {
id: id,
name: name,
published: false,
questions: [],
}
state[id] = form
},
prepare: (name: string) => {
return {
payload: {
id: generateFormId(),
name: name,
},
}
},
},
formSettingsUpdated: (
state,
action: PayloadAction<{ id: FormId; name: string; published: boolean }>
) => {
const { id, name, published } = action.payload
state[id].name = name
state[id].published = published
},
questionAdded: {
reducer: (
state,
action: PayloadAction<{ formId: FormId; formQuestion: FormQuestion }>
) => {
const { formId, formQuestion } = action.payload
state[formId].questions.push(formQuestion)
},
prepare: (formId: FormId, formQuestion: FormQuestion) => {
return {
payload: {
formId: formId,
formQuestion: produce(formQuestion, (draft) => {
draft.question.id = generateQuestionId()
}),
},
}
},
},
questionUpdated: (
state,
action: PayloadAction<{ formId: FormId; formQuestion: FormQuestion }>
) => {
const { formId, formQuestion } = action.payload
const questionIndex = state[formId].questions.findIndex(
(x) => x.question.id === formQuestion.question.id
)
state[formId].questions[questionIndex] = formQuestion
},
questionDeleted: (
state,
action: PayloadAction<{ formId: FormId; questionId: QuestionId }>
) => {
const { formId, questionId } = action.payload
state[formId].questions = state[formId].questions.filter(
(x) => x.question.id !== questionId
)
},
},
})
export const {
formAdded,
formSettingsUpdated,
questionAdded,
questionUpdated,
questionDeleted,
} = formsSlice.actions
export const selectFormById = (state: RootState, id: FormId): Form => {
const result = state.forms[id]
if (!result) {
throw new Error(`Expected to find form with id ${id}`)
}
return result
}
export const selectAllFormsSortByName = (state: RootState): Array<Form> => {
return Object.values(state.forms).sort((a, b) => a.name.localeCompare(b.name))
}
export const formsReducer = formsSlice.reducer