Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different mechanism for handling new type #547

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/components/form/AddTypeModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ const validationSchema = Yup.object().shape({
notes: Yup.string(),
})

const AddTypeModal = ({ initialName }) => {
const AddTypeModal = ({ initialName, onTypeAdded }) => {
const { i18n } = useTranslation()
const dispatch = useDispatch()
const { locationId } = useSelector((state) => state.location)
const { isAddTypeModalOpen } = useSelector((state) => state.type)

// Initialize state
Expand Down Expand Up @@ -164,9 +163,10 @@ const AddTypeModal = ({ initialName }) => {
addTypeAndUpdate({
submitData,
language: i18n.language,
locationId,
}),
)
).then((action) => {
onTypeAdded(action.payload.newMenuEntry)
})
setSubmitting(false)
}

Expand Down
29 changes: 9 additions & 20 deletions src/components/form/TypesSelect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useFormikContext } from 'formik'
import { uniqBy } from 'lodash'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import React, { useCallback, useMemo, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { openAddTypeModal } from '../../redux/typeSlice'
Expand All @@ -9,34 +9,23 @@ import { AddTypeModal } from './AddTypeModal'
import { CreatableSelect } from './FormikWrappers'

const TypesSelect = () => {
const { typesAccess, recentlyAddedTypesByLocation } = useSelector(
(state) => state.type,
)
const { locationId } = useSelector((state) => state.location)
const { typesAccess } = useSelector((state) => state.type)
const { values, setFieldValue } = useFormikContext()
const dispatch = useDispatch()

const typeOptions = useMemo(() => typesAccess.asMenuEntries(), [typesAccess])

const [newTypeInput, setNewTypeInput] = useState('')

const recentlyAddedTypes = useMemo(
() =>
(recentlyAddedTypesByLocation[locationId] || [])
.map((typeId) => typesAccess.getMenuEntry(typeId))
.filter(Boolean),
[recentlyAddedTypesByLocation, locationId, typesAccess],
)

useEffect(() => {
if (recentlyAddedTypes.length > 0) {
const handleNewType = useCallback(
(newTypeOption) => {
const updatedTypes = uniqBy(
[...(values.types || []), ...recentlyAddedTypes],
[...(values.types || []), newTypeOption],
'value',
)
setFieldValue('types', updatedTypes)
}
}, [recentlyAddedTypes.join(', ')]) //eslint-disable-line
},
[values.types, setFieldValue],
)

const handleCreateOption = useCallback(
(inputValue) => {
Expand Down Expand Up @@ -71,7 +60,7 @@ const TypesSelect = () => {
onCreateOption={handleCreateOption}
formatCreateLabel={(inputValue) => inputValue}
/>
<AddTypeModal initialName={newTypeInput} />
<AddTypeModal initialName={newTypeInput} onTypeAdded={handleNewType} />
</>
)
}
Expand Down
20 changes: 11 additions & 9 deletions src/redux/typeSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ export const fetchAndLocalizeTypes = createAsyncThunk(

export const addTypeAndUpdate = createAsyncThunk(
'type/addTypeAndUpdate',
async ({ submitData, language, locationId }) => {
async ({ submitData, language }, { getState }) => {
const response = await addType(submitData)
return { ...response, language, locationId }
const state = getState()
const updatedTypesAccess = state.type.typesAccess.addType(
response,
language,
)
return {
newMenuEntry: updatedTypesAccess.getMenuEntry(response.id),
updatedTypesAccess,
}
},
)

Expand All @@ -25,7 +33,6 @@ const typeSlice = createSlice({
initialState: {
isLoading: false,
typesAccess: typesAccessInLanguage([], ''),
recentlyAddedTypesByLocation: {},
isAddTypeModalOpen: false,
},
reducers: {
Expand All @@ -45,13 +52,8 @@ const typeSlice = createSlice({
state.isLoading = false
},
[addTypeAndUpdate.fulfilled]: (state, action) => {
const { id, language, locationId } = action.payload
state.typesAccess = state.typesAccess.addType(action.payload, language)
state.typesAccess = action.payload.updatedTypesAccess
toast.success('New type added successfully!')
if (!state.recentlyAddedTypesByLocation[locationId]) {
state.recentlyAddedTypesByLocation[locationId] = []
}
state.recentlyAddedTypesByLocation[locationId].push(id)
state.isAddTypeModalOpen = false
},
[addTypeAndUpdate.rejected]: () => {
Expand Down
Loading