[PUI] Api state extension/cleanup (#5934)

* added option to replace path parts

* use function instead of f-string

* remove doube defintion

* added user pk

* Update src/frontend/src/states/ApiState.tsx

Co-authored-by: Lukas <76838159+wolflu05@users.noreply.github.com>

* fix typing issue

---------

Co-authored-by: Lukas <76838159+wolflu05@users.noreply.github.com>
This commit is contained in:
Matthias Mair 2023-11-20 03:54:04 +01:00 committed by GitHub
parent 829d427a76
commit 70a96942c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 12 deletions

View File

@ -86,7 +86,7 @@ export function UserDrawer({
function setPermission(pk: number, data: any) { function setPermission(pk: number, data: any) {
setLocked(true); setLocked(true);
api api
.patch(`${apiUrl(ApiPaths.user_list)}${pk}/`, data) .patch(apiUrl(ApiPaths.user_list, pk), data)
.then(() => { .then(() => {
notifications.show({ notifications.show({
title: t`User permission changed successfully`, title: t`User permission changed successfully`,
@ -110,7 +110,7 @@ export function UserDrawer({
function setActive(pk: number, active: boolean) { function setActive(pk: number, active: boolean) {
setLocked(true); setLocked(true);
api api
.patch(`${apiUrl(ApiPaths.user_list)}${pk}/`, { .patch(apiUrl(ApiPaths.user_list, pk), {
is_active: active is_active: active
}) })
.then(() => { .then(() => {

View File

@ -21,6 +21,7 @@ import { api, queryClient } from '../../../../App';
import { PlaceholderPill } from '../../../../components/items/Placeholder'; import { PlaceholderPill } from '../../../../components/items/Placeholder';
import { ApiPaths } from '../../../../enums/ApiEndpoints'; import { ApiPaths } from '../../../../enums/ApiEndpoints';
import { apiUrl } from '../../../../states/ApiState'; import { apiUrl } from '../../../../states/ApiState';
import { useUserState } from '../../../../states/UserState';
export function SecurityContent() { export function SecurityContent() {
const [isSsoEnabled, setIsSsoEnabled] = useState<boolean>(false); const [isSsoEnabled, setIsSsoEnabled] = useState<boolean>(false);
@ -91,6 +92,7 @@ export function SecurityContent() {
function EmailContent({}: {}) { function EmailContent({}: {}) {
const [value, setValue] = useState<string>(''); const [value, setValue] = useState<string>('');
const [newEmailValue, setNewEmailValue] = useState(''); const [newEmailValue, setNewEmailValue] = useState('');
const [user] = useUserState((state) => [state.user]);
const { isLoading, data, refetch } = useQuery({ const { isLoading, data, refetch } = useQuery({
queryKey: ['emails'], queryKey: ['emails'],
queryFn: () => api.get(apiUrl(ApiPaths.user_emails)).then((res) => res.data) queryFn: () => api.get(apiUrl(ApiPaths.user_emails)).then((res) => res.data)
@ -98,7 +100,7 @@ function EmailContent({}: {}) {
function runServerAction(url: ApiPaths) { function runServerAction(url: ApiPaths) {
api api
.post(apiUrl(url).replace('$id', value), {}) .post(apiUrl(url, undefined, { id: value }), {})
.then(() => { .then(() => {
refetch(); refetch();
}) })
@ -108,7 +110,8 @@ function EmailContent({}: {}) {
function addEmail() { function addEmail() {
api api
.post(apiUrl(ApiPaths.user_emails), { .post(apiUrl(ApiPaths.user_emails), {
email: newEmailValue email: newEmailValue,
user: user?.pk
}) })
.then(() => { .then(() => {
refetch(); refetch();
@ -219,7 +222,7 @@ function SsoContent({ dataProvider }: { dataProvider: any | undefined }) {
function removeProvider() { function removeProvider() {
api api
.post(apiUrl(ApiPaths.user_sso_remove).replace('$id', value)) .post(apiUrl(ApiPaths.user_sso_remove, undefined, { id: value }))
.then(() => { .then(() => {
queryClient.removeQueries({ queryClient.removeQueries({
queryKey: ['sso-list'] queryKey: ['sso-list']

View File

@ -85,15 +85,15 @@ export function apiEndpoint(path: ApiPaths): string {
case ApiPaths.user_sso: case ApiPaths.user_sso:
return 'auth/social/'; return 'auth/social/';
case ApiPaths.user_sso_remove: case ApiPaths.user_sso_remove:
return 'auth/social/$id/disconnect/'; return 'auth/social/:id/disconnect/';
case ApiPaths.user_emails: case ApiPaths.user_emails:
return 'auth/emails/'; return 'auth/emails/';
case ApiPaths.user_email_remove: case ApiPaths.user_email_remove:
return 'auth/emails/$id/remove/'; return 'auth/emails/:id/remove/';
case ApiPaths.user_email_verify: case ApiPaths.user_email_verify:
return 'auth/emails/$id/verify/'; return 'auth/emails/:id/verify/';
case ApiPaths.user_email_primary: case ApiPaths.user_email_primary:
return 'auth/emails/$id/primary/'; return 'auth/emails/:id/primary/';
case ApiPaths.currency_list: case ApiPaths.currency_list:
return 'currency/exchange/'; return 'currency/exchange/';
case ApiPaths.currency_refresh: case ApiPaths.currency_refresh:
@ -116,8 +116,6 @@ export function apiEndpoint(path: ApiPaths): string {
return 'version/'; return 'version/';
case ApiPaths.sso_providers: case ApiPaths.sso_providers:
return 'auth/providers/'; return 'auth/providers/';
case ApiPaths.user_list:
return 'user/';
case ApiPaths.group_list: case ApiPaths.group_list:
return 'user/group/'; return 'user/group/';
case ApiPaths.owner_list: case ApiPaths.owner_list:
@ -194,7 +192,11 @@ export function apiEndpoint(path: ApiPaths): string {
/** /**
* Construct an API URL with an endpoint and (optional) pk value * Construct an API URL with an endpoint and (optional) pk value
*/ */
export function apiUrl(path: ApiPaths, pk?: any): string { export function apiUrl(
path: ApiPaths,
pk?: any,
data?: Record<string, string | number>
): string {
let _url = apiEndpoint(path); let _url = apiEndpoint(path);
// If the URL does not start with a '/', add the API prefix // If the URL does not start with a '/', add the API prefix
@ -206,5 +208,11 @@ export function apiUrl(path: ApiPaths, pk?: any): string {
_url += `${pk}/`; _url += `${pk}/`;
} }
if (_url && data) {
for (const key in data) {
_url = _url.replace(`:${key}`, `${data[key]}`);
}
}
return _url; return _url;
} }

View File

@ -42,6 +42,7 @@ export const useUserState = create<UserStateProps>((set, get) => ({
}) })
.then((response) => { .then((response) => {
const user: UserProps = { const user: UserProps = {
pk: response.data.pk,
first_name: response.data?.first_name ?? '', first_name: response.data?.first_name ?? '',
last_name: response.data?.last_name ?? '', last_name: response.data?.last_name ?? '',
email: response.data.email, email: response.data.email,

View File

@ -9,6 +9,7 @@ export interface HostList {
// Type interface fully defining the current user // Type interface fully defining the current user
export interface UserProps { export interface UserProps {
pk: number;
username: string; username: string;
first_name: string; first_name: string;
last_name: string; last_name: string;