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

imprv: Show modal when you delete plugin #7875

Merged
merged 28 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d5bb417
125483 create delete plugin modal
soumaeda Jul 12, 2023
050acf5
125483 show delete modal
soumaeda Jul 12, 2023
b064e96
125483 add onCancelDeletePlugin
soumaeda Jul 13, 2023
789aa44
125487 apply i18n
soumaeda Jul 13, 2023
d1dc3f5
125483 rename DeletePlugin to PluginDelete
soumaeda Jul 13, 2023
6cd6c7b
125487 remove cancel button
soumaeda Jul 13, 2023
5f4b1ee
125483 add visible cypress
soumaeda Jul 14, 2023
e27d8db
125483 merge master
soumaeda Jul 14, 2023
65eedab
125483 add getByTestId to cypress
soumaeda Jul 14, 2023
6b8832c
125283
soumaeda Jul 14, 2023
191c5c9
125483 remove if from content
soumaeda Jul 18, 2023
03c158c
125483 remove a part of classname
soumaeda Jul 18, 2023
850d457
125483 use swr to show modal
soumaeda Aug 4, 2023
a53a1f1
125483 apply common.json to delete
soumaeda Aug 4, 2023
b9893f0
125483 apply common to plugin card
soumaeda Aug 4, 2023
1e95c16
125483 use mutate in modal
soumaeda Aug 6, 2023
f6a7818
125483 delete margin
soumaeda Aug 6, 2023
7eeeee2
125483 remove extra discription
soumaeda Aug 7, 2023
4c07d2e
Merge branch 'master' into imprv/111416-125483-show-modal-when-click-…
soumaeda Aug 7, 2023
8b047d1
125483 use icon-fire on plugin delete modal
soumaeda Aug 7, 2023
af7ef49
125483 remove mutate from PlaginCard and Extention
soumaeda Aug 7, 2023
b29a725
125483 use initialStatus
soumaeda Aug 7, 2023
00e4fad
125483 reduce extra margin
soumaeda Aug 7, 2023
a77bd37
125843 hand over directly
soumaeda Aug 8, 2023
a206c4f
125483 use promise
soumaeda Aug 8, 2023
bce0ad2
125483 do not use type assertion
soumaeda Aug 8, 2023
ce10162
125483 define as promise void
soumaeda Aug 8, 2023
1cc4b16
125483 add component PluginsExtensionPageContents
soumaeda Aug 21, 2023
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
2 changes: 1 addition & 1 deletion apps/app/public/static/locales/en_US/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@
"plugin_card": "Plugin Card",
"plugin_is_not_installed": "Plugin is not installed",
"install": "Install",
"delete": "Delete"
"confirm": "Delete plugin?"
},
"cloud_setting_management": {
"to_cloud_settings": "Open GROWI.cloud Settings"
Expand Down
2 changes: 1 addition & 1 deletion apps/app/public/static/locales/ja_JP/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@
"plugin_card": "プラグインカード",
"plugin_is_not_installed": "プラグインがインストールされていません",
"install": "インストール",
"delete": "削除"
"confirm": "プラグインを削除しますか?"
},
"cloud_setting_management": {
"to_cloud_settings": "GROWI.cloud の管理画面へ"
Expand Down
2 changes: 1 addition & 1 deletion apps/app/public/static/locales/zh_CN/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@
"plugin_card": "Plugin Card",
"plugin_is_not_installed": "Plugin is not installed",
"install": "Install",
"delete": "Delete"
"confirm": "Delete plugin?"
},
"cloud_setting_management": {
"to_cloud_settings": "進入 GROWI.cloud 的管理界面"
Expand Down
jam411 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useState } from 'react';
import { useTranslation } from 'next-i18next';
import Link from 'next/link';

import { apiv3Delete, apiv3Put } from '~/client/util/apiv3-client';
import { apiv3Put } from '~/client/util/apiv3-client';
import { toastSuccess, toastError } from '~/client/util/toastr';

import styles from './PluginCard.module.scss';
Expand All @@ -13,14 +13,14 @@ type Props = {
name: string,
url: string,
isEnalbed: boolean,
mutate: () => void,
desc?: string,
onDelete: () => void,
}

export const PluginCard = (props: Props): JSX.Element => {

const {
id, name, url, isEnalbed, desc, mutate,
id, name, url, isEnalbed, desc,
} = props;

const { t } = useTranslation('admin');
Expand Down Expand Up @@ -70,30 +70,14 @@ export const PluginCard = (props: Props): JSX.Element => {

const PluginDeleteButton = (): JSX.Element => {

const onClickPluginDeleteBtnHandler = async() => {
const reqUrl = `/plugins/${id}/remove`;

try {
const res = await apiv3Delete(reqUrl);
const pluginName = res.data.pluginName;
toastSuccess(t('toaster.remove_plugin_success', { pluginName }));
}
catch (err) {
toastError(err);
}
finally {
mutate();
}
};

return (
<div className="">
<button
type="submit"
className="btn btn-primary"
onClick={() => onClickPluginDeleteBtnHandler()}
onClick={props.onDelete}
>
{t('plugins.delete')}
{t('Delete')}
</button>
</div>
);
Expand Down
jam411 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { useCallback } from 'react';

import { useTranslation } from 'next-i18next';
import Link from 'next/link';
import {
Button, Modal, ModalHeader, ModalBody, ModalFooter,
} from 'reactstrap';

import { apiv3Delete } from '~/client/util/apiv3-client';
import { toastSuccess, toastError } from '~/client/util/toastr';

import { useSWRxAdminPlugins, usePluginDeleteModal } from '../../../stores/admin-plugins';

export const PluginDeleteModal: React.FC = () => {

const { t } = useTranslation('admin');
const { mutate } = useSWRxAdminPlugins();
const { data: pluginDeleteModalData, close: closePluginDeleteModal } = usePluginDeleteModal();
const isOpen = pluginDeleteModalData?.isOpen;
const id = pluginDeleteModalData?.id;
const name = pluginDeleteModalData?.name;
const url = pluginDeleteModalData?.url;

const toggleHandler = useCallback(() => {
closePluginDeleteModal();
}, [closePluginDeleteModal]);

const onClickDeleteButtonHandler = useCallback(async() => {
const reqUrl = `/plugins/${id}/remove`;

try {
const res = await apiv3Delete(reqUrl);
const pluginName = res.data.pluginName;
closePluginDeleteModal();
toastSuccess(t('toaster.remove_plugin_success', { pluginName }));
mutate();
}
catch (err) {
toastError(err);
}
}, [id, closePluginDeleteModal, t, mutate]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更にコード品質を上げるために、いくつか後続ストーリーを作ってください。

  • apiv3Delete をカスタムフック化する
    • client/services/page-operation.ts が参考になる
  • toast 表示および Modal の close を PluginsExtensionPageContents にやらせる
    • 責務の観点から


return (
<Modal isOpen={isOpen} toggle={toggleHandler}>
<ModalHeader tag="h4" toggle={toggleHandler} className="bg-danger text-light" name={name}>
<span>
<i className="icon-fw icon-fire"></i>
{t('plugins.confirm')}
</span>
</ModalHeader>
<ModalBody>
<div className="card well mt-2 p-2" key={id}>
<Link href={`${url}`} legacyBehavior>{name}</Link>
</div>
</ModalBody>
<ModalFooter>
<Button color="danger" onClick={onClickDeleteButtonHandler}>
<i className="icon-fw icon-fire"></i>
{t('Delete')}
</Button>
</ModalFooter>
</Modal>
);
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';

import { useTranslation } from 'next-i18next';
import dynamic from 'next/dynamic';
import { Spinner } from 'reactstrap';

import { useSWRxAdminPlugins } from '../../../stores/admin-plugins';
import { useSWRxAdminPlugins, usePluginDeleteModal } from '../../../stores/admin-plugins';

import { PluginCard } from './PluginCard';
import { PluginInstallerForm } from './PluginInstallerForm';
Expand All @@ -18,8 +19,10 @@ const Loading = (): JSX.Element => {

export const PluginsExtensionPageContents = (): JSX.Element => {
const { t } = useTranslation('admin');

const PluginDeleteModal = dynamic(() => import('./PluginDeleteModal')
.then(mod => mod.PluginDeleteModal), { ssr: false });
const { data, mutate } = useSWRxAdminPlugins();
const { open: openPluginDeleteModal } = usePluginDeleteModal();

return (
<div>
Expand All @@ -45,28 +48,22 @@ export const PluginsExtensionPageContents = (): JSX.Element => {
{ data.plugins.length === 0 && (
<div>{t('plugins.plugin_is_not_installed')}</div>
)}
{ data.plugins.map((plugin) => {
const pluginId = plugin._id;
const pluginName = plugin.meta.name;
const pluginUrl = plugin.origin.url;
const pluginIsEnabled = plugin.isEnabled;
const pluginDiscription = plugin.meta.desc;
return (
<PluginCard
key={pluginId}
id={pluginId}
name={pluginName}
url={pluginUrl}
isEnalbed={pluginIsEnabled}
desc={pluginDiscription}
mutate={mutate}
/>
);
})}
{data.plugins.map(plugin => (
<PluginCard
key={plugin._id}
id={plugin._id}
name={plugin.meta.name}
url={plugin.origin.url}
isEnalbed={plugin.isEnabled}
desc={plugin.meta.desc}
onDelete={() => openPluginDeleteModal(plugin)}
/>
))}
</div>
)}
</div>
</div>
<PluginDeleteModal />

</div>
);
Expand Down
47 changes: 47 additions & 0 deletions apps/app/src/features/growi-plugin/client/stores/admin-plugins.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import useSWR, { SWRResponse } from 'swr';

import { apiv3Get } from '~/client/util/apiv3-client';
import { useStaticSWR } from '~/stores/use-static-swr';

import type { IGrowiPluginHasId } from '../../interfaces';

Expand All @@ -22,3 +23,49 @@ export const useSWRxAdminPlugins = (): SWRResponse<Plugins, Error> => {
},
);
};

/*
* PluginDeleteModal
*/
type PluginDeleteModalStatus = {
isOpen: boolean,
id: string,
name: string,
url: string,
}

type PluginDeleteModalUtils = {
open(plugin: IGrowiPluginHasId): Promise<void>,
close(): Promise<void>,
}

export const usePluginDeleteModal = (): SWRResponse<PluginDeleteModalStatus, Error> & PluginDeleteModalUtils => {
const initialStatus: PluginDeleteModalStatus = {
isOpen: false,
id: '',
name: '',
url: '',
};

const swrResponse = useStaticSWR<PluginDeleteModalStatus, Error>('pluginDeleteModal', undefined, { fallbackData: initialStatus });
const { mutate } = swrResponse;

const open = async(plugin) => {
mutate({
isOpen: true,
id: plugin._id,
name: plugin.meta.name,
url: plugin.origin.url,
});
};

const close = async() => {
mutate(initialStatus);
};

return {
...swrResponse,
open,
close,
};
};