mirror of
https://github.com/strapi/strapi.git
synced 2025-09-21 14:31:16 +00:00
RelationInputWrapper: Fix 1:1 relations reducer
This commit is contained in:
parent
9c2dbecb16
commit
26747086d0
@ -156,11 +156,12 @@ const EditViewDataManagerProvider = ({
|
|||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const connectRelation = useCallback(({ target: { name, value } }) => {
|
const connectRelation = useCallback(({ target: { name, value, replace } }) => {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'CONNECT_RELATION',
|
type: 'CONNECT_RELATION',
|
||||||
keys: name.split('.'),
|
keys: name.split('.'),
|
||||||
value,
|
value,
|
||||||
|
replace,
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
@ -84,10 +84,14 @@ const reducer = (state, action) =>
|
|||||||
}
|
}
|
||||||
case 'CONNECT_RELATION': {
|
case 'CONNECT_RELATION': {
|
||||||
const path = ['modifiedData', ...action.keys];
|
const path = ['modifiedData', ...action.keys];
|
||||||
const { value } = action;
|
const { value, replace = false } = action;
|
||||||
|
|
||||||
|
if (replace) {
|
||||||
|
set(draftState, [...path, 'connect'], [value]);
|
||||||
|
} else {
|
||||||
const nextValue = get(draftState, [...path, 'connect']);
|
const nextValue = get(draftState, [...path, 'connect']);
|
||||||
nextValue.push(value);
|
nextValue.push(value);
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -258,28 +258,27 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('CONNECT_RELATION', () => {
|
describe('CONNECT_RELATION', () => {
|
||||||
it.skip('should add a relation in the modifiedData', () => {
|
it('should add a relation in the modifiedData', () => {
|
||||||
const state = {
|
const state = {
|
||||||
...initialState,
|
...initialState,
|
||||||
|
|
||||||
initialData: {
|
initialData: {},
|
||||||
name: 'name',
|
|
||||||
},
|
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
name: 'name',
|
relation: {
|
||||||
|
connect: [],
|
||||||
|
disconnect: [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const expected = {
|
const expected = {
|
||||||
...initialState,
|
...initialState,
|
||||||
componentsDataStructure: {},
|
componentsDataStructure: {},
|
||||||
initialData: {
|
initialData: {},
|
||||||
name: 'name',
|
|
||||||
},
|
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
name: 'name',
|
|
||||||
relation: {
|
relation: {
|
||||||
connect: [{ id: 1 }],
|
connect: [{ id: 1 }],
|
||||||
|
disconnect: [],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -292,6 +291,59 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
|
|||||||
|
|
||||||
expect(reducer(state, action)).toEqual(expected);
|
expect(reducer(state, action)).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should overwrite existing data, when replace is set to true', () => {
|
||||||
|
const state = {
|
||||||
|
...initialState,
|
||||||
|
|
||||||
|
initialData: {},
|
||||||
|
modifiedData: {
|
||||||
|
relation: {
|
||||||
|
connect: [],
|
||||||
|
disconnect: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const action = {
|
||||||
|
type: 'CONNECT_RELATION',
|
||||||
|
keys: ['relation'],
|
||||||
|
value: { id: 1 },
|
||||||
|
};
|
||||||
|
|
||||||
|
let nextState = reducer(state, action);
|
||||||
|
|
||||||
|
expect(nextState).toEqual({
|
||||||
|
...initialState,
|
||||||
|
componentsDataStructure: {},
|
||||||
|
initialData: {},
|
||||||
|
modifiedData: {
|
||||||
|
relation: {
|
||||||
|
connect: [{ id: 1 }],
|
||||||
|
disconnect: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
nextState = reducer(nextState, {
|
||||||
|
type: 'CONNECT_RELATION',
|
||||||
|
keys: ['relation'],
|
||||||
|
value: { id: 2 },
|
||||||
|
replace: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(nextState).toEqual({
|
||||||
|
...initialState,
|
||||||
|
componentsDataStructure: {},
|
||||||
|
initialData: {},
|
||||||
|
modifiedData: {
|
||||||
|
relation: {
|
||||||
|
connect: [{ id: 2 }],
|
||||||
|
disconnect: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('LOAD_RELATION', () => {
|
describe('LOAD_RELATION', () => {
|
||||||
@ -310,7 +362,9 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
|
|||||||
expect(nextState).toStrictEqual({
|
expect(nextState).toStrictEqual({
|
||||||
...initialState,
|
...initialState,
|
||||||
initialData: {
|
initialData: {
|
||||||
relation: [{ id: 1 }],
|
relation: {
|
||||||
|
results: [{ id: 1 }],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
relation: {
|
relation: {
|
||||||
@ -329,7 +383,7 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
|
|||||||
).toStrictEqual({
|
).toStrictEqual({
|
||||||
...initialState,
|
...initialState,
|
||||||
initialData: {
|
initialData: {
|
||||||
relation: [{ id: 2 }, { id: 1 }],
|
relation: { results: [{ id: 2 }, { id: 1 }] },
|
||||||
},
|
},
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
relation: {
|
relation: {
|
||||||
@ -342,27 +396,26 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('DISCONNECT_RELATION', () => {
|
describe('DISCONNECT_RELATION', () => {
|
||||||
it.skip('should remove a relation from modifiedData', () => {
|
it('should remove a relation from modifiedData', () => {
|
||||||
const state = {
|
const state = {
|
||||||
...initialState,
|
...initialState,
|
||||||
|
|
||||||
initialData: {
|
initialData: {},
|
||||||
name: 'name',
|
|
||||||
},
|
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
name: 'name',
|
relation: {
|
||||||
|
connect: [],
|
||||||
|
disconnect: [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const expected = {
|
const expected = {
|
||||||
...initialState,
|
...initialState,
|
||||||
componentsDataStructure: {},
|
componentsDataStructure: {},
|
||||||
initialData: {
|
initialData: {},
|
||||||
name: 'name',
|
|
||||||
},
|
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
name: 'name',
|
|
||||||
relation: {
|
relation: {
|
||||||
|
connect: [],
|
||||||
disconnect: [{ id: 1 }],
|
disconnect: [{ id: 1 }],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -82,11 +82,7 @@ export const RelationInputWrapper = ({
|
|||||||
}, [isMorph, isCreatingEntry, editable, isFieldAllowed, isFieldReadable]);
|
}, [isMorph, isCreatingEntry, editable, isFieldAllowed, isFieldReadable]);
|
||||||
|
|
||||||
const handleRelationAdd = (relation) => {
|
const handleRelationAdd = (relation) => {
|
||||||
if (isSingleRelation) {
|
connectRelation({ target: { name, value: relation, replace: isSingleRelation } });
|
||||||
// TODO remove all relations from relations before
|
|
||||||
}
|
|
||||||
|
|
||||||
connectRelation({ target: { name, value: relation } });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRelationRemove = (relation) => {
|
const handleRelationRemove = (relation) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user