Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement updateViewState Mutation for Interactive ViewState Rearrangement #31

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/src/__generated__/resolvers-types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions backend/src/resolvers/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,61 @@ const Query: QueryResolvers = {
};

const Mutation: MutationResolvers = {
updateViewState: async (
parent,
{ input: { id, positionIndex } },
{ db, dataLoaderContext, EXPECTED_OPTIONS_KEY }
) => {
const result = await db.sequelize.transaction(async (transaction) => {
const container = await db.BoardContainer.findByPk(Number(id.replace('container-', '')), { transaction });

if (!container) throw Error('Container not found');

const board = await db.Board.findByPk(Number(container.boardId), {
include: [
{
model: db.BoardContainer,
as: 'containers',
include: [
{
model: db.ContainerItem,
as: 'items',
include: [
{
model: db.Issue,
as: 'issue',
},
],
},
],
},
],
// [EXPECTED_OPTIONS_KEY]: dataLoaderContext,
transaction,
});

board.version += 1;

await board.save({ transaction });

const sortedItems = arrayMoveImmutable(
board.containers,
board.containers.findIndex((boardContainer) => boardContainer.id === container.id),
positionIndex
);

for (let i = 0; i < sortedItems.length; i++) {
sortedItems[i].position = i;
}

await Promise.all(sortedItems.map((item) => item.save({ transaction })));
await board.reload({ transaction });

return board;
});

return buildViewState(result);
},
createViewState: async (
parent,
{ input: { boardId, positionIndex, title } },
Expand Down
6 changes: 6 additions & 0 deletions backend/src/type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ const typeDefs = gql`
title: String
}

input UpdateViewStateInput {
id: ID!
positionIndex: Int
}

# Mutations
type Mutation {
createProject(input: CreateProjectInput): Project
Expand Down Expand Up @@ -434,6 +439,7 @@ const typeDefs = gql`
createViewState(input: CreateViewStateInput!): [ViewState]
addItemToViewState(input: AddItemToViewStateItemInput!): [ViewState]
removeItemFromViewState(input: RemoveItemFromViewStateItemInput!): [ViewState]
updateViewState(input: UpdateViewStateInput!): [ViewState]
}

# Queries
Expand Down
18 changes: 17 additions & 1 deletion frontend/components/KanbanBoard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
GET_ME,
GET_PROJECT_INFO,
UPDATE_ISSUE_MUTATION,
UPDATE_VIEW_STATE_MUTATION,
} from '@/gql/gql-queries-mutations';
import useWsAuthenticatedSocket from '@/hooks/useWsAuthenticatedSocket';
import { getDomainName } from '@/services/utils';
Expand Down Expand Up @@ -104,6 +105,7 @@ export default function KanbanBoard({
const [updateIssue] = useMutation(UPDATE_ISSUE_MUTATION);
const [addIssueStatus] = useMutation(CREATE_ISSUE_STATUS_MUTATION);
const [addItemToViewState] = useMutation(ADD_ITEM_TO_VIEW_STATE);
const [updateViewState] = useMutation(UPDATE_VIEW_STATE_MUTATION);

const getMe = useQuery(GET_ME);
const getProjectInfo = useQuery(GET_PROJECT_INFO, {
Expand Down Expand Up @@ -485,7 +487,21 @@ export default function KanbanBoard({
containers: newItems,
};
});
// console.log({ newItems, containers });

updateViewState({
onCompleted: (data) => {
setPageState((prevState) => ({
...prevState,
boardVersion: prevState.boardVersion + 1,
}));
},
variables: {
input: {
id: containers[activeContainerIndex].id,
positionIndex: overContainerIndex,
},
},
});
}

// Handling item moving to another container
Expand Down
11 changes: 11 additions & 0 deletions frontend/gql/__generated__/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions frontend/gql/gql-queries-mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,11 @@ export const ADD_ITEM_TO_VIEW_STATE = gql(/* GraphQL */ `
}
}
`);

export const UPDATE_VIEW_STATE_MUTATION = gql(/* GraphQL */ `
mutation UpdateViewState($input: UpdateViewStateInput!) {
updateViewState(input: $input) {
...ViewStateFields
}
}
`);