import React, { useMemo, useRef, useState } from "react";
import { FastGrid } from "fastgrid-react";
import "../Tutorial.css";
export default function App() {
const gridRef = useRef(null);
const [name, setName] = useState("");
const [password, setPassword] = useState("");
const [editMode, setEditMode] = useState(false);
const img = "";
const srcLayout = useMemo(() => ({
Script: {
//. Shows dialog (in message) to rate the school after click to Rate button
RateSchool: function(G,row){
var A = "
";
A += "
Please rate and review the school
";
A += "
"+G.GetString(row,G.Cols.CName)+"
";
A += "
";
A += "
";
A += "
No star
"
for(var i=1;i<=5;i++) A += "
"+img+"
";
A += "
";
A += "
";
A += "
";
A += "
";
A +="
";
G.ShowMessage(A,"Confirm",G.Script.Rate.bind(null,G,row));
},
//. Called after click Ok in RateSchool message, sends the new rating to server
Rate: function (G,row,button){
if(button!="Ok") return;
var I = FGrid.GetDocument().querySelectorAll(".stars");
for(var i=0;iNot rated yet',
'
'+img+'
',
'
'+img+'
',
'
'+img+'
',
'
'+img+'
',
'
'+img+'
',
'
'+img+'
']
},
{ id:'CButton', Type:"String", NoFilterBy:1, },
{ id:'CUser', Type:'String', Width:80 },
{ id:'id', NoEdit:1, NoGroup:1, Formula:"Row.id" }
],
LeftCols_Cfg: { MinWidth:170 },
Cols_Cfg: { MinWidth:170 },
RightCols_Cfg: { MinWidth:50 },
DefRows:{
Row: { Height:19, V: { CCountry:0, CState:0, CCounty:0, CLevel:0, CType:0, CGrade1:0, CGrade2:0 } }, //. Default for all rows
Main: { DA:"Main", NoSort:1, CD:'', Collapsed:1, F: { CRating:'return Grid.GetValue(Grid.GetLastChild(Row,4),Grid.Cols.CRating)' }, //. Root row with the school definition
A: { CButton: { TextColor:"white", CellBorder:"ButtonHigh", Value:"rate", Button:"Rate", OnClick:"Grid.Script.RateSchool(Grid,Row)" } }
},
Data: { L:1, NoFilterRow:1, CD:1, NoDelete:1, CS: { CCountry:18 }, A: { CCountry: { Button:"", RightIcons:"" }, CUser: { NoEdit:1 } } }, //. Base for every child row and group row
Address: { D:'Data', V: { CName:'Address' } },
Phone: { D:'Data', V: { CName:'Phone' } },
Link: { D:'Data', V: { CName:'Link for more information' }, A: { CCountry:"Link" } },
Map: { D:'Data', V: { CName:'Show map' }, A: { CCountry:"Link" } },
Reviews: { //. Default row for the parent row with reviews
D:'Data', Collapsed:1,
V: { CName:'Parent reviews' }, F: { CCountry:'Row.Children||count()', CRating:"return Math.round(Row.Children?Value/Row.Children:sum()/(count()||1))" },
A: { CCountry: { List:'|The school has not been reviewed yet|The school was reviewed once|The school was reviewed twice', NoListValue:'The school was reviewed *Value times' } }
},
Review: { D:'Data', L:0, NoEdit:1, Wrap:5, A: { CName: { Type:'Date', Format:'MM/dd/yyyy' } } }, //. One review row on the third level in tree
Group: { D:'Data', Group:1, Collapsed:1, F: { CRating:"var a = count(); return ''+a+' school'+(a>1?'s':'')" }, A: { CRating:{ List:"" } } } //. Row created by grouping
},
Head: [
{ D:"Header", id:"Header", SortIcons:2, LevelsPart:"Body", CS: { CRating:2 },
V: { CName:'School name', CCountry:'Country', CState:'State', CCounty:'County', CTown:'Town', CLevel:'Level', CType:'Type', CGrade1:'From grade', CGrade2:'To grade',
CEnrollment:'Enrollment', CStudents:'Students / teacher', CUser:'User', CRating: 'Rating', id:'id (debug)' },
},
{ D:'Filter', id:'Filter',
A: { CRating: { List:'|Not rated|No star|One star|Two stars|Three stars|Four stars|Five stars', RightIcons:"List", Button:"List", Range:1, LeftIcons:"", FilterRaw:1 },
CButton: { CellBorder:"", Value:"" } }
}
],
Body: { CD:'Main', Tree:"CName" },
Top: [ { id:"Data", Cells:"Settings,Save,Reload,Print,Pdf,Export,Copy,ExpandRows,CollapseRows,AddRowEnd,Undo,Redo" }, "Layout",
{ id:"Group", D:"Group", Cells:[
{ D:"Group" },
{ id:"GroupList", D:"GroupList", Width:220,
List: {
"":"none", "CState,CCounty":"State -> County", "CState,CCounty,CTown":"State -> County -> Town", "CCountry,CState,CCounty,CTown":"Country -> State -> County -> Town",
CLevel:"Level", "CLevel,CState,CCounty":"Level -> State -> County", CType:"Type", CRating:"Rating"
}
},
] },
],
Media: {
Mobile: {
Top: [{ id:"Group", Cells: [{ id:"GroupList", Width:160 }] }]
}
}
}), []);
const src = useMemo(() => [
srcLayout,
{ Url: "http://localhost:8000/SQLSchools.js?func=get", Method: "post", Param: {} },
{
Cfg: { SaveUrl: { Url: "http://localhost:8000/SQLSchools.js?func=set", Method: "Post", Param: {} }, PdfUrl: "http://localhost:8000/Pdf.js?func=getpdf" },
DefRows: { Reviews: { ChildrenUrl: { Url: "http://localhost:8000/SQLSchools.js?func=getratings", Method: "Post" } } }
}
], []);
function getGrid() { return gridRef.current; }
function reload(type) {
const grid = getGrid();
if(!name && type !== 1) { alert("Please enter your name !"); return; }
const user = name.replace("$", "_");
if(type===2 && !confirm(`Do you want to add new user '${user}' ${password ? "with" : "without"} password?`)) return;
const sourceGrid = grid?.Sources?.[1];
const saveSource = grid?.Sources?.[2];
if(!sourceGrid || !saveSource) return;
sourceGrid.Param = { ...sourceGrid.Param, User: type === 1 ? "" : user, Pass: type === 1 ? "" : password, New: type === 2 ? "1" : "0" };
saveSource.Cfg.SaveUrl.Param = { ...saveSource.Cfg.SaveUrl.Param, User: type === 1 ? "" : user, Pass: type === 1 ? "" : password };
setEditMode(type !== 1);
grid.Reload();
}
function addSchool() {
const grid = getGrid(); let max = 0;
grid.IterAll("Body",function(r){ var num = r.id.split("$")[1]-0; if(max
function onLoadError() { return { IO: { Message: "Sample node server is not running on localhost:8000 You need to install and start node package in ./server/" } }; }
return (
<>
6. School list and ratings
Demonstration of framework of item list with ratings and reviews. Using SQLite database table on Node server
Sample Node server must be running on http://localhost:8000. The sample Node server package is located at ./server/.
The sample data are loaded from http://localhost:8000/SQLSchools.js?func=get
and saved to url http://localhost:8000/SQLSchools.js?func=set.
PDF is generated on url http://localhost:8000/Pdf.js?func=getpdf.
The reviews are loaded from http://localhost:8000/SQLSchools.js?func=getratings. The review is added on http://localhost:8000/SQLSchools.js?func=setreview.
The sample data on server are stored in ./server/sqlite.db database file in tables Schools_Schools, Schools_Ratings, Schools_Reviews
and the script loading and saving data is located at ./server/SQLSchools.js
There are three modes to work with data:
a) View all data and rate and review the schools.
Default mode, without log in.
b) Edit data as user. Every user has his own data.
You can log in as Jan, Magda, David,
Adam, Linda, all without password.
Or you can add new user by entering new name and password.
c) Edit all data as administrator. You can edit all data or even assign it to another user.
You can log in as Admin, without password.
Log in to add or edit your events
Name: setName(event.target.value)} />
Password: setPassword(event.target.value)} />