Skip to content

Commit

Permalink
feat: Allow to manage Page Template Status - MEED-6845 - Meeds-io/MIP…
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed May 20, 2024
1 parent edb0c96 commit 118366d
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public List<PageTemplate> getPageTemplates(HttpServletRequest request) {
return pageTemplateService.getPageTemplates(request.getLocale(), true);
}

@GetMapping("{id}")
@Secured("users")
@Operation(summary = "Retrieve a page template designated by its id", method = "GET",
description = "This will retrieve a page template designated by its id")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Request fulfilled"), })
public PageTemplate getPageTemplate(
HttpServletRequest request,
@Parameter(description = "Page template identifier")
@PathVariable("id")
long id) {
return pageTemplateService.getPageTemplate(id, request.getLocale(), true);
}

@PostMapping
@Secured("users")
@Operation(summary = "Create a page template", method = "POST", description = "This creates a new page template")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ public List<PageTemplate> getPageTemplates(boolean expand) {
public List<PageTemplate> getPageTemplates(Locale locale, boolean expand) {
List<PageTemplate> pageTemplates = pageTemplateStorage.getPageTemplates();
if (expand) {
pageTemplates.forEach(pageTemplate -> {
pageTemplate.setName(getLabel(pageTemplate.getId(), PageTemplateTranslationPlugin.TITLE_FIELD_NAME, locale));
pageTemplate.setDescription(getLabel(pageTemplate.getId(), PageTemplateTranslationPlugin.DESCRIPTION_FIELD_NAME, locale));
List<String> attachmentFileIds = attachmentService.getAttachmentFileIds(PageTemplateAttachmentPlugin.OBJECT_TYPE,
String.valueOf(pageTemplate.getId()));
if (CollectionUtils.isNotEmpty(attachmentFileIds)) {
pageTemplate.setIllustrationId(Long.parseLong(attachmentFileIds.get(0)));
}
});
pageTemplates.forEach(pageTemplate -> computePageTemplateAttributes(locale, pageTemplate));
}
return pageTemplates;
}

public PageTemplate getPageTemplate(long id, Locale locale, boolean expand) {
PageTemplate pageTemplate = pageTemplateStorage.getPageTemplate(id);
if (expand) {
computePageTemplateAttributes(locale, pageTemplate);
}
return pageTemplate;
}

public PageTemplate getPageTemplate(long id) {
return pageTemplateStorage.getPageTemplate(id);
}
Expand Down Expand Up @@ -157,4 +157,14 @@ private String getLabel(long templateId, String fieldName, Locale locale) {
}
}

private void computePageTemplateAttributes(Locale locale, PageTemplate pageTemplate) {
pageTemplate.setName(getLabel(pageTemplate.getId(), PageTemplateTranslationPlugin.TITLE_FIELD_NAME, locale));
pageTemplate.setDescription(getLabel(pageTemplate.getId(), PageTemplateTranslationPlugin.DESCRIPTION_FIELD_NAME, locale));
List<String> attachmentFileIds = attachmentService.getAttachmentFileIds(PageTemplateAttachmentPlugin.OBJECT_TYPE,
String.valueOf(pageTemplate.getId()));
if (CollectionUtils.isNotEmpty(attachmentFileIds)) {
pageTemplate.setIllustrationId(Long.parseLong(attachmentFileIds.get(0)));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,66 @@ public void getPageTemplatesWithExpand() throws ObjectNotFoundException {
assertEquals(32l, pageTemplates.get(0).getIllustrationId());
}

@Test
public void getPageTemplateWithExpand() throws ObjectNotFoundException {
PageTemplate template = new PageTemplate(2l, false, LAYOUT_CATEGORY, LAYOUT_CONTENT);
when(localeConfigService.getDefaultLocaleConfig()).thenReturn(defaultLocaleConfig);
when(defaultLocaleConfig.getLocale()).thenReturn(Locale.ENGLISH);

when(pageTemplateStorage.getPageTemplate(2l)).thenReturn(template);

PageTemplate retrievedPageTemplate = pageTemplateService.getPageTemplate(2l);
assertNotNull(retrievedPageTemplate);
assertEquals(template.getId(), retrievedPageTemplate.getId());
assertEquals(template.getContent(), retrievedPageTemplate.getContent());
assertNull(retrievedPageTemplate.getName());
assertNull(retrievedPageTemplate.getDescription());
assertEquals(0l, retrievedPageTemplate.getIllustrationId());

when(translationService.getTranslationField(PageTemplateTranslationPlugin.OBJECT_TYPE,
template.getId(),
PageTemplateTranslationPlugin.TITLE_FIELD_NAME)).thenThrow(ObjectNotFoundException.class);
retrievedPageTemplate = pageTemplateService.getPageTemplate(2l, Locale.FRENCH, true);
assertNotNull(retrievedPageTemplate);

reset(translationService);

TranslationField titleTranslationField = mock(TranslationField.class);
when(translationService.getTranslationField(PageTemplateTranslationPlugin.OBJECT_TYPE,
template.getId(),
PageTemplateTranslationPlugin.TITLE_FIELD_NAME)).thenReturn(titleTranslationField);
retrievedPageTemplate = pageTemplateService.getPageTemplate(2l, Locale.FRENCH, true);
assertNotNull(retrievedPageTemplate);
assertEquals(template.getId(), retrievedPageTemplate.getId());
assertEquals(template.getContent(), retrievedPageTemplate.getContent());
assertNull(retrievedPageTemplate.getName());
assertNull(retrievedPageTemplate.getDescription());
assertEquals(0l, retrievedPageTemplate.getIllustrationId());

String frTitle = "testTitle";
when(titleTranslationField.getLabels()).thenReturn(Collections.singletonMap(Locale.FRENCH, frTitle));

retrievedPageTemplate = pageTemplateService.getPageTemplate(2l, Locale.FRENCH, true);
assertEquals(frTitle, retrievedPageTemplate.getName());

TranslationField descriptionTranslationField = mock(TranslationField.class);
when(translationService.getTranslationField(PageTemplateTranslationPlugin.OBJECT_TYPE,
template.getId(),
PageTemplateTranslationPlugin.DESCRIPTION_FIELD_NAME)).thenReturn(descriptionTranslationField);
String enDesc = "testDescription";
when(descriptionTranslationField.getLabels()).thenReturn(Collections.singletonMap(Locale.ENGLISH, enDesc));

retrievedPageTemplate = pageTemplateService.getPageTemplate(2l, Locale.ENGLISH, true);
assertNotNull(retrievedPageTemplate);
assertEquals(enDesc, retrievedPageTemplate.getDescription());

when(attachmentService.getAttachmentFileIds(PageTemplateAttachmentPlugin.OBJECT_TYPE,
"2")).thenReturn(Collections.singletonList("32"));
retrievedPageTemplate = pageTemplateService.getPageTemplate(2l, Locale.GERMAN, true);
assertNotNull(retrievedPageTemplate);
assertEquals(32l, retrievedPageTemplate.getIllustrationId());
}

@Test
public void getPageTemplate() {
when(pageTemplateStorage.getPageTemplate(2l)).thenReturn(pageTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@ pageTemplates.filter.placeholder=Filter by name, description
pageTemplates.label.name=Name
pageTemplates.label.description=Description
pageTemplates.label.category=Category
pageTemplates.label.status=Status
pageTemplate.label.preview=Preview of {0} template
pageTemplate.status.update.success=Template status successfully updated
pageTemplate.status.update.error=An unknown error occurred while updating template status. Please contact the administrator or try agan later.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ export function getPageTemplates() {
});
}

export function getPageTemplate(id) {
return fetch(`/layout/rest/pageTemplates/${id}`, {
method: 'GET',
credentials: 'include',
}).then(resp => {
if (!resp?.ok) {
throw new Error('Error when retrieving page template');
} else {
return resp.json();
}
});
}

export function createPageTemplate(pageContent) {
return fetch('/layout/rest/pageTemplates', {
credentials: 'include',
Expand All @@ -49,16 +62,14 @@ export function createPageTemplate(pageContent) {
});
}

export function updatePageTemplate(pageContent, id) {
return fetch(`/layout/rest/pageTemplates/${id}`, {
export function updatePageTemplate(pageTemplate) {
return fetch(`/layout/rest/pageTemplates/${pageTemplate.id}`, {
credentials: 'include',
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: JSON.stringify(pageContent),
}),
body: JSON.stringify(pageTemplate),
}).then((resp) => {
if (!resp?.ok) {
throw new Error('Error when creating page template');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ export default {
save() {
const pageLayout = this.$layoutUtils.cleanAttributes(this.$root.layout, true, true);
const savePageRequest =
this.templateId ? this.$pageTemplateService.updatePageTemplate(pageLayout, this.templateId)
this.templateId ?
this.$pageTemplateService.getPageTemplate(this.templateId)
.then(pageTemplate => this.$pageTemplateService.updatePageTemplate({
...pageTemplate,
content: JSON.stringify(pageLayout),
}))
: this.$pageTemplateService.createPageTemplate(pageLayout);
return savePageRequest
.then(pageTemplate => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@
<td
v-if="!$root.isMobile"
align="left"
class="text-truncate"
class="text-truncate text-center"
width="120px">
{{ category }}
</td>
<td
v-if="!$root.isMobile"
align="left"
class="d-flex align-center ps-2"
width="120px">
<v-switch
v-model="enabled"
:loading="loading"
class="mt-0 mx-auto"
@click="changeStatus" />
</td>
</tr>
</template>
<script>
Expand All @@ -42,6 +53,9 @@ export default {
pageTemplateId() {
return this.pageTemplate?.id;
},
enabled() {
return !this.pageTemplate?.disabled;
},
name() {
return this.$te(this.pageTemplate?.name) ? this.$t(this.pageTemplate?.name) : this.pageTemplate?.name;
},
Expand All @@ -61,5 +75,22 @@ export default {
|| this.$t('layout.pageTemplate.category.customized');
},
},
methods: {
changeStatus() {
this.$root.$emit('close-alert-message');
this.loading = true;
this.$pageTemplateService.getPageTemplate(this.pageTemplate.id)
.then(pageTemplate => this.$pageTemplateService.updatePageTemplate({
...pageTemplate,
disabled: this.enabled,
}))
.then(() => {
this.$root.$emit('page-templates-refresh');
this.$root.$emit('alert-message', this.$t('pageTemplate.status.update.success'), 'success');
})
.catch(() => this.$root.$emit('alert-message', this.$t('pageTemplate.status.update.error'), 'error'))
.finally(() => this.loading = false);
},
},
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ export default {
{
text: this.$t('pageTemplates.label.category'),
value: 'category',
align: 'left',
sortable: false,
align: 'center',
sortable: true,
class: 'page-template-category-header',
width: '120px'
},
{
text: this.$t('pageTemplates.label.status'),
value: 'disabled',
align: 'center',
sortable: true,
class: 'page-template-category-header',
width: '120px'
},
Expand All @@ -74,8 +82,12 @@ export default {
},
},
created() {
this.$root.$on('page-templates-refresh', this.refreshPageTemplates);
this.refreshPageTemplates();
},
beforeDestroy() {
this.$root.$off('page-templates-refresh', this.refreshPageTemplates);
},
methods: {
refreshPageTemplates() {
this.loading = true;
Expand Down

0 comments on commit 118366d

Please sign in to comment.