Skip to content

Commit 4ca60ed

Browse files
jardakotesovecipula
authored andcommitted
improved creating invitation workflow structure
1 parent 9c4ac43 commit 4ca60ed

9 files changed

+450
-553
lines changed

src/composables/useForm.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import {ref} from 'vue';
1+
import {ref, watch} from 'vue';
22

33
function getField(form, name) {
44
const fields = form.fields;
55

66
return fields.find((field) => field.name === name);
77
}
88

9+
function doesFieldExist(form, name) {
10+
const fields = form.fields;
11+
12+
return !!fields.find((field) => field.name === name);
13+
}
14+
915
function getClearValue(field, localeKey = null) {
1016
if (localeKey) {
1117
if (field.component === 'field-slider') {
@@ -27,6 +33,34 @@ function mapFromSelectedToValue(selected) {
2733
export function useForm(_form) {
2834
const form = ref(_form);
2935

36+
function connectWithPayload(payload) {
37+
watch(
38+
payload,
39+
(newPayload) => {
40+
Object.keys(newPayload).forEach((key) => {
41+
if (doesFieldExist(form.value, key)) {
42+
if (getValue(key) !== newPayload[key]) {
43+
setValue(key, newPayload[key]);
44+
}
45+
}
46+
});
47+
},
48+
{immediate: true},
49+
);
50+
}
51+
52+
function set(key, data) {
53+
Object.keys(data).forEach(function (dataKey) {
54+
form.value[dataKey] = data[dataKey];
55+
});
56+
}
57+
58+
function getValue(name) {
59+
const field = getField(form.value, name);
60+
61+
return field.value;
62+
}
63+
3064
function setValue(name, inputValue) {
3165
const field = getField(form.value, name);
3266
if (!field) {
@@ -69,8 +103,11 @@ export function useForm(_form) {
69103
}
70104

71105
return {
106+
set,
72107
setValue,
108+
getValue,
73109
clearForm,
74110
form,
111+
connectWithPayload,
75112
};
76113
}

src/pages/userInvitation/UserInvitationDetailsFormStep.vue

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<template>
22
<div v-if="store.user === null">
33
<pkp-form
4-
v-if="section.type === 'form'"
5-
v-bind="section.form"
6-
ref="autosaveForms"
4+
v-bind="userForm"
75
class="userInvitation__stepForm"
8-
@set="store.updateAutosaveForm"
6+
@set="updateUserForm"
97
></pkp-form>
108
</div>
119
<div v-if="store.user !== null">
@@ -40,7 +38,7 @@
4038
</div>
4139
</div>
4240
<br />
43-
<UserInvitationUserGroupsTable />
41+
<UserInvitationUserGroupsTable :section="section" />
4442
</template>
4543

4644
<script setup>
@@ -49,12 +47,25 @@ import PkpForm from '@/components/Form/Form.vue';
4947
import {useTranslation} from '@/composables/useTranslation';
5048
import UserInvitationUserGroupsTable from './UserInvitationUserGroupsTable.vue';
5149
import {useUserInvitationPageStore} from './UserInvitationPageStore';
50+
import {useForm} from '@/composables/useForm';
51+
52+
function updateUserForm(a, {fields}, c, d) {
53+
fields.forEach((field) => {
54+
if (store.invitationPayload[field.name] !== field.value) {
55+
store.updatePayload(field.name, field.value);
56+
}
57+
});
58+
}
5259
53-
defineProps({
54-
section: {type: Object, required: true},
55-
});
5660
const {t} = useTranslation();
5761
const store = useUserInvitationPageStore();
62+
63+
const props = defineProps({
64+
section: {type: Object, required: true},
65+
});
66+
67+
const {form: userForm, connectWithPayload} = useForm(props.section.form);
68+
connectWithPayload(store.invitationPayload);
5869
</script>
5970
<style lang="less">
6071
select {

src/pages/userInvitation/UserInvitationEmailComposerStep.vue

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:attachments="section.email.attachments"
99
:bcc="section.email.bcc"
1010
:bcc-label="t('email.bcc')"
11-
:body="section.email.body"
1211
:body-label="t('stageParticipants.notify.message')"
1312
:can-change-recipients="section.email.canChangeRecipients"
1413
:cc="section.email.cc"
@@ -34,24 +33,42 @@
3433
:remove-item-label="t('common.removeItem')"
3534
:searching-label="t('common.searching')"
3635
:search-results-label="t('search.searchResults')"
37-
:subject="section.email.subject"
3836
:subject-label="t('email.subject')"
3937
:switch-to-label="t('common.switchTo')"
4038
:switch-to-named-language-label="t('common.switchToNamedItem')"
4139
:variables="section.email.variables"
42-
@set="store.updateStep"
40+
v-bind="emailComposer"
41+
@set="(componentName, update) => updateEmail(update)"
4342
></composer>
4443
</template>
4544

4645
<script setup>
46+
import {computed} from 'vue';
4747
import Composer from '@/components/Composer/Composer.vue';
4848
import {useTranslation} from '@/composables/useTranslation';
4949
import {defineProps} from 'vue';
5050
import {useUserInvitationPageStore} from './UserInvitationPageStore';
5151
52-
defineProps({
52+
const props = defineProps({
5353
section: {type: Object, required: true},
5454
});
5555
const {t} = useTranslation();
56+
5657
const store = useUserInvitationPageStore();
58+
const emailComposer = computed(() => store.invitationPayload.emailComposer);
59+
60+
function updateEmail(update) {
61+
const emailComposerUpdate = {
62+
...store.invitationPayload.emailComposer,
63+
...update,
64+
};
65+
store.updatePayload('emailComposer', emailComposerUpdate);
66+
}
67+
68+
if (!store.invitationPayload.body) {
69+
updateEmail({
70+
subject: props.section.email.subject,
71+
body: props.section.email.body,
72+
});
73+
}
5774
</script>

src/pages/userInvitation/UserInvitationPage.stories.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const Init = {
2727
for (let param of params) {
2828
allParams[param[0]] = param[1];
2929
}
30-
console.log(allParams);
3130
if (
3231
allParams.searchPhrase.replaceAll(/\s/g, '') ===
3332
@@ -41,6 +40,24 @@ export const Init = {
4140
}
4241
},
4342
),
43+
http.post(
44+
'https://mock/index.php/publicknowledge/api/v1/invitations',
45+
() => {
46+
return HttpResponse.json({invitationId: 15});
47+
},
48+
),
49+
http.post(
50+
'https://mock/index.php/publicknowledge/api/v1/invitations/15',
51+
() => {
52+
return HttpResponse.json({});
53+
},
54+
),
55+
http.post(
56+
'https://mock/index.php/publicknowledge/api/v1/invitations/15/submit',
57+
() => {
58+
return HttpResponse.json({});
59+
},
60+
),
4461
http.post(
4562
'https://mock/index.php/publicknowledge/api/v1/user/_invite',
4663
() => {

src/pages/userInvitation/UserInvitationPage.vue

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
:key="step.id"
2222
:label="step.name"
2323
>
24-
<panel class="decision__stepPanel">
24+
<panel
25+
v-if="store.currentStep.id === step.id"
26+
class="decision__stepPanel"
27+
>
2528
<panel-section class="decision__stepHeader">
2629
<h2>{{ store.stepTitle }}</h2>
2730
<p class="error">{{ store.errors.error }}</p>
@@ -82,9 +85,7 @@
8285
<pkp-button @click="store.cancel">Cancel</pkp-button>
8386
<pkp-button @click="store.previousStep">Previous</pkp-button>
8487
<pkp-button :is-primary="true" @click="store.nextStep">
85-
<template v-if="store.isOnLastStep">Invite user to role</template>
86-
<template v-else-if="store.isOnFirstStep">Search user</template>
87-
<template v-else>Save and continue</template>
88+
{{ store.currentStep.nextButtonLabel }}
8889
</pkp-button>
8990
</button-row>
9091
<modal
@@ -171,8 +172,8 @@ const props = defineProps({
171172
return null;
172173
},
173174
},
175+
invitationPayload: {type: Object, required: true},
174176
});
175-
console.log(props);
176177
const {t} = useTranslation();
177178
const wrapper = ref(null);
178179
const store = useUserInvitationPageStore(props);

0 commit comments

Comments
 (0)