Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
429780a
commit c70ec15
Showing
5 changed files
with
232 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,111 @@ | ||
import TaskList from "./TaskList"; | ||
import * as TaskStories from "./Task.stories"; | ||
|
||
import { Provider } from "react-redux"; | ||
|
||
import { configureStore, createSlice } from "@reduxjs/toolkit"; | ||
|
||
// A super-simple mock of the state of the store | ||
export const MockedState = { | ||
tasks: [ | ||
{ ...TaskStories.Default.args.task, id: "1", title: "Task 1" }, | ||
{ ...TaskStories.Default.args.task, id: "2", title: "Task 2" }, | ||
{ ...TaskStories.Default.args.task, id: "3", title: "Task 3" }, | ||
{ ...TaskStories.Default.args.task, id: "4", title: "Task 4" }, | ||
{ ...TaskStories.Default.args.task, id: "5", title: "Task 5" }, | ||
{ ...TaskStories.Default.args.task, id: "6", title: "Task 6" }, | ||
], | ||
status: "idle", | ||
error: null, | ||
}; | ||
|
||
// A super-simple mock of a redux store | ||
const Mockstore = ({ taskboxState, children }) => ( | ||
<Provider | ||
store={configureStore({ | ||
reducer: { | ||
taskbox: createSlice({ | ||
name: "taskbox", | ||
initialState: taskboxState, | ||
reducers: { | ||
updateTaskState: (state, action) => { | ||
const { id, newTaskState } = action.payload; | ||
const task = state.tasks.findIndex((task) => task.id === id); | ||
if (task >= 0) { | ||
state.tasks[task].state = newTaskState; | ||
} | ||
}, | ||
}, | ||
}).reducer, | ||
}, | ||
})} | ||
> | ||
{children} | ||
</Provider> | ||
); | ||
|
||
export default { | ||
component: TaskList, | ||
title: "TaskList", | ||
decorators: [(story) => <div style={{ padding: "3rem" }}>{story()}</div>], | ||
tags: ["autodocs"], | ||
decorators: [(story) => <div style={{ padding: "3rem" }}>{story()}</div>], | ||
excludeStories: /.*MockedState$/, | ||
}; | ||
|
||
export const Default = { | ||
args: { | ||
// Shaping the stories through args composition. | ||
// The data was inherited from the Default story in Task.stories.js. | ||
tasks: [ | ||
{ ...TaskStories.Default.args.task, id: "1", title: "Task 1" }, | ||
{ ...TaskStories.Default.args.task, id: "2", title: "Task 2" }, | ||
{ ...TaskStories.Default.args.task, id: "3", title: "Task 3" }, | ||
{ ...TaskStories.Default.args.task, id: "4", title: "Task 4" }, | ||
{ ...TaskStories.Default.args.task, id: "5", title: "Task 5" }, | ||
{ ...TaskStories.Default.args.task, id: "6", title: "Task 6" }, | ||
], | ||
}, | ||
decorators: [ | ||
(story) => <Mockstore taskboxState={MockedState}>{story()}</Mockstore>, | ||
], | ||
}; | ||
|
||
export const WithPinnedTasks = { | ||
args: { | ||
tasks: [ | ||
...Default.args.tasks.slice(0, 5), | ||
{ id: "6", title: "Task 6 (pinned)", state: "TASK_PINNED" }, | ||
], | ||
}, | ||
decorators: [ | ||
(story) => { | ||
const pinnedtasks = [ | ||
...MockedState.tasks.slice(0, 5), | ||
{ id: "6", title: "Task 6 (pinned)", state: "TASK_PINNED" }, | ||
]; | ||
|
||
return ( | ||
<Mockstore | ||
taskboxState={{ | ||
...MockedState, | ||
tasks: pinnedtasks, | ||
}} | ||
> | ||
{story()} | ||
</Mockstore> | ||
); | ||
}, | ||
], | ||
}; | ||
|
||
export const Loading = { | ||
args: { | ||
tasks: [], | ||
loading: true, | ||
}, | ||
decorators: [ | ||
(story) => ( | ||
<Mockstore | ||
taskboxState={{ | ||
...MockedState, | ||
status: "loading", | ||
}} | ||
> | ||
{story()} | ||
</Mockstore> | ||
), | ||
], | ||
}; | ||
|
||
export const Empty = { | ||
args: { | ||
// Shaping the stories through args composition. | ||
// Inherited data coming from the Loading story. | ||
...Loading.args, | ||
loading: false, | ||
}, | ||
decorators: [ | ||
(story) => ( | ||
<Mockstore | ||
taskboxState={{ | ||
...MockedState, | ||
tasks: [], | ||
}} | ||
> | ||
{story()} | ||
</Mockstore> | ||
), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* A simple redux store/actions/reducer implementation. | ||
* A true app would be more complex and separated into different files. | ||
*/ | ||
import { configureStore, createSlice } from "@reduxjs/toolkit"; | ||
|
||
/* | ||
* The initial state of our store when the app loads. | ||
* Usually, you would fetch this from a server. Let's not worry about that now | ||
*/ | ||
const defaultTasks = [ | ||
{ id: "1", title: "Something", state: "TASK_INBOX" }, | ||
{ id: "2", title: "Something more", state: "TASK_INBOX" }, | ||
{ id: "3", title: "Something else", state: "TASK_INBOX" }, | ||
{ id: "4", title: "Something again", state: "TASK_INBOX" }, | ||
]; | ||
const TaskBoxData = { | ||
tasks: defaultTasks, | ||
status: "idle", | ||
error: null, | ||
}; | ||
|
||
/* | ||
* The store is created here. | ||
* You can read more about Redux Toolkit's slices in the docs: | ||
* https://redux-toolkit.js.org/api/createSlice | ||
*/ | ||
const TasksSlice = createSlice({ | ||
name: "taskbox", | ||
initialState: TaskBoxData, | ||
reducers: { | ||
updateTaskState: (state, action) => { | ||
const { id, newTaskState } = action.payload; | ||
const task = state.tasks.findIndex((task) => task.id === id); | ||
if (task >= 0) { | ||
state.tasks[task].state = newTaskState; | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
// The actions contained in the slice are exported for usage in our components | ||
export const { updateTaskState } = TasksSlice.actions; | ||
|
||
/* | ||
* Our app's store configuration goes here. | ||
* Read more about Redux's configureStore in the docs: | ||
* https://redux-toolkit.js.org/api/configureStore | ||
*/ | ||
const store = configureStore({ | ||
reducer: { | ||
taskbox: TasksSlice.reducer, | ||
}, | ||
}); | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters