mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-21 23:43:15 +01:00
feat: sync workspaces with idb
This commit is contained in:
parent
008704c4e1
commit
819e8c2ccd
@ -2,9 +2,10 @@ import React, { useEffect, useRef } from 'react';
|
|||||||
import Portal from "components/Portal/index";
|
import Portal from "components/Portal/index";
|
||||||
import Modal from "components/Modal/index";
|
import Modal from "components/Modal/index";
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import { addWorkspace } from 'providers/ReduxStore/slices/workspaces';
|
import { addWorkspace } from 'providers/ReduxStore/slices/workspaces/actions';
|
||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
const AddWorkspace = ({onClose}) => {
|
const AddWorkspace = ({onClose}) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -21,8 +22,12 @@ const AddWorkspace = ({onClose}) => {
|
|||||||
.required('name is required')
|
.required('name is required')
|
||||||
}),
|
}),
|
||||||
onSubmit: (values) => {
|
onSubmit: (values) => {
|
||||||
dispatch(addWorkspace({name: values.name}));
|
dispatch(addWorkspace(values.name))
|
||||||
onClose();
|
.then(() => {
|
||||||
|
toast.success("Workspace created!");
|
||||||
|
onClose();
|
||||||
|
})
|
||||||
|
.catch(() => toast.error("An error occured while creating the workspace"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,15 +1,20 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Portal from "components/Portal/index";
|
import Portal from "components/Portal/index";
|
||||||
import Modal from "components/Modal/index";
|
import Modal from "components/Modal/index";
|
||||||
import { deleteWorkspace } from 'providers/ReduxStore/slices/workspaces';
|
import { deleteWorkspace } from 'providers/ReduxStore/slices/workspaces/actions';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
|
|
||||||
const DeleteWorkspace = ({onClose, workspace}) => {
|
const DeleteWorkspace = ({onClose, workspace}) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const onConfirm = () =>{
|
const onConfirm = () =>{
|
||||||
dispatch(deleteWorkspace({workspaceUid: workspace.uid}))
|
dispatch(deleteWorkspace(workspace.uid))
|
||||||
onClose();
|
.then(() => {
|
||||||
|
toast.success("Workspace deleted!");
|
||||||
|
onClose();
|
||||||
|
})
|
||||||
|
.catch(() => toast.error("An error occured while deleting the workspace"));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -2,9 +2,10 @@ import React, { useEffect, useRef } from 'react';
|
|||||||
import Portal from "components/Portal/index";
|
import Portal from "components/Portal/index";
|
||||||
import Modal from "components/Modal/index";
|
import Modal from "components/Modal/index";
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import { renameWorkspace } from 'providers/ReduxStore/slices/workspaces';
|
import { renameWorkspace } from 'providers/ReduxStore/slices/workspaces/actions';
|
||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
const EditWorkspace = ({onClose, workspace}) => {
|
const EditWorkspace = ({onClose, workspace}) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -21,8 +22,12 @@ const EditWorkspace = ({onClose, workspace}) => {
|
|||||||
.required('name is required')
|
.required('name is required')
|
||||||
}),
|
}),
|
||||||
onSubmit: (values) => {
|
onSubmit: (values) => {
|
||||||
dispatch(renameWorkspace({name: values.name, uid: workspace.uid}));
|
dispatch(renameWorkspace(values.name, workspace.uid))
|
||||||
onClose();
|
.then(() => {
|
||||||
|
toast.success("Workspace renamed!");
|
||||||
|
onClose();
|
||||||
|
})
|
||||||
|
.catch(() => toast.error("An error occured while renaming the workspace"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,15 +1,103 @@
|
|||||||
import { getWorkspacesFromIdb } from 'utils/idb/workspaces';
|
import find from 'lodash/find';
|
||||||
|
import { uuid } from 'utils/common';
|
||||||
|
import { getWorkspacesFromIdb, saveWorkspaceToIdb, deleteWorkspaceInIdb } from 'utils/idb/workspaces';
|
||||||
import {
|
import {
|
||||||
loadWorkspaces
|
loadWorkspaces,
|
||||||
|
addWorkspace as _addWorkspace,
|
||||||
|
renameWorkspace as _renameWorkspace,
|
||||||
|
deleteWorkspace as _deleteWorkspace
|
||||||
} from './index';
|
} from './index';
|
||||||
|
|
||||||
|
const seedWorkpace = () => {
|
||||||
|
const uid = uuid();
|
||||||
|
const workspace = {
|
||||||
|
uid: uid,
|
||||||
|
name: 'My workspace'
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
saveWorkspaceToIdb(window.__idb, workspace)
|
||||||
|
.then(() => resolve([workspace]))
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const loadWorkspacesFromIdb = () => (dispatch) => {
|
export const loadWorkspacesFromIdb = () => (dispatch) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
getWorkspacesFromIdb(window.__idb)
|
getWorkspacesFromIdb(window.__idb)
|
||||||
|
.then((workspaces) => {
|
||||||
|
if(!workspaces || !workspaces.length) {
|
||||||
|
return seedWorkpace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return workspaces;
|
||||||
|
})
|
||||||
.then((workspaces) => dispatch(loadWorkspaces({
|
.then((workspaces) => dispatch(loadWorkspaces({
|
||||||
workspaces: workspaces
|
workspaces: workspaces
|
||||||
})))
|
})))
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const addWorkspace = (workspaceName) => (dispatch) => {
|
||||||
|
const newWorkspace = {
|
||||||
|
uid: uuid(),
|
||||||
|
name: workspaceName
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
saveWorkspaceToIdb(window.__idb, newWorkspace)
|
||||||
|
.then(() => dispatch(_addWorkspace({
|
||||||
|
workspace: newWorkspace
|
||||||
|
})))
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const renameWorkspace = (newName, uid) => (dispatch, getState) => {
|
||||||
|
const state = getState();
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const workspace = find(state.workspaces.workspaces, (w) => w.uid === uid);
|
||||||
|
|
||||||
|
if(!workspace) {
|
||||||
|
return reject(new Error('Workspace not found'));
|
||||||
|
}
|
||||||
|
|
||||||
|
saveWorkspaceToIdb(window.__idb, {
|
||||||
|
uid: workspace.uid,
|
||||||
|
name: newName,
|
||||||
|
})
|
||||||
|
.then(() => dispatch(_renameWorkspace({
|
||||||
|
uid: uid,
|
||||||
|
name: newName
|
||||||
|
})))
|
||||||
|
.then(resolve)
|
||||||
|
.catch((err) => console.log(err));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteWorkspace = (workspaceUid) => (dispatch, getState) => {
|
||||||
|
const state = getState();
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if(state.workspaces.activeWorkspaceUid === workspaceUid) {
|
||||||
|
throw new Error("User cannot delete current workspace");
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspace = find(state.workspaces.workspaces, (w) => w.uid === workspaceUid);
|
||||||
|
|
||||||
|
if(!workspace) {
|
||||||
|
return reject(new Error('Workspace not found'));
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteWorkspaceInIdb(window.__idb, workspaceUid)
|
||||||
|
.then(() => dispatch(_deleteWorkspace({
|
||||||
|
workspaceUid: workspaceUid
|
||||||
|
})))
|
||||||
|
.then(resolve)
|
||||||
|
.catch((err) => console.log(err));
|
||||||
|
});
|
||||||
};
|
};
|
@ -1,6 +1,6 @@
|
|||||||
import { createSlice } from '@reduxjs/toolkit';
|
import { createSlice } from '@reduxjs/toolkit';
|
||||||
import { each } from 'lodash';
|
import each from 'lodash/each';
|
||||||
import { uuid } from 'utils/common';
|
import find from 'lodash/find';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
workspaces: [],
|
workspaces: [],
|
||||||
@ -12,28 +12,22 @@ export const workspacesSlice = createSlice({
|
|||||||
initialState,
|
initialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
loadWorkspaces: (state, action) => {
|
loadWorkspaces: (state, action) => {
|
||||||
const workspaces = action.payload.workspaces;
|
state.workspaces = action.payload.workspaces;
|
||||||
|
|
||||||
if(!workspaces || !workspaces.length) {
|
if(state.workspaces && state.workspaces.length) {
|
||||||
const uid = uuid();
|
state.activeWorkspaceUid = state.workspaces[0].uid;
|
||||||
state.workspaces.push({
|
|
||||||
uid: uid,
|
|
||||||
name: 'My workspace'
|
|
||||||
});
|
|
||||||
state.activeWorkspaceUid = uid;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
each(workspaces, w => state.workspaces.push(w));
|
|
||||||
},
|
},
|
||||||
selectWorkspace: (state, action) => {
|
selectWorkspace: (state, action) => {
|
||||||
state.activeWorkspaceUid = action.payload.uid;
|
state.activeWorkspaceUid = action.payload.uid;
|
||||||
},
|
},
|
||||||
renameWorkspace: (state, action) => {
|
renameWorkspace: (state, action) => {
|
||||||
const { name, uid } = action.payload;
|
const { name, uid } = action.payload;
|
||||||
const { workspaces } = state;
|
const workspace = find(state.workspaces, (w) => w.uid === uid);
|
||||||
const workspaceIndex = workspaces.findIndex(workspace => workspace.uid == uid);
|
|
||||||
workspaces[workspaceIndex].name = name;
|
if(workspace) {
|
||||||
|
workspace.name = name;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
deleteWorkspace: (state, action) => {
|
deleteWorkspace: (state, action) => {
|
||||||
if(state.activeWorkspaceUid === action.payload.workspaceUid) {
|
if(state.activeWorkspaceUid === action.payload.workspaceUid) {
|
||||||
@ -42,11 +36,7 @@ export const workspacesSlice = createSlice({
|
|||||||
state.workspaces = state.workspaces.filter((workspace) => workspace.uid !== action.payload.workspaceUid);
|
state.workspaces = state.workspaces.filter((workspace) => workspace.uid !== action.payload.workspaceUid);
|
||||||
},
|
},
|
||||||
addWorkspace: (state, action) => {
|
addWorkspace: (state, action) => {
|
||||||
const newWorkspace = {
|
state.workspaces.push(action.payload.workspace);
|
||||||
uid: uuid(),
|
|
||||||
name: action.payload.name
|
|
||||||
}
|
|
||||||
state.workspaces.push(newWorkspace);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -15,8 +15,12 @@ export const saveWorkspaceToIdb = (connection, workspace) => {
|
|||||||
workspaceStore.put(workspace);
|
workspaceStore.put(workspace);
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve();
|
return new Promise((res, rej) => {
|
||||||
|
tx.oncomplete = () => res();
|
||||||
|
tx.onerror = () => rej(tx.error);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
.then(resolve)
|
||||||
.catch((err) => reject(err));
|
.catch((err) => reject(err));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user