Load site on edit

This commit is contained in:
Jerko Steiner 2019-04-03 19:42:48 +08:00
parent b68a0c0380
commit bc64a9bfa7
3 changed files with 42 additions and 60 deletions

View File

@ -312,24 +312,20 @@ describe('CRUD', () => {
describe('create', () => { describe('create', () => {
it('resets form.create state', () => { it('resets form.create state', () => {
store.dispatch(actions.create({name: 'a'})) store.dispatch(actions.create({name: 'a'}))
expect(store.getState().Crud.form.create).toEqual({ expect(store.getState().Crud.form.createItem).toEqual({
item: {
name: 'a', name: 'a',
},
errors: {},
}) })
expect(store.getState().Crud.form.createErrors).toEqual({})
}) })
}) })
describe('change', () => { describe('change', () => {
it('sets value', () => { it('sets value', () => {
store.dispatch(actions.change({key: 'name', value: 'test'})) store.dispatch(actions.change({key: 'name', value: 'test'}))
expect(store.getState().Crud.form.create).toEqual({ expect(store.getState().Crud.form.createItem).toEqual({
item: {
name: 'test', name: 'test',
},
errors: {},
}) })
expect(store.getState().Crud.form.createErrors).toEqual({})
}) })
}) })
@ -352,20 +348,14 @@ describe('CRUD', () => {
body: {name: 'test'}, body: {name: 'test'},
})).payload })).payload
store.dispatch(actions.edit({id: 100})) store.dispatch(actions.edit({id: 100}))
expect(store.getState().Crud.form.byId[100]).toEqual({ expect(store.getState().Crud.form.itemsById[100]).toEqual({
item: {
id: 100, id: 100,
name: 'test', name: 'test',
},
errors: {},
}) })
store.dispatch(actions.change({id: 100, key: 'name', value: 'grrr'})) store.dispatch(actions.change({id: 100, key: 'name', value: 'grrr'}))
expect(store.getState().Crud.form.byId[100]).toEqual({ expect(store.getState().Crud.form.itemsById[100]).toEqual({
item: {
id: 100, id: 100,
name: 'grrr', name: 'grrr',
},
errors: {},
}) })
}) })
}) })

View File

@ -72,6 +72,9 @@ export class CRUDField<T> extends React.PureComponent<ICRUDFieldProps<T>> {
} }
export class CRUDForm<T> extends React.PureComponent<ICRUDFormProps<T>> { export class CRUDForm<T> extends React.PureComponent<ICRUDFormProps<T>> {
static defaultProps = {
errors: {},
}
handleSubmit = (e: React.FormEvent) => { handleSubmit = (e: React.FormEvent) => {
e.preventDefault() e.preventDefault()
const {onSubmit, item} = this.props const {onSubmit, item} = this.props

View File

@ -20,14 +20,11 @@ export interface ICRUDState<T extends ICRUDEntity> {
} }
export interface ICRUDForm<T extends ICRUDEntity> { export interface ICRUDForm<T extends ICRUDEntity> {
readonly create: { readonly createItem: Pick<T, Exclude<keyof T, 'id'>>,
readonly item: Pick<T, Exclude<keyof T, 'id'>>, readonly createErrors: Partial<Record<keyof T, string>>
readonly errors: Partial<Record<keyof T, string>>
} readonly itemsById: Record<number, T>
readonly byId: Record<number, { readonly errorsById: Record<number, Partial<Record<keyof T, string>>>
readonly item: T,
readonly errors: Partial<Record<keyof T, string>>
}>
} }
export interface ICRUDStatus { export interface ICRUDStatus {
@ -54,11 +51,10 @@ export class CRUDReducer<
ids: [], ids: [],
byId: {}, byId: {},
form: { form: {
byId: {}, itemsById: {},
create: { errorsById: {},
item: newItem, createItem: newItem,
errors: {}, createErrors: {},
},
}, },
status: { status: {
@ -188,13 +184,11 @@ export class CRUDReducer<
...state, ...state,
form: { form: {
...state.form, ...state.form,
create: { createItem: {
item: {
...this.newItem, ...this.newItem,
...payload, ...payload,
}, },
errors: {}, createErrors: {},
},
}, },
} }
} }
@ -204,12 +198,13 @@ export class CRUDReducer<
...state, ...state,
form: { form: {
...state.form, ...state.form,
byId: { itemsById: {
...state.form.byId, ...state.form.itemsById,
[id]: { [id]: state.byId[id],
item: state.byId[id],
errors: {},
}, },
errorsById: {
...state.form.errorsById,
[id]: {},
}, },
}, },
} }
@ -227,14 +222,11 @@ export class CRUDReducer<
...state, ...state,
form: { form: {
...state.form, ...state.form,
create: { createItem: {
...state.form.create, ...state.form.createItem,
item: {
...state.form.create.item,
[key]: value, [key]: value,
}, },
}, },
},
} }
} }
@ -242,17 +234,14 @@ export class CRUDReducer<
...state, ...state,
form: { form: {
...state.form, ...state.form,
byId: { itemsById: {
...state.form.byId, ...state.form.itemsById,
[id]: { [id]: {
...state.form.byId[id], ...state.form.itemsById[id],
item: {
...state.form.byId[id].item,
[key]: value, [key]: value,
}, },
}, },
}, },
},
} }
} }