How to show Modal when we click on it using reactjs? - reactjs

I'm new to react and wanted to create a Calendar and wanted to add event to the respective date in the Calendar, when we click on the field it should show the modal popup but modal is not getting displayed. Here i'm using only semantic-ui-react for designing the every component. Can anyone help me to resolve this issue?
first Component:
import React, { Component } from "react";
import { render } from "react-dom";
import { Calendar, momentLocalizer } from "react-big-calendar";
import moment from "moment";
import "react-big-calendar/lib/css/react-big-calendar.css";
import CreateEvent from "./CreteEvent";
const localizer = momentLocalizer(moment);
class ShowCalendar extends Component {
constructor() {
super();
const now = new Date();
const events = [
{
id: 0,
title: "All Day Event very long title",
allDay: true,
start: new Date(2015, 3, 0),
end: new Date(2015, 3, 1)
},
{
id: 1,
title: "Long Event",
start: new Date(2015, 3, 7),
end: new Date(2015, 3, 10)
},
{
id: 2,
title: "DTS STARTS",
start: new Date(2016, 2, 13, 0, 0, 0),
end: new Date(2016, 2, 20, 0, 0, 0)
},
{
id: 3,
title: "DTS ENDS",
start: new Date(2016, 10, 6, 0, 0, 0),
end: new Date(2016, 10, 13, 0, 0, 0)
},
{
id: 4,
title: "Some Event",
start: new Date(2015, 3, 9, 0, 0, 0),
end: new Date(2015, 3, 10, 0, 0, 0)
},
{
id: 5,
title: "Conference",
start: new Date(2015, 3, 11),
end: new Date(2015, 3, 13),
desc: "Big conference for important people"
},
{
id: 6,
title: "Meeting",
start: new Date(2015, 3, 12, 10, 30, 0, 0),
end: new Date(2015, 3, 12, 12, 30, 0, 0),
desc: "Pre-meeting meeting, to prepare for the meeting"
},
{
id: 7,
title: "Lunch",
start: new Date(2015, 3, 12, 12, 0, 0, 0),
end: new Date(2015, 3, 12, 13, 0, 0, 0),
desc: "Power lunch"
},
{
id: 8,
title: "Meeting",
start: new Date(2015, 3, 12, 14, 0, 0, 0),
end: new Date(2015, 3, 12, 15, 0, 0, 0)
},
{
id: 9,
title: "Happy Hour",
start: new Date(2015, 3, 12, 17, 0, 0, 0),
end: new Date(2015, 3, 12, 17, 30, 0, 0),
desc: "Most important meal of the day"
},
{
id: 10,
title: "Dinner",
start: new Date(2015, 3, 12, 20, 0, 0, 0),
end: new Date(2015, 3, 12, 21, 0, 0, 0)
},
{
id: 11,
title: "Birthday Party",
start: new Date(2015, 3, 13, 7, 0, 0),
end: new Date(2015, 3, 13, 10, 30, 0)
},
{
id: 12,
title: "Late Night Event",
start: new Date(2015, 3, 17, 19, 30, 0),
end: new Date(2015, 3, 18, 2, 0, 0)
},
{
id: 12.5,
title: "Late Same Night Event",
start: new Date(2015, 3, 17, 19, 30, 0),
end: new Date(2015, 3, 17, 23, 30, 0)
},
{
id: 13,
title: "Multi-day Event",
start: new Date(2015, 3, 20, 19, 30, 0),
end: new Date(2015, 3, 22, 2, 0, 0)
},
{
id: 14,
title: "Today",
start: new Date(new Date().setHours(new Date().getHours() - 3)),
end: new Date(new Date().setHours(new Date().getHours() + 3))
},
{
id: 15,
title: "Point in Time Event",
start: now,
end: now
}
];
this.state = {
name: "React",
showModal: false,
events
};
this.openModal = this.openModal.bind(this);
this.handleCloseModal = this.handleCloseModal.bind(this);
}
handleCloseModal() {
this.setState({ showModal: false });
}
openModal() {
this.setState({ showModal: true });
}
render() {
return (
<div>
<div style={{ height: "500pt" }}>
<Calendar
events={this.state.events}
startAccessor="start"
endAccessor="end"
defaultDate={moment().toDate()}
localizer={localizer}
onDrillDown={this.openModal}
/>
</div>
{this.state.showModal ? (
<CreateEvent
isOpen={this.state.showModal}
onClose={this.handleCloseModal}
/>
) : (
""
)}
</div>
);
}
}
export default ShowCalendar;
2nd Component: Modal.jsx
import React, { Component } from "react";
import { Modal, Button, Header, Icon } from "semantic-ui-react";
export default class CreteEvent extends Component {
render() {
return (
<div>
<Modal isOpen={this.props.isOpen}>
<Modal.Header>Create Event</Modal.Header>
<Modal.Content>
<Modal.Description>Hi Everyone</Modal.Description>
</Modal.Content>
<Modal.Actions>
<Button primary onClick={this.props.onClose}>
Close <Icon name="right chevron" />
</Button>
</Modal.Actions>
</Modal>
</div>
);
}
}
Here is the entire code "Code: "https://codesandbox.io/s/strange-bhaskara-1n1jc""

So I have got in to the office and could have look, you never include the semantic UI css: https://react.semantic-ui.com/usage/#theme
In index.js https://react.semantic-ui.com/usage/#theme and add semantic-ui-css as a dependency, and as #PompolutZ mentioned the modal needs open vs isOpen
There is probably some other issues, but changing the default state to true now shows the modal - might just need to fix up how the modal gets displayed, i.e. when does openModal get called. (Ah clicking the date is the driver (never used the calendar component before: had to look at the docs)
https://codesandbox.io/s/morning-darkness-hz06m?fontsize=14&hidenavigation=1&theme=dark

Add dependency to semantic-ui-css
Import css in your main index.js file:
import 'semantic-ui-css/semantic.min.css';
You need to change isOpen to just open in Modal:
import React, { Component } from "react";
import { Modal, Button, Header, Icon } from "semantic-ui-react";
export default class CreteEvent extends Component {
render() {
return (
<div>
<Modal open={this.props.isOpen}>
<Modal.Header>Create Event</Modal.Header>
<Modal.Content>
<Modal.Description>Hi Everyone</Modal.Description>
</Modal.Content>
<Modal.Actions>
<Button primary onClick={this.props.onClose}>
Close <Icon name="right chevron" />
</Button>
</Modal.Actions>
</Modal>
</div>
);
}
}
Here is fixed codesandbox: https://codesandbox.io/s/sad-lamport-y6jqg (with aforementioned import)

Related

How to fetch data from event's resource and display on week view in React-Big-Calendar

I am trying to add custom data for month, week and day view. I created custom event file where I separate date and date time for week's event.
I am able to display data if I export event file like this
export default [
{
progress: '80',
title: 'streak minus one',
end: new Date(2022, 2, 21, 10, 8, 0),
start: new Date(2022, 2, 21, 10, 0, 0),
resource: {
month: {
status: true,
progress: 80
}
}
},
{
progress: '80',
title: 'streak minus one',
end: new Date(2022, 2, 21, 10, 19, 0),
start: new Date(2022, 2, 21, 10, 9, 0),
resource: {
month: {
status: true,
progress: 80
}
}
}
]
But not like this
export default [
{
title: 'streak minus one',
end: new Date(2022, 2, 21),
start: new Date(2022, 2, 21),
resource: {
month: {
progress: 70
},
week: [
{
endDateTime: new Date(2022, 2, 21, 10, 8, 0),
startDateTime: new Date(2022, 2, 21, 10, 0, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 19, 0),
startDateTime: new Date(2022, 2, 21, 10, 9, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 26, 0),
startDateTime: new Date(2022, 2, 21, 10, 19, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 33, 0),
startDateTime: new Date(2022, 2, 21, 10, 26, 0)
}
]
}
},
{
title: 'second event',
end: new Date(2022, 2, 23),
start: new Date(2022, 2, 23),
resource: {
month: {
status: true,
progress: 45
},
week: [
{
endDateTime: new Date(2022, 2, 22, 10, 15, 0),
startDateTime: new Date(2022, 2, 22, 10, 0, 0)
}
]
}
}
]
How to display events by fetching it from resource on Week View in React-big-calendar
React-Big-Calendar expects the events individual 'event' data structures to be consistent, and uses the same data 'accessors' (startAccessor, endAccessor, titleAccessor) in all views. You can use onRangeChange to reload your events, but it would still expect the data structures to be the same.

Error in rendering a React highcharts x-range chart

I am trying to re-create the highcharts x-range demo chart (https://www.highcharts.com/demo/x-range) using the official React wrapper and I'm getting a "ReferenceError: Highcharts is not defined" error. I'm new to both react and highcharts so not sure what I'm missing here
here's the code:
import React, { useState } from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const AccountContractHistory = (props) => {
const [chartOptions, setChartOptions] = useState({
chart: {
type: 'xrange'
},
title: {
text: 'Highcharts X-range'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: ''
},
categories: ['Prototyping', 'Development', 'Testing'],
reversed: true
},
series: [{
name: 'Project 1',
// pointPadding: 0,
// groupPadding: 0,
borderColor: 'gray',
pointWidth: 20,
data: [{
x: Date.UTC(2014, 10, 21),
x2: Date.UTC(2014, 11, 2),
y: 0,
partialFill: 0.25
}, {
x: Date.UTC(2014, 11, 2),
x2: Date.UTC(2014, 11, 5),
y: 1
}, {
x: Date.UTC(2014, 11, 8),
x2: Date.UTC(2014, 11, 9),
y: 2
}, {
x: Date.UTC(2014, 11, 9),
x2: Date.UTC(2014, 11, 19),
y: 1
}, {
x: Date.UTC(2014, 11, 10),
x2: Date.UTC(2014, 11, 23),
y: 2
}],
dataLabels: {
enabled: true
}
}],
});
return (
<div>
<HighchartsReact
highcharts={Highcharts}
options={chartOptions}
/>
</div>
)
};
export default AccountContractHistory;
Would appreciate any help in figuring out what I'm doing wrong
Thanks
You can find a demo of this chart in React here:
https://stackblitz.com/edit/react-highcharts-xrange-demo
You have to remember to properly load highcharts modules:
https://github.com/highcharts/highcharts-react#how-to-add-a-module
Seeing same error but (using Typescript) for my fix i needed to pass in Highcharts to the xrange like so:
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
import xrange from 'highcharts/modules/xrange';
xrange(Highcharts);
and then the markup:
<HighchartsReact
highcharts={Highcharts}
options={options}
{...this.props}
/>

Passing an array of objects through props in React

(Solved): Solution was compatibility issues between chart.js and the react version. The recent update to the react version solved it.
I was having trouble finding the solution to this, maybe someone will know why this error is happening:
I am passing an object array via props, but I'm getting an error when it comes to using the prop:
Object array sample (The data for score is fed in through an API and is working):
var teamData = [
{
teamName: "Cowboys",
score: 0,
color: "rgba(0, 34, 68, 0.9)",
},
{
teamName: "Cardinals",
score: 0,
color: "rgba(135, 0, 39, 0.9)",
},
{
teamName: "Patriots",
score: 0,
color: "rgba(0, 21, 50, 0.9)",
},
{
teamName: "49ers",
score: 0,
color: "rgba(170, 0, 0.9)",
},
App.js example
const App = () => {
getData();
return (
<div>
<Header />
<BarChart teamData={teamData} />
<Calculator teamData={teamData} />
<Footer />
</div>
);
};
export default App;
And here is where I pass the props to insert one by one into a chart.js component({props[0].score} is where the error is, it says: SyntaxError C:\Users\name\Documents\Websites\React Based\madden-ranker\src\components\BarChart.js: Unexpected token, expected "," (14:37):
import React from "react";
import { Bar, HorizontalBar } from "react-chartjs-2";
const BarChart = (props) => {
return (
<HorizontalBar
// prettier-ignore
data={{
labels: ['Team1' ],
datasets: [
{
label: 'Cumulative Score',
data: [{props[0].score}, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 10, 20],
backgroundColor: []
},
],
}}
height={500}
width={600}
options={{
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
},
},
}}
/>
);
};
export default BarChart;
Interestingly, I tried console logging it inside the teamData component and got this:
line5: console.log(props);
line6: console.log(props.teamData[0].score);
.
Just add a condition to check if props[0] exists before rendering, cause the first time the props is empty, and when you setstate after getting data from API the component rerenders. Hopefully this helps.
I think you had a few commas in places that they shouldn't be
Try this
const BarChart = (props) => {
return (
<HorizontalBar
// prettier-ignore
data={{
labels: ['Team1' ],
datasets: [
{
label: 'Cumulative Score',
data: [{props[0].score}, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 10, 20],
backgroundColor: []
}
]
}}
height={500}
width={600}
options={{
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
}
}
}}
/>
);
};

How to change all react-big-calendar to French?

I try to add culture='fr' on the BigCalendar but, I get an error.
My code is:
import moment from "moment";
BigCalendar.momentLocalizer(moment);
export default class Agenda extends Component {
constructor(props){
super(props);
this.state = {
events: [
{
title: 'Calendar 1',
start: new Date(2019, 2, 19, 15, 0, 0), //03:00 PM
end: new Date(2019, 2, 19, 16, 30, 0), //04:30 PM
},
{
title: 'Calendar 2 ',
start: new Date(2019, 2, 20, 12, 30, 0), //08:30 AM
end: new Date(2019, 2, 20, 18, 0, 0), //18:00 PM
},
{
title: 'Calendar 3 ',
start: new Date(2019, 2, 22, 10, 30, 0), //10:30 AM
end: new Date(2019, 2, 22, 19, 0, 0), //07:00 PM
},
{
title: 'Calendar 4 ',
start: new Date(2019, 2, 23, 7, 30, 0), //08:30 AM
end: new Date(2019, 2, 23, 11, 0, 0), //11:00 AM
},
],
}
render() {
return (
<div>
<BigCalendar
selectable
events={this.state.events}
defaultDate={new Date(2019, 2, 19)}
defaultView="week"
culture = 'fr'
style={{ height: "100vh" }}
/>
</div>
)
}
};
When I run it, I get :
How can I fix that ?
I resolve that by adding import 'moment/locale/fr'; on my component.
You could do it this way, I highly recommend you use the "moment" library
It makes things much easier in terms of dates apart you can translate to the language of your choice
import React, { Fragment, useState } from 'react'
import { Calendar, momentLocalizer } from 'react-big-calendar'
import "react-big-calendar/lib/css/react-big-calendar.css";
import moment from "moment";
require('moment/locale/es.js')
const localizer = momentLocalizer(moment);
function Citas() {
const [Events, setEvents] = useState([
{
start: moment().toDate(),
end: moment()
.add(1, "days")
.toDate(),
title: "Some title"
}
]);
return (
<Fragment>
<div className="font-weight-bold mb-2 px-3 shadow-sm p-2 bg-light">CITAS </div>
<div className="px-3">mas datos sobre citas
<Calendar
localizer={localizer}
defaultDate={new Date()}
defaultView="month"
events={Events}
style={{ height: "100vh" }}
messages={{
next: "sig",
previous: "ant",
today: "Hoy",
month: "Mes",
week: "Semana",
day: "Día"
}}
/>
</div>
</Fragment>
)
}
export default Citas
The error that you posted is likely because you didn't pass localizer as props to the BigCalendar. To fix that, you can try assigning a variable
const localizer = BigCalendar.momentLocalizer(moment);
and then pass it as a prop
<BigCalendar
localizer={localizer}
...
/>
Hope it helps!
reference: http://intljusticemission.github.io/react-big-calendar/examples/index.html#intro
This works for me in Spanish using react-big-calendar.
import { Calendar, momentLocalizer } from "react-big-calendar"
import moment from "moment"
import 'moment/locale/es';
//...
const localizer = momentLocalizer(moment)
//...
<DragAndDropCalendar localizer={localizer} /*otherprops*/ />
you need provide the culture props :
import { Calendar, dateFnsLocalizer } from "react-big-calendar";
import format from "date-fns/format";
import parse from "date-fns/parse";
import startOfWeek from "date-fns/startOfWeek";
import getDay from "date-fns/getDay";
import fr from "date-fns/locale/fr";
const messages = {
allDay: "Tous les jours",
previous: "Précédent",
next: "Suivant",
today: "Aujourd'hui",
month: "Mois",
week: "Semaine",
day: "Jour",
agenda: "Agenda",
date: "Date",
time: "Heure",
event: "Evenement",
};
const locales = {
fr: fr,
};
const localizer = dateFnsLocalizer({
format,
parse,
startOfWeek,
getDay,
locales,
});
..........
<Calendar
localizer={localizer}
culture="fr"

Defining a task color for each row in Angular-Gantt

I am using angular-gantt to represent a series of tasks and I would like to have the tasks of a specific row with a specific color.
To add to this I have added the modules gantt-movable and gant-draw-task.
I know how to fix the color of a task at the start but not how to modify it according to the row in which the task is in.
Thank you for any pointers.
EDIT:
Ctrl
(function () {
'use strict';
angular.module('Dashboard.pages.devices.planning')
.controller('PlanningGanttCtrl', PlanningGanttCtrl);
function PlanningGanttCtrl($scope,$sessionStorage) {
$scope.data=$sessionStorage.planning[$scope.dayNumber];
$scope.headersFormats = {
day: 'D',
hour: 'H',
minute:'HH:mm'
};
$scope.data = [
{name: '20°C', tasks: [
{name: '', color: '#F1C232', from: new Date(2013, 10, 5, 0, 0, 0), to: new Date(2013, 10, 5, 6, 0, 0) }
]},
{name: '19°C', tasks: [
{name: '', color: '#F1C232', from: new Date(2013, 10, 5, 7, 0, 0), to: new Date(2013, 10, 5, 10, 0, 0)}
]},
{name: '18°C', tasks: [
{name: '', color: '#F1C232', from: new Date(2013, 10, 5, 10, 0, 0), to: new Date(2013, 10, 5, 12, 0, 0)}
]},
{name: '17°C', tasks: [
{name: '', color: '#F1C232', from: new Date(2013, 10, 5, 12, 0, 0), to: new Date(2013, 10, 5, 15, 0, 0)}
]}
];
$scope.drawTaskFactory = function() {
var newTask = {
id: 5,
name: 'New Task'
// Other properties
};
return newTask;
}
}
})();
Html
<div gantt data=data headers="['hour']" headers-format="headersFormats" task-out-of-range="expand" expand-to-fit="true" >
<gantt-table></gantt-table>
<gantt-movable></gantt-movable>
<gantt-tooltips></gantt-tooltips>
<gantt-resize-sensor></gantt-resize-sensor>
<gantt-draw-task enabled="true" task-factory="drawTaskFactory" move-threshold="4"></gantt-draw-task>
<gantt-overlap enabled="false" global="true"></gantt-overlap>
</div>
And the directive even if I don't believe it is useful,
(function () {
'use strict';
angular.module('Dashboard.pages.devices.planning')
.directive('planningGantt', planningGantt);
/** #ngInject */
function planningGantt() {
return {
restrict: 'E',
controller: 'PlanningGanttCtrl',
templateUrl: 'app/pages/devices/planning/planningGantt/planningGantt.html',
scope: {
dayNumber: '=info'
},
};
}
})();
Is the colour and other data of the current column in which task is - is known to you?
Then you could use event system to change colour property of the task, when it's moved. It may look approximately like this:
//in single task directive's controller
data = {
id://column id
color: //column color
};
$scope.$emit('taskChangeEvt', data);
//in PlanningGanttCtrl
$scope.$on('taskChangeEvt', function(event, eventData){
$scope.data.forEach(function(item){
if(item.id == eventData.id){ //find task that fit that column by ID
//update that task's colour
item.color = eventData.color;
}
})
})

Resources