Skip to content

Commit 970572d

Browse files
committed
feat: reset state when switching routes
1 parent 90b8649 commit 970572d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/store.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export const store = reactive({
4141
code: {},
4242
config: {
4343
template: '',
44-
include_test: false,
45-
output_dir: './logs',
46-
log_every_iters: 2
44+
include_test: false
4745
}
4846
})
4947

@@ -62,7 +60,7 @@ export function saveConfig(key, value) {
6260
// render the code if there are fetched files for current selected template
6361
export function genCode() {
6462
const currentFiles = files[store.config.template]
65-
store.code = {}
63+
store.code = {} // empty the `store.code` after changing templates
6664
if (currentFiles && Object.keys(currentFiles).length) {
6765
for (const file in currentFiles) {
6866
if (!store.config.include_test && file === 'test_all.py') {

src/views/Create.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import SplitPane from '../components/PaneSplit.vue'
1919
import PaneRight from '../components/PaneRight.vue'
2020
import PaneLeft from '../components/PaneLeft.vue'
2121
import Footer from '../components/Footer.vue'
22+
import { onUnmounted } from 'vue'
23+
import { store } from '../store'
2224
2325
export default {
2426
components: {
@@ -27,6 +29,22 @@ export default {
2729
PaneRight,
2830
PaneLeft,
2931
Footer
32+
},
33+
setup() {
34+
onUnmounted(() => {
35+
// delete each property of config
36+
// since `store.config` is being watched in store.js
37+
// and Vue watch function does not tracked
38+
// if we reassign `store.config` (i.e. store.config = {})
39+
for (const config in store.config) {
40+
delete store.config[config]
41+
}
42+
// since we delete each property,
43+
// it is better to reassign the initial values
44+
// which are defined in store.js
45+
store.config.template = ''
46+
store.config.include_test = false
47+
})
3048
}
3149
}
3250
</script>

0 commit comments

Comments
 (0)