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,19 +87,27 @@ const User: Component<Props> = (props) => {
return false; return false;
}; };
const addSubscription = (userDocument: any) => { console.log({
caller: 'User / submitHandler',
previousSubscriptions: previousSubscriptions(),
updatedSubscriptions,
});
const addSubscriptionFactory =
(subscriptionUserName: string) => (userDocument: any) => {
const subscriptions = userDocument.subscriptions || []; const subscriptions = userDocument.subscriptions || [];
subscriptions.push({ username, direction: '' }); subscriptions.push({ username, direction: '' });
userDocument.subscriptions = subscriptions; userDocument.subscriptions = subscriptions;
return { username, ...userDocument }; return { username: subscriptionUserName, ...userDocument };
}; };
const removeSubscription = (userDocument: any) => { const removeSubscriptionFactory =
(subscriptionUserName: string) => (userDocument: any) => {
const subscriptions = userDocument.subscriptions || []; const subscriptions = userDocument.subscriptions || [];
userDocument.subscriptions = subscriptions.filter( userDocument.subscriptions = subscriptions.filter(
(subscription: any) => subscription.username !== username (subscription: any) => subscription.username !== username
); );
return { username, ...userDocument }; return { username: subscriptionUserName, ...userDocument };
}; };
const defaultUserDocument = { const defaultUserDocument = {
@ -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,8 +17,10 @@ export const put = async (
if (!isNew) { if (!isNew) {
const previous = await get(id, db); const previous = await get(id, db);
if (!!previous) {
content._rev = previous._rev; content._rev = previous._rev;
} }
}
const headers = headersWithAuth(); const headers = headersWithAuth();
if (!headers) { if (!headers) {