From 94b6cbcc1a97af8bb4fdf25c55518ad3924c3025 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Mon, 27 Jan 2020 19:05:19 +0530 Subject: [PATCH] redux - Add and expose CLEAR_SOURCEFILE action --- src/index.tsx | 25 +++++++++++++++++++------ src/store/sourceFiles/actions.ts | 8 +++++++- src/store/sourceFiles/reducer.ts | 5 ++++- src/store/sourceFiles/types.ts | 8 +++++++- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index f2bcd8c..c363d5f 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -7,7 +7,11 @@ import ASTOutline from "./components/ASTOutline"; import CodeOutputDiff from "./components/CodeOutput"; import configureStore from "./store"; -import { initSourceFile, updateSourceFile } from "./store/sourceFiles/actions"; +import { + initSourceFile, + updateSourceFile, + clearSourceFile +} from "./store/sourceFiles/actions"; import styles from "./styles.css"; @@ -34,8 +38,12 @@ export function ASTVisualizer() {

Code output Diff

- Input - Output + + Input + + + Output + @@ -47,10 +55,15 @@ export function ASTVisualizer() { const { initSourceFile: initSourceFileConnected, - updateSourceFile: updateSourceFileConnected -} = bindActionCreators({ initSourceFile, updateSourceFile }, store.dispatch); + updateSourceFile: updateSourceFileConnected, + clearSourceFile: clearSourceFileConnected +} = bindActionCreators( + { initSourceFile, updateSourceFile, clearSourceFile }, + store.dispatch +); export { initSourceFileConnected as initSourceFile, - updateSourceFileConnected as updateSourceFile + updateSourceFileConnected as updateSourceFile, + clearSourceFileConnected as clearSourceFile }; diff --git a/src/store/sourceFiles/actions.ts b/src/store/sourceFiles/actions.ts index 6f1d41e..c0a00ff 100644 --- a/src/store/sourceFiles/actions.ts +++ b/src/store/sourceFiles/actions.ts @@ -1,5 +1,5 @@ import * as ts from "typescript"; -import { INIT_SOURCEFILE, UPDATE_SOURCEFILE } from "./types"; +import { INIT_SOURCEFILE, UPDATE_SOURCEFILE, CLEAR_SOURCEFILE } from "./types"; export function initSourceFile(sourceFile: ts.SourceFile) { return { @@ -14,3 +14,9 @@ export function updateSourceFile(sourceFile: ts.SourceFile) { payload: { sourceFile } }; } + +export function clearSourceFile() { + return { + type: CLEAR_SOURCEFILE, + }; +} diff --git a/src/store/sourceFiles/reducer.ts b/src/store/sourceFiles/reducer.ts index 2e80ef7..ea6491c 100644 --- a/src/store/sourceFiles/reducer.ts +++ b/src/store/sourceFiles/reducer.ts @@ -1,7 +1,8 @@ import { - SourceFilesState, INIT_SOURCEFILE, UPDATE_SOURCEFILE, + CLEAR_SOURCEFILE, + SourceFilesState, SourceFilesActionTypes } from "./types"; @@ -26,6 +27,8 @@ export function sourceFilesReducer( ...state, currentSourceFile: action.payload.sourceFile }; + case CLEAR_SOURCEFILE: + return initialState; default: return state; } diff --git a/src/store/sourceFiles/types.ts b/src/store/sourceFiles/types.ts index 899528e..ae75d4e 100644 --- a/src/store/sourceFiles/types.ts +++ b/src/store/sourceFiles/types.ts @@ -9,6 +9,7 @@ export interface SourceFilesState { // Describing the different ACTION NAMES available export const INIT_SOURCEFILE = "INIT_SOURCEFILE"; export const UPDATE_SOURCEFILE = "UPDATE_SOURCEFILE"; +export const CLEAR_SOURCEFILE = "CLEAR_SOURCEFILE"; interface InitSourceFileAction { type: typeof INIT_SOURCEFILE; @@ -20,6 +21,11 @@ interface UpdateSourceFileAction { payload: { sourceFile: ts.SourceFile }; } +interface ClearSourceFileAction { + type: typeof CLEAR_SOURCEFILE; +} + export type SourceFilesActionTypes = | InitSourceFileAction - | UpdateSourceFileAction; + | UpdateSourceFileAction + | ClearSourceFileAction;