Logic for subscriptions add/remove
This commit is contained in:
parent
90fe60c10c
commit
6b2569aed0
|
@ -49,9 +49,45 @@ const User: Component<Props> = (props) => {
|
||||||
};
|
};
|
||||||
await put(couchUserId, userDoc, isNew(), '_users');
|
await put(couchUserId, userDoc, isNew(), '_users');
|
||||||
|
|
||||||
if (isNew()) {
|
const subscriptions = !!props?.values
|
||||||
navigate(`/user/${id}`);
|
? props.values().subscriptions ?? []
|
||||||
|
: [];
|
||||||
|
const updatedSubscriptions = values.subscriptions ?? [];
|
||||||
|
|
||||||
|
const isIn = (username: string, subs: any[]) => {
|
||||||
|
for (let i = 0; i < subs.length; i++) {
|
||||||
|
let sub = subs[i];
|
||||||
|
if (username === sub.username) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (let i = 0; i < updatedSubscriptions.length; i++) {
|
||||||
|
let subscription = updatedSubscriptions[i];
|
||||||
|
if (!isIn(subscription.username, subscriptions)) {
|
||||||
|
console.log({
|
||||||
|
caller: 'User / submitHandler / new subscription',
|
||||||
|
username: subscription.username,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < subscriptions.length; i++) {
|
||||||
|
let subscription = subscriptions[i];
|
||||||
|
if (!isIn(subscription.username, updatedSubscriptions)) {
|
||||||
|
console.log({
|
||||||
|
caller: 'User / submitHandler / deleted subscription',
|
||||||
|
username: subscription.username,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setInitialValues(values);
|
||||||
|
setIsDirty(false);
|
||||||
|
|
||||||
|
navigate(`/user/${id}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const cancelHandler = () => {
|
const cancelHandler = () => {
|
||||||
|
@ -116,6 +152,7 @@ const User: Component<Props> = (props) => {
|
||||||
form,
|
form,
|
||||||
data,
|
data,
|
||||||
setData,
|
setData,
|
||||||
|
setIsDirty,
|
||||||
setInitialValues,
|
setInitialValues,
|
||||||
reset,
|
reset,
|
||||||
addField,
|
addField,
|
||||||
|
@ -141,12 +178,17 @@ const User: Component<Props> = (props) => {
|
||||||
const subscriptions = () => data('subscriptions');
|
const subscriptions = () => data('subscriptions');
|
||||||
|
|
||||||
function removeSubscription(index: number) {
|
function removeSubscription(index: number) {
|
||||||
return () => unsetField(`subscriptions.${index}`);
|
return () => {
|
||||||
|
unsetField(`subscriptions.${index}`);
|
||||||
|
setIsDirty(true);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSubscription(index?: number) {
|
function addSubscription(index?: number) {
|
||||||
return () =>
|
return () => {
|
||||||
addField(`subscriptions`, { username: '', direction: '' }, index);
|
addField(`subscriptions`, { username: '', direction: '' }, index);
|
||||||
|
setIsDirty(true);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log({
|
console.log({
|
||||||
|
|
Loading…
Reference in New Issue