Logic for subscriptions add/remove

This commit is contained in:
Eric van der Vlist 2023-03-02 11:33:38 +01:00
parent 90fe60c10c
commit 6b2569aed0
1 changed files with 46 additions and 4 deletions

View File

@ -49,9 +49,45 @@ const User: Component<Props> = (props) => {
};
await put(couchUserId, userDoc, isNew(), '_users');
if (isNew()) {
navigate(`/user/${id}`);
const subscriptions = !!props?.values
? 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 = () => {
@ -116,6 +152,7 @@ const User: Component<Props> = (props) => {
form,
data,
setData,
setIsDirty,
setInitialValues,
reset,
addField,
@ -141,12 +178,17 @@ const User: Component<Props> = (props) => {
const subscriptions = () => data('subscriptions');
function removeSubscription(index: number) {
return () => unsetField(`subscriptions.${index}`);
return () => {
unsetField(`subscriptions.${index}`);
setIsDirty(true);
};
}
function addSubscription(index?: number) {
return () =>
return () => {
addField(`subscriptions`, { username: '', direction: '' }, index);
setIsDirty(true);
};
}
console.log({