From notifications-return-50192-archive-asf-public=cust-asf.ponee.io@superset.apache.org Fri Sep 4 18:33:30 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mailroute1-lw-us.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with ESMTPS id 80EC818037A for ; Fri, 4 Sep 2020 20:33:30 +0200 (CEST) Received: from mail.apache.org (localhost [127.0.0.1]) by mailroute1-lw-us.apache.org (ASF Mail Server at mailroute1-lw-us.apache.org) with SMTP id B6564122178 for ; Fri, 4 Sep 2020 18:33:29 +0000 (UTC) Received: (qmail 80313 invoked by uid 500); 4 Sep 2020 18:33:29 -0000 Mailing-List: contact notifications-help@superset.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@superset.apache.org Delivered-To: mailing list notifications@superset.apache.org Received: (qmail 80303 invoked by uid 99); 4 Sep 2020 18:33:29 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Sep 2020 18:33:29 +0000 From: =?utf-8?q?GitBox?= To: notifications@superset.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bincubator-superset=5D_riahk_commented_on_a_change_?= =?utf-8?q?in_pull_request_=2310745=3A_feat=3A_add/edit_database_modal_form_?= =?utf-8?q?sections_UI?= Message-ID: <159924440937.32230.10621621434681457786.asfpy@gitbox.apache.org> Date: Fri, 04 Sep 2020 18:33:29 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit In-Reply-To: References: riahk commented on a change in pull request #10745: URL: https://github.com/apache/incubator-superset/pull/10745#discussion_r483787839 ########## File path: superset-frontend/src/views/CRUD/hooks.ts ########## @@ -168,6 +168,167 @@ export function useListViewResource( }; } +// In the same vein as above, a hook for viewing a single instance of a resource (given id) +interface SingleViewResourceState { + loading: boolean; + resource: D | null; + permissions: string[]; +} + +export function useSingleViewResource( + resourceName: string, + resourceLabel: string, // resourceLabel for translations + handleErrorMsg: (errorMsg: string) => void, +) { + const [state, setState] = useState>({ + loading: false, + resource: null, + permissions: [], + }); + + function updateState(update: Partial>) { + setState(currentState => ({ ...currentState, ...update })); + } + + useEffect(() => { + SupersetClient.get({ + endpoint: `/api/v1/${resourceName}/_info`, + }).then( + ({ json: infoJson = {} }) => { + updateState({ + permissions: infoJson.permissions, + }); + }, + createErrorHandler(errMsg => + handleErrorMsg( + t( + 'An error occurred while fetching %ss info: %s', + resourceLabel, + errMsg, + ), + ), + ), + ); + }, []); + + function hasPerm(perm: string) { + if (!state.permissions.length) { + return false; + } + + return Boolean(state.permissions.find(p => p === perm)); + } + + const fetchResource = useCallback((resourceID: number) => { + // Set loading state + updateState({ + loading: true, + }); + + return SupersetClient.get({ + endpoint: `/api/v1/${resourceName}/${resourceID}`, + }) + .then( + ({ json = {} }) => { + updateState({ + resource: json.result, + }); + }, + createErrorHandler(errMsg => + handleErrorMsg( + t( + 'An error occurred while fetching %ss: %s', + resourceLabel, + errMsg, + ), + ), + ), + ) + .finally(() => { + updateState({ loading: false }); + }); + }, []); + + const createResource = useCallback((resource: D) => { + // Set loading state + updateState({ + loading: true, + }); + + return SupersetClient.post({ + endpoint: `/api/v1/${resourceName}/`, + body: JSON.stringify(resource), + headers: { 'Content-Type': 'application/json' }, + }) + .then( + ({ json = {} }) => { + updateState({ + resource: json.result, + }); + }, + createErrorHandler(errMsg => + handleErrorMsg( + t( + 'An error occurred while fetching %ss: %s', + resourceLabel, + errMsg, + ), + ), + ), + ) + .finally(() => { + updateState({ loading: false }); + }); + }, []); + + const updateResource = useCallback((resourceID: number, resource: D) => { + // Set loading state + updateState({ + loading: true, + }); + + return SupersetClient.put({ + endpoint: `/api/v1/${resourceName}/${resourceID}`, + body: JSON.stringify(resource), + headers: { 'Content-Type': 'application/json' }, + }) + .then( + ({ json = {} }) => { + updateState({ + resource: json.result, + }); + }, + createErrorHandler(errMsg => + handleErrorMsg( + t( + 'An error occurred while fetching %ss: %s', + resourceLabel, + errMsg, + ), + ), + ), + ) + .finally(() => { + updateState({ loading: false }); + }); + }, []); + + return { + state: { + loading: state.loading, + resource: state.resource, + }, + setResource: (update: D) => + updateState({ + resource: update, + }), + hasPerm, Review comment: We have it for `useListViewResource`, so I assumed it was necessary to work properly, but I can remove it if it isn't! ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org For additional commands, e-mail: notifications-help@superset.apache.org