forked from hack4impact-uiuc/3dp4me
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateStepModal.tsx
More file actions
127 lines (110 loc) · 4.07 KB
/
Copy pathCreateStepModal.tsx
File metadata and controls
127 lines (110 loc) · 4.07 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
116
117
118
119
120
121
122
123
124
125
126
127
import './CreateStepModal.scss'
import { BaseStep, Field, Language } from '@3dp4me/types'
import Button from '@mui/material/Button'
import Modal from '@mui/material/Modal'
import clone from 'lodash/clone'
import { useState } from 'react'
import { useErrorWrap } from '../../hooks/useErrorWrap'
import { useTranslations } from '../../hooks/useTranslations'
import { ADMIN_ID, ERR_LANGUAGE_VALIDATION_FAILED } from '../../utils/constants'
import { FormOption } from '../Fields/FormOption'
import MultiSelectField from '../Fields/MultiSelectField'
import LanguageInput from '../LanguageInput/LanguageInput'
export interface CreateStepModalProps {
isOpen: boolean
onModalClose: () => void
allRoles: FormOption[]
onAddNewStep: (step: BaseStep) => void
}
const CreateStepModal = ({
isOpen,
onModalClose,
allRoles,
onAddNewStep,
}: CreateStepModalProps) => {
const [translations, selectedLang] = useTranslations()
const [selectedRoles, setSelectedRoles] = useState([ADMIN_ID])
const [displayName, setDisplayName] = useState({ EN: '', AR: '' })
const errorWrap = useErrorWrap()
const onRolesChange = (id: string, roleIds: string[]) => {
setSelectedRoles(roleIds)
}
const updateDisplayName = (value: string, language: Language) => {
const updatedDisplayName = clone(displayName)
updatedDisplayName[language] = value
setDisplayName(updatedDisplayName)
}
const validateStep = (stepData: BaseStep) => {
if (stepData.displayName.EN.trim() === '' || stepData.displayName.AR.trim() === '') {
throw new Error(ERR_LANGUAGE_VALIDATION_FAILED)
}
}
const generateFields = () => (
<div className="create-step-modal-field-container">
<span>{translations.components.swal.step.stepTitle}</span>
<LanguageInput
fieldValues={displayName}
fieldKey="displayName"
handleFieldChange={(value, language) => {
updateDisplayName(value, language)
}}
/>
</div>
)
const clearState = () => {
setSelectedRoles([ADMIN_ID])
setDisplayName({ EN: '', AR: '' })
}
const onDiscard = () => {
clearState()
onModalClose()
}
const saveNewStep = () => {
const newStepData = {
readableGroups: selectedRoles,
writableGroups: selectedRoles,
displayName,
fields: [] as Field[],
}
errorWrap(
() => {
validateStep(newStepData)
},
() => {
onAddNewStep(newStepData)
onModalClose()
clearState()
}
)
}
return (
<Modal open={isOpen} onClose={onModalClose} className="create-step-modal">
<div className="create-step-modal-wrapper">
<h2 className="create-step-modal-title">
{translations.components.swal.step.createStepHeader}
</h2>
<div className="create-step-modal-text">{generateFields()}</div>
<div className="create-step-multiselect">
<MultiSelectField
title={translations.components.swal.step.clearance}
langKey={selectedLang}
options={allRoles}
selectedOptions={selectedRoles}
onChange={onRolesChange}
isDisabled={false}
disabledOptions={[ADMIN_ID]}
/>
</div>
<div>
<Button onClick={saveNewStep} className="save-step-button">
{translations.components.swal.step.buttons.save}
</Button>
<Button onClick={onDiscard} className="discard-step-button">
{translations.components.swal.step.buttons.discard}
</Button>
</div>
</div>
</Modal>
)
}
export default CreateStepModal