mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-20 07:13:33 +01:00
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
import rxjs from "../../lib/rx.js";
|
|
import ajax from "../../lib/ajax.js";
|
|
|
|
const isSaving$ = new rxjs.BehaviorSubject(false);
|
|
|
|
const config$ = isSaving$.pipe(
|
|
rxjs.filter((loading) => !loading),
|
|
rxjs.switchMapTo(ajax({
|
|
url: "admin/api/config",
|
|
method: "GET",
|
|
responseType: "json"
|
|
})),
|
|
rxjs.map((res) => res.responseJSON.result),
|
|
rxjs.shareReplay(1),
|
|
);
|
|
|
|
export async function initConfig() {
|
|
if (isSaving$.value === true) isSaving$.next(false);
|
|
}
|
|
|
|
export function isSaving() {
|
|
return isSaving$.asObservable();
|
|
}
|
|
|
|
export function get() {
|
|
return config$;
|
|
}
|
|
|
|
export function save() {
|
|
return rxjs.pipe(
|
|
rxjs.tap(() => isSaving$.next(true)),
|
|
rxjs.debounceTime(800),
|
|
rxjs.mergeMap((formData) => ajax({
|
|
url: "admin/api/config",
|
|
method: "POST",
|
|
responseType: "json",
|
|
body: formData,
|
|
})),
|
|
rxjs.tap(() => isSaving$.next(false)),
|
|
rxjs.catchError((err) => {
|
|
isSaving$.next(false);
|
|
return rxjs.throwError(err);
|
|
}),
|
|
);
|
|
}
|