Skip to content

Commit

Permalink
Implemented action to rename project
Browse files Browse the repository at this point in the history
  • Loading branch information
stever committed Jun 28, 2023
1 parent 855e474 commit 76d81ed
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
8 changes: 4 additions & 4 deletions apps/web/src/components/ProjectEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function ProjectEditor() {
const renameInputReference = useRef(null);

const [renameDialogVisible, setRenameDialogVisible] = useState(false);
const [newProjectName, setNewProjectName] = useState(undefined);
const [newProjectName, setNewProjectName] = useState('');

const lang = useSelector(state => state?.project.lang);
const code = useSelector(state => state?.project.code);
Expand Down Expand Up @@ -139,7 +139,7 @@ export function ProjectEditor() {
label="Cancel"
icon="pi pi-times"
onClick={() => {
setNewProjectName(undefined);
setNewProjectName('');
setRenameDialogVisible(false);
}}
className="p-button-text"
Expand All @@ -149,7 +149,7 @@ export function ProjectEditor() {
icon="pi pi-check"
onClick={() => {
dispatch(renameProject(newProjectName));
setNewProjectName(undefined);
setNewProjectName('');
setRenameDialogVisible(false);
}}
autoFocus
Expand All @@ -168,7 +168,7 @@ export function ProjectEditor() {
onKeyDown={(e) => {
if (e.key === 'Enter') {
dispatch(renameProject(newProjectName));
setNewProjectName(undefined);
setNewProjectName('');
setRenameDialogVisible(false);
}
}}
Expand Down
11 changes: 9 additions & 2 deletions apps/web/src/redux/project/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const actionTypes = {
saveCodeChanges: 'project/saveCodeChanges',
deleteProject: 'project/deleteProject',
renameProject: 'project/renameProject',
setProjectTitle: 'project/setProjectTitle',
setErrorItems: 'project/setErrorItems',
};

Expand Down Expand Up @@ -54,8 +55,14 @@ export const deleteProject = () => ({
type: actionTypes.deleteProject
});

export const renameProject = () => ({
type: actionTypes.renameProject
export const renameProject = (title) => ({
type: actionTypes.renameProject,
title
});

export const setProjectTitle = (title) => ({
type: actionTypes.setProjectTitle,
title
});

export const setErrorItems = (errorItems) => ({
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/redux/project/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ function setErrorItems(state, action) {
};
}

function setProjectTitle(state, action) {
return {
...state,
title: action.title,
};
}

// -----------------------------------------------------------------------------
// Reducer
// -----------------------------------------------------------------------------
Expand All @@ -83,6 +90,7 @@ const actionsMap = {
[actionTypes.setCode]: setCode,
[actionTypes.setSavedCode]: setSavedCode,
[actionTypes.setErrorItems]: setErrorItems,
[actionTypes.setProjectTitle]: setProjectTitle,
};

export default function reducer(state = initialState, action) {
Expand Down
37 changes: 36 additions & 1 deletion apps/web/src/redux/project/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {takeLatest, put, select, call} from "redux-saga/effects";
import gql from "graphql-tag";
import {history} from "../store";
import {gqlFetch} from "../../graphql_fetch";
import {actionTypes, reset, receiveLoadedProject, setSavedCode, setSelectedTabIndex} from "./actions";
import {actionTypes, reset, receiveLoadedProject, setSavedCode, setSelectedTabIndex, setProjectTitle} from "./actions";
import {pause, reset as resetMachine} from "../jsspeccy/actions";
import {handleException} from "../../errors";

Expand Down Expand Up @@ -35,6 +35,11 @@ export function* watchForDeleteProjectActions() {
yield takeLatest(actionTypes.deleteProject, handleDeleteProjectActions);
}

// noinspection JSUnusedGlobalSymbols
export function* watchForRenameProjectActions() {
yield takeLatest(actionTypes.renameProject, handleRenameProjectActions);
}

// -----------------------------------------------------------------------------
// Action handlers
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -180,3 +185,33 @@ function* handleDeleteProjectActions(_) {
handleException(e);
}
}

function* handleRenameProjectActions(action) {
try {
const userId = yield select((state) => state.identity.userId);
const projectId = yield select((state) => state.project.id);

const query = gql`
mutation ($project_id: uuid!, $title: String!) {
update_project_by_pk(pk_columns: {project_id: $project_id}, _set: {title: $title}) {
project_id
}
}
`;

const variables = {
'project_id': projectId,
'title': action.title
};

// noinspection JSCheckFunctionSignatures
const response = yield call(gqlFetch, userId, query, variables);

// noinspection JSUnresolvedVariable
console.assert(response?.data?.update_project_by_pk?.project_id, response);

yield put(setProjectTitle(action.title));
} catch (e) {
handleException(e);
}
}

0 comments on commit 76d81ed

Please sign in to comment.