Bug fix (previous values were not updated in case of several save actions on the same user)

This commit is contained in:
Eric van der Vlist 2023-03-02 17:29:13 +01:00
parent f7a8d6ad9a
commit b7dd6aa15a
3 changed files with 40 additions and 23 deletions

View File

@ -29,6 +29,12 @@ const User: Component<Props> = (props) => {
const [progress, setProgress] = createSignal(-1); const [progress, setProgress] = createSignal(-1);
const defaultValues = () => ({});
const values = props.values ?? defaultValues;
const [previousSubscriptions, setPreviousSubscriptions] = createSignal(
values().subscriptions ?? []
);
const credentials = adminCredentials(); const credentials = adminCredentials();
const { database } = credentials || { database: null }; const { database } = credentials || { database: null };
@ -38,7 +44,7 @@ const User: Component<Props> = (props) => {
if (isNew()) { if (isNew()) {
return { database, subscriptions: [] }; return { database, subscriptions: [] };
} }
return { subscriptions: [], ...props.values() }; return { subscriptions: [], ...values() };
}; };
const submitHandler = async (values: any, context: any) => { const submitHandler = async (values: any, context: any) => {
@ -69,9 +75,6 @@ const User: Component<Props> = (props) => {
setProgress(1); setProgress(1);
const subscriptions = !!props?.values
? props.values().subscriptions ?? []
: [];
const updatedSubscriptions = values.subscriptions ?? []; const updatedSubscriptions = values.subscriptions ?? [];
const isIn = (username: string, subs: any[]) => { const isIn = (username: string, subs: any[]) => {
@ -84,20 +87,28 @@ const User: Component<Props> = (props) => {
return false; return false;
}; };
const addSubscription = (userDocument: any) => { console.log({
const subscriptions = userDocument.subscriptions || []; caller: 'User / submitHandler',
subscriptions.push({ username, direction: '' }); previousSubscriptions: previousSubscriptions(),
userDocument.subscriptions = subscriptions; updatedSubscriptions,
return { username, ...userDocument }; });
};
const removeSubscription = (userDocument: any) => { const addSubscriptionFactory =
const subscriptions = userDocument.subscriptions || []; (subscriptionUserName: string) => (userDocument: any) => {
userDocument.subscriptions = subscriptions.filter( const subscriptions = userDocument.subscriptions || [];
(subscription: any) => subscription.username !== username subscriptions.push({ username, direction: '' });
); userDocument.subscriptions = subscriptions;
return { username, ...userDocument }; return { username: subscriptionUserName, ...userDocument };
}; };
const removeSubscriptionFactory =
(subscriptionUserName: string) => (userDocument: any) => {
const subscriptions = userDocument.subscriptions || [];
userDocument.subscriptions = subscriptions.filter(
(subscription: any) => subscription.username !== username
);
return { username: subscriptionUserName, ...userDocument };
};
const defaultUserDocument = { const defaultUserDocument = {
database, database,
@ -105,10 +116,10 @@ const User: Component<Props> = (props) => {
for (let i = 0; i < updatedSubscriptions.length; i++) { for (let i = 0; i < updatedSubscriptions.length; i++) {
let subscription = updatedSubscriptions[i]; let subscription = updatedSubscriptions[i];
if (!isIn(subscription.username, subscriptions)) { if (!isIn(subscription.username, previousSubscriptions())) {
await update( await update(
userId(subscription.username, database), userId(subscription.username, database),
addSubscription, addSubscriptionFactory(subscription.username),
defaultUserDocument defaultUserDocument
); );
console.log({ console.log({
@ -120,12 +131,12 @@ const User: Component<Props> = (props) => {
setProgress(2); setProgress(2);
for (let i = 0; i < subscriptions.length; i++) { for (let i = 0; i < previousSubscriptions().length; i++) {
let subscription = subscriptions[i]; let subscription = previousSubscriptions()[i];
if (!isIn(subscription.username, updatedSubscriptions)) { if (!isIn(subscription.username, updatedSubscriptions)) {
await update( await update(
userId(subscription.username, database), userId(subscription.username, database),
removeSubscription, removeSubscriptionFactory(subscription.username),
defaultUserDocument defaultUserDocument
); );
console.log({ console.log({
@ -137,6 +148,7 @@ const User: Component<Props> = (props) => {
setProgress(3); setProgress(3);
setInitialValues(values); setInitialValues(values);
setPreviousSubscriptions(values.subscriptions);
setIsDirty(false); setIsDirty(false);
setProgress(-1); setProgress(-1);

View File

@ -20,5 +20,8 @@ export const get = async (id: string, db = 'dyomedea_users') => {
headers, headers,
}); });
if (response.status >= 300) {
return null;
}
return await response.json(); return await response.json();
}; };

View File

@ -17,7 +17,9 @@ export const put = async (
if (!isNew) { if (!isNew) {
const previous = await get(id, db); const previous = await get(id, db);
content._rev = previous._rev; if (!!previous) {
content._rev = previous._rev;
}
} }
const headers = headersWithAuth(); const headers = headersWithAuth();