Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update resource service #364

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions web/src/components/ResourcesDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import copy from "copy-to-clipboard";
import { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import * as utils from "../helpers/utils";
import useLoading from "../hooks/useLoading";
import { resourceService } from "../services";
import { useAppSelector } from "../store";
import Icon from "./Icon";
import toastHelper from "./Toast";
import Dropdown from "./common/Dropdown";
import { generateDialog } from "./Dialog";
import { showCommonDialog } from "./Dialog/CommonDialog";
import showPreviewImageDialog from "./PreviewImageDialog";
import Icon from "./Icon";
import toastHelper from "./Toast";
import "../less/resources-dialog.less";
import * as utils from "../helpers/utils";
import showChangeResourceFilenameDialog from "./ChangeResourceFilenameDialog";
import { useAppSelector } from "../store";
import "../less/resources-dialog.less";

type Props = DialogProps;

Expand All @@ -28,8 +28,10 @@ const ResourcesDialog: React.FC<Props> = (props: Props) => {
const [state, setState] = useState<State>({
isUploadingResource: false,
});

useEffect(() => {
fetchResources()
resourceService
.fetchResourceList()
.catch((error) => {
console.error(error);
toastHelper.error(error.response.data.message);
Expand All @@ -39,10 +41,6 @@ const ResourcesDialog: React.FC<Props> = (props: Props) => {
});
}, []);

const fetchResources = async () => {
const data = await resourceService.getResourceList();
};

const handleUploadFileBtnClick = async () => {
if (state.isUploadingResource) {
return;
Expand Down Expand Up @@ -81,7 +79,6 @@ const ResourcesDialog: React.FC<Props> = (props: Props) => {
}

document.body.removeChild(inputEl);
await fetchResources();
};
inputEl.click();
};
Expand Down Expand Up @@ -116,7 +113,6 @@ const ResourcesDialog: React.FC<Props> = (props: Props) => {
style: "warning",
onConfirm: async () => {
await resourceService.deleteResourceById(resource.id);
await fetchResources();
},
});
};
Expand Down
19 changes: 14 additions & 5 deletions web/src/services/resourceService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as api from "../helpers/api";
import store from "../store";
import { patchResource, setResources } from "../store/modules/resource";
import { patchResource, setResources, deleteResource } from "../store/modules/resource";

const convertResponseModelResource = (resource: Resource): Resource => {
return {
Expand All @@ -11,12 +11,17 @@ const convertResponseModelResource = (resource: Resource): Resource => {
};

const resourceService = {
async getResourceList(): Promise<Resource[]> {
getState: () => {
return store.getState().resource;
},

async fetchResourceList(): Promise<Resource[]> {
const { data } = (await api.getResourceList()).data;
const resourceList = data.map((m) => convertResponseModelResource(m));
store.dispatch(setResources(resourceList));
return resourceList;
},

async upload(file: File): Promise<Resource> {
const { name: filename, size } = file;

Expand All @@ -27,11 +32,15 @@ const resourceService = {
const formData = new FormData();
formData.append("file", file, filename);
const { data } = (await api.uploadFile(formData)).data;

return data;
const resource = convertResponseModelResource(data);
const resourceList = resourceService.getState().resources;
store.dispatch(setResources(resourceList.concat(resource)));
return resource;
},

async deleteResourceById(id: ResourceId) {
return api.deleteResourceById(id);
await api.deleteResourceById(id);
store.dispatch(deleteResource(id));
},

async patchResource(resourcePatch: ResourcePatch): Promise<Resource> {
Expand Down
10 changes: 9 additions & 1 deletion web/src/store/modules/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ const resourceSlice = createSlice({
}),
};
},
deleteResource: (state, action: PayloadAction<ResourceId>) => {
return {
...state,
resources: state.resources.filter((resource) => {
return resource.id !== action.payload;
}),
};
},
},
});

export const { setResources, patchResource } = resourceSlice.actions;
export const { setResources, patchResource, deleteResource } = resourceSlice.actions;

export default resourceSlice.reducer;