Skip to content

Commit a2a86ae

Browse files
committed
feat(persist-data): storing data in store to redisplay when clicked on back button
1 parent 873cc8e commit a2a86ae

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

pages/index.vue

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
type="text"
2323
class="flex-1 flex-grow-0 w-full bg-transparent rounded-md"
2424
placeholder="Add label to this text block"
25-
value="Original Text"
25+
:value="lhsLabel"
2626
/>
27-
<span class="sr-only"
28-
>Add label to this original text bloack</span
29-
>
27+
<span class="sr-only">Add label to this original text block</span>
3028
</label>
3129
<textarea
3230
id="lhs"
3331
rows="28"
3432
name="lhs"
3533
class="flex-1 w-full bg-transparent rounded-md resize-none form-textarea"
34+
v-html="lhs"
3635
></textarea>
3736
</div>
3837
<div class="flex flex-col w-1/2 gap-4">
@@ -43,18 +42,18 @@
4342
type="text"
4443
class="flex-1 flex-grow-0 w-full bg-transparent rounded-md"
4544
placeholder="Add label to this text block"
46-
value="Changed text"
45+
:value="rhsLabel"
4746
/>
48-
<span class="sr-only"
49-
>Add label to this original text bloack</span
50-
>
47+
<span class="sr-only">Add label to this changed text block</span>
5148
</label>
5249
<textarea
5350
id="rhs"
5451
rows="28"
5552
name="rhs"
5653
class="flex-1 w-full bg-transparent rounded-md resize-none form-textarea"
57-
></textarea>
54+
v-html="rhs"
55+
>
56+
</textarea>
5857
</div>
5958
</section>
6059
<div class="self-end flex-grow-0 w-full text-center">
@@ -82,6 +81,7 @@ export default Vue.extend({
8281
return {
8382
isDarkMode: this.$isDarkMode,
8483
isSkipTutorial: this.$isSkipTutorial,
84+
...this.$store.state.data,
8585
}
8686
},
8787
async mounted() {
@@ -125,31 +125,17 @@ export default Vue.extend({
125125
const lhsLabel = formData.get('lhsLabel')
126126
const rhsLabel = formData.get('rhsLabel')
127127
if (!lhs || !rhs) {
128-
this.$store.commit('toast/show', {
129-
show: true,
130-
content: 'Please enter some data on both sides to compare',
131-
iconHTML: `
132-
<svg
133-
class="w-6 h-6"
134-
fill="none"
135-
stroke="currentColor"
136-
viewBox="0 0 24 24"
137-
xmlns="http://www.w3.org/2000/svg"
138-
>
139-
<path
140-
stroke-linecap="round"
141-
stroke-linejoin="round"
142-
stroke-width="2"
143-
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
144-
></path>
145-
</svg>
146-
`,
147-
theme: 'error',
148-
})
128+
this.showError()
149129
return
150130
}
151131
const originalLhs = lhs
152132
const originalRhs = rhs
133+
this.$store.commit('data/set', {
134+
lhs,
135+
rhs,
136+
lhsLabel,
137+
rhsLabel,
138+
})
153139
const diff = dmp.diff_main(originalLhs, originalRhs)
154140
const gzip = Buffer.from(
155141
pako.gzip(
@@ -165,6 +151,29 @@ export default Vue.extend({
165151
hash: `#${doUrlSafeBase64(gzip)}`,
166152
})
167153
},
154+
showError() {
155+
this.$store.commit('toast/show', {
156+
show: true,
157+
content: 'Please enter some data on both sides to compare',
158+
iconHTML: `
159+
<svg
160+
class="w-6 h-6"
161+
fill="none"
162+
stroke="currentColor"
163+
viewBox="0 0 24 24"
164+
xmlns="http://www.w3.org/2000/svg"
165+
>
166+
<path
167+
stroke-linecap="round"
168+
stroke-linejoin="round"
169+
stroke-width="2"
170+
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
171+
></path>
172+
</svg>
173+
`,
174+
theme: 'error',
175+
})
176+
},
168177
},
169178
})
170179
</script>

store/data.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { GetterTree, MutationTree } from 'vuex/types'
2+
3+
export const state = () => ({
4+
lhs: '',
5+
rhs: '',
6+
lhsLabel: 'Original Text',
7+
rhsLabel: 'Changed Text',
8+
})
9+
10+
export type DataState = ReturnType<typeof state>
11+
12+
export const getters: GetterTree<DataState, DataState> = {
13+
get: (state) => ({
14+
...state,
15+
}),
16+
}
17+
18+
export const mutations: MutationTree<DataState> = {
19+
set(state, { lhs, rhs, rhsLabel, lhsLabel }: DataState) {
20+
state.lhs = lhs
21+
state.rhs = rhs
22+
state.lhsLabel = lhsLabel
23+
state.rhsLabel = rhsLabel
24+
},
25+
}

0 commit comments

Comments
 (0)