Bug fix (previous values were not updated in case of several save actions on the same user)
This commit is contained in:
parent
f7a8d6ad9a
commit
b7dd6aa15a
|
@ -29,6 +29,12 @@ const User: Component<Props> = (props) => {
|
|||
|
||||
const [progress, setProgress] = createSignal(-1);
|
||||
|
||||
const defaultValues = () => ({});
|
||||
const values = props.values ?? defaultValues;
|
||||
const [previousSubscriptions, setPreviousSubscriptions] = createSignal(
|
||||
values().subscriptions ?? []
|
||||
);
|
||||
|
||||
const credentials = adminCredentials();
|
||||
const { database } = credentials || { database: null };
|
||||
|
||||
|
@ -38,7 +44,7 @@ const User: Component<Props> = (props) => {
|
|||
if (isNew()) {
|
||||
return { database, subscriptions: [] };
|
||||
}
|
||||
return { subscriptions: [], ...props.values() };
|
||||
return { subscriptions: [], ...values() };
|
||||
};
|
||||
|
||||
const submitHandler = async (values: any, context: any) => {
|
||||
|
@ -69,9 +75,6 @@ const User: Component<Props> = (props) => {
|
|||
|
||||
setProgress(1);
|
||||
|
||||
const subscriptions = !!props?.values
|
||||
? props.values().subscriptions ?? []
|
||||
: [];
|
||||
const updatedSubscriptions = values.subscriptions ?? [];
|
||||
|
||||
const isIn = (username: string, subs: any[]) => {
|
||||
|
@ -84,19 +87,27 @@ const User: Component<Props> = (props) => {
|
|||
return false;
|
||||
};
|
||||
|
||||
const addSubscription = (userDocument: any) => {
|
||||
console.log({
|
||||
caller: 'User / submitHandler',
|
||||
previousSubscriptions: previousSubscriptions(),
|
||||
updatedSubscriptions,
|
||||
});
|
||||
|
||||
const addSubscriptionFactory =
|
||||
(subscriptionUserName: string) => (userDocument: any) => {
|
||||
const subscriptions = userDocument.subscriptions || [];
|
||||
subscriptions.push({ username, direction: '' });
|
||||
userDocument.subscriptions = subscriptions;
|
||||
return { username, ...userDocument };
|
||||
return { username: subscriptionUserName, ...userDocument };
|
||||
};
|
||||
|
||||
const removeSubscription = (userDocument: any) => {
|
||||
const removeSubscriptionFactory =
|
||||
(subscriptionUserName: string) => (userDocument: any) => {
|
||||
const subscriptions = userDocument.subscriptions || [];
|
||||
userDocument.subscriptions = subscriptions.filter(
|
||||
(subscription: any) => subscription.username !== username
|
||||
);
|
||||
return { username, ...userDocument };
|
||||
return { username: subscriptionUserName, ...userDocument };
|
||||
};
|
||||
|
||||
const defaultUserDocument = {
|
||||
|
@ -105,10 +116,10 @@ const User: Component<Props> = (props) => {
|
|||
|
||||
for (let i = 0; i < updatedSubscriptions.length; i++) {
|
||||
let subscription = updatedSubscriptions[i];
|
||||
if (!isIn(subscription.username, subscriptions)) {
|
||||
if (!isIn(subscription.username, previousSubscriptions())) {
|
||||
await update(
|
||||
userId(subscription.username, database),
|
||||
addSubscription,
|
||||
addSubscriptionFactory(subscription.username),
|
||||
defaultUserDocument
|
||||
);
|
||||
console.log({
|
||||
|
@ -120,12 +131,12 @@ const User: Component<Props> = (props) => {
|
|||
|
||||
setProgress(2);
|
||||
|
||||
for (let i = 0; i < subscriptions.length; i++) {
|
||||
let subscription = subscriptions[i];
|
||||
for (let i = 0; i < previousSubscriptions().length; i++) {
|
||||
let subscription = previousSubscriptions()[i];
|
||||
if (!isIn(subscription.username, updatedSubscriptions)) {
|
||||
await update(
|
||||
userId(subscription.username, database),
|
||||
removeSubscription,
|
||||
removeSubscriptionFactory(subscription.username),
|
||||
defaultUserDocument
|
||||
);
|
||||
console.log({
|
||||
|
@ -137,6 +148,7 @@ const User: Component<Props> = (props) => {
|
|||
setProgress(3);
|
||||
|
||||
setInitialValues(values);
|
||||
setPreviousSubscriptions(values.subscriptions);
|
||||
setIsDirty(false);
|
||||
|
||||
setProgress(-1);
|
||||
|
|
|
@ -20,5 +20,8 @@ export const get = async (id: string, db = 'dyomedea_users') => {
|
|||
headers,
|
||||
});
|
||||
|
||||
if (response.status >= 300) {
|
||||
return null;
|
||||
}
|
||||
return await response.json();
|
||||
};
|
||||
|
|
|
@ -17,8 +17,10 @@ export const put = async (
|
|||
|
||||
if (!isNew) {
|
||||
const previous = await get(id, db);
|
||||
if (!!previous) {
|
||||
content._rev = previous._rev;
|
||||
}
|
||||
}
|
||||
|
||||
const headers = headersWithAuth();
|
||||
if (!headers) {
|
||||
|
|
Loading…
Reference in New Issue