Refactor cache reset functionality: replace old CacheSettingsContainer with TypeScript version, update SystemController to handle reset cache, and enhance SWR mutation fetcher.

master^2
Tudor Stanciu 2024-11-11 01:29:07 +02:00
parent 8af7b64a60
commit ca388cb639
4 changed files with 33 additions and 25 deletions

View File

@ -44,8 +44,9 @@ namespace NetworkResurrector.Api.Controllers
[HttpPost("reset-cache")] [HttpPost("reset-cache")]
[Authorize(Policy = Policies.SystemAdministration)] [Authorize(Policy = Policies.SystemAdministration)]
public async Task<IActionResult> WakeMachine([FromBody] ResetCache resetCache) public async Task<IActionResult> ResetCache()
{ {
var resetCache = new ResetCache();
var result = await _mediator.Send(resetCache); var result = await _mediator.Send(resetCache);
return Ok(result); return Ok(result);
} }

View File

@ -1,22 +0,0 @@
import React, { useCallback } from "react";
import CacheSettingsComponent from "./CacheSettingsComponent";
import { useTranslation } from "react-i18next";
import { routes, post } from "utils/api";
import { blip } from "utils";
const CacheSettingsContainer = () => {
const { t } = useTranslation();
const handleResetCache = useCallback(async () => {
await post(
routes.resetCache,
{},
{
onCompleted: () => blip.info(t("Settings.Cache.ResetInfo"))
}
);
}, [t]);
return <CacheSettingsComponent onResetCache={handleResetCache} />;
};
export default CacheSettingsContainer;

View File

@ -0,0 +1,25 @@
import React, { useCallback } from "react";
import CacheSettingsComponent from "./CacheSettingsComponent";
import { useTranslation } from "react-i18next";
import { endpoints } from "utils/api";
import { blip } from "utils";
import useSWRMutation from "swr/mutation";
import { Key } from "swr";
import { mutationFetcher } from "utils/swr";
const CacheSettingsContainer: React.FC = () => {
const { t } = useTranslation();
const { trigger } = useSWRMutation<void, Error, Key, void>(endpoints.system.resetCache, mutationFetcher<void>, {
onError: err => {
blip.error(err.message);
},
onSuccess: () => blip.info(t("Settings.Cache.ResetInfo"))
});
const handleResetCache = useCallback(() => trigger(), [t]);
return <CacheSettingsComponent onResetCache={handleResetCache} />;
};
export default CacheSettingsContainer;

View File

@ -23,10 +23,14 @@ const getHeaders = (): HeadersInit => {
const fetcher = (url: string) => fetch(url, { method: "GET", headers: getHeaders() }).then(res => res.json()); const fetcher = (url: string) => fetch(url, { method: "GET", headers: getHeaders() }).then(res => res.json());
async function mutationFetcher<Command>(url: string, { arg }: { arg: Command }) { async function mutationFetcher<Command>(url: string, { arg }: { arg: Command }) {
const hasBody = arg !== undefined && arg !== null;
const headers = getHeaders();
const body = hasBody ? JSON.stringify(arg) : undefined;
return fetch(url, { return fetch(url, {
method: "POST", method: "POST",
headers: getHeaders(), headers,
body: JSON.stringify(arg) body
}).then(res => res.json()); }).then(res => res.json());
} }