Still refactoring... User CRUD seems to be working fine.

This commit is contained in:
Eric van der Vlist 2023-02-24 21:02:10 +01:00
parent b375cfbdd7
commit 81ff27fd0c
5 changed files with 25 additions and 10 deletions

View File

@ -35,6 +35,15 @@ const User: Component<Props> = (props) => {
}); });
const id = getValues()?._id ?? `user:${uuid()}`; const id = getValues()?._id ?? `user:${uuid()}`;
await put(id, values, isNew()); await put(id, values, isNew());
const couchUserId = `org.couchdb.user:${values.username}`;
const userDoc = {
_id: couchUserId,
name: values.username,
password: values.password,
type: 'user',
roles: [],
};
await put(couchUserId, userDoc, isNew(), '_users');
if (isNew()) { if (isNew()) {
navigate(`/user/${id}`); navigate(`/user/${id}`);
} }

View File

@ -1,6 +1,6 @@
import { put } from './put'; import { put } from './put';
export const del = async (id: string) => { export const del = async (id: string, db = 'dyomedea_users') => {
const content = { _deleted: true }; const content = { _deleted: true };
return await put(id, content, false); return await put(id, content, false, db);
}; };

View File

@ -1,10 +1,10 @@
import { adminCredentials } from '~/components/credentials'; import { adminCredentials } from '~/components/credentials';
export const getUrl = (id: string) => { export const getUrl = (id: string, db = 'dyomedea_users') => {
const credentials = adminCredentials(); const credentials = adminCredentials();
if (!credentials) { if (!credentials) {
return ''; return '';
} }
const { database } = credentials; const { database } = credentials;
return `${database}/dyomedea_users/${id}`; return `${database}/${db}/${id}`;
}; };

View File

@ -1,7 +1,8 @@
import { adminCredentials } from '~/components/credentials'; import { adminCredentials } from '~/components/credentials';
import { getUrl } from './get-url';
import { headersWithAuth } from './headers-with-auth'; import { headersWithAuth } from './headers-with-auth';
export const get = async (id: string) => { export const get = async (id: string, db = 'dyomedea_users') => {
const credentials = adminCredentials(); const credentials = adminCredentials();
if (!credentials) { if (!credentials) {
return null; return null;
@ -13,7 +14,7 @@ export const get = async (id: string) => {
return null; return null;
} }
const response = await fetch(`${database}/dyomedea_users/${id}`, { const response = await fetch(getUrl(id, db), {
method: 'GET', method: 'GET',
mode: 'cors', mode: 'cors',
headers, headers,

View File

@ -3,7 +3,12 @@ import { get } from './get';
import { getUrl } from './get-url'; import { getUrl } from './get-url';
import { headersWithAuth } from './headers-with-auth'; import { headersWithAuth } from './headers-with-auth';
export const put = async (id: string, content: any, isNew: boolean = false) => { export const put = async (
id: string,
content: any,
isNew: boolean = false,
db = 'dyomedea_users'
) => {
const credentials = adminCredentials(); const credentials = adminCredentials();
if (!credentials) { if (!credentials) {
return null; return null;
@ -11,7 +16,7 @@ export const put = async (id: string, content: any, isNew: boolean = false) => {
const { database } = credentials; const { database } = credentials;
if (!isNew) { if (!isNew) {
const previous = await get(id); const previous = await get(id, db);
content._rev = previous._rev; content._rev = previous._rev;
} }
@ -21,12 +26,12 @@ export const put = async (id: string, content: any, isNew: boolean = false) => {
} }
headers.set('Content-type', 'application/json; charset=UTF-8'); headers.set('Content-type', 'application/json; charset=UTF-8');
const response = await fetch(getUrl(id), { const response = await fetch(getUrl(id, db), {
method: 'PUT', method: 'PUT',
mode: 'cors', mode: 'cors',
headers, headers,
body: JSON.stringify(content), body: JSON.stringify(content),
}); });
console.log({ caller: 'put', id, isNew, db, status: response.status });
return await response.json(); return await response.json();
}; };