Skip to content

Commit

Permalink
deploy: eef0b83
Browse files Browse the repository at this point in the history
  • Loading branch information
edvincandon committed Feb 10, 2024
1 parent d0f5522 commit 67c0722
Show file tree
Hide file tree
Showing 8 changed files with 4,379 additions and 2,808 deletions.
7,127 changes: 4,341 additions & 2,786 deletions dist/index.js

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions lib/components/todo/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { useSelector } from 'react-redux';

import type { TransitionAction } from '~transitions';

import cloneDeep from 'lodash/cloneDeep';
import { CheckMark, Cross, Spinner } from '~usecases/lib/components/todo/Icons';
import type { OptimisticActions } from '~usecases/lib/store/actions';
import { createTodo, editTodo } from '~usecases/lib/store/actions';
import { useTodoState } from '~usecases/lib/store/hooks';
import { selectTodo } from '~usecases/lib/store/selectors';
import type { Todo } from '~usecases/lib/store/types';

type Props = {
todo: Todo;
onRetry: (action: TransitionAction) => void;
onRetry: (action: TransitionAction<OptimisticActions>) => void;
onEdit: (todo: Todo) => void;
onDelete: (todo: Todo) => void;
};
Expand All @@ -21,9 +24,9 @@ const TodoConflict: FC<{ id: string }> = ({ id }) => {
const Tag: keyof JSX.IntrinsicElements = todo.done ? 's' : 'em';

return (
<span className="text-xs text-red-400">
<div className="text-[9px] text-red-300">
Conflict : "<Tag>{todo.value}</Tag>"
</span>
</div>
);
};

Expand All @@ -37,15 +40,17 @@ export const TodoItem: FC<Props> = ({ todo, onEdit, onRetry, onDelete }) => {
if (loading) return;

if (failedAction) {
onRetry({
...failedAction,
payload: {
todo: {
...failedAction.payload.todo,
...mutation,
},
},
});
if (createTodo.stage.match(failedAction)) {
const create = cloneDeep(failedAction);
create.payload.todo = { ...create.payload.todo, ...mutation };
onRetry(create);
}

if (editTodo.stage.match(failedAction)) {
const edit = cloneDeep(failedAction);
edit.payload.todo = { ...edit.payload.todo, ...mutation };
onRetry(edit);
}
} else onEdit({ ...todo, revision: todo.revision + 1, ...mutation });
};

Expand Down
5 changes: 5 additions & 0 deletions lib/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export const createTodo = createTransitions('todos::add')(create);
export const editTodo = createTransitions('todos::edit')(edit);
export const deleteTodo = createTransitions('todos::delete', TransitionDedupeMode.TRAILING)(remove);

export type OptimisticActions =
| ReturnType<typeof createTodo.stage>
| ReturnType<typeof editTodo.stage>
| ReturnType<typeof deleteTodo.stage>;

export const sync = createAction('todos::sync');
4 changes: 2 additions & 2 deletions lib/store/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Middleware } from 'redux';
import { isAction, type Middleware } from 'redux';
import { isTransition, type TransitionAction } from '~transitions';

type TransitionEvent = CustomEvent<TransitionAction>;
Expand Down Expand Up @@ -31,7 +31,7 @@ export const createOptimistronMiddlware = (): [Middleware, TransitionEventBus] =

return [
() => (next) => (action) => {
if (isTransition(action)) eventBus.publish(action);
if (isAction(action) && isTransition(action)) eventBus.publish(action);
next(action);
},
eventBus,
Expand Down
8 changes: 7 additions & 1 deletion lib/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export const todos = optimistron(
/** Simulate items all being bumped to a new revision. If we
* have ongoing transitions, these should create conflicts */
return Object.fromEntries(
Object.entries(getState()).map(([key, todo]) => [key, { ...todo, revision: todo.revision + 10 }]),
Object.entries(getState()).map(([key, todo]) => [
key,
{
...todo,
revision: todo.revision + 10,
},
]),
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
selectIsOptimistic,
selectOptimistic,
} from '~selectors';
import { type State } from '~usecases/lib/store/store';
import type { State } from '~usecases/lib/store/store';

export const selectTodo = (id: string) =>
createSelector(
Expand Down
4 changes: 2 additions & 2 deletions thunks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { FC } from 'react';
import { useEffect } from 'react';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { thunk } from 'redux-thunk';

import { TransitionHistoryProvider } from '~usecases/lib/components/graph/TransitionHistoryProvider';
import { useMockApi } from '~usecases/lib/components/mocks/MockApiProvider';
import { createDebugStore } from '~usecases/lib/store/store';
import { App } from '~usecases/thunks/App';

export const { store, eventBus } = createDebugStore(thunk.withExtraArgument({}));
export const { store, eventBus } = createDebugStore(thunk);

const Usecase: FC = () => {
const mockApi = useMockApi();
Expand Down
8 changes: 4 additions & 4 deletions thunks/thunk.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AnyAction } from 'redux';
import type { Action } from 'redux';
import type { ThunkAction } from 'redux-thunk';

import { createTodo, deleteTodo, editTodo } from '~usecases/lib/store/actions';
import type { State } from '~usecases/lib/store/store';
import type { Todo } from '~usecases/lib/store/types';
import { generateId, simulateAPIRequest } from '~usecases/lib/utils/mock-api';

export const createTodoThunk = (todo: Todo): ThunkAction<void, State, undefined, AnyAction> => {
export const createTodoThunk = (todo: Todo): ThunkAction<void, State, undefined, Action> => {
return async (dispatch) => {
const transitionId = todo.id;

Expand All @@ -21,7 +21,7 @@ export const createTodoThunk = (todo: Todo): ThunkAction<void, State, undefined,
};
};

export const editTodoThunk = (id: string, update: Todo): ThunkAction<void, State, undefined, AnyAction> => {
export const editTodoThunk = (id: string, update: Todo): ThunkAction<void, State, undefined, Action> => {
return async (dispatch) => {
const transitionId = id;

Expand All @@ -35,7 +35,7 @@ export const editTodoThunk = (id: string, update: Todo): ThunkAction<void, State
};
};

export const deleteTodoTunk = (id: string): ThunkAction<void, State, undefined, AnyAction> => {
export const deleteTodoTunk = (id: string): ThunkAction<void, State, undefined, Action> => {
return async (dispatch) => {
const transitionId = id;

Expand Down

0 comments on commit 67c0722

Please sign in to comment.