Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install packages for React, create ReactController #901

Merged
merged 2 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions VocaDbWeb/Controllers/ReactController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace VocaDb.Web.Controllers
{
public class ReactController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
9 changes: 9 additions & 0 deletions VocaDbWeb/Scripts/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

import './i18n';

const App = (): React.ReactElement => {
return <></>;
};

export default App;
21 changes: 21 additions & 0 deletions VocaDbWeb/Scripts/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
load: 'languageOnly',
fallbackLng: 'en',

interpolation: {
escapeValue: false,
},

react: {
useSuspense: false,
},
});
13 changes: 13 additions & 0 deletions VocaDbWeb/Scripts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import ReactDOM from 'react-dom';

import App from './App';

const app = document.getElementById('app');

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
app,
);
2 changes: 2 additions & 0 deletions VocaDbWeb/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ void HandleHttpError(int code, string? description = null, string? msg = null)
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

endpoints.MapFallbackToController(action: "Index", controller: "React");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ReactController controller will handle requests to the React app, which will simply serve the single page in the React app.

});
}
}
Expand Down
43 changes: 43 additions & 0 deletions VocaDbWeb/Views/React/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@using System.Globalization
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: Some features in this file are not yet implemented.

@using VocaDb.Model.DataContracts.Users
@using VocaDb.Model.Domain.Globalization
@using VocaDb.Model.Helpers
@using VocaDb.Model.Utils
@using VocaDb.Web.Models.Shared
@inherits VocaDbPage
@addTagHelper *, VocaDb.ReMikus

@{
Layout = null;

var stylesheet = Login.IsLoggedIn && !string.IsNullOrEmpty(Login.User.Stylesheet) ? Login.User.Stylesheet : Config.SiteSettings.DefaultStylesheet;
}

<!DOCTYPE html>
<html lang="@InterfaceLanguage.GetAvailableLanguageCode(CultureInfo.CurrentUICulture)">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vocaloid Database</title>

<link rel="shortcut icon" href="@(Config.Assets.FavIconUrl.EmptyToNull() ?? Url.Content("~/Content/favicon.ico"))" type="image/x-icon" />
<remikus path="/Content/css.css" />
@if (!string.IsNullOrEmpty(stylesheet))
{
<link href="@Url.Content("~/Content/Styles/" + stylesheet)" rel="stylesheet" type="text/css" />
}
<link href="@Url.Content("~/Content/Styles/Icons.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/themes/redmond/jquery-ui-1.10.1.custom.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Scripts/qTip/jquery.qtip.css")" rel="stylesheet" type="text/css" />
<link rel="search" type="application/opensearchdescription+xml" title="@BrandableStrings.SiteName" href="@Config.SiteSettings.OpenSearchPath" />

<partial name="Partials/_GoogleAnalytics" />
</head>
<body class="vdb">
<div id="app"></div>
<script>window.vdb = @ToJS(new { Values = new GlobalValues(this) })</script>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vdb global variable is used to pass data from backend to frontend. This variable is initialized only once, when the page is first loaded. See also the GlobalValues class.

<remikus path="/bundles/manifest.js" />
<remikus path="/bundles/vendor.js" />
<remikus path="/bundles/index.js" />
Comment on lines +39 to +41
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Powered by ReMikus. :)

</body>
</html>
Loading