From 08a3f33d854d0eb716ae03b13ab65b2e15a55037 Mon Sep 17 00:00:00 2001 From: Franxois Date: Fri, 7 Jun 2019 17:15:08 +0200 Subject: [PATCH] fix(share): Fix sharers list refresh when extend share --- .../integration/1-add-new-note_spec.js | 6 ----- client/src/actions/notes.js | 20 ++++++++++++++++ client/src/components/ShareNote.js | 4 +--- client/src/constants/notes.js | 2 ++ client/src/reducers/notes.js | 2 ++ client/src/services/notes.js | 24 ++++++++++--------- 6 files changed, 38 insertions(+), 20 deletions(-) diff --git a/client/cypress/integration/1-add-new-note_spec.js b/client/cypress/integration/1-add-new-note_spec.js index 95b1e6d..699915b 100644 --- a/client/cypress/integration/1-add-new-note_spec.js +++ b/client/cypress/integration/1-add-new-note_spec.js @@ -186,12 +186,6 @@ describe(`Notes sharing ${seed}`, function() { cy.contains("Save").click(); cy.contains("Save").should("not.exist"); - // Force refresh, status of note should change. - // TODO : remove the need to refresh - cy.wait(2000); - cy.get('[data-test="refresh"]').click(); - cy.wait(2000); - // Click on shared button cy.contains("div.panel-body", encryptedSharedContent, { timeout: 20000 }) .parentsUntil("li") diff --git a/client/src/actions/notes.js b/client/src/actions/notes.js index 66236c3..cfb6ff6 100644 --- a/client/src/actions/notes.js +++ b/client/src/actions/notes.js @@ -19,6 +19,25 @@ function addNote(Title, Content, sharedIds) { }; } +function shareNote(note, sharers) { + return async dispatch => { + try { + await notesService.shareNote(note, sharers); + const newNote = await notesService.getNote(note.ID); + dispatch(success(newNote)); + } catch (error) { + dispatch(failure(error)); + } + dispatch(uiActions.closeModal(uiConstants.ShareNoteModal)); + }; + function success(note) { + return { type: notesConstants.SHARE_SUCCESS, note }; + } + function failure(error) { + return { type: notesConstants.SHARE_FAILURE, error }; + } +} + function deleteNote(id) { return async (dispatch, getState) => { let del = { @@ -108,6 +127,7 @@ function getNotes(group) { export const noteActions = { addNote, + shareNote, deleteNote, getNotes }; diff --git a/client/src/components/ShareNote.js b/client/src/components/ShareNote.js index b9a177b..ed52403 100644 --- a/client/src/components/ShareNote.js +++ b/client/src/components/ShareNote.js @@ -5,7 +5,6 @@ import ShareSelect from "./ShareSelect"; import { uiConstants } from "../constants"; import { noteActions, uiActions } from "../actions"; -import { notesService } from "../services"; class ShareNote extends React.Component { constructor(props, context) { @@ -86,8 +85,7 @@ class ShareNote extends React.Component { const { payload: { note } } = this.props; - await notesService.shareNote(note, this.state.sharingList); - this.props.closeModal(uiConstants.ShareNoteModal); + this.props.shareNote(note, this.state.sharingList); } catch (e) { console.log(e); } diff --git a/client/src/constants/notes.js b/client/src/constants/notes.js index ca4fd39..b26996f 100644 --- a/client/src/constants/notes.js +++ b/client/src/constants/notes.js @@ -7,6 +7,8 @@ export const notesConstants = { POST_REQUEST: "NOTES_POST_REQUEST", POST_SUCCESS: "NOTES_POST_SUCCESS", POST_FAILURE: "NOTES_POST_FAILURE", + SHARE_SUCCESS: "NOTES_SHARE_SUCCESS", + SHARE_FAILURE: "NOTES_SHARE_FAILURE", DEL_FAILURE: "NOTES_DEL_FAILURE", GET_USERS_LIST: "NOTES_GET_USERS" }; diff --git a/client/src/reducers/notes.js b/client/src/reducers/notes.js index e394280..3163586 100644 --- a/client/src/reducers/notes.js +++ b/client/src/reducers/notes.js @@ -4,6 +4,8 @@ export const notes = (state = [], action) => { switch (action.type) { case notesConstants.POST_SUCCESS: return [...state, action.note]; + case notesConstants.SHARE_SUCCESS: + return state.map(n => (n.ID === action.note.ID ? action.note : n)); case notesConstants.DELETE_NOTE: return state.map(note => note.ID === action.id diff --git a/client/src/services/notes.js b/client/src/services/notes.js index ab69214..3ea873e 100644 --- a/client/src/services/notes.js +++ b/client/src/services/notes.js @@ -108,17 +108,19 @@ export async function shareNote(note, sharingList) { note.resourceID, sharingList.map(u => getLogin(u, process.env.REACT_APP_DATAPEPS_APP_ID)) ); - await sharingList.map(u => { - const requestOptions = { - method: "POST", - headers: authHeader(false) - }; - - return fetch( - `${process.env.REACT_APP_API_URL}/auth/share/${note.ID}/${u}`, - requestOptions - ); - }); + await Promise.all( + sharingList.map(async u => { + const requestOptions = { + method: "POST", + headers: authHeader(false) + }; + + return await fetch( + `${process.env.REACT_APP_API_URL}/auth/share/${note.ID}/${u}`, + requestOptions + ); + }) + ); } async function handleNotesResponse(response) {