From 81ff27fd0ccd05757ec7faaeb72d6adf79683ca1 Mon Sep 17 00:00:00 2001 From: evlist Date: Fri, 24 Feb 2023 21:02:10 +0100 Subject: [PATCH] Still refactoring... User CRUD seems to be working fine. --- src/components/user/User.tsx | 9 +++++++++ src/lib/del.ts | 4 ++-- src/lib/get-url.ts | 4 ++-- src/lib/get.ts | 5 +++-- src/lib/put.ts | 13 +++++++++---- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/components/user/User.tsx b/src/components/user/User.tsx index a0013ed..8d602a6 100644 --- a/src/components/user/User.tsx +++ b/src/components/user/User.tsx @@ -35,6 +35,15 @@ const User: Component = (props) => { }); const id = getValues()?._id ?? `user:${uuid()}`; 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()) { navigate(`/user/${id}`); } diff --git a/src/lib/del.ts b/src/lib/del.ts index 9cdaa45..62c5aa8 100644 --- a/src/lib/del.ts +++ b/src/lib/del.ts @@ -1,6 +1,6 @@ import { put } from './put'; -export const del = async (id: string) => { +export const del = async (id: string, db = 'dyomedea_users') => { const content = { _deleted: true }; - return await put(id, content, false); + return await put(id, content, false, db); }; diff --git a/src/lib/get-url.ts b/src/lib/get-url.ts index c6b267c..03d2f66 100644 --- a/src/lib/get-url.ts +++ b/src/lib/get-url.ts @@ -1,10 +1,10 @@ import { adminCredentials } from '~/components/credentials'; -export const getUrl = (id: string) => { +export const getUrl = (id: string, db = 'dyomedea_users') => { const credentials = adminCredentials(); if (!credentials) { return ''; } const { database } = credentials; - return `${database}/dyomedea_users/${id}`; + return `${database}/${db}/${id}`; }; diff --git a/src/lib/get.ts b/src/lib/get.ts index 65bb728..ad939db 100644 --- a/src/lib/get.ts +++ b/src/lib/get.ts @@ -1,7 +1,8 @@ import { adminCredentials } from '~/components/credentials'; +import { getUrl } from './get-url'; import { headersWithAuth } from './headers-with-auth'; -export const get = async (id: string) => { +export const get = async (id: string, db = 'dyomedea_users') => { const credentials = adminCredentials(); if (!credentials) { return null; @@ -13,7 +14,7 @@ export const get = async (id: string) => { return null; } - const response = await fetch(`${database}/dyomedea_users/${id}`, { + const response = await fetch(getUrl(id, db), { method: 'GET', mode: 'cors', headers, diff --git a/src/lib/put.ts b/src/lib/put.ts index a06aca3..db2c395 100644 --- a/src/lib/put.ts +++ b/src/lib/put.ts @@ -3,7 +3,12 @@ import { get } from './get'; import { getUrl } from './get-url'; 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(); if (!credentials) { return null; @@ -11,7 +16,7 @@ export const put = async (id: string, content: any, isNew: boolean = false) => { const { database } = credentials; if (!isNew) { - const previous = await get(id); + const previous = await get(id, db); 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'); - const response = await fetch(getUrl(id), { + const response = await fetch(getUrl(id, db), { method: 'PUT', mode: 'cors', headers, body: JSON.stringify(content), }); - + console.log({ caller: 'put', id, isNew, db, status: response.status }); return await response.json(); };