From 6de7367ea622f03e2aa77ad34dd6da26b05f7388 Mon Sep 17 00:00:00 2001 From: Virginie Ky Date: Wed, 18 Dec 2019 09:45:21 +0100 Subject: [PATCH] Webhook delete with popup warning --- .../src/components/ListRow/StyledListRow.js | 44 ++++++++++++ .../admin/src/components/ListRow/index.js | 68 ++++++++++++------- .../containers/Webhooks/ListView/Wrapper.js | 28 -------- .../src/containers/Webhooks/ListView/index.js | 31 ++++++--- .../containers/Webhooks/ListView/reducer.js | 8 +++ 5 files changed, 116 insertions(+), 63 deletions(-) create mode 100644 packages/strapi-admin/admin/src/components/ListRow/StyledListRow.js diff --git a/packages/strapi-admin/admin/src/components/ListRow/StyledListRow.js b/packages/strapi-admin/admin/src/components/ListRow/StyledListRow.js new file mode 100644 index 0000000000..189a6c8998 --- /dev/null +++ b/packages/strapi-admin/admin/src/components/ListRow/StyledListRow.js @@ -0,0 +1,44 @@ +/** + * + * StyledListRow + * + */ + +import styled from 'styled-components'; +import { CustomRow as Row } from '@buffetjs/styles'; +import { sizes } from 'strapi-helper-plugin'; + +const StyledListRow = styled(Row)` + td { + p { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + } + &:first-of-type { + padding-left: 30px; + width: 65px; + } + &:nth-of-type(2) { + max-width: 158px; + } + &:nth-of-type(3) { + max-width: 300px; + } + &:nth-of-type(4) { + min-width: 125px; + } + &:nth-of-type(5) { + .popup-wrapper { + width: 0; + } + } + @media (min-width: ${sizes.wide}) { + &:nth-of-type(3) { + max-width: 400px; + } + } + } +`; + +export default StyledListRow; diff --git a/packages/strapi-admin/admin/src/components/ListRow/index.js b/packages/strapi-admin/admin/src/components/ListRow/index.js index eb6071ce55..61304b45fc 100644 --- a/packages/strapi-admin/admin/src/components/ListRow/index.js +++ b/packages/strapi-admin/admin/src/components/ListRow/index.js @@ -4,12 +4,13 @@ * */ -import React from 'react'; +import React, { useState } from 'react'; import PropTypes from 'prop-types'; - -import { CustomRow as Row } from '@buffetjs/styles'; +import { PopUpWarning } from 'strapi-helper-plugin'; import { Checkbox, IconLinks } from '@buffetjs/core'; + import Switch from '../Switch'; +import StyledListRow from './StyledListRow'; function ListRow({ id, @@ -20,6 +21,8 @@ function ListRow({ onDeleteCLick, onEditClick, }) { + const [showModal, setShowModal] = useState(false); + const links = [ { icon: 'pencil', @@ -30,7 +33,7 @@ function ListRow({ { icon: 'trash', onClick: () => { - onDeleteCLick(id); + setShowModal(true); }, }, ]; @@ -39,28 +42,43 @@ function ListRow({ onEnabledChange(value, id); }; + const handleDeleteConfirm = () => { + onDeleteCLick(id); + setShowModal(false); + }; + return ( - - - {}} /> - - -

{name}

- - -

{url}

- - - - - - - -
+ <> + + + {}} /> + + +

{name}

+ + +

{url}

+ + + + + + +
+ setShowModal(!showModal)} + popUpWarningType="danger" + onConfirm={handleDeleteConfirm} + /> +
+ +
+ ); } diff --git a/packages/strapi-admin/admin/src/containers/Webhooks/ListView/Wrapper.js b/packages/strapi-admin/admin/src/containers/Webhooks/ListView/Wrapper.js index 219267c759..c3a959f0ce 100644 --- a/packages/strapi-admin/admin/src/containers/Webhooks/ListView/Wrapper.js +++ b/packages/strapi-admin/admin/src/containers/Webhooks/ListView/Wrapper.js @@ -5,7 +5,6 @@ */ import styled from 'styled-components'; -import { sizes } from 'strapi-helper-plugin'; const Wrapper = styled.div` > div:first-of-type { @@ -21,33 +20,6 @@ const Wrapper = styled.div` border-bottom-left-radius: 0; border-bottom-right-radius: 0; } - table tr { - td { - p { - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; - } - &:first-of-type { - padding-left: 30px; - width: 65px; - } - &:nth-of-type(2) { - max-width: 158px; - } - &:nth-of-type(3) { - max-width: 300px; - } - &:nth-of-type(4) { - min-width: 125px; - } - @media (min-width: ${sizes.wide}) { - &:nth-of-type(3) { - max-width: 400px; - } - } - } - } } p { margin-bottom: 0; diff --git a/packages/strapi-admin/admin/src/containers/Webhooks/ListView/index.js b/packages/strapi-admin/admin/src/containers/Webhooks/ListView/index.js index 5ef833db18..ee778da2a1 100644 --- a/packages/strapi-admin/admin/src/containers/Webhooks/ListView/index.js +++ b/packages/strapi-admin/admin/src/containers/Webhooks/ListView/index.js @@ -29,9 +29,6 @@ function ListView() { try { const { data } = await request(`/admin/webhooks`, { method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, }); dispatch({ @@ -48,6 +45,9 @@ function ListView() { fetchData(); }, []); + const webhookIndex = id => webhooks.findIndex(webhook => webhook.id === id); + + // New button const addBtnLabel = formatMessage({ id: `Settings.webhooks.list.button.add`, }); @@ -108,14 +108,28 @@ function ListView() { }; const handleDeleteClick = id => { - console.log(id); + deleteWebhook(id); }; - const webhookById = id => webhooks.find(webhook => webhook.id === id); - const webhookIndex = id => webhooks.findIndex(webhook => webhook.id === id); + const deleteWebhook = async id => { + try { + await request(`/admin/webhooks/${id}`, { + method: 'DELETE', + }); + + dispatch({ + type: 'WEBHOOK_DELETED', + index: webhookIndex(id), + }); + } catch (err) { + if (err.code !== 20) { + strapi.notification.error('notification.error'); + } + } + }; const handleEnabledChange = async (value, id) => { - const initialWebhookProps = webhookById(id); + const initialWebhookProps = webhooks[webhookIndex]; const body = { ...initialWebhookProps, isEnabled: value, @@ -124,9 +138,6 @@ function ListView() { try { await request(`/admin/webhooks/${id}`, { method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, body, }); diff --git a/packages/strapi-admin/admin/src/containers/Webhooks/ListView/reducer.js b/packages/strapi-admin/admin/src/containers/Webhooks/ListView/reducer.js index 6f6c7930e4..3161d23590 100644 --- a/packages/strapi-admin/admin/src/containers/Webhooks/ListView/reducer.js +++ b/packages/strapi-admin/admin/src/containers/Webhooks/ListView/reducer.js @@ -10,6 +10,14 @@ const reducer = (state, action) => { return state.update('webhooks', () => fromJS(action.data)); case 'SET_WEBHOOK_ENABLED': return state.updateIn(['webhooks', ...action.keys], () => action.value); + case 'WEBHOOK_DELETED': { + console.log(state.get('webhooks')); + console.log(action.index); + + return state.update('webhooks', webhooks => + webhooks.splice(action.index, 1) + ); + } default: return state; }