Compare commits

...

2 commits

Author SHA1 Message Date
e194b32541 replace note deletion API call with new request script
All checks were successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
2022-12-29 03:02:32 +01:00
ef0f6af97f add request script for API requests 2022-12-29 03:02:11 +01:00
2 changed files with 57 additions and 7 deletions

View file

@ -7,6 +7,7 @@ import * as os from '@/os';
import copyToClipboard from '@/scripts/copy-to-clipboard';
import { url } from '@/config';
import { noteActions } from '@/store';
import { del } from '@/scripts/request';
export function getNoteMenu(props: {
note: foundkey.entities.Note;
@ -25,20 +26,18 @@ export function getNoteMenu(props: {
const appearNote = isRenote ? props.note.renote as foundkey.entities.Note : props.note;
function del(): void {
function deleteNote(): void {
os.confirm({
type: 'warning',
text: i18n.ts.noteDeleteConfirm,
}).then(({ canceled }) => {
if (canceled) return;
os.api('notes/delete', {
noteId: appearNote.id,
});
del(`/api/v2/notes/${appearNote.id}`);
});
}
function delEdit(): void {
function deleteEditNote(): void {
os.confirm({
type: 'warning',
text: i18n.ts.deleteAndEditConfirm,
@ -310,13 +309,13 @@ export function getNoteMenu(props: {
appearNote.userId === $i.id ? {
icon: 'fas fa-edit',
text: i18n.ts.deleteAndEdit,
action: delEdit,
action: deleteEditNote,
} : undefined,
{
icon: 'fas fa-trash-alt',
text: i18n.ts.delete,
danger: true,
action: del,
action: deleteNote,
}]
: []
)]

View file

@ -0,0 +1,51 @@
import { $i } from '@/account';
export const request = async (url: string, body?: Record<string, any>, options?: Record<string, any>, token?: string) => {
let requestOptions = {};
const authorizationToken = token ?? $i?.token ?? undefined;
const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined;
requestOptions = {
...{
headers: {
'content-type': 'application/json',
...(authorization ? { authorization } : {}),
},
},
...options,
};
const response = await fetch(url, {
...requestOptions,
body: body ? JSON.stringify(body) : undefined,
});
const responseBody = response.status === 204 ? null : await response.json();
if (response.status === 200) {
return responseBody;
}
else if (response.status === 204) {
return null;
}
else {
throw new Error(responseBody.error);
}
};
export const get = async (url: string, options?: Record<string, any>, token?: string) => {
return request(url, undefined, { method: 'GET' }, token);
};
export const post = async (url: string, body: Record<string, any>, options?: Record<string, any>, token?: string) => {
return request(url, body, { method: 'POST' }, token);
};
export const put = async (url: string, body: Record<string, any>, options?: Record<string, any>, token?: string) => {
return request(url, body, { method: 'PUT' }, token);
};
export const del = async (url: string, options?: Record<string, any>, token?: string) => {
return request(url, undefined, { method: 'DELETE' }, token);
};