PluginInstallerForm.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useCallback } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { apiv3Post } from '~/client/util/apiv3-client';
  4. import { toastSuccess, toastError } from '~/client/util/toastr';
  5. import { useSWRxPlugins } from '~/stores/plugin';
  6. export const PluginInstallerForm = (): JSX.Element => {
  7. const { mutate } = useSWRxPlugins();
  8. const { t } = useTranslation('admin');
  9. const submitHandler = useCallback(async(e) => {
  10. e.preventDefault();
  11. const formData = e.target.elements;
  12. const {
  13. 'pluginInstallerForm[url]': { value: url },
  14. // 'pluginInstallerForm[ghBranch]': { value: ghBranch },
  15. // 'pluginInstallerForm[ghTag]': { value: ghTag },
  16. } = formData;
  17. const pluginInstallerForm = {
  18. url,
  19. // ghBranch,
  20. // ghTag,
  21. };
  22. try {
  23. const res = await apiv3Post('/plugins', { pluginInstallerForm });
  24. const pluginName = res.data.pluginName;
  25. toastSuccess(t('toaster.install_plugin_success', { pluginName }));
  26. }
  27. catch (e) {
  28. toastError(e);
  29. }
  30. finally {
  31. mutate();
  32. }
  33. }, [mutate, t]);
  34. return (
  35. <form role="form" onSubmit={submitHandler}>
  36. <div className='form-group row'>
  37. <label className="text-left text-md-right col-md-3 col-form-label">{t('plugins.repository_url')}</label>
  38. <div className="col-md-6">
  39. <input
  40. className="form-control"
  41. type="text"
  42. name="pluginInstallerForm[url]"
  43. placeholder="https://github.com/growi/plugins"
  44. required
  45. />
  46. <p className="form-text text-muted">{t('plugins.description')}</p>
  47. </div>
  48. </div>
  49. <div className="row my-3">
  50. <div className="mx-auto">
  51. <button type="submit" className="btn btn-primary">{t('plugins.install')}</button>
  52. </div>
  53. </div>
  54. </form>
  55. );
  56. };