Skip to content

Commit

Permalink
Merge pull request #369 from buiphukhuyen/main
Browse files Browse the repository at this point in the history
Add Vietnamese language and fix Newsletter feature
dorthl authored Oct 24, 2023
2 parents 4793d7c + 8c2e2d4 commit ba588fa
Showing 4 changed files with 831 additions and 38 deletions.
792 changes: 792 additions & 0 deletions src/Blogifier.Shared/Resources/Resource.vi-VN.resx

Large diffs are not rendered by default.

55 changes: 22 additions & 33 deletions src/Blogifier.Themes.Standard/assets/js/main.js
Original file line number Diff line number Diff line change
@@ -4,17 +4,21 @@ const form_email = document.getElementById("newsletter_email");
const form_status = document.getElementById("newsletter_status");

// Success, Loading and Error functions
function successNewsletter() {
const newsletterSucessMsg = form_status.dataset.success, newsletterErrorMsg = form_status.dataset.error;
form_status.innerHTML = `<div class="newsletter-msg bg-success"><div class="m-auto"> ${newsletterSucessMsg} </div></div>`;
setTimeout(resetNewsletter, 2000);
function successNewsletter(message) {
form_status.innerHTML = `<div class="newsletter-msg bg-success"><div class="m-auto">${message}</div></div>`;
setTimeout(() => {
resetNewsletter();
}, 2000);
}

function loadingNewsletter() {
form_status.innerHTML = '<div class="newsletter-msg"><div class="m-auto spinner-border" role="status"></div></div>'
form_status.innerHTML = '<div class="newsletter-msg"><div class="m-auto spinner-border" role="status"></div></div>';
}
function errorNewsletter(msg) {
form_status.innerHTML = `<div class="newsletter-msg bg-danger"><div class="m-auto">${newsletterErrorMsg} <br> ${msg}</div></div>`;

function errorNewsletter(message) {
form_status.innerHTML = `<div class="newsletter-msg bg-danger"><div class="m-auto">${message}</div></div>`;
}

function resetNewsletter() {
form.reset();
form_status.innerHTML = "";
@@ -25,20 +29,20 @@ function subscribeNewsletter(url, data) {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data)
}
};

fetch(url, options)
.then((response) => {
if (response.status == 200) {
return response.json();
if (response.status === 200) {
successNewsletter('Đăng ký nhận thông báo mới cho email này thành công!');
} else if (response.status === 400) {
errorNewsletter('Email này đã tồn tại trong danh sách đăng ký.');
} else {
throw new Error('The Newsletter is not working!');
errorNewsletter('Lỗi không xác định.');
}
})
.then(() => {
successNewsletter();
})
.catch((err) => {
errorNewsletter(err);
errorNewsletter(err.message);
});
}

@@ -51,23 +55,8 @@ form.addEventListener("submit", function (e) {
Country: "unknown",
Region: "unknown"
};
fetch('https://ipapi.co/json/')
.then((response) => {
if (response.status == 200) {
return response.json();
} else {
throw new Error('Not sure where you are!');
}
})
.then((loc) => {
subscriber_data.Ip = loc.ip;
subscriber_data.Country = loc.country_name;
subscriber_data.Region = loc.region;
subscribeNewsletter(form.action, subscriber_data);
})
.catch((err) => {
subscribeNewsletter(form.action, subscriber_data);
});

subscribeNewsletter(form.action, subscriber_data);
});

// search modal auto focus
@@ -76,4 +65,4 @@ if (myModal) {
myModal.addEventListener('shown.bs.modal', function () {
document.getElementById('searchFormInput').focus()
})
}
}
12 changes: 10 additions & 2 deletions src/Blogifier/Interfaces/SubscriberController.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
using Blogifier.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

@@ -33,8 +34,15 @@ public async Task DeleteAsync([FromRoute] int id)
}

[HttpPost("apply")]
public async Task Apply([FromForm] SubscriberApplyDto subscriber)
public async Task<IActionResult> ApplyAsync([FromBody] SubscriberApplyDto input)
{
await _subscriberProvider.ApplyAsync(subscriber);
var res = await _subscriberProvider.ApplyAsync(input);

if(res == 1)
{
return Ok();
}

return BadRequest();
}
}
10 changes: 7 additions & 3 deletions src/Blogifier/Newsletters/SubscriberProvider.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
using Blogifier.Data;
using Blogifier.Shared;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -24,14 +25,17 @@ public async Task<IEnumerable<SubscriberDto>> GetItemsAsync()
return await _mapper.ProjectTo<SubscriberDto>(query).ToListAsync();
}

public async Task ApplyAsync(SubscriberApplyDto input)
public async Task<int> ApplyAsync(SubscriberApplyDto input)
{

if (await _dbContext.Subscribers.AnyAsync(m => m.Email == input.Email))
return 0;
else
{
var data = _mapper.Map<Subscriber>(input);
_dbContext.Subscribers.Add(data);
await _dbContext.SaveChangesAsync();
return 1;
}
}

}
}

0 comments on commit ba588fa

Please sign in to comment.